diff --git a/cmd/http-server/main.go b/cmd/http-server/main.go index 6ee205b..ea96186 100644 --- a/cmd/http-server/main.go +++ b/cmd/http-server/main.go @@ -127,6 +127,7 @@ func main() { paymentRoutes := api.Group("/payment", middleware.AuthMiddleware()) { paymentRoutes.POST("/add", handlers.PaymentAdd) + paymentRoutes.GET("/all", handlers.PaymentGetAll) } currencyRoutes := api.Group("/currency", middleware.AuthMiddleware()) { diff --git a/handlers/payment.go b/handlers/payment.go index 7e9c3fb..976b2ec 100644 --- a/handlers/payment.go +++ b/handlers/payment.go @@ -229,3 +229,33 @@ func PaymentAdd(c *gin.Context) { c.JSON(200, types.Message{Info: fmt.Sprintf("Entity with %d ID is created successfully!", payment.ID)}) } + +// @Summary Get all payments for user +// @Description Get all payments for user +// @Tags type +// @Produce json +// @Param Authorization header string true "Bearer token" +// @Success 200 {object} []types.DbPayment +// @Failure 401 {object} types.ErrorResponse +// @Failure 500 {object} types.ErrorResponse +// @Security ApiKeyAuth +// @Router /payment/all [get] +func PaymentGetAll(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.Payment + if err := dbc.Preload("Items.Item").Find(&entities, db.Payment{UserID: userID}).Error; err != nil { + c.JSON(500, types.ErrorResponse{Message: err.Error()}) + return + } + + var ret []types.DbPayment + for _, entity := range entities { + ret = append(ret, paymentTransform(entity)) + } + c.JSON(200, ret) +}