214 lines
5.0 KiB
Go
214 lines
5.0 KiB
Go
package http
|
|
|
|
import (
|
|
"enaklo-pos-be/internal/common/errors"
|
|
"enaklo-pos-be/internal/entity"
|
|
"enaklo-pos-be/internal/handlers/request"
|
|
"enaklo-pos-be/internal/handlers/response"
|
|
"enaklo-pos-be/internal/services/v2/inprogress_order"
|
|
"enaklo-pos-be/internal/services/v2/product"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/go-playground/validator/v10"
|
|
"net/http"
|
|
"strconv"
|
|
)
|
|
|
|
type MenuHandler struct {
|
|
service product.Service
|
|
orderService inprogress_order.InProgressOrderService
|
|
}
|
|
|
|
func NewMenuHandler(service product.Service, orderService inprogress_order.InProgressOrderService,
|
|
) *MenuHandler {
|
|
return &MenuHandler{
|
|
service: service,
|
|
orderService: orderService,
|
|
}
|
|
}
|
|
|
|
func (h *MenuHandler) Route(group *gin.RouterGroup, jwt gin.HandlerFunc) {
|
|
route := group.Group("/menu")
|
|
|
|
route.GET("/:partner_id", h.GetProducts)
|
|
route.POST("/order/create", h.OrderCreate)
|
|
route.POST("/order/member/create", jwt, h.OrderMemberCreate)
|
|
route.GET("/order", h.GetOrderID)
|
|
}
|
|
|
|
func (h *MenuHandler) GetProducts(c *gin.Context) {
|
|
ctx := request.GetMyContext(c)
|
|
|
|
partnerIDParam := c.Param("partner_id")
|
|
partnerID, err := strconv.ParseInt(partnerIDParam, 10, 64)
|
|
if err != nil {
|
|
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
|
return
|
|
}
|
|
|
|
if partnerID <= 0 {
|
|
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
|
return
|
|
}
|
|
|
|
var req request.ProductParam
|
|
if err := c.ShouldBindQuery(&req); err != nil {
|
|
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
|
return
|
|
}
|
|
|
|
searchParam := req.ToEntity()
|
|
searchParam.PartnerID = partnerID
|
|
|
|
products, total, err := h.service.GetProductsByPartnerID(ctx, searchParam)
|
|
if err != nil {
|
|
response.ErrorWrapper(c, err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, response.BaseResponse{
|
|
Success: true,
|
|
Status: http.StatusOK,
|
|
Data: h.toProductResponseList(products, int64(total), req),
|
|
})
|
|
}
|
|
|
|
func (h *MenuHandler) toProductResponseList(resp []*entity.Product, total int64, req request.ProductParam) response.ProductList {
|
|
var products []response.Product
|
|
for _, b := range resp {
|
|
products = append(products, h.toProductResponse(b))
|
|
}
|
|
|
|
return response.ProductList{
|
|
Products: products,
|
|
Total: total,
|
|
Limit: req.Limit,
|
|
Offset: req.Offset,
|
|
}
|
|
}
|
|
|
|
func (h *MenuHandler) toProductResponse(resp *entity.Product) response.Product {
|
|
return response.Product{
|
|
ID: resp.ID,
|
|
Name: resp.Name,
|
|
Type: resp.Type,
|
|
Price: resp.Price,
|
|
Status: resp.Status,
|
|
Description: resp.Description,
|
|
Image: resp.Image,
|
|
}
|
|
}
|
|
|
|
func (h *MenuHandler) OrderCreate(c *gin.Context) {
|
|
ctx := request.GetMyContext(c)
|
|
|
|
var req request.OrderCustomer
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
|
return
|
|
}
|
|
|
|
validate := validator.New()
|
|
if err := validate.Struct(req); err != nil {
|
|
response.ErrorWrapper(c, err)
|
|
return
|
|
}
|
|
|
|
if req.PartnerID == 0 {
|
|
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
|
return
|
|
}
|
|
|
|
orderRequest := req.ToEntity(req.PartnerID, 0)
|
|
|
|
order, err := h.orderService.Save(ctx, orderRequest)
|
|
if err != nil {
|
|
response.ErrorWrapper(c, err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, response.BaseResponse{
|
|
Success: true,
|
|
Status: http.StatusOK,
|
|
Data: MapToOrderCreateResponse(order),
|
|
})
|
|
}
|
|
|
|
func (h *MenuHandler) OrderMemberCreate(c *gin.Context) {
|
|
ctx := request.GetMyContext(c)
|
|
|
|
var req request.OrderCustomer
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
|
return
|
|
}
|
|
|
|
validate := validator.New()
|
|
if err := validate.Struct(req); err != nil {
|
|
response.ErrorWrapper(c, err)
|
|
return
|
|
}
|
|
|
|
if req.PartnerID == 0 {
|
|
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
|
return
|
|
}
|
|
|
|
userID := ctx.RequestedBy()
|
|
orderRequest := req.ToEntity(req.PartnerID, userID)
|
|
orderRequest.CustomerID = &userID
|
|
|
|
order, err := h.orderService.Save(ctx, orderRequest)
|
|
if err != nil {
|
|
response.ErrorWrapper(c, err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, response.BaseResponse{
|
|
Success: true,
|
|
Status: http.StatusOK,
|
|
Data: MapToOrderCreateResponse(order),
|
|
})
|
|
}
|
|
|
|
type GetOrderParam struct {
|
|
PartnerID int64 `form:"partner_id" json:"partner_id"`
|
|
OrderID int64 `form:"order_id" json:"order_id"`
|
|
}
|
|
|
|
func (h *MenuHandler) GetOrderID(c *gin.Context) {
|
|
ctx := request.GetMyContext(c)
|
|
|
|
var req GetOrderParam
|
|
if err := c.ShouldBindQuery(&req); err != nil {
|
|
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
|
return
|
|
}
|
|
|
|
order, err := h.orderService.GetOrderByOrderAndPartnerID(ctx, req.PartnerID, req.OrderID)
|
|
if err != nil {
|
|
response.ErrorWrapper(c, err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, response.BaseResponse{
|
|
Success: true,
|
|
Status: http.StatusOK,
|
|
Data: MapToOrderCreateResponse(order),
|
|
})
|
|
}
|
|
|
|
func MapToOrderCreateResponse(result *entity.Order) response.OrderResponse {
|
|
resp := response.OrderResponse{
|
|
ID: result.ID,
|
|
Status: result.Status,
|
|
Amount: result.Amount,
|
|
Tax: result.Tax,
|
|
Total: result.Total,
|
|
PaymentType: result.PaymentType,
|
|
CreatedAt: result.CreatedAt,
|
|
Items: response.MapToOrderItemResponses(result.OrderItems),
|
|
}
|
|
|
|
return resp
|
|
}
|