Reforming and adding all endpoint for types

This commit is contained in:
qowevisa 2024-11-11 21:30:40 +02:00
parent ba94ad6084
commit 9b77237d50
2 changed files with 42 additions and 16 deletions

View File

@ -83,6 +83,7 @@ func main() {
{ {
typesRoutes.POST("/add", handlers.TypeAdd) typesRoutes.POST("/add", handlers.TypeAdd)
typesRoutes.GET("/:id", handlers.TypeGetId) typesRoutes.GET("/:id", handlers.TypeGetId)
typesRoutes.GET("/all", handlers.TypeGetAll)
typesRoutes.PUT("/edit/:id", handlers.TypePutId) typesRoutes.PUT("/edit/:id", handlers.TypePutId)
typesRoutes.DELETE("/delete/:id", handlers.TypeDeleteId) typesRoutes.DELETE("/delete/:id", handlers.TypeDeleteId)
} }

View File

@ -6,6 +6,15 @@ import (
"github.com/gin-gonic/gin" "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,
}
}
// @Summary Get dbtype by id // @Summary Get dbtype by id
// @Description Get dbtype by id // @Description Get dbtype by id
// @Tags dbtype // @Tags dbtype
@ -21,14 +30,37 @@ import (
// @Security ApiKeyAuth // @Security ApiKeyAuth
// @Router /dbtype/:id [get] // @Router /dbtype/:id [get]
func TypeGetId(c *gin.Context) { func TypeGetId(c *gin.Context) {
GetHandler(func(inp *db.Type) types.DbType { GetHandler(typeTransform)(c)
return types.DbType{ }
ID: inp.ID,
Name: inp.Name, // @Summary Get all types for user
Comment: inp.Comment, // @Description Get all types for user
Color: inp.Color, // @Tags type
} // @Produce json
})(c) // @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)
} }
// @Summary Get dbtype by id // @Summary Get dbtype by id
@ -74,14 +106,7 @@ func TypePutId(c *gin.Context) {
dst.Comment = updates.Comment dst.Comment = updates.Comment
dst.Color = updates.Color dst.Color = updates.Color
}, },
func(inp *db.Type) types.DbType { typeTransform,
return types.DbType{
ID: inp.ID,
Name: inp.Name,
Comment: inp.Comment,
Color: inp.Color,
}
},
)(c) )(c)
} }