From 30c6c59e630f34a3877bbf81ea1e422713f9003b Mon Sep 17 00:00:00 2001 From: qowevisa Date: Thu, 31 Oct 2024 11:41:49 +0200 Subject: [PATCH] Add type handlers --- cmd/http-server/main.go | 7 +++ handlers/type.go | 104 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 handlers/type.go diff --git a/cmd/http-server/main.go b/cmd/http-server/main.go index 39b2e11..5a6af1d 100644 --- a/cmd/http-server/main.go +++ b/cmd/http-server/main.go @@ -77,6 +77,13 @@ func main() { incomeRoutes.PUT("/edit/:id", handlers.IncomePutId) incomeRoutes.DELETE("/delete/:id", handlers.IncomeDeleteId) } + typesRoutes := api.Group("/type", middleware.AuthMiddleware()) + { + typesRoutes.POST("/add", handlers.TypeAdd) + typesRoutes.GET("/:id", handlers.TypeGetId) + typesRoutes.PUT("/edit/:id", handlers.TypePutId) + typesRoutes.DELETE("/delete/:id", handlers.TypeDeleteId) + } } r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) diff --git a/handlers/type.go b/handlers/type.go new file mode 100644 index 0000000..0705aff --- /dev/null +++ b/handlers/type.go @@ -0,0 +1,104 @@ +package handlers + +import ( + "git.qowevisa.me/Qowevisa/gonuts/db" + "git.qowevisa.me/Qowevisa/gonuts/types" + "github.com/gin-gonic/gin" +) + +// @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(func(inp *db.Type) types.DbType { + return types.DbType{ + ID: inp.ID, + Name: inp.Name, + Comment: inp.Comment, + Color: inp.Color, + } + })(c) +} + +// @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 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 + }, + func(inp *db.Type) types.DbType { + return types.DbType{ + ID: inp.ID, + Name: inp.Name, + Comment: inp.Comment, + Color: inp.Color, + } + }, + )(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) +}