fin-check-api/handlers/category.go

100 lines
2.8 KiB
Go
Raw Normal View History

2024-08-03 12:40:11 +02:00
package handlers
import (
2024-11-04 16:55:14 +01:00
"git.qowevisa.me/Qowevisa/fin-check-api/db"
"git.qowevisa.me/Qowevisa/fin-check-api/types"
2024-08-03 12:40:11 +02:00
"github.com/gin-gonic/gin"
)
// @Summary Get category by id
// @Description Get category by id
// @Tags category
// @Accept json
// @Produce json
// @Param Authorization header string true "Bearer token"
// @Param category path int true "id"
// @Success 200 {object} types.DbCategory
// @Failure 400 {object} types.ErrorResponse
// @Failure 401 {object} types.ErrorResponse
2024-10-31 22:06:42 +01:00
// @Failure 403 {object} types.ErrorResponse
2024-08-03 12:40:11 +02:00
// @Failure 500 {object} types.ErrorResponse
// @Security ApiKeyAuth
// @Router /category/:id [get]
func CategoryGetId(c *gin.Context) {
2024-10-31 22:06:42 +01:00
GetHandler(func(inp *db.Category) types.DbCategory {
return types.DbCategory{
ID: inp.ID,
Name: inp.Name,
ParentID: inp.ParentID,
}
})(c)
2024-08-03 12:40:11 +02:00
}
// @Summary Get category by id
// @Description Get category by id
// @Tags category
// @Accept json
// @Produce json
// @Param Authorization header string true "Bearer token"
// @Param category body types.DbCategory true "Category"
// @Success 200 {object} types.Message
// @Failure 400 {object} types.ErrorResponse
// @Failure 500 {object} types.ErrorResponse
// @Security ApiKeyAuth
// @Router /category/add [post]
func CategoryAdd(c *gin.Context) {
2024-10-31 22:06:42 +01:00
CreateHandler(&db.Category{}, func(src types.DbCategory, dst *db.Category) {
dst.Name = src.Name
dst.ParentID = src.ParentID
})(c)
2024-08-03 12:40:11 +02:00
}
// @Summary Edit category by id
// @Description Edit category by id
// @Tags category
// @Accept json
// @Produce json
// @Param Authorization header string true "Bearer token"
// @Param categoryID path int true "id"
// @Param category body types.DbCategory true "Category"
// @Success 200 {object} types.DbCategory
// @Failure 400 {object} types.ErrorResponse
// @Failure 401 {object} types.ErrorResponse
2024-10-31 22:06:42 +01:00
// @Failure 403 {object} types.ErrorResponse
2024-08-03 12:40:11 +02:00
// @Failure 500 {object} types.ErrorResponse
// @Security ApiKeyAuth
// @Router /category/edit/:id [put]
func CategoryPutId(c *gin.Context) {
2024-10-31 22:06:42 +01:00
UpdateHandler(
func(src types.DbCategory, dst *db.Category) {
dst.Name = src.Name
dst.ParentID = src.ParentID
},
func(inp *db.Category) types.DbCategory {
return types.DbCategory{
ID: inp.ID,
Name: inp.Name,
ParentID: inp.ParentID,
}
},
)(c)
2024-08-03 12:40:11 +02:00
}
// @Summary Delete category by id
// @Description Delete category by id
// @Tags category
// @Accept json
// @Produce json
// @Param Authorization header string true "Bearer token"
// @Param categoryID path int true "id"
// @Success 200 {object} types.DbCategory
// @Failure 400 {object} types.ErrorResponse
// @Failure 401 {object} types.ErrorResponse
2024-10-31 22:06:42 +01:00
// @Failure 403 {object} types.ErrorResponse
2024-08-03 12:40:11 +02:00
// @Failure 500 {object} types.ErrorResponse
// @Security ApiKeyAuth
// @Router /category/delete/:id [delete]
func CategoryDeleteId(c *gin.Context) {
2024-10-31 22:06:42 +01:00
DeleteHandler[*db.Category]()(c)
2024-08-03 12:40:11 +02:00
}