2025-11-15 17:43:23 +00:00
|
|
|
package controller
|
|
|
|
|
|
|
|
|
|
import (
|
2025-11-17 15:30:00 +00:00
|
|
|
"encoding/json"
|
2025-11-15 17:43:23 +00:00
|
|
|
"jaecoo-be/app/module/products/request"
|
|
|
|
|
"jaecoo-be/app/module/products/service"
|
|
|
|
|
"jaecoo-be/utils/paginator"
|
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
|
|
|
|
|
|
utilRes "jaecoo-be/utils/response"
|
|
|
|
|
utilVal "jaecoo-be/utils/validator"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type productsController struct {
|
|
|
|
|
productsService service.ProductsService
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type ProductsController 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
|
2025-11-17 15:30:00 +00:00
|
|
|
Viewer(c *fiber.Ctx) error
|
2025-11-15 17:43:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewProductsController(productsService service.ProductsService) ProductsController {
|
|
|
|
|
return &productsController{
|
|
|
|
|
productsService: productsService,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// All Products
|
|
|
|
|
// @Summary Get all Products
|
|
|
|
|
// @Description API for getting all Products
|
|
|
|
|
// @Tags Products
|
|
|
|
|
// @Security Bearer
|
|
|
|
|
// @Param X-Client-Key header string true "Insert the X-Client-Key"
|
|
|
|
|
// @Param req query request.ProductsQueryRequestContext 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 /products [get]
|
|
|
|
|
func (_i *productsController) All(c *fiber.Ctx) error {
|
|
|
|
|
paginate, err := paginator.Paginate(c)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reqContext := request.ProductsQueryRequestContext{
|
|
|
|
|
Title: c.Query("title"),
|
|
|
|
|
Variant: c.Query("variant"),
|
|
|
|
|
}
|
|
|
|
|
req := reqContext.ToParamRequest()
|
|
|
|
|
req.Pagination = paginate
|
|
|
|
|
|
|
|
|
|
productsData, paging, err := _i.productsService.GetAll(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return utilRes.Resp(c, utilRes.Response{
|
|
|
|
|
Success: true,
|
|
|
|
|
Messages: utilRes.Messages{"Products list successfully retrieved"},
|
|
|
|
|
Data: productsData,
|
|
|
|
|
Meta: paging,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Show Product
|
|
|
|
|
// @Summary Get Product by ID
|
|
|
|
|
// @Description API for getting Product by ID
|
|
|
|
|
// @Tags Products
|
|
|
|
|
// @Security Bearer
|
|
|
|
|
// @Param X-Client-Key header string true "Insert the X-Client-Key"
|
|
|
|
|
// @Param id path int true "Product ID"
|
|
|
|
|
// @Success 200 {object} response.Response
|
|
|
|
|
// @Failure 400 {object} response.BadRequestError
|
|
|
|
|
// @Failure 401 {object} response.UnauthorizedError
|
|
|
|
|
// @Failure 500 {object} response.InternalServerError
|
|
|
|
|
// @Router /products/{id} [get]
|
|
|
|
|
func (_i *productsController) Show(c *fiber.Ctx) error {
|
|
|
|
|
id, err := strconv.ParseUint(c.Params("id"), 10, 0)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
productData, err := _i.productsService.GetOne(uint(id))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return utilRes.Resp(c, utilRes.Response{
|
|
|
|
|
Success: true,
|
|
|
|
|
Messages: utilRes.Messages{"Product successfully retrieved"},
|
|
|
|
|
Data: productData,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Save Product
|
|
|
|
|
// @Summary Create Product
|
2025-11-17 15:30:00 +00:00
|
|
|
// @Description API for creating Product with file upload
|
2025-11-15 17:43:23 +00:00
|
|
|
// @Tags Products
|
|
|
|
|
// @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 "Product title"
|
|
|
|
|
// @Param variant formData string false "Product variant"
|
2025-11-17 15:44:55 +00:00
|
|
|
// @Param price formData string false "Product price"
|
2025-11-17 15:30:00 +00:00
|
|
|
// @Param colors formData string false "Product colors (JSON array)"
|
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 /products [post]
|
|
|
|
|
func (_i *productsController) 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.ProductsCreateRequest{
|
|
|
|
|
Title: c.FormValue("title"),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if variant := c.FormValue("variant"); variant != "" {
|
|
|
|
|
req.Variant = &variant
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-17 15:44:55 +00:00
|
|
|
if price := c.FormValue("price"); price != "" {
|
|
|
|
|
req.Price = &price
|
2025-11-15 17:43:23 +00:00
|
|
|
}
|
|
|
|
|
|
2025-11-17 15:30:00 +00:00
|
|
|
// Handle colors (JSON array string)
|
|
|
|
|
if colorsStr := c.FormValue("colors"); colorsStr != "" {
|
|
|
|
|
var colors []string
|
|
|
|
|
if err := json.Unmarshal([]byte(colorsStr), &colors); err == nil {
|
|
|
|
|
req.Colors = colors
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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.productsService.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{"Product successfully created"},
|
|
|
|
|
Data: dataResult,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update Product
|
|
|
|
|
// @Summary Update Product
|
2025-11-19 04:18:19 +00:00
|
|
|
// @Description API for updating Product with file upload
|
2025-11-15 17:43:23 +00:00
|
|
|
// @Tags Products
|
|
|
|
|
// @Security Bearer
|
|
|
|
|
// @Param X-Client-Key header string true "Insert the X-Client-Key"
|
|
|
|
|
// @Param id path int true "Product ID"
|
2025-11-19 04:18:19 +00:00
|
|
|
// @Param file formData file false "Upload file"
|
|
|
|
|
// @Param title formData string false "Product title"
|
|
|
|
|
// @Param variant formData string false "Product variant"
|
|
|
|
|
// @Param price formData string false "Product price"
|
|
|
|
|
// @Param colors formData string false "Product colors (JSON array)"
|
|
|
|
|
// @Param is_active formData bool false "Product is_active"
|
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 /products/{id} [put]
|
|
|
|
|
func (_i *productsController) Update(c *fiber.Ctx) error {
|
|
|
|
|
id, err := strconv.ParseUint(c.Params("id"), 10, 0)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-19 04:18:19 +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.ProductsUpdateRequest{}
|
|
|
|
|
|
|
|
|
|
if title := c.FormValue("title"); title != "" {
|
|
|
|
|
req.Title = &title
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if variant := c.FormValue("variant"); variant != "" {
|
|
|
|
|
req.Variant = &variant
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if price := c.FormValue("price"); price != "" {
|
|
|
|
|
req.Price = &price
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Handle colors (JSON array string)
|
|
|
|
|
if colorsStr := c.FormValue("colors"); colorsStr != "" {
|
|
|
|
|
var colors []string
|
|
|
|
|
if err := json.Unmarshal([]byte(colorsStr), &colors); err == nil {
|
|
|
|
|
req.Colors = colors
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if isActiveStr := c.FormValue("is_active"); isActiveStr != "" {
|
|
|
|
|
isActive := isActiveStr == "true" || isActiveStr == "1"
|
|
|
|
|
req.IsActive = &isActive
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if file is uploaded
|
|
|
|
|
if len(form.File["file"]) > 0 {
|
|
|
|
|
// File will be handled in service
|
2025-11-15 17:43:23 +00:00
|
|
|
}
|
|
|
|
|
|
2025-11-19 04:18:19 +00:00
|
|
|
dataResult, err := _i.productsService.Update(c, uint(id), 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{"Product successfully updated"},
|
|
|
|
|
Data: dataResult,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Delete Product
|
|
|
|
|
// @Summary Delete Product
|
|
|
|
|
// @Description API for deleting Product (soft delete)
|
|
|
|
|
// @Tags Products
|
|
|
|
|
// @Security Bearer
|
|
|
|
|
// @Param X-Client-Key header string true "Insert the X-Client-Key"
|
|
|
|
|
// @Param id path int true "Product ID"
|
|
|
|
|
// @Success 200 {object} response.Response
|
|
|
|
|
// @Failure 400 {object} response.BadRequestError
|
|
|
|
|
// @Failure 401 {object} response.UnauthorizedError
|
|
|
|
|
// @Failure 500 {object} response.InternalServerError
|
|
|
|
|
// @Router /products/{id} [delete]
|
|
|
|
|
func (_i *productsController) Delete(c *fiber.Ctx) error {
|
|
|
|
|
id, err := strconv.ParseUint(c.Params("id"), 10, 0)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = _i.productsService.Delete(uint(id))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return utilRes.Resp(c, utilRes.Response{
|
|
|
|
|
Success: true,
|
|
|
|
|
Messages: utilRes.Messages{"Product successfully deleted"},
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-17 15:30:00 +00:00
|
|
|
// Viewer Product
|
|
|
|
|
// @Summary Viewer Product
|
|
|
|
|
// @Description API for viewing Product file
|
|
|
|
|
// @Tags Products
|
|
|
|
|
// @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 "Product 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 /products/viewer/{filename} [get]
|
|
|
|
|
func (_i *productsController) Viewer(c *fiber.Ctx) error {
|
|
|
|
|
return _i.productsService.Viewer(c)
|
|
|
|
|
}
|