50 lines
1.6 KiB
Go
50 lines
1.6 KiB
Go
package handler
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"github.com/gin-gonic/gin"
|
||
|
|
|
||
|
|
"apskel-pos-be/internal/constants"
|
||
|
|
"apskel-pos-be/internal/contract"
|
||
|
|
"apskel-pos-be/internal/models"
|
||
|
|
"apskel-pos-be/internal/service"
|
||
|
|
"apskel-pos-be/internal/util"
|
||
|
|
)
|
||
|
|
|
||
|
|
// CustomerOrderHandler serves the customer app's order history.
|
||
|
|
type CustomerOrderHandler struct {
|
||
|
|
orders service.CustomerOrderService
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewCustomerOrderHandler(orders service.CustomerOrderService) *CustomerOrderHandler {
|
||
|
|
return &CustomerOrderHandler{orders: orders}
|
||
|
|
}
|
||
|
|
|
||
|
|
// List is GET /customer/orders?page=&limit=.
|
||
|
|
func (h *CustomerOrderHandler) List(c *gin.Context) {
|
||
|
|
customerID, ok := customerIDFromGin(c, "CustomerOrderHandler::List")
|
||
|
|
if !ok {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
var query models.ListCustomerOrdersQuery
|
||
|
|
if err := c.ShouldBindQuery(&query); err != nil {
|
||
|
|
util.HandleResponse(c.Writer, c.Request, contract.BuildErrorResponse([]*contract.ResponseError{
|
||
|
|
contract.NewResponseError(constants.MalformedFieldErrorCode, constants.RequestEntity, "page and limit must be whole numbers"),
|
||
|
|
}), "CustomerOrderHandler::List")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
util.HandleResponse(c.Writer, c.Request, h.orders.List(c.Request.Context(), customerID, query), "CustomerOrderHandler::List")
|
||
|
|
}
|
||
|
|
|
||
|
|
// Detail is GET /customer/orders/:id.
|
||
|
|
func (h *CustomerOrderHandler) Detail(c *gin.Context) {
|
||
|
|
customerID, ok := customerIDFromGin(c, "CustomerOrderHandler::Detail")
|
||
|
|
if !ok {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
orderID, ok := parseUUIDParam(c, "id", "CustomerOrderHandler::Detail")
|
||
|
|
if !ok {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
util.HandleResponse(c.Writer, c.Request, h.orders.Detail(c.Request.Context(), customerID, orderID), "CustomerOrderHandler::Detail")
|
||
|
|
}
|