feat(client): Add GetAll functionality for client retrieval and update filters for pagination
Deploy Application / deploy (push) Successful in 25s Details

feat(role): Implement GetAll method for role retrieval without pagination and update role service and repository
feat(product): Add SearchKey field to ProductVendorResponse for enhanced product search capabilities
This commit is contained in:
Habib Fatkhul Rohman 2025-11-05 16:03:29 +07:00
parent 0453d4df70
commit a452e55e25
8 changed files with 78 additions and 6 deletions

View File

@ -191,6 +191,19 @@ func (c *clientController) GetAll(ctx *gin.Context) {
return return
} }
getAll := ctx.Query("get_all")
if getAll != "" {
clients, _, err := c.clientService.GetAll(ctx, filter)
if err != nil {
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_CLIENT, err.Error(), nil)
ctx.JSON(http.StatusBadRequest, res)
return
}
response := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_GET_CLIENT, clients)
ctx.JSON(http.StatusOK, response)
return
}
// Ambil limit & offset dari query param (default: limit=10, offset=0) // Ambil limit & offset dari query param (default: limit=10, offset=0)
perPage := utils.ParseInt(ctx.DefaultQuery("per_page", "10")) perPage := utils.ParseInt(ctx.DefaultQuery("per_page", "10"))
page := utils.ParseInt(ctx.DefaultQuery("page", "1")) page := utils.ParseInt(ctx.DefaultQuery("page", "1"))

View File

@ -14,6 +14,7 @@ type ClientFilter struct {
MenuName string `form:"menu_name"` // tambahkan field ini MenuName string `form:"menu_name"` // tambahkan field ini
PerPage int `form:"per_page"` PerPage int `form:"per_page"`
Page int `form:"page"` Page int `form:"page"`
GetAll bool `form:"get_all"`
} }
func ApplyClientFilters(db *gorm.DB, filter ClientFilter) *gorm.DB { func ApplyClientFilters(db *gorm.DB, filter ClientFilter) *gorm.DB {

View File

@ -39,12 +39,18 @@ func (r *clientRepository) GetAll(ctx context.Context, filter query.ClientFilter
return nil, 0, err return nil, 0, err
} }
// Ambil data dengan pagination dan preload menus queryDb := db.Preload("Menus")
err := db. if filter.GetAll == false { // tambahkan field GetAll bool di ClientFilter
Preload("Menus"). queryDb = queryDb.Limit(filter.PerPage).Offset(filter.Page)
Limit(filter.PerPage). }
Offset(filter.Page).
Find(&clients).Error // // Ambil data dengan pagination dan preload menus
// err := db.
// Preload("Menus").
// Limit(filter.PerPage).
// Offset(filter.Page).
// Find(&clients).Error
err := queryDb.Find(&clients).Error
return clients, total, err return clients, total, err
} }

View File

@ -146,6 +146,7 @@ type (
ProductVendorResponse struct { ProductVendorResponse struct {
ID string `json:"id"` ID string `json:"id"`
Name string `json:"name"` Name string `json:"name"`
SearchKey string `json:"search_key"`
Address string `json:"address"` Address string `json:"address"`
ContactPerson string `json:"contact_person"` ContactPerson string `json:"contact_person"`
} }

View File

@ -312,6 +312,7 @@ func mapProductToResponse(product entities.MProductEntity) dto.ProductResponse {
Name: v.Vendor.Name, Name: v.Vendor.Name,
Address: v.Vendor.Address, Address: v.Vendor.Address,
ContactPerson: v.Vendor.ContactPerson, ContactPerson: v.Vendor.ContactPerson,
SearchKey: v.Vendor.SearchKey,
}) })
} }

View File

@ -242,10 +242,25 @@ func (r *roleController) GetRoleByID(ctx *gin.Context) {
func (r *roleController) GetRoles(ctx *gin.Context) { func (r *roleController) GetRoles(ctx *gin.Context) {
// clientId := ctx.MustGet("client_id").(string) // clientId := ctx.MustGet("client_id").(string)
// logrus.Info("Client ID: ", clientId) // logrus.Info("Client ID: ", clientId)
getAll := ctx.Query("get_all")
var filter = &query.RoleFilter{ var filter = &query.RoleFilter{
ClientID: ctx.Query("client_id"), ClientID: ctx.Query("client_id"),
Name: ctx.Query("name"), Name: ctx.Query("name"),
} }
if getAll == "true" {
// Ambil semua data tanpa paginasi
roles, err := r.roleService.GetAll(ctx.Request.Context())
if err != nil {
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_LIST_ROLE, err.Error(), nil)
ctx.JSON(http.StatusBadRequest, res)
return
}
// Tidak perlu ToUserResponses, karena sudah []dto.UserResponse
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_GET_LIST_ROLE, roles)
ctx.JSON(http.StatusOK, res)
return
}
// logrus.Info("Filter: ", filter) // logrus.Info("Filter: ", filter)
filter.BindPagination(ctx) filter.BindPagination(ctx)
ctx.ShouldBindQuery(filter) ctx.ShouldBindQuery(filter)

View File

@ -32,12 +32,31 @@ type RoleRepository interface {
CheckRoleName(ctx context.Context, tx *gorm.DB, name string) (entities.M_Role, bool, error) CheckRoleName(ctx context.Context, tx *gorm.DB, name string) (entities.M_Role, bool, error)
AssignMenusToRole(ctx context.Context, tx *gorm.DB, roleId string, menus []string) error AssignMenusToRole(ctx context.Context, tx *gorm.DB, roleId string, menus []string) error
RemoveMenusFromRole(ctx context.Context, tx *gorm.DB, roleId string, menus []string) error RemoveMenusFromRole(ctx context.Context, tx *gorm.DB, roleId string, menus []string) error
GetAll(ctx context.Context, tx *gorm.DB) ([]entities.M_Role, error)
} }
type roleRepository struct { type roleRepository struct {
db *gorm.DB db *gorm.DB
} }
// GetAll implements RoleRepository.
func (r *roleRepository) GetAll(ctx context.Context, tx *gorm.DB) ([]entities.M_Role, error) {
if tx == nil {
tx = r.db
}
var roles []entities.M_Role
if err := tx.WithContext(ctx).
Preload("Client").
Preload("Permissions").
Preload("Menus").
Find(&roles).Error; err != nil {
return nil, err
}
return roles, nil
}
// AssignMenusToRole implements RoleRepository. // AssignMenusToRole implements RoleRepository.
func (r *roleRepository) AssignMenusToRole(ctx context.Context, tx *gorm.DB, roleId string, menus []string) error { func (r *roleRepository) AssignMenusToRole(ctx context.Context, tx *gorm.DB, roleId string, menus []string) error {
if tx == nil { if tx == nil {

View File

@ -31,6 +31,7 @@ type RoleService interface {
GetRolesByUserID(ctx context.Context, userId string) ([]dto.RoleResponse, error) GetRolesByUserID(ctx context.Context, userId string) ([]dto.RoleResponse, error)
AssignMenusToRole(ctx context.Context, roleId string, menuIds []string) error AssignMenusToRole(ctx context.Context, roleId string, menuIds []string) error
RemoveMenusFromRole(ctx context.Context, roleId string, menuIds []string) error RemoveMenusFromRole(ctx context.Context, roleId string, menuIds []string) error
GetAll(ctx context.Context) ([]dto.RoleResponse, error)
} }
type roleService struct { type roleService struct {
@ -41,6 +42,21 @@ type roleService struct {
db *gorm.DB db *gorm.DB
} }
// GetAll implements RoleService.
func (r *roleService) GetAll(ctx context.Context) ([]dto.RoleResponse, error) {
roles, err := r.roleRepo.GetAll(ctx, r.db)
if err != nil {
return nil, err
}
var responses []dto.RoleResponse
for _, role := range roles {
responses = append(responses, ToRoleResponse(role))
}
return responses, nil
}
// AssignMenusToRole implements RoleService. // AssignMenusToRole implements RoleService.
func (r *roleService) AssignMenusToRole(ctx context.Context, roleId string, menuIds []string) error { func (r *roleService) AssignMenusToRole(ctx context.Context, roleId string, menuIds []string) error {
if len(menuIds) == 0 { if len(menuIds) == 0 {