82 lines
1.7 KiB
Go
82 lines
1.7 KiB
Go
package http
|
|
|
|
import (
|
|
"enaklo-pos-be/internal/services/v2/customer"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"enaklo-pos-be/internal/common/errors"
|
|
"enaklo-pos-be/internal/entity"
|
|
"enaklo-pos-be/internal/handlers/request"
|
|
"enaklo-pos-be/internal/handlers/response"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/go-playground/validator/v10"
|
|
)
|
|
|
|
type CustomerHandler struct {
|
|
service customer.Service
|
|
}
|
|
|
|
func NewCustomerHandler(service customer.Service) *CustomerHandler {
|
|
return &CustomerHandler{
|
|
service: service,
|
|
}
|
|
}
|
|
|
|
func (h *CustomerHandler) Route(group *gin.RouterGroup, jwt gin.HandlerFunc) {
|
|
route := group.Group("/customers")
|
|
|
|
route.GET("/list", jwt, h.GetCustomerList)
|
|
}
|
|
|
|
func (h *CustomerHandler) GetCustomerList(c *gin.Context) {
|
|
ctx := request.GetMyContext(c)
|
|
|
|
searchQuery := c.DefaultQuery("search", "")
|
|
limitStr := c.DefaultQuery("limit", "10")
|
|
offsetStr := c.DefaultQuery("offset", "0")
|
|
|
|
// Convert limit and offset to integers
|
|
limit, err := strconv.Atoi(limitStr)
|
|
if err != nil {
|
|
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
|
return
|
|
}
|
|
|
|
offset, err := strconv.Atoi(offsetStr)
|
|
if err != nil {
|
|
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
|
return
|
|
}
|
|
|
|
req := &entity.MemberSearch{
|
|
Search: searchQuery,
|
|
Limit: limit,
|
|
Offset: offset,
|
|
}
|
|
|
|
validate := validator.New()
|
|
if err := validate.Struct(req); err != nil {
|
|
response.ErrorWrapper(c, err)
|
|
return
|
|
}
|
|
|
|
customerList, totalCount, err := h.service.GetAllCustomers(ctx, req)
|
|
if err != nil {
|
|
response.ErrorWrapper(c, err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, response.BaseResponse{
|
|
Success: true,
|
|
Status: http.StatusOK,
|
|
Data: response.MapToCustomerListResponse(customerList),
|
|
PagingMeta: &response.PagingMeta{
|
|
Page: offset + 1,
|
|
Total: int64(totalCount),
|
|
Limit: limit,
|
|
},
|
|
})
|
|
}
|