fin-check-api/handlers/type.go

130 lines
3.5 KiB
Go
Raw Permalink Normal View History

2024-10-31 10:41:49 +01: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-10-31 10:41:49 +01:00
"github.com/gin-gonic/gin"
)
var typeTransform func(inp *db.Type) types.DbType = func(inp *db.Type) types.DbType {
return types.DbType{
ID: inp.ID,
Name: inp.Name,
Comment: inp.Comment,
Color: inp.Color,
}
}
2024-10-31 10:41:49 +01:00
// @Summary Get dbtype by id
// @Description Get dbtype by id
// @Tags dbtype
// @Accept json
// @Produce json
// @Param Authorization header string true "Bearer token"
// @Param dbtype path int true "id"
// @Success 200 {object} types.DbType
// @Failure 400 {object} types.ErrorResponse
// @Failure 401 {object} types.ErrorResponse
// @Failure 403 {object} types.ErrorResponse
// @Failure 500 {object} types.ErrorResponse
// @Security ApiKeyAuth
// @Router /dbtype/:id [get]
func TypeGetId(c *gin.Context) {
GetHandler(typeTransform)(c)
}
// @Summary Get all types for user
// @Description Get all types for user
// @Tags type
// @Produce json
// @Param Authorization header string true "Bearer token"
// @Success 200 {object} []types.DbType
// @Failure 401 {object} types.ErrorResponse
// @Failure 500 {object} types.ErrorResponse
// @Security ApiKeyAuth
// @Router /type/all [get]
func TypeGetAll(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.Type
if err := dbc.Find(&entities, db.Type{UserID: userID}).Error; err != nil {
c.JSON(500, types.ErrorResponse{Message: err.Error()})
return
}
var ret []types.DbType
for _, entity := range entities {
ret = append(ret, typeTransform(entity))
}
c.JSON(200, ret)
2024-10-31 10:41:49 +01:00
}
2024-11-16 10:45:47 +01:00
// @Summary Add dbtype
// @Description Add dbtype
2024-10-31 10:41:49 +01:00
// @Tags dbtype
// @Accept json
// @Produce json
// @Param Authorization header string true "Bearer token"
// @Param dbtype body types.DbType true "Type"
// @Success 200 {object} types.Message
// @Failure 400 {object} types.ErrorResponse
// @Failure 500 {object} types.ErrorResponse
// @Security ApiKeyAuth
// @Router /dbtype/add [post]
func TypeAdd(c *gin.Context) {
t := &db.Type{}
CreateHandler(t, func(update types.DbType, dst *db.Type) {
dst.Name = update.Name
dst.Comment = update.Comment
dst.Color = update.Color
})(c)
}
// @Summary Edit dbtype by id
// @Description Edit dbtype by id
// @Tags dbtype
// @Accept json
// @Produce json
// @Param Authorization header string true "Bearer token"
// @Param dbtypeID path int true "id"
// @Param dbtype body types.DbType true "Type"
// @Success 200 {object} types.DbType
// @Failure 400 {object} types.ErrorResponse
// @Failure 401 {object} types.ErrorResponse
// @Failure 403 {object} types.ErrorResponse
// @Failure 500 {object} types.ErrorResponse
// @Security ApiKeyAuth
// @Router /dbtype/edit/:id [put]
func TypePutId(c *gin.Context) {
UpdateHandler(
func(updates types.DbType, dst *db.Type) {
dst.Name = updates.Name
dst.Comment = updates.Comment
dst.Color = updates.Color
},
typeTransform,
2024-10-31 10:41:49 +01:00
)(c)
}
// @Summary Delete dbtype by id
// @Description Delete dbtype by id
// @Tags dbtype
// @Accept json
// @Produce json
// @Param Authorization header string true "Bearer token"
// @Param dbtypeID path int true "id"
// @Success 200 {object} types.DbType
// @Failure 400 {object} types.ErrorResponse
// @Failure 401 {object} types.ErrorResponse
// @Failure 403 {object} types.ErrorResponse
// @Failure 500 {object} types.ErrorResponse
// @Security ApiKeyAuth
// @Router /dbtype/delete/:id [delete]
func TypeDeleteId(c *gin.Context) {
DeleteHandler[*db.Type]()(c)
}