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 Approve(c *fiber.Ctx) error Reject(c *fiber.Ctx) error Comment(c *fiber.Ctx) error Viewer(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 with file upload // @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" // @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"), } 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) 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"}, }) } // 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 } // 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"}, }) } promotionData, err := _i.promotionsService.Approve(uint(id), authToken) if err != nil { return err } 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) if err != nil { return err } return utilRes.Resp(c, utilRes.Response{ Success: true, Messages: utilRes.Messages{"Promotion successfully rejected"}, Data: promotionData, }) } // Comment Banner // @Summary Comment Banner // @Description API for comment Banner (only admin) // @Tags Banners // @Security BearerAuth // @Param id path int true "Banner ID" // @Param body body request.CommentRequest true "Comment payload" // @Success 200 {object} response.Response // @Router /promotions/{id}/comment [put] func (_i *promotionsController) Comment(c *fiber.Ctx) error { id, err := strconv.ParseUint(c.Params("id"), 10, 0) if err != nil { return err } authToken := c.Get("Authorization") if authToken == "" { return utilRes.Resp(c, utilRes.Response{ Success: false, Messages: utilRes.Messages{"Unauthorized"}, }) } var req request.CommentRequest if err := c.BodyParser(&req); err != nil { return utilRes.Resp(c, utilRes.Response{ Success: false, Messages: utilRes.Messages{"Invalid request"}, }) } bannerData, err := _i.promotionsService.Comment(uint(id), authToken, &req.Message) if err != nil { return err } return utilRes.Resp(c, utilRes.Response{ Success: true, Messages: utilRes.Messages{"Komentar berhasil disimpan"}, Data: bannerData, }) } // 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 Name (e.g., user_277788.png)" // @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) }