Update Filter
This commit is contained in:
parent
065cfc4df6
commit
c7495b8bff
@ -103,6 +103,7 @@ type UserSearch struct {
|
||||
Name string
|
||||
RoleID int64
|
||||
PartnerID int64
|
||||
SiteID int64
|
||||
Limit int
|
||||
Offset int
|
||||
}
|
||||
|
||||
@ -75,10 +75,11 @@ func (Partner) TableName() string {
|
||||
}
|
||||
|
||||
type PartnerSearch struct {
|
||||
Search string
|
||||
Name string
|
||||
Limit int
|
||||
Offset int
|
||||
Search string
|
||||
PartnerID *int64
|
||||
Name string
|
||||
Limit int
|
||||
Offset int
|
||||
}
|
||||
|
||||
type PartnerList []*PartnerDB
|
||||
|
||||
@ -24,7 +24,7 @@ func (h *Handler) Route(group *gin.RouterGroup, jwt gin.HandlerFunc) {
|
||||
isSuperAdmin := middlewares.SuperAdminMiddleware()
|
||||
|
||||
route.POST("/", jwt, isSuperAdmin, h.Create)
|
||||
route.GET("/list", jwt, isSuperAdmin, h.GetAll)
|
||||
route.GET("/list", jwt, h.GetAll)
|
||||
route.PUT("/:id", jwt, isSuperAdmin, h.Update)
|
||||
route.GET("/:id", jwt, isSuperAdmin, h.GetByID)
|
||||
route.DELETE("/:id", jwt, isSuperAdmin, h.Delete)
|
||||
@ -140,7 +140,9 @@ func (h *Handler) GetAll(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
Partners, total, err := h.service.GetAll(c.Request.Context(), req.ToEntity())
|
||||
ctx := request.GetMyContext(c)
|
||||
|
||||
Partners, total, err := h.service.GetAll(c.Request.Context(), req.ToEntity(ctx))
|
||||
if err != nil {
|
||||
response.ErrorWrapper(c, err)
|
||||
return
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package request
|
||||
|
||||
import (
|
||||
"furtuna-be/internal/common/mycontext"
|
||||
"furtuna-be/internal/entity"
|
||||
)
|
||||
|
||||
@ -11,12 +12,13 @@ type PartnerParam struct {
|
||||
Offset int `form:"offset" json:"offset" example:"0"`
|
||||
}
|
||||
|
||||
func (p *PartnerParam) ToEntity() entity.PartnerSearch {
|
||||
func (p *PartnerParam) ToEntity(ctx mycontext.Context) entity.PartnerSearch {
|
||||
return entity.PartnerSearch{
|
||||
Search: p.Search,
|
||||
Name: p.Name,
|
||||
Limit: p.Limit,
|
||||
Offset: p.Offset,
|
||||
Search: p.Search,
|
||||
PartnerID: ctx.GetPartnerID(),
|
||||
Name: p.Name,
|
||||
Limit: p.Limit,
|
||||
Offset: p.Offset,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -45,22 +45,29 @@ type UserParam struct {
|
||||
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 *UserParam) ToEntity(ctx mycontext.Context) entity.UserSearch {
|
||||
partnerID := p.PartnerID
|
||||
siteID := p.SiteID
|
||||
|
||||
if !ctx.IsAdmin() {
|
||||
partnerID = *ctx.GetPartnerID()
|
||||
}
|
||||
|
||||
if ctx.GetSiteID() != nil {
|
||||
siteID = *ctx.GetSiteID()
|
||||
}
|
||||
|
||||
return entity.UserSearch{
|
||||
Search: p.Search,
|
||||
Name: p.Name,
|
||||
RoleID: p.RoleID,
|
||||
PartnerID: partnerID,
|
||||
SiteID: siteID,
|
||||
Limit: p.Limit,
|
||||
Offset: p.Offset,
|
||||
}
|
||||
|
||||
@ -93,6 +93,10 @@ func (b *PartnerRepository) GetAll(ctx context.Context, req entity.PartnerSearch
|
||||
query = query.Where("p.name ILIKE ?", "%"+req.Name+"%")
|
||||
}
|
||||
|
||||
if req.PartnerID != nil {
|
||||
query = query.Where("p.id = ?", req.PartnerID)
|
||||
}
|
||||
|
||||
if req.Limit > 0 {
|
||||
query = query.Limit(req.Limit)
|
||||
}
|
||||
|
||||
@ -101,6 +101,10 @@ func (b *UserRepository) GetAllUsers(ctx context.Context, req entity.UserSearch)
|
||||
query = query.Where("ur.partner_id = ? ", req.PartnerID)
|
||||
}
|
||||
|
||||
if req.SiteID > 0 {
|
||||
query = query.Where("ur.site_id = ? ", req.SiteID)
|
||||
}
|
||||
|
||||
// Get the total count without applying the limit and offset.
|
||||
if err := query.Model(&entity.UserDB{}).Count(&total).Error; err != nil {
|
||||
logger.ContextLogger(ctx).Error("error when count users", zap.Error(err))
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user