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"
|
|
|
|
)
|
|
|
|
|
2024-11-11 21:27:16 +01:00
|
|
|
var categoryTransform func(*db.Category) types.DbCategory = func(inp *db.Category) types.DbCategory {
|
|
|
|
return types.DbCategory{
|
|
|
|
ID: inp.ID,
|
|
|
|
Name: inp.Name,
|
|
|
|
ParentID: inp.ParentID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 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-11-11 21:27:16 +01:00
|
|
|
GetHandler(categoryTransform)(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// @Summary Get all categories for user
|
|
|
|
// @Description Get all categories for user
|
|
|
|
// @Tags type
|
|
|
|
// @Produce json
|
|
|
|
// @Param Authorization header string true "Bearer token"
|
|
|
|
// @Success 200 {object} []types.DbCategory
|
|
|
|
// @Failure 401 {object} types.ErrorResponse
|
|
|
|
// @Failure 500 {object} types.ErrorResponse
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Router /category/all [get]
|
|
|
|
func CategoryGetAll(c *gin.Context) {
|
|
|
|
userID, err := GetUserID(c)
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(500, types.ErrorResponse{Message: err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dbc := db.Connect()
|
|
|
|
var entities []*db.Category
|
|
|
|
if err := dbc.Find(&entities, db.Category{UserID: userID}).Error; err != nil {
|
|
|
|
c.JSON(500, types.ErrorResponse{Message: err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var ret []types.DbCategory
|
|
|
|
for _, entity := range entities {
|
|
|
|
ret = append(ret, categoryTransform(entity))
|
|
|
|
}
|
|
|
|
c.JSON(200, ret)
|
2024-08-03 12:40:11 +02:00
|
|
|
}
|
|
|
|
|
2024-11-16 10:45:47 +01:00
|
|
|
// @Summary Add category
|
|
|
|
// @Description Add category
|
2024-08-03 12:40:11 +02:00
|
|
|
// @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
|
|
|
|
},
|
2024-11-11 21:27:16 +01:00
|
|
|
categoryTransform,
|
2024-10-31 22:06:42 +01:00
|
|
|
)(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
|
|
|
}
|