Refactor handlers/income.go

This commit is contained in:
qowevisa 2024-11-18 19:38:44 +02:00
parent 78d420e1d2
commit 21afd95d98

View File

@ -1,15 +1,21 @@
package handlers
import (
"fmt"
"strconv"
"git.qowevisa.me/Qowevisa/fin-check-api/db"
"git.qowevisa.me/Qowevisa/fin-check-api/types"
"git.qowevisa.me/Qowevisa/fin-check-api/utils"
"github.com/gin-gonic/gin"
)
var incomeTransform func(inp *db.Income) types.DbIncome = func(inp *db.Income) types.DbIncome {
return types.DbIncome{
ID: inp.ID,
CardID: inp.CardID,
Comment: inp.Comment,
Value: inp.Value,
Date: inp.Date,
}
}
// @Summary Get income by id
// @Description Get income by id
// @Tags income
@ -24,53 +30,7 @@ import (
// @Security ApiKeyAuth
// @Router /income/:id [get]
func IncomeGetId(c *gin.Context) {
userIDAny, exists := c.Get("UserID")
if !exists {
c.JSON(500, types.ErrorResponse{Message: "Internal error 001"})
return
}
var userID uint
if userIDVal, ok := userIDAny.(uint); !ok {
c.JSON(500, types.ErrorResponse{Message: "Internal error 002"})
return
} else {
userID = userIDVal
}
idStr := c.Param("id")
var id uint
if idVal, err := strconv.ParseUint(idStr, 10, 32); err != nil {
c.JSON(400, types.ErrorResponse{Message: "Invalid request"})
return
} else {
id = uint(idVal)
}
var dbIncome db.Income
dbc := db.Connect()
if err := dbc.Find(&dbIncome, id).Error; err != nil {
c.JSON(500, types.ErrorResponse{Message: err.Error()})
return
}
if dbIncome.ID == 0 {
c.JSON(500, types.ErrorResponse{Message: "DAFUQ003"})
return
}
if dbIncome.UserID != userID {
c.JSON(401, types.ErrorResponse{Message: "This income.id is not yours, you sneaky."})
return
}
ret := types.DbIncome{
ID: dbIncome.ID,
CardID: dbIncome.CardID,
Comment: dbIncome.Comment,
Value: dbIncome.Value,
Date: dbIncome.Date,
UserID: dbIncome.UserID,
}
c.JSON(200, ret)
GetHandler(incomeTransform)(c)
}
// @Summary Add income
@ -87,52 +47,15 @@ func IncomeGetId(c *gin.Context) {
// @Security ApiKeyAuth
// @Router /income/add [post]
func IncomeAdd(c *gin.Context) {
userIDAny, exists := c.Get("UserID")
if !exists {
c.JSON(500, types.ErrorResponse{Message: "Internal error 001"})
return
}
var userID uint
if userIDVal, ok := userIDAny.(uint); !ok {
c.JSON(500, types.ErrorResponse{Message: "Internal error 002"})
return
} else {
userID = userIDVal
}
var income types.DbIncome
if err := c.ShouldBindJSON(&income); err != nil {
c.JSON(400, types.ErrorResponse{Message: "Invalid request"})
return
}
if income.UserID != 0 && userID != income.UserID {
c.JSON(403, types.ErrorResponse{Message: "UserID in body is different than yours!"})
}
if income.UserID == 0 {
income.UserID = userID
}
dbIncome := &db.Income{
CardID: income.CardID,
Comment: income.Comment,
Value: income.Value,
Date: income.Date,
UserID: income.UserID,
}
dbc := db.Connect()
if err := dbc.Create(&dbIncome).Error; err != nil {
c.JSON(500, types.ErrorResponse{Message: err.Error()})
return
}
if dbIncome.ID == 0 {
c.JSON(500, types.ErrorResponse{Message: "DAFUQ004"})
return
}
msg := types.Message{
Info: fmt.Sprintf("Income with id %d was successfully created!", dbIncome.ID),
}
c.JSON(200, msg)
CreateHandler(
&db.Income{},
func(src types.DbIncome, dst *db.Income) {
dst.CardID = src.CardID
dst.Value = src.Value
dst.Comment = src.Comment
dst.Date = src.Date
},
)
}
// @Summary Edit income by id
@ -150,65 +73,15 @@ func IncomeAdd(c *gin.Context) {
// @Security ApiKeyAuth
// @Router /income/edit/:id [put]
func IncomePutId(c *gin.Context) {
userIDAny, exists := c.Get("UserID")
if !exists {
c.JSON(500, types.ErrorResponse{Message: "Internal error 001"})
return
}
var userID uint
if userIDVal, ok := userIDAny.(uint); !ok {
c.JSON(500, types.ErrorResponse{Message: "Internal error 002"})
return
} else {
userID = userIDVal
}
idStr := c.Param("id")
var id uint
if idVal, err := strconv.ParseUint(idStr, 10, 32); err != nil {
c.JSON(400, types.ErrorResponse{Message: "Invalid request"})
return
} else {
id = uint(idVal)
}
var dbIncome db.Income
dbc := db.Connect()
if err := dbc.Find(&dbIncome, id).Error; err != nil {
c.JSON(500, types.ErrorResponse{Message: err.Error()})
return
}
if dbIncome.ID == 0 {
c.JSON(500, types.ErrorResponse{Message: "DAFUQ003"})
return
}
if dbIncome.UserID != userID {
c.JSON(401, types.ErrorResponse{Message: "This income.id is not yours, you sneaky."})
return
}
var income types.DbIncome
if err := c.ShouldBindJSON(&income); err != nil {
c.JSON(400, types.ErrorResponse{Message: "Invalid request"})
return
}
utils.MergeNonZeroFields(income, dbIncome)
if err := dbc.Save(dbIncome).Error; err != nil {
c.JSON(500, types.ErrorResponse{Message: err.Error()})
return
}
ret := types.DbIncome{
ID: dbIncome.ID,
CardID: dbIncome.CardID,
Comment: dbIncome.Comment,
Value: dbIncome.Value,
Date: dbIncome.Date,
UserID: dbIncome.UserID,
}
c.JSON(200, ret)
UpdateHandler(
func(src types.DbIncome, dst *db.Income) {
dst.CardID = src.CardID
dst.Value = src.Value
dst.Comment = src.Comment
dst.Date = src.Date
},
incomeTransform,
)(c)
}
// @Summary Delete income by id
@ -225,55 +98,5 @@ func IncomePutId(c *gin.Context) {
// @Security ApiKeyAuth
// @Router /income/delete/:id [delete]
func IncomeDeleteId(c *gin.Context) {
userIDAny, exists := c.Get("UserID")
if !exists {
c.JSON(500, types.ErrorResponse{Message: "Internal error 001"})
return
}
var userID uint
if userIDVal, ok := userIDAny.(uint); !ok {
c.JSON(500, types.ErrorResponse{Message: "Internal error 002"})
return
} else {
userID = userIDVal
}
idStr := c.Param("id")
var id uint
if idVal, err := strconv.ParseUint(idStr, 10, 32); err != nil {
c.JSON(400, types.ErrorResponse{Message: "Invalid request"})
return
} else {
id = uint(idVal)
}
var dbIncome db.Income
dbc := db.Connect()
if err := dbc.Find(&dbIncome, id).Error; err != nil {
c.JSON(500, types.ErrorResponse{Message: err.Error()})
return
}
if dbIncome.ID == 0 {
c.JSON(500, types.ErrorResponse{Message: "DAFUQ003"})
return
}
if dbIncome.UserID != userID {
c.JSON(401, types.ErrorResponse{Message: "This income.id is not yours, you sneaky."})
return
}
if err := dbc.Delete(dbIncome).Error; err != nil {
c.JSON(500, types.ErrorResponse{Message: err.Error()})
return
}
ret := types.DbIncome{
ID: dbIncome.ID,
CardID: dbIncome.CardID,
Comment: dbIncome.Comment,
Value: dbIncome.Value,
Date: dbIncome.Date,
UserID: dbIncome.UserID,
}
c.JSON(200, ret)
DeleteHandler[*db.Income]()(c)
}