Add payment/all route and handler

This commit is contained in:
qowevisa 2024-11-28 15:08:41 +02:00
parent b6f393bd46
commit 2e621a6841
2 changed files with 31 additions and 0 deletions

View File

@ -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())
{

View File

@ -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)
}