jaecoo-be/app/module/promotions/controller/promotions.controller.go

240 lines
7.1 KiB
Go
Raw Normal View History

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
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
// @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"
// @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 {
// 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
}
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"},
})
}
// 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"
// @Param filename path string true "Promotion File Path"
// @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)
}