From 9731073e9f9a19ddda0f9922f1e9184b57e22ce8 Mon Sep 17 00:00:00 2001 From: qowevisa Date: Sat, 2 Nov 2024 00:05:53 +0200 Subject: [PATCH] Refactor Debt handlers --- handlers/debt.go | 271 +++++++++-------------------------------------- 1 file changed, 51 insertions(+), 220 deletions(-) diff --git a/handlers/debt.go b/handlers/debt.go index 6201dae..5eb6871 100644 --- a/handlers/debt.go +++ b/handlers/debt.go @@ -1,12 +1,8 @@ package handlers import ( - "fmt" - "strconv" - "git.qowevisa.me/Qowevisa/gonuts/db" "git.qowevisa.me/Qowevisa/gonuts/types" - "git.qowevisa.me/Qowevisa/gonuts/utils" "github.com/gin-gonic/gin" ) @@ -20,60 +16,23 @@ import ( // @Success 200 {object} types.DbDebt // @Failure 400 {object} types.ErrorResponse // @Failure 401 {object} types.ErrorResponse +// @Failure 403 {object} types.ErrorResponse // @Failure 500 {object} types.ErrorResponse // @Security ApiKeyAuth // @Router /debt/:id [get] func DebtGetId(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 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) + GetHandler(func(inp *db.Debt) types.DbDebt { + return types.DbDebt{ + ID: inp.ID, + CardID: inp.CardID, + Comment: inp.Comment, + Value: inp.Value, + IOwe: inp.IOwe, + Date: inp.Date, + DateEnd: inp.DateEnd, + Finished: inp.Finished, + } + })(c) } // @Summary Get debt by id @@ -85,60 +44,22 @@ func DebtGetId(c *gin.Context) { // @Param debt body types.DbDebt true "Debt" // @Success 200 {object} types.Message // @Failure 400 {object} types.ErrorResponse -// @Failure 403 {object} types.ErrorResponse // @Failure 500 {object} types.ErrorResponse // @Security ApiKeyAuth // @Router /debt/add [post] func DebtAdd(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 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) + debt := &db.Debt{} + CreateHandler(debt, + func(src types.DbDebt, dst *db.Debt) { + dst.CardID = src.CardID + dst.Comment = src.Comment + dst.Value = src.Value + dst.IOwe = src.IOwe + dst.Date = src.Date + dst.DateEnd = src.DateEnd + dst.Finished = src.Finished + }, + )(c) } // @Summary Edit debt by id @@ -152,72 +73,34 @@ func DebtAdd(c *gin.Context) { // @Success 200 {object} types.DbDebt // @Failure 400 {object} types.ErrorResponse // @Failure 401 {object} types.ErrorResponse +// @Failure 403 {object} types.ErrorResponse // @Failure 500 {object} types.ErrorResponse // @Security ApiKeyAuth // @Router /debt/edit/:id [put] func DebtPutId(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 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) + UpdateHandler( + // Filter used to apply only needed changes from srt to dst before updating dst + func(src types.DbDebt, dst *db.Debt) { + dst.CardID = src.CardID + dst.Comment = src.Comment + dst.Value = src.Value + dst.IOwe = src.IOwe + dst.Date = src.Date + dst.DateEnd = src.DateEnd + dst.Finished = src.Finished + }, + func(inp *db.Debt) types.DbDebt { + return types.DbDebt{ + ID: inp.ID, + CardID: inp.CardID, + Comment: inp.Comment, + Value: inp.Value, + IOwe: inp.IOwe, + Date: inp.Date, + DateEnd: inp.DateEnd, + Finished: inp.Finished, + } + })(c) } // @Summary Delete debt by id @@ -230,62 +113,10 @@ func DebtPutId(c *gin.Context) { // @Success 200 {object} types.DbDebt // @Failure 400 {object} types.ErrorResponse // @Failure 401 {object} types.ErrorResponse +// @Failure 403 {object} types.ErrorResponse // @Failure 500 {object} types.ErrorResponse // @Security ApiKeyAuth // @Router /debt/delete/:id [delete] func DebtDeleteId(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 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) + DeleteHandler[*db.Debt]()(c) }