feat(dto): Add Permissions field to MenuResponse and update related services
Deploy Application / deploy (push) Successful in 20s
Details
Deploy Application / deploy (push) Successful in 20s
Details
This commit is contained in:
parent
8fe9a5bb5c
commit
8fe0448aa8
|
|
@ -63,14 +63,15 @@ type (
|
||||||
}
|
}
|
||||||
|
|
||||||
MenuResponse struct {
|
MenuResponse struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
IconUrl string `json:"icon_url"`
|
IconUrl string `json:"icon_url"`
|
||||||
Url string `json:"url"`
|
Url string `json:"url"`
|
||||||
Sequence int `json:"sequence"`
|
Sequence int `json:"sequence"`
|
||||||
Mode string `json:"mode"`
|
Mode string `json:"mode"`
|
||||||
Status string `json:"status"`
|
Status string `json:"status"`
|
||||||
Parent *MenuParentResponse `json:"parent"`
|
Parent *MenuParentResponse `json:"parent"`
|
||||||
|
Permissions []dto.PermissionResponse `json:"permissions,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
MenuParentResponse struct {
|
MenuParentResponse struct {
|
||||||
|
|
|
||||||
|
|
@ -39,17 +39,9 @@ func (f *MenuFilter) ApplyFilters(q *gorm.DB) *gorm.DB {
|
||||||
q = q.Where("client_id = ?", f.ClientID)
|
q = q.Where("client_id = ?", f.ClientID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// allow selective preloads for performance
|
q = q.Model(entities.M_Menu{}).Preload("Parent", func(db *gorm.DB) *gorm.DB {
|
||||||
for _, include := range f.Includes {
|
return db.Select("m_menus.id", "m_menus.name")
|
||||||
if include == "Parent" {
|
})
|
||||||
// q = q.Model(entities.M_Menu{}).Preload("Parent", func(db *gorm.DB) *gorm.DB {
|
|
||||||
// return db.Select("id", "name")
|
|
||||||
// })
|
|
||||||
q = q.Model(entities.M_Menu{}).Preload("Parent", func(db *gorm.DB) *gorm.DB {
|
|
||||||
return db.Select("m_menus.id", "m_menus.name")
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return q
|
return q
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -56,7 +56,10 @@ func (m *menuRepository) GetById(ctx context.Context, tx *gorm.DB, menuId string
|
||||||
|
|
||||||
var menu entities.M_Menu
|
var menu entities.M_Menu
|
||||||
// preload Parent relation jika entitas M_Menu punya association Parent
|
// preload Parent relation jika entitas M_Menu punya association Parent
|
||||||
if err := tx.WithContext(ctx).Preload("Parent").First(&menu, "id = ?", menuId).Error; err != nil {
|
if err := tx.WithContext(ctx).
|
||||||
|
Preload("Parent").
|
||||||
|
Preload("Permissions").
|
||||||
|
First(&menu, "id = ?", menuId).Error; err != nil {
|
||||||
return entities.M_Menu{}, err
|
return entities.M_Menu{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"github.com/Caknoooo/go-gin-clean-starter/modules/auth/service"
|
"github.com/Caknoooo/go-gin-clean-starter/modules/auth/service"
|
||||||
"github.com/Caknoooo/go-gin-clean-starter/modules/menu/dto"
|
"github.com/Caknoooo/go-gin-clean-starter/modules/menu/dto"
|
||||||
"github.com/Caknoooo/go-gin-clean-starter/modules/menu/repository"
|
"github.com/Caknoooo/go-gin-clean-starter/modules/menu/repository"
|
||||||
|
staticDto "github.com/Caknoooo/go-gin-clean-starter/pkg/dto"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
|
@ -121,6 +122,17 @@ func (m *menuService) GetById(ctx context.Context, menuId string) (dto.MenuRespo
|
||||||
res.Parent = &dto.MenuParentResponse{ID: entity.ParentID.String()}
|
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,
|
||||||
|
// Description: perm.Description,
|
||||||
|
// Code: perm.Code,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -15,4 +15,9 @@ type (
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PermissionResponse struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue