Refactor Debt handlers

This commit is contained in:
qowevisa 2024-11-02 00:05:53 +02:00
parent 1402825a72
commit 9731073e9f

View File

@ -1,12 +1,8 @@
package handlers package handlers
import ( import (
"fmt"
"strconv"
"git.qowevisa.me/Qowevisa/gonuts/db" "git.qowevisa.me/Qowevisa/gonuts/db"
"git.qowevisa.me/Qowevisa/gonuts/types" "git.qowevisa.me/Qowevisa/gonuts/types"
"git.qowevisa.me/Qowevisa/gonuts/utils"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@ -20,60 +16,23 @@ import (
// @Success 200 {object} types.DbDebt // @Success 200 {object} types.DbDebt
// @Failure 400 {object} types.ErrorResponse // @Failure 400 {object} types.ErrorResponse
// @Failure 401 {object} types.ErrorResponse // @Failure 401 {object} types.ErrorResponse
// @Failure 403 {object} types.ErrorResponse
// @Failure 500 {object} types.ErrorResponse // @Failure 500 {object} types.ErrorResponse
// @Security ApiKeyAuth // @Security ApiKeyAuth
// @Router /debt/:id [get] // @Router /debt/:id [get]
func DebtGetId(c *gin.Context) { func DebtGetId(c *gin.Context) {
userIDAny, exists := c.Get("UserID") GetHandler(func(inp *db.Debt) types.DbDebt {
if !exists { return types.DbDebt{
c.JSON(500, types.ErrorResponse{Message: "Internal error 001"}) ID: inp.ID,
return CardID: inp.CardID,
} Comment: inp.Comment,
Value: inp.Value,
var userID uint IOwe: inp.IOwe,
if userIDVal, ok := userIDAny.(uint); !ok { Date: inp.Date,
c.JSON(500, types.ErrorResponse{Message: "Internal error 002"}) DateEnd: inp.DateEnd,
return Finished: inp.Finished,
} else { }
userID = userIDVal })(c)
}
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 dbDebt db.Debt
dbc := db.Connect()
if err := dbc.Find(&dbDebt, id).Error; err != nil {
c.JSON(500, types.ErrorResponse{Message: err.Error()})
return
}
if dbDebt.ID == 0 {
c.JSON(500, types.ErrorResponse{Message: "DAFUQ003"})
return
}
if dbDebt.UserID != userID {
c.JSON(401, types.ErrorResponse{Message: "This debt.id is not yours, you sneaky."})
return
}
ret := types.DbDebt{
ID: dbDebt.ID,
CardID: dbDebt.CardID,
Comment: dbDebt.Comment,
Value: dbDebt.Value,
IOwe: dbDebt.IOwe,
Date: dbDebt.Date,
DateEnd: dbDebt.DateEnd,
Finished: dbDebt.Finished,
UserID: dbDebt.UserID,
}
c.JSON(200, ret)
} }
// @Summary Get debt by id // @Summary Get debt by id
@ -85,60 +44,22 @@ func DebtGetId(c *gin.Context) {
// @Param debt body types.DbDebt true "Debt" // @Param debt body types.DbDebt true "Debt"
// @Success 200 {object} types.Message // @Success 200 {object} types.Message
// @Failure 400 {object} types.ErrorResponse // @Failure 400 {object} types.ErrorResponse
// @Failure 403 {object} types.ErrorResponse
// @Failure 500 {object} types.ErrorResponse // @Failure 500 {object} types.ErrorResponse
// @Security ApiKeyAuth // @Security ApiKeyAuth
// @Router /debt/add [post] // @Router /debt/add [post]
func DebtAdd(c *gin.Context) { func DebtAdd(c *gin.Context) {
userIDAny, exists := c.Get("UserID") debt := &db.Debt{}
if !exists { CreateHandler(debt,
c.JSON(500, types.ErrorResponse{Message: "Internal error 001"}) func(src types.DbDebt, dst *db.Debt) {
return dst.CardID = src.CardID
} dst.Comment = src.Comment
dst.Value = src.Value
var userID uint dst.IOwe = src.IOwe
if userIDVal, ok := userIDAny.(uint); !ok { dst.Date = src.Date
c.JSON(500, types.ErrorResponse{Message: "Internal error 002"}) dst.DateEnd = src.DateEnd
return dst.Finished = src.Finished
} else { },
userID = userIDVal )(c)
}
var debt types.DbDebt
if err := c.ShouldBindJSON(&debt); err != nil {
c.JSON(400, types.ErrorResponse{Message: "Invalid request"})
return
}
if debt.UserID != 0 && userID != debt.UserID {
c.JSON(403, types.ErrorResponse{Message: "UserID in body is different than yours!"})
}
if debt.UserID == 0 {
debt.UserID = userID
}
dbDebt := &db.Debt{
CardID: debt.CardID,
Comment: debt.Comment,
Value: debt.Value,
IOwe: debt.IOwe,
Date: debt.Date,
DateEnd: debt.DateEnd,
Finished: debt.Finished,
UserID: debt.UserID,
}
dbc := db.Connect()
if err := dbc.Create(&dbDebt).Error; err != nil {
c.JSON(500, types.ErrorResponse{Message: err.Error()})
return
}
if dbDebt.ID == 0 {
c.JSON(500, types.ErrorResponse{Message: "DAFUQ004"})
return
}
msg := types.Message{
Message: fmt.Sprintf("Debt with id %d was successfully created!", dbDebt.ID),
}
c.JSON(200, msg)
} }
// @Summary Edit debt by id // @Summary Edit debt by id
@ -152,72 +73,34 @@ func DebtAdd(c *gin.Context) {
// @Success 200 {object} types.DbDebt // @Success 200 {object} types.DbDebt
// @Failure 400 {object} types.ErrorResponse // @Failure 400 {object} types.ErrorResponse
// @Failure 401 {object} types.ErrorResponse // @Failure 401 {object} types.ErrorResponse
// @Failure 403 {object} types.ErrorResponse
// @Failure 500 {object} types.ErrorResponse // @Failure 500 {object} types.ErrorResponse
// @Security ApiKeyAuth // @Security ApiKeyAuth
// @Router /debt/edit/:id [put] // @Router /debt/edit/:id [put]
func DebtPutId(c *gin.Context) { func DebtPutId(c *gin.Context) {
userIDAny, exists := c.Get("UserID") UpdateHandler(
if !exists { // Filter used to apply only needed changes from srt to dst before updating dst
c.JSON(500, types.ErrorResponse{Message: "Internal error 001"}) func(src types.DbDebt, dst *db.Debt) {
return dst.CardID = src.CardID
} dst.Comment = src.Comment
dst.Value = src.Value
var userID uint dst.IOwe = src.IOwe
if userIDVal, ok := userIDAny.(uint); !ok { dst.Date = src.Date
c.JSON(500, types.ErrorResponse{Message: "Internal error 002"}) dst.DateEnd = src.DateEnd
return dst.Finished = src.Finished
} else { },
userID = userIDVal func(inp *db.Debt) types.DbDebt {
} return types.DbDebt{
ID: inp.ID,
idStr := c.Param("id") CardID: inp.CardID,
var id uint Comment: inp.Comment,
if idVal, err := strconv.ParseUint(idStr, 10, 32); err != nil { Value: inp.Value,
c.JSON(400, types.ErrorResponse{Message: "Invalid request"}) IOwe: inp.IOwe,
return Date: inp.Date,
} else { DateEnd: inp.DateEnd,
id = uint(idVal) Finished: inp.Finished,
} }
})(c)
var dbDebt db.Debt
dbc := db.Connect()
if err := dbc.Find(&dbDebt, id).Error; err != nil {
c.JSON(500, types.ErrorResponse{Message: err.Error()})
return
}
if dbDebt.ID == 0 {
c.JSON(500, types.ErrorResponse{Message: "DAFUQ003"})
return
}
if dbDebt.UserID != userID {
c.JSON(401, types.ErrorResponse{Message: "This debt.id is not yours, you sneaky."})
return
}
var debt types.DbDebt
if err := c.ShouldBindJSON(&debt); err != nil {
c.JSON(400, types.ErrorResponse{Message: "Invalid request"})
return
}
utils.MergeNonZeroFields(debt, dbDebt)
if err := dbc.Save(dbDebt).Error; err != nil {
c.JSON(500, types.ErrorResponse{Message: err.Error()})
return
}
ret := types.DbDebt{
ID: dbDebt.ID,
CardID: dbDebt.CardID,
Comment: dbDebt.Comment,
Value: dbDebt.Value,
IOwe: dbDebt.IOwe,
Date: dbDebt.Date,
DateEnd: dbDebt.DateEnd,
Finished: dbDebt.Finished,
UserID: dbDebt.UserID,
}
c.JSON(200, ret)
} }
// @Summary Delete debt by id // @Summary Delete debt by id
@ -230,62 +113,10 @@ func DebtPutId(c *gin.Context) {
// @Success 200 {object} types.DbDebt // @Success 200 {object} types.DbDebt
// @Failure 400 {object} types.ErrorResponse // @Failure 400 {object} types.ErrorResponse
// @Failure 401 {object} types.ErrorResponse // @Failure 401 {object} types.ErrorResponse
// @Failure 403 {object} types.ErrorResponse
// @Failure 500 {object} types.ErrorResponse // @Failure 500 {object} types.ErrorResponse
// @Security ApiKeyAuth // @Security ApiKeyAuth
// @Router /debt/delete/:id [delete] // @Router /debt/delete/:id [delete]
func DebtDeleteId(c *gin.Context) { func DebtDeleteId(c *gin.Context) {
userIDAny, exists := c.Get("UserID") DeleteHandler[*db.Debt]()(c)
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 dbDebt db.Debt
dbc := db.Connect()
if err := dbc.Find(&dbDebt, id).Error; err != nil {
c.JSON(500, types.ErrorResponse{Message: err.Error()})
return
}
if dbDebt.ID == 0 {
c.JSON(500, types.ErrorResponse{Message: "DAFUQ003"})
return
}
if dbDebt.UserID != userID {
c.JSON(401, types.ErrorResponse{Message: "This debt.id is not yours, you sneaky."})
return
}
if err := dbc.Delete(dbDebt).Error; err != nil {
c.JSON(500, types.ErrorResponse{Message: err.Error()})
return
}
ret := types.DbDebt{
ID: dbDebt.ID,
CardID: dbDebt.CardID,
Comment: dbDebt.Comment,
Value: dbDebt.Value,
IOwe: dbDebt.IOwe,
Date: dbDebt.Date,
DateEnd: dbDebt.DateEnd,
Finished: dbDebt.Finished,
UserID: dbDebt.UserID,
}
c.JSON(200, ret)
} }