Refactor category handlers

This commit is contained in:
qowevisa 2024-10-31 23:06:42 +02:00
parent faa2bf7a9e
commit 340b39c35e

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,54 +16,18 @@ import (
// @Success 200 {object} types.DbCategory // @Success 200 {object} types.DbCategory
// @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 /category/:id [get] // @Router /category/:id [get]
func CategoryGetId(c *gin.Context) { func CategoryGetId(c *gin.Context) {
userIDAny, exists := c.Get("UserID") GetHandler(func(inp *db.Category) types.DbCategory {
if !exists { return types.DbCategory{
c.JSON(500, types.ErrorResponse{Message: "Internal error 001"}) ID: inp.ID,
return Name: inp.Name,
ParentID: inp.ParentID,
} }
})(c)
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 dbCategory db.Category
dbc := db.Connect()
if err := dbc.Find(&dbCategory, id).Error; err != nil {
c.JSON(500, types.ErrorResponse{Message: err.Error()})
return
}
if dbCategory.ID == 0 {
c.JSON(500, types.ErrorResponse{Message: "DAFUQ003"})
return
}
if dbCategory.UserID != userID {
c.JSON(401, types.ErrorResponse{Message: "This category.id is not yours, you sneaky."})
return
}
category := types.DbCategory{
Name: dbCategory.Name,
ParentID: dbCategory.ParentID,
UserID: userID,
}
c.JSON(200, category)
} }
// @Summary Get category by id // @Summary Get category by id
@ -83,44 +43,10 @@ func CategoryGetId(c *gin.Context) {
// @Security ApiKeyAuth // @Security ApiKeyAuth
// @Router /category/add [post] // @Router /category/add [post]
func CategoryAdd(c *gin.Context) { func CategoryAdd(c *gin.Context) {
userIDAny, exists := c.Get("UserID") CreateHandler(&db.Category{}, func(src types.DbCategory, dst *db.Category) {
if !exists { dst.Name = src.Name
c.JSON(500, types.ErrorResponse{Message: "Internal error 001"}) dst.ParentID = src.ParentID
return })(c)
}
var userID uint
if userIDVal, ok := userIDAny.(uint); !ok {
c.JSON(500, types.ErrorResponse{Message: "Internal error 002"})
return
} else {
userID = userIDVal
}
var category types.DbCategory
if err := c.ShouldBindJSON(&category); err != nil {
c.JSON(400, types.ErrorResponse{Message: "Invalid request"})
return
}
dbCategory := &db.Category{
Name: category.Name,
ParentID: category.ParentID,
UserID: userID,
}
dbc := db.Connect()
if err := dbc.Create(&dbCategory).Error; err != nil {
c.JSON(500, types.ErrorResponse{Message: err.Error()})
return
}
if dbCategory.ID == 0 {
c.JSON(500, types.ErrorResponse{Message: "DAFUQ004"})
return
}
msg := types.Message{
Message: fmt.Sprintf("Category with id %d was successfully created!", dbCategory.ID),
}
c.JSON(200, msg)
} }
// @Summary Edit category by id // @Summary Edit category by id
@ -134,67 +60,24 @@ func CategoryAdd(c *gin.Context) {
// @Success 200 {object} types.DbCategory // @Success 200 {object} types.DbCategory
// @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 /category/edit/:id [put] // @Router /category/edit/:id [put]
func CategoryPutId(c *gin.Context) { func CategoryPutId(c *gin.Context) {
userIDAny, exists := c.Get("UserID") UpdateHandler(
if !exists { func(src types.DbCategory, dst *db.Category) {
c.JSON(500, types.ErrorResponse{Message: "Internal error 001"}) dst.Name = src.Name
return dst.ParentID = src.ParentID
},
func(inp *db.Category) types.DbCategory {
return types.DbCategory{
ID: inp.ID,
Name: inp.Name,
ParentID: inp.ParentID,
} }
},
var userID uint )(c)
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 dbCategory db.Category
dbc := db.Connect()
if err := dbc.Find(&dbCategory, id).Error; err != nil {
c.JSON(500, types.ErrorResponse{Message: err.Error()})
return
}
if dbCategory.ID == 0 {
c.JSON(500, types.ErrorResponse{Message: "DAFUQ003"})
return
}
if dbCategory.UserID != userID {
c.JSON(401, types.ErrorResponse{Message: "This category.id is not yours, you sneaky."})
return
}
var category types.DbCategory
if err := c.ShouldBindJSON(&category); err != nil {
c.JSON(400, types.ErrorResponse{Message: "Invalid request"})
return
}
utils.MergeNonZeroFields(category, dbCategory)
if err := dbc.Save(dbCategory).Error; err != nil {
c.JSON(500, types.ErrorResponse{Message: err.Error()})
return
}
ret := types.DbCategory{
ID: dbCategory.ID,
Name: dbCategory.Name,
ParentID: dbCategory.ParentID,
UserID: dbCategory.UserID,
}
c.JSON(200, ret)
} }
// @Summary Delete category by id // @Summary Delete category by id
@ -207,57 +90,10 @@ func CategoryPutId(c *gin.Context) {
// @Success 200 {object} types.DbCategory // @Success 200 {object} types.DbCategory
// @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 /category/delete/:id [delete] // @Router /category/delete/:id [delete]
func CategoryDeleteId(c *gin.Context) { func CategoryDeleteId(c *gin.Context) {
userIDAny, exists := c.Get("UserID") DeleteHandler[*db.Category]()(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 dbCategory db.Category
dbc := db.Connect()
if err := dbc.Find(&dbCategory, id).Error; err != nil {
c.JSON(500, types.ErrorResponse{Message: err.Error()})
return
}
if dbCategory.ID == 0 {
c.JSON(500, types.ErrorResponse{Message: "DAFUQ003"})
return
}
if dbCategory.UserID != userID {
c.JSON(401, types.ErrorResponse{Message: "This category.id is not yours, you sneaky."})
return
}
if err := dbc.Delete(dbCategory).Error; err != nil {
c.JSON(500, types.ErrorResponse{Message: err.Error()})
return
}
ret := types.DbCategory{
ID: dbCategory.ID,
Name: dbCategory.Name,
ParentID: dbCategory.ParentID,
UserID: dbCategory.UserID,
}
c.JSON(200, ret)
} }