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 } 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 // @Tags Promotions // @Security Bearer // @Param X-Client-Key header string true "Insert the X-Client-Key" // @Param payload body request.PromotionsCreateRequest 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 [post] func (_i *promotionsController) Save(c *fiber.Ctx) error { req := new(request.PromotionsCreateRequest) if err := utilVal.ParseAndValidate(c, req); err != nil { return err } dataResult, err := _i.promotionsService.Create(*req) 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"}, }) }