-
Notifications
You must be signed in to change notification settings - Fork 1
/
promotion_handler.go
107 lines (92 loc) · 2.41 KB
/
promotion_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package api
import (
"strconv"
"github.com/Furkan-Gulsen/Checkout-System/internal/application"
"github.com/Furkan-Gulsen/Checkout-System/internal/interfaces/dto"
"github.com/gin-gonic/gin"
)
type PromotionHandler struct {
promotionApp application.PromotionAppInterface
}
func NewPromotionHandler(promotionApp application.PromotionAppInterface) *PromotionHandler {
return &PromotionHandler{
promotionApp: promotionApp,
}
}
// @Summary List promotions
// @Description Get a list of promotions
// @Tags Promotion
// @Produce json
// @Success 200 {object} []entity.Promotion
// @Router /api/v1/promotion/list [get]
func (h *PromotionHandler) List(c *gin.Context) {
promotions, err := h.promotionApp.List()
if err != nil {
c.JSON(500, gin.H{"message": err.Error()})
return
}
c.JSON(200, gin.H{
"message": "Promotions listed successfully",
"data": promotions,
})
}
// @Summary Create a promotion
// @Description Create a new promotion
// @Tags Promotion
// @Accept json
// @Produce json
// @Param promotion body dto.PromotionRequest true "Promotion object"
// @Success 200 {string} string "Promotion created successfully"
// @Router /api/v1/promotion [post]
func (h *PromotionHandler) Create(c *gin.Context) {
var data dto.PromotionRequest
if err := c.ShouldBindJSON(&data); err != nil {
c.JSON(400, gin.H{"message": err.Error()})
return
}
entityData := data.ToEntity()
err := entityData.Validate()
if err != nil {
c.JSON(400, gin.H{
"message": err.Error(),
})
return
}
response, createErr := h.promotionApp.Create(&entityData)
if createErr != nil {
c.JSON(500, gin.H{"message": err.Error()})
return
}
c.JSON(200, gin.H{
"message": "Promotion created successfully",
"data": response,
})
}
// @Summary Get a promotion by ID
// @Description Get a promotion by its ID
// @Tags Promotion
// @Produce json
// @Param id path int true "Promotion ID"
// @Success 200 {object} string
// @Router /api/v1/promotion/{id} [get]
func (h *PromotionHandler) GetById(c *gin.Context) {
paramID := c.Param("id")
if paramID == "" {
c.JSON(400, gin.H{"message": "ID is required"})
return
}
id, err := strconv.Atoi(paramID)
if err != nil {
c.JSON(400, gin.H{"message": "Invalid ID format"})
return
}
promotion, err := h.promotionApp.GetById(id)
if err != nil {
c.JSON(500, gin.H{"message": err.Error()})
return
}
c.JSON(200, gin.H{
"message": "Promotion found",
"data": promotion,
})
}