feat(menu): Implement GetAll functionality for menus in MenuService and MenuRepository
Deploy Application / deploy (push) Successful in 22s
Details
Deploy Application / deploy (push) Successful in 22s
Details
This commit is contained in:
parent
13e6c86acb
commit
e0a5af65a1
|
|
@ -88,12 +88,28 @@ func (m *menuController) CreateMenu(ctx *gin.Context) {
|
||||||
// @Failure 400 {object} utils.Response
|
// @Failure 400 {object} utils.Response
|
||||||
// @Router /menus [get]
|
// @Router /menus [get]
|
||||||
func (m *menuController) GetMenus(ctx *gin.Context) {
|
func (m *menuController) GetMenus(ctx *gin.Context) {
|
||||||
|
getAll := ctx.Query("get_all")
|
||||||
var filter = &query.MenuFilter{
|
var filter = &query.MenuFilter{
|
||||||
ClientID: ctx.Query("client_id"),
|
ClientID: ctx.Query("client_id"),
|
||||||
Name: ctx.Query("name"),
|
Name: ctx.Query("name"),
|
||||||
ParentID: ctx.Query("parent_id"),
|
ParentID: ctx.Query("parent_id"),
|
||||||
Includes: ctx.QueryArray("includes"),
|
Includes: ctx.QueryArray("includes"),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if getAll == "true" {
|
||||||
|
// Ambil semua data tanpa paginasi
|
||||||
|
menus, err := m.menuService.GetAll(ctx.Request.Context())
|
||||||
|
if err != nil {
|
||||||
|
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_LIST_MENU, err.Error(), nil)
|
||||||
|
ctx.JSON(http.StatusBadRequest, res)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// Tidak perlu ToMenuResponses, karena sudah []dto.MenuResponse
|
||||||
|
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_GET_LIST_MENU, menus)
|
||||||
|
ctx.JSON(http.StatusOK, res)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
filter.BindPagination(ctx)
|
filter.BindPagination(ctx)
|
||||||
ctx.ShouldBindQuery(filter)
|
ctx.ShouldBindQuery(filter)
|
||||||
menus, total, err := pagination.PaginatedQueryWithIncludable[query.M_Menu](m.db, filter)
|
menus, total, err := pagination.PaginatedQueryWithIncludable[query.M_Menu](m.db, filter)
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,7 @@ type (
|
||||||
GetByName(ctx context.Context, tx *gorm.DB, name string) (entities.M_Menu, error)
|
GetByName(ctx context.Context, tx *gorm.DB, name string) (entities.M_Menu, error)
|
||||||
Update(ctx context.Context, tx *gorm.DB, menu entities.M_Menu) (entities.M_Menu, error)
|
Update(ctx context.Context, tx *gorm.DB, menu entities.M_Menu) (entities.M_Menu, error)
|
||||||
Delete(ctx context.Context, tx *gorm.DB, menuId string) error
|
Delete(ctx context.Context, tx *gorm.DB, menuId string) error
|
||||||
|
GetAll(ctx context.Context, tx *gorm.DB) ([]entities.M_Menu, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
menuRepository struct {
|
menuRepository struct {
|
||||||
|
|
@ -21,6 +22,24 @@ type (
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// GetAll implements MenuRepository.
|
||||||
|
func (m *menuRepository) GetAll(ctx context.Context, tx *gorm.DB) ([]entities.M_Menu, error) {
|
||||||
|
if tx == nil {
|
||||||
|
tx = m.db
|
||||||
|
}
|
||||||
|
|
||||||
|
var menus []entities.M_Menu
|
||||||
|
err := tx.WithContext(ctx).
|
||||||
|
Preload("Parent").
|
||||||
|
Preload("Permissions").
|
||||||
|
Find(&menus).Error
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return menus, nil
|
||||||
|
}
|
||||||
|
|
||||||
// Create implements MenuRepository.
|
// Create implements MenuRepository.
|
||||||
func (m *menuRepository) Create(ctx context.Context, tx *gorm.DB, menu entities.M_Menu) (entities.M_Menu, error) {
|
func (m *menuRepository) Create(ctx context.Context, tx *gorm.DB, menu entities.M_Menu) (entities.M_Menu, error) {
|
||||||
if tx == nil {
|
if tx == nil {
|
||||||
|
|
@ -85,7 +104,7 @@ func (m *menuRepository) Update(ctx context.Context, tx *gorm.DB, menu entities.
|
||||||
if tx == nil {
|
if tx == nil {
|
||||||
tx = m.db
|
tx = m.db
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := tx.WithContext(ctx).Model(&entities.M_Menu{}).
|
if err := tx.WithContext(ctx).Model(&entities.M_Menu{}).
|
||||||
Where("id = ?", menu.ID).
|
Where("id = ?", menu.ID).
|
||||||
Select("*").
|
Select("*").
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@ type MenuService interface {
|
||||||
GetById(ctx context.Context, menuId string) (dto.MenuResponse, error)
|
GetById(ctx context.Context, menuId string) (dto.MenuResponse, error)
|
||||||
GetByName(ctx context.Context, name string) (dto.MenuResponse, error)
|
GetByName(ctx context.Context, name string) (dto.MenuResponse, error)
|
||||||
Update(ctx context.Context, menuId string, menu dto.MenuUpdateRequest) (dto.MenuResponse, error)
|
Update(ctx context.Context, menuId string, menu dto.MenuUpdateRequest) (dto.MenuResponse, error)
|
||||||
|
GetAll(ctx context.Context) ([]dto.MenuResponse, error)
|
||||||
Delete(ctx context.Context, menuId string) error
|
Delete(ctx context.Context, menuId string) error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -27,6 +28,50 @@ type menuService struct {
|
||||||
db *gorm.DB
|
db *gorm.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetAll implements MenuService.
|
||||||
|
func (m *menuService) GetAll(ctx context.Context) ([]dto.MenuResponse, error) {
|
||||||
|
entities, err := m.menuRepository.GetAll(ctx, m.db)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
responses := make([]dto.MenuResponse, 0, len(entities))
|
||||||
|
for _, entity := range entities {
|
||||||
|
res := dto.MenuResponse{
|
||||||
|
ID: entity.ID.String(),
|
||||||
|
Name: entity.Name,
|
||||||
|
IconUrl: entity.IconUrl,
|
||||||
|
Url: entity.Url,
|
||||||
|
Sequence: entity.Sequence,
|
||||||
|
Mode: entity.Mode,
|
||||||
|
Status: entity.Status,
|
||||||
|
}
|
||||||
|
|
||||||
|
if entity.Parent != nil {
|
||||||
|
res.Parent = &dto.MenuParentResponse{
|
||||||
|
ID: entity.Parent.ID.String(),
|
||||||
|
Name: entity.Parent.Name,
|
||||||
|
}
|
||||||
|
} else if entity.ParentID != nil {
|
||||||
|
res.Parent = &dto.MenuParentResponse{ID: entity.ParentID.String()}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(entity.Permissions) > 0 {
|
||||||
|
res.Permissions = make([]staticDto.PermissionResponse, 0, len(entity.Permissions))
|
||||||
|
for _, perm := range entity.Permissions {
|
||||||
|
res.Permissions = append(res.Permissions, staticDto.PermissionResponse{
|
||||||
|
ID: perm.ID.String(),
|
||||||
|
Name: perm.Name,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
responses = append(responses, res)
|
||||||
|
}
|
||||||
|
|
||||||
|
return responses, nil
|
||||||
|
}
|
||||||
|
|
||||||
// Create implements MenuService.
|
// Create implements MenuService.
|
||||||
func (m *menuService) Create(ctx context.Context, menu dto.MenuResponse) (dto.MenuResponse, error) {
|
func (m *menuService) Create(ctx context.Context, menu dto.MenuResponse) (dto.MenuResponse, error) {
|
||||||
var entity entities.M_Menu
|
var entity entities.M_Menu
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue