197 lines
5.7 KiB
Go
197 lines
5.7 KiB
Go
|
|
package controller
|
||
|
|
|
||
|
|
import (
|
||
|
|
"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
|
||
|
|
}
|
||
|
|
|
||
|
|
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
|
||
|
|
// @Description API for creating Product
|
||
|
|
// @Tags Products
|
||
|
|
// @Security Bearer
|
||
|
|
// @Param X-Client-Key header string true "Insert the X-Client-Key"
|
||
|
|
// @Param payload body request.ProductsCreateRequest true "Required payload"
|
||
|
|
// @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 {
|
||
|
|
req := new(request.ProductsCreateRequest)
|
||
|
|
if err := utilVal.ParseAndValidate(c, req); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
dataResult, err := _i.productsService.Create(*req)
|
||
|
|
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
|
||
|
|
// @Description API for updating Product
|
||
|
|
// @Tags Products
|
||
|
|
// @Security Bearer
|
||
|
|
// @Param X-Client-Key header string true "Insert the X-Client-Key"
|
||
|
|
// @Param id path int true "Product ID"
|
||
|
|
// @Param payload body request.ProductsUpdateRequest true "Required payload"
|
||
|
|
// @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
|
||
|
|
}
|
||
|
|
|
||
|
|
req := new(request.ProductsUpdateRequest)
|
||
|
|
if err := utilVal.ParseAndValidate(c, req); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
dataResult, err := _i.productsService.Update(uint(id), *req)
|
||
|
|
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"},
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|