From 83f69a2135ba35dd20def5e8390659bf09d57b72 Mon Sep 17 00:00:00 2001 From: qowevisa Date: Tue, 19 Nov 2024 10:12:04 +0200 Subject: [PATCH] Add /all endpoint and handler for income --- cmd/http-server/main.go | 1 + handlers/income.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/cmd/http-server/main.go b/cmd/http-server/main.go index c6502ea..240af3f 100644 --- a/cmd/http-server/main.go +++ b/cmd/http-server/main.go @@ -77,6 +77,7 @@ func main() { { incomeRoutes.POST("/add", handlers.IncomeAdd) incomeRoutes.GET("/:id", handlers.IncomeGetId) + incomeRoutes.GET("/all", handlers.IncomeGetAll) incomeRoutes.PUT("/edit/:id", handlers.IncomePutId) incomeRoutes.DELETE("/delete/:id", handlers.IncomeDeleteId) } diff --git a/handlers/income.go b/handlers/income.go index 5418db9..8673048 100644 --- a/handlers/income.go +++ b/handlers/income.go @@ -33,6 +33,36 @@ func IncomeGetId(c *gin.Context) { GetHandler(incomeTransform)(c) } +// @Summary Get all incomes for user +// @Description Get all incomes for user +// @Tags type +// @Produce json +// @Param Authorization header string true "Bearer token" +// @Success 200 {object} []types.DbIncome +// @Failure 401 {object} types.ErrorResponse +// @Failure 500 {object} types.ErrorResponse +// @Security ApiKeyAuth +// @Router /income/all [get] +func IncomeGetAll(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.Income + if err := dbc.Find(&entities, db.Income{UserID: userID}).Error; err != nil { + c.JSON(500, types.ErrorResponse{Message: err.Error()}) + return + } + + var ret []types.DbIncome + for _, entity := range entities { + ret = append(ret, incomeTransform(entity)) + } + c.JSON(200, ret) +} + // @Summary Add income // @Description Add income // @Tags income