From a452e55e25314884fecc2d5e506033c121854885 Mon Sep 17 00:00:00 2001 From: Habib Fatkhul Rohman Date: Wed, 5 Nov 2025 16:03:29 +0700 Subject: [PATCH] feat(client): Add GetAll functionality for client retrieval and update filters for pagination 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 --- .../client/controller/client_controller.go | 13 +++++++++++++ modules/client/query/client_query.go | 1 + .../client/repository/client_repository.go | 18 ++++++++++++------ modules/product/dto/product_dto.go | 1 + modules/product/service/product_service.go | 1 + modules/role/controller/role_controller.go | 15 +++++++++++++++ modules/role/repository/role_repository.go | 19 +++++++++++++++++++ modules/role/service/role_service.go | 16 ++++++++++++++++ 8 files changed, 78 insertions(+), 6 deletions(-) diff --git a/modules/client/controller/client_controller.go b/modules/client/controller/client_controller.go index 6309cc1..5eae963 100644 --- a/modules/client/controller/client_controller.go +++ b/modules/client/controller/client_controller.go @@ -191,6 +191,19 @@ func (c *clientController) GetAll(ctx *gin.Context) { 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) perPage := utils.ParseInt(ctx.DefaultQuery("per_page", "10")) page := utils.ParseInt(ctx.DefaultQuery("page", "1")) diff --git a/modules/client/query/client_query.go b/modules/client/query/client_query.go index 2145131..7f72067 100644 --- a/modules/client/query/client_query.go +++ b/modules/client/query/client_query.go @@ -14,6 +14,7 @@ type ClientFilter struct { MenuName string `form:"menu_name"` // tambahkan field ini PerPage int `form:"per_page"` Page int `form:"page"` + GetAll bool `form:"get_all"` } func ApplyClientFilters(db *gorm.DB, filter ClientFilter) *gorm.DB { diff --git a/modules/client/repository/client_repository.go b/modules/client/repository/client_repository.go index ac490a4..8590608 100644 --- a/modules/client/repository/client_repository.go +++ b/modules/client/repository/client_repository.go @@ -39,12 +39,18 @@ func (r *clientRepository) GetAll(ctx context.Context, filter query.ClientFilter return nil, 0, err } - // Ambil data dengan pagination dan preload menus - err := db. - Preload("Menus"). - Limit(filter.PerPage). - Offset(filter.Page). - Find(&clients).Error + queryDb := db.Preload("Menus") + if filter.GetAll == false { // tambahkan field GetAll bool di ClientFilter + queryDb = queryDb.Limit(filter.PerPage).Offset(filter.Page) + } + + // // 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 } diff --git a/modules/product/dto/product_dto.go b/modules/product/dto/product_dto.go index 757c1a8..3a127dd 100644 --- a/modules/product/dto/product_dto.go +++ b/modules/product/dto/product_dto.go @@ -146,6 +146,7 @@ type ( ProductVendorResponse struct { ID string `json:"id"` Name string `json:"name"` + SearchKey string `json:"search_key"` Address string `json:"address"` ContactPerson string `json:"contact_person"` } diff --git a/modules/product/service/product_service.go b/modules/product/service/product_service.go index e49ed60..2c33e7c 100644 --- a/modules/product/service/product_service.go +++ b/modules/product/service/product_service.go @@ -312,6 +312,7 @@ func mapProductToResponse(product entities.MProductEntity) dto.ProductResponse { Name: v.Vendor.Name, Address: v.Vendor.Address, ContactPerson: v.Vendor.ContactPerson, + SearchKey: v.Vendor.SearchKey, }) } diff --git a/modules/role/controller/role_controller.go b/modules/role/controller/role_controller.go index b32690e..8c2e0e4 100644 --- a/modules/role/controller/role_controller.go +++ b/modules/role/controller/role_controller.go @@ -242,10 +242,25 @@ func (r *roleController) GetRoleByID(ctx *gin.Context) { func (r *roleController) GetRoles(ctx *gin.Context) { // clientId := ctx.MustGet("client_id").(string) // logrus.Info("Client ID: ", clientId) + getAll := ctx.Query("get_all") var filter = &query.RoleFilter{ ClientID: ctx.Query("client_id"), 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) filter.BindPagination(ctx) ctx.ShouldBindQuery(filter) diff --git a/modules/role/repository/role_repository.go b/modules/role/repository/role_repository.go index 31cedfa..6102945 100644 --- a/modules/role/repository/role_repository.go +++ b/modules/role/repository/role_repository.go @@ -32,12 +32,31 @@ type RoleRepository interface { 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 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 { 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. func (r *roleRepository) AssignMenusToRole(ctx context.Context, tx *gorm.DB, roleId string, menus []string) error { if tx == nil { diff --git a/modules/role/service/role_service.go b/modules/role/service/role_service.go index 6530cfb..e0f3e6a 100644 --- a/modules/role/service/role_service.go +++ b/modules/role/service/role_service.go @@ -31,6 +31,7 @@ type RoleService interface { GetRolesByUserID(ctx context.Context, userId string) ([]dto.RoleResponse, error) AssignMenusToRole(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 { @@ -41,6 +42,21 @@ type roleService struct { 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. func (r *roleService) AssignMenusToRole(ctx context.Context, roleId string, menuIds []string) error { if len(menuIds) == 0 {