feat: customer list
This commit is contained in:
@@ -25,6 +25,7 @@ func (h *Handler) Route(group *gin.RouterGroup, jwt gin.HandlerFunc) {
|
||||
|
||||
route.POST("/", jwt, h.Create)
|
||||
route.GET("/list", jwt, h.GetAll)
|
||||
route.GET("/customer/list", jwt, h.GetAllCustomer)
|
||||
route.GET("/:id", jwt, h.GetByID)
|
||||
route.PUT("/:id", jwt, h.Update)
|
||||
route.DELETE("/:id", jwt, h.Delete)
|
||||
@@ -163,7 +164,8 @@ func (h *Handler) GetAll(c *gin.Context) {
|
||||
ctx := request.GetMyContext(c)
|
||||
var req request.UserParam
|
||||
if err := c.ShouldBindQuery(&req); err != nil {
|
||||
|
||||
response.ErrorWrapper(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
users, total, err := h.service.GetAll(ctx, req.ToEntity(ctx))
|
||||
@@ -179,6 +181,27 @@ func (h *Handler) GetAll(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
func (h *Handler) GetAllCustomer(c *gin.Context) {
|
||||
ctx := request.GetMyContext(c)
|
||||
var req request.CustomerParam
|
||||
if err := c.ShouldBindQuery(&req); err != nil {
|
||||
response.ErrorWrapper(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
users, total, err := h.service.GetAllCustomer(ctx, req.ToEntity(ctx))
|
||||
if err != nil {
|
||||
response.ErrorWrapper(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response.BaseResponse{
|
||||
Success: true,
|
||||
Status: http.StatusOK,
|
||||
Data: h.toCustomerResponseList(users, int64(total), req),
|
||||
})
|
||||
}
|
||||
|
||||
// GetByID retrieves details of a specific user by ID.
|
||||
// @Summary Get details of a user by ID
|
||||
// @Description Get details of a user based on the provided ID.
|
||||
@@ -289,3 +312,32 @@ func (h *Handler) toUserResponseList(resp []*entity.User, total int64, req reque
|
||||
Offset: req.Offset,
|
||||
}
|
||||
}
|
||||
func (h *Handler) toCustomerResponse(resp *entity.Customer) response.Customer {
|
||||
return response.Customer{
|
||||
ID: resp.ID,
|
||||
Name: resp.Name,
|
||||
Email: resp.Email,
|
||||
PhoneNumber: resp.PhoneNumber,
|
||||
Status: string(resp.Status),
|
||||
RoleID: int64(resp.RoleID),
|
||||
RoleName: resp.RoleName,
|
||||
PartnerID: resp.PartnerID,
|
||||
PartnerName: resp.PartnerName,
|
||||
CreatedAt: resp.CreatedAt.Format(time.RFC3339),
|
||||
UpdatedAt: resp.CreatedAt.Format(time.RFC3339),
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) toCustomerResponseList(resp []*entity.Customer, total int64, req request.CustomerParam) response.CustomerList {
|
||||
var users []response.Customer
|
||||
for _, b := range resp {
|
||||
users = append(users, h.toCustomerResponse(b))
|
||||
}
|
||||
|
||||
return response.CustomerList{
|
||||
Users: users,
|
||||
Total: total,
|
||||
Limit: req.Limit,
|
||||
Offset: req.Offset,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,6 +72,38 @@ func (p *UserParam) ToEntity(ctx mycontext.Context) entity.UserSearch {
|
||||
Offset: p.Offset,
|
||||
}
|
||||
}
|
||||
type CustomerParam struct {
|
||||
Search string `form:"search" json:"search" example:"admin,branch1"`
|
||||
Name string `form:"name" json:"name" example:"Admin 1"`
|
||||
RoleID int64 `form:"role_id" json:"role_id" example:"1"`
|
||||
PartnerID int64 `form:"partner_id" json:"partner_id" example:"1"`
|
||||
SiteID int64 `form:"site_id" json:"site_id" example:"1"`
|
||||
Limit int `form:"limit,default=10" json:"limit" example:"10"`
|
||||
Offset int `form:"offset,default=0" json:"offset" example:"0"`
|
||||
}
|
||||
|
||||
func (p *CustomerParam) ToEntity(ctx mycontext.Context) entity.CustomerSearch {
|
||||
partnerID := p.PartnerID
|
||||
siteID := p.SiteID
|
||||
|
||||
if !ctx.IsAdmin() {
|
||||
partnerID = *ctx.GetPartnerID()
|
||||
}
|
||||
|
||||
if ctx.GetSiteID() != nil {
|
||||
siteID = *ctx.GetSiteID()
|
||||
}
|
||||
|
||||
return entity.CustomerSearch{
|
||||
Search: p.Search,
|
||||
Name: p.Name,
|
||||
RoleID: p.RoleID,
|
||||
PartnerID: partnerID,
|
||||
SiteID: siteID,
|
||||
Limit: p.Limit,
|
||||
Offset: p.Offset,
|
||||
}
|
||||
}
|
||||
|
||||
type UserRegister struct {
|
||||
Name string `json:"name" validate:"required" binding:"required"`
|
||||
|
||||
@@ -21,6 +21,26 @@ type UserList struct {
|
||||
Limit int `json:"limit"`
|
||||
Offset int `json:"offset"`
|
||||
}
|
||||
type Customer struct {
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Email string `json:"email"`
|
||||
PhoneNumber string `json:"phone_number"`
|
||||
Status string `json:"status"`
|
||||
RoleID int64 `json:"role_id"`
|
||||
RoleName string `json:"role_name"`
|
||||
PartnerID *int64 `json:"partner_id"`
|
||||
PartnerName string `json:"partner_name"`
|
||||
CreatedAt string `json:"created_at,omitempty"`
|
||||
UpdatedAt string `json:"updated_at,omitempty"`
|
||||
}
|
||||
|
||||
type CustomerList struct {
|
||||
Users []Customer `json:"users"`
|
||||
Total int64 `json:"total"`
|
||||
Limit int `json:"limit"`
|
||||
Offset int `json:"offset"`
|
||||
}
|
||||
|
||||
type UserRegister struct {
|
||||
ID int64 `json:"id"`
|
||||
|
||||
Reference in New Issue
Block a user