2025-11-15 17:43:23 +00:00
|
|
|
package controller
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"jaecoo-be/app/module/promotions/request"
|
|
|
|
|
"jaecoo-be/app/module/promotions/service"
|
|
|
|
|
"jaecoo-be/utils/paginator"
|
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
|
|
|
|
|
|
utilRes "jaecoo-be/utils/response"
|
|
|
|
|
utilVal "jaecoo-be/utils/validator"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type promotionsController struct {
|
|
|
|
|
promotionsService service.PromotionsService
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type PromotionsController interface {
|
|
|
|
|
All(c *fiber.Ctx) error
|
|
|
|
|
Show(c *fiber.Ctx) error
|
|
|
|
|
Save(c *fiber.Ctx) error
|
|
|
|
|
Update(c *fiber.Ctx) error
|
|
|
|
|
Delete(c *fiber.Ctx) error
|
2026-01-20 01:08:14 +00:00
|
|
|
Approve(c *fiber.Ctx) error
|
2026-01-20 03:34:58 +00:00
|
|
|
Reject(c *fiber.Ctx) error
|
2025-11-17 15:30:00 +00:00
|
|
|
Viewer(c *fiber.Ctx) error
|
2025-11-15 17:43:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewPromotionsController(promotionsService service.PromotionsService) PromotionsController {
|
|
|
|
|
return &promotionsController{
|
|
|
|
|
promotionsService: promotionsService,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// All Promotions
|
|
|
|
|
// @Summary Get all Promotions
|
|
|
|
|
// @Description API for getting all Promotions
|
|
|
|
|
// @Tags Promotions
|
|
|
|
|
// @Security Bearer
|
|
|
|
|
// @Param X-Client-Key header string true "Insert the X-Client-Key"
|
|
|
|
|
// @Param req query request.PromotionsQueryRequestContext false "query parameters"
|
|
|
|
|
// @Param req query paginator.Pagination false "pagination parameters"
|
|
|
|
|
// @Success 200 {object} response.Response
|
|
|
|
|
// @Failure 400 {object} response.BadRequestError
|
|
|
|
|
// @Failure 401 {object} response.UnauthorizedError
|
|
|
|
|
// @Failure 500 {object} response.InternalServerError
|
|
|
|
|
// @Router /promotions [get]
|
|
|
|
|
func (_i *promotionsController) All(c *fiber.Ctx) error {
|
|
|
|
|
paginate, err := paginator.Paginate(c)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reqContext := request.PromotionsQueryRequestContext{
|
|
|
|
|
Title: c.Query("title"),
|
|
|
|
|
}
|
|
|
|
|
req := reqContext.ToParamRequest()
|
|
|
|
|
req.Pagination = paginate
|
|
|
|
|
|
|
|
|
|
promotionsData, paging, err := _i.promotionsService.GetAll(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return utilRes.Resp(c, utilRes.Response{
|
|
|
|
|
Success: true,
|
|
|
|
|
Messages: utilRes.Messages{"Promotions list successfully retrieved"},
|
|
|
|
|
Data: promotionsData,
|
|
|
|
|
Meta: paging,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Show Promotion
|
|
|
|
|
// @Summary Get Promotion by ID
|
|
|
|
|
// @Description API for getting Promotion by ID
|
|
|
|
|
// @Tags Promotions
|
|
|
|
|
// @Security Bearer
|
|
|
|
|
// @Param X-Client-Key header string true "Insert the X-Client-Key"
|
|
|
|
|
// @Param id path int true "Promotion ID"
|
|
|
|
|
// @Success 200 {object} response.Response
|
|
|
|
|
// @Failure 400 {object} response.BadRequestError
|
|
|
|
|
// @Failure 401 {object} response.UnauthorizedError
|
|
|
|
|
// @Failure 500 {object} response.InternalServerError
|
|
|
|
|
// @Router /promotions/{id} [get]
|
|
|
|
|
func (_i *promotionsController) Show(c *fiber.Ctx) error {
|
|
|
|
|
id, err := strconv.ParseUint(c.Params("id"), 10, 0)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
promotionData, err := _i.promotionsService.GetOne(uint(id))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return utilRes.Resp(c, utilRes.Response{
|
|
|
|
|
Success: true,
|
|
|
|
|
Messages: utilRes.Messages{"Promotion successfully retrieved"},
|
|
|
|
|
Data: promotionData,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Save Promotion
|
|
|
|
|
// @Summary Create Promotion
|
2025-11-17 15:30:00 +00:00
|
|
|
// @Description API for creating Promotion with file upload
|
2025-11-15 17:43:23 +00:00
|
|
|
// @Tags Promotions
|
|
|
|
|
// @Security Bearer
|
|
|
|
|
// @Param X-Client-Key header string true "Insert the X-Client-Key"
|
2025-11-17 15:30:00 +00:00
|
|
|
// @Param file formData file false "Upload file"
|
|
|
|
|
// @Param title formData string true "Promotion title"
|
|
|
|
|
// @Param description formData string false "Promotion description"
|
2025-11-15 17:43:23 +00:00
|
|
|
// @Success 200 {object} response.Response
|
|
|
|
|
// @Failure 400 {object} response.BadRequestError
|
|
|
|
|
// @Failure 401 {object} response.UnauthorizedError
|
|
|
|
|
// @Failure 500 {object} response.InternalServerError
|
|
|
|
|
// @Router /promotions [post]
|
|
|
|
|
func (_i *promotionsController) Save(c *fiber.Ctx) error {
|
2025-11-17 15:30:00 +00:00
|
|
|
// Parse multipart form
|
|
|
|
|
form, err := c.MultipartForm()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return utilRes.Resp(c, utilRes.Response{
|
|
|
|
|
Success: false,
|
|
|
|
|
Messages: utilRes.Messages{"Failed to parse form data"},
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Extract form values
|
|
|
|
|
req := request.PromotionsCreateRequest{
|
|
|
|
|
Title: c.FormValue("title"),
|
2025-11-15 17:43:23 +00:00
|
|
|
}
|
|
|
|
|
|
2025-11-17 15:30:00 +00:00
|
|
|
if description := c.FormValue("description"); description != "" {
|
|
|
|
|
req.Description = &description
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Validate required fields
|
|
|
|
|
if req.Title == "" {
|
|
|
|
|
return utilRes.Resp(c, utilRes.Response{
|
|
|
|
|
Success: false,
|
|
|
|
|
Messages: utilRes.Messages{"Title is required"},
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if file is uploaded
|
|
|
|
|
if len(form.File["file"]) > 0 {
|
|
|
|
|
// File will be handled in service
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dataResult, err := _i.promotionsService.Create(c, req)
|
2025-11-15 17:43:23 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return utilRes.Resp(c, utilRes.Response{
|
|
|
|
|
Success: true,
|
|
|
|
|
Messages: utilRes.Messages{"Promotion successfully created"},
|
|
|
|
|
Data: dataResult,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update Promotion
|
|
|
|
|
// @Summary Update Promotion
|
|
|
|
|
// @Description API for updating Promotion
|
|
|
|
|
// @Tags Promotions
|
|
|
|
|
// @Security Bearer
|
|
|
|
|
// @Param X-Client-Key header string true "Insert the X-Client-Key"
|
|
|
|
|
// @Param id path int true "Promotion ID"
|
|
|
|
|
// @Param payload body request.PromotionsUpdateRequest true "Required payload"
|
|
|
|
|
// @Success 200 {object} response.Response
|
|
|
|
|
// @Failure 400 {object} response.BadRequestError
|
|
|
|
|
// @Failure 401 {object} response.UnauthorizedError
|
|
|
|
|
// @Failure 500 {object} response.InternalServerError
|
|
|
|
|
// @Router /promotions/{id} [put]
|
|
|
|
|
func (_i *promotionsController) Update(c *fiber.Ctx) error {
|
|
|
|
|
id, err := strconv.ParseUint(c.Params("id"), 10, 0)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
req := new(request.PromotionsUpdateRequest)
|
|
|
|
|
if err := utilVal.ParseAndValidate(c, req); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dataResult, err := _i.promotionsService.Update(uint(id), *req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return utilRes.Resp(c, utilRes.Response{
|
|
|
|
|
Success: true,
|
|
|
|
|
Messages: utilRes.Messages{"Promotion successfully updated"},
|
|
|
|
|
Data: dataResult,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Delete Promotion
|
|
|
|
|
// @Summary Delete Promotion
|
|
|
|
|
// @Description API for deleting Promotion (soft delete)
|
|
|
|
|
// @Tags Promotions
|
|
|
|
|
// @Security Bearer
|
|
|
|
|
// @Param X-Client-Key header string true "Insert the X-Client-Key"
|
|
|
|
|
// @Param id path int true "Promotion ID"
|
|
|
|
|
// @Success 200 {object} response.Response
|
|
|
|
|
// @Failure 400 {object} response.BadRequestError
|
|
|
|
|
// @Failure 401 {object} response.UnauthorizedError
|
|
|
|
|
// @Failure 500 {object} response.InternalServerError
|
|
|
|
|
// @Router /promotions/{id} [delete]
|
|
|
|
|
func (_i *promotionsController) Delete(c *fiber.Ctx) error {
|
|
|
|
|
id, err := strconv.ParseUint(c.Params("id"), 10, 0)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = _i.promotionsService.Delete(uint(id))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return utilRes.Resp(c, utilRes.Response{
|
|
|
|
|
Success: true,
|
|
|
|
|
Messages: utilRes.Messages{"Promotion successfully deleted"},
|
|
|
|
|
})
|
|
|
|
|
}
|
2025-11-17 15:30:00 +00:00
|
|
|
|
2026-01-20 01:08:14 +00:00
|
|
|
// Approve Promotion
|
|
|
|
|
// @Summary Approve Promotion
|
|
|
|
|
// @Description API for approving Promotion (only for admin with roleId = 1)
|
|
|
|
|
// @Tags Promotions
|
|
|
|
|
// @Security Bearer
|
|
|
|
|
// @Param X-Client-Key header string true "Insert the X-Client-Key"
|
|
|
|
|
// @Param id path int true "Promotion ID"
|
|
|
|
|
// @Success 200 {object} response.Response
|
|
|
|
|
// @Failure 400 {object} response.BadRequestError
|
|
|
|
|
// @Failure 401 {object} response.UnauthorizedError
|
|
|
|
|
// @Failure 500 {object} response.InternalServerError
|
|
|
|
|
// @Router /promotions/{id}/approve [put]
|
|
|
|
|
func (_i *promotionsController) Approve(c *fiber.Ctx) error {
|
|
|
|
|
id, err := strconv.ParseUint(c.Params("id"), 10, 0)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-20 03:34:58 +00:00
|
|
|
// Get token from Authorization header
|
|
|
|
|
authToken := c.Get("Authorization")
|
|
|
|
|
if authToken == "" {
|
2026-01-20 01:08:14 +00:00
|
|
|
return utilRes.Resp(c, utilRes.Response{
|
|
|
|
|
Success: false,
|
2026-01-20 03:34:58 +00:00
|
|
|
Messages: utilRes.Messages{"Unauthorized: token not found"},
|
2026-01-20 01:08:14 +00:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-20 03:34:58 +00:00
|
|
|
promotionData, err := _i.promotionsService.Approve(uint(id), authToken)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
2026-01-20 01:08:14 +00:00
|
|
|
}
|
|
|
|
|
|
2026-01-20 03:34:58 +00:00
|
|
|
return utilRes.Resp(c, utilRes.Response{
|
|
|
|
|
Success: true,
|
|
|
|
|
Messages: utilRes.Messages{"Promotion successfully approved"},
|
|
|
|
|
Data: promotionData,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Reject Promotion
|
|
|
|
|
// @Summary Reject Promotion
|
|
|
|
|
// @Description API for rejecting Promotion (only for admin with roleId = 1)
|
|
|
|
|
// @Tags Promotions
|
|
|
|
|
// @Security Bearer
|
|
|
|
|
// @Param X-Client-Key header string true "Insert the X-Client-Key"
|
|
|
|
|
// @Param id path int true "Promotion ID"
|
|
|
|
|
// @Param message body string false "Rejection message"
|
|
|
|
|
// @Success 200 {object} response.Response
|
|
|
|
|
// @Failure 400 {object} response.BadRequestError
|
|
|
|
|
// @Failure 401 {object} response.UnauthorizedError
|
|
|
|
|
// @Failure 500 {object} response.InternalServerError
|
|
|
|
|
// @Router /promotions/{id}/reject [put]
|
|
|
|
|
func (_i *promotionsController) Reject(c *fiber.Ctx) error {
|
|
|
|
|
id, err := strconv.ParseUint(c.Params("id"), 10, 0)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get token from Authorization header
|
|
|
|
|
authToken := c.Get("Authorization")
|
|
|
|
|
if authToken == "" {
|
|
|
|
|
return utilRes.Resp(c, utilRes.Response{
|
|
|
|
|
Success: false,
|
|
|
|
|
Messages: utilRes.Messages{"Unauthorized: token not found"},
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get optional message from request body
|
|
|
|
|
var body struct {
|
|
|
|
|
Message *string `json:"message"`
|
|
|
|
|
}
|
|
|
|
|
if err := c.BodyParser(&body); err != nil {
|
|
|
|
|
body.Message = nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
promotionData, err := _i.promotionsService.Reject(uint(id), authToken, body.Message)
|
2026-01-20 01:08:14 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return utilRes.Resp(c, utilRes.Response{
|
|
|
|
|
Success: true,
|
2026-01-20 03:34:58 +00:00
|
|
|
Messages: utilRes.Messages{"Promotion successfully rejected"},
|
2026-01-20 01:08:14 +00:00
|
|
|
Data: promotionData,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-17 15:30:00 +00:00
|
|
|
// Viewer Promotion
|
|
|
|
|
// @Summary Viewer Promotion
|
|
|
|
|
// @Description API for viewing Promotion file
|
|
|
|
|
// @Tags Promotions
|
|
|
|
|
// @Security Bearer
|
|
|
|
|
// @Param X-Client-Key header string true "Insert the X-Client-Key"
|
2025-11-17 15:44:55 +00:00
|
|
|
// @Param filename path string true "Promotion File Name (e.g., user_277788.png)"
|
2025-11-17 15:30:00 +00:00
|
|
|
// @Success 200 {file} file
|
|
|
|
|
// @Failure 400 {object} response.BadRequestError
|
|
|
|
|
// @Failure 401 {object} response.UnauthorizedError
|
|
|
|
|
// @Failure 500 {object} response.InternalServerError
|
|
|
|
|
// @Router /promotions/viewer/{filename} [get]
|
|
|
|
|
func (_i *promotionsController) Viewer(c *fiber.Ctx) error {
|
|
|
|
|
return _i.promotionsService.Viewer(c)
|
|
|
|
|
}
|