feat(client): Add GetAll functionality for client retrieval and update filters for pagination
Deploy Application / deploy (push) Successful in 25s
Details
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:
parent
0453d4df70
commit
a452e55e25
|
|
@ -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"))
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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"`
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Reference in New Issue