优化结构
This commit is contained in:
@@ -3,31 +3,30 @@ package system
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"kra/internal/biz/system"
|
||||
"time"
|
||||
|
||||
"kra/internal/biz"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type menuRepo struct{ data Provider }
|
||||
|
||||
func NewMenuRepo(data Provider) biz.MenuRepo { return &menuRepo{data: data} }
|
||||
func NewMenuRepo(data Provider) system.MenuRepo { return &menuRepo{data: data} }
|
||||
|
||||
func menuFromPO(po menuPO) *biz.Menu {
|
||||
func menuFromPO(po menuPO) *system.Menu {
|
||||
var deletedAt *time.Time
|
||||
if po.DeletedAt.Valid {
|
||||
value := po.DeletedAt.Time
|
||||
deletedAt = &value
|
||||
}
|
||||
return &biz.Menu{CreatedAt: po.CreatedAt, UpdatedAt: po.UpdatedAt, DeletedAt: deletedAt, ID: po.ID, ParentID: po.ParentID, Path: po.Path, Name: po.Name, Hidden: po.Hidden, Component: po.Component, Sort: po.Sort, ActiveName: po.ActiveName, KeepAlive: po.KeepAlive, DefaultMenu: po.DefaultMenu, Title: po.Title, Icon: po.Icon, CloseTab: po.CloseTab, TransitionType: po.TransitionType}
|
||||
return &system.Menu{CreatedAt: po.CreatedAt, UpdatedAt: po.UpdatedAt, DeletedAt: deletedAt, ID: po.ID, ParentID: po.ParentID, Path: po.Path, Name: po.Name, Hidden: po.Hidden, Component: po.Component, Sort: po.Sort, ActiveName: po.ActiveName, KeepAlive: po.KeepAlive, DefaultMenu: po.DefaultMenu, Title: po.Title, Icon: po.Icon, CloseTab: po.CloseTab, TransitionType: po.TransitionType}
|
||||
}
|
||||
|
||||
func menuToPO(v *biz.Menu) menuPO {
|
||||
func menuToPO(v *system.Menu) menuPO {
|
||||
return menuPO{ID: v.ID, CreatedAt: v.CreatedAt, UpdatedAt: v.UpdatedAt, ParentID: v.ParentID, Path: v.Path, Name: v.Name, Hidden: v.Hidden, Component: v.Component, Sort: v.Sort, ActiveName: v.ActiveName, KeepAlive: v.KeepAlive, DefaultMenu: v.DefaultMenu, Title: v.Title, Icon: v.Icon, CloseTab: v.CloseTab, TransitionType: v.TransitionType}
|
||||
}
|
||||
|
||||
func replaceMenuRelations(tx *gorm.DB, menu *biz.Menu) error {
|
||||
func replaceMenuRelations(tx *gorm.DB, menu *system.Menu) error {
|
||||
if err := tx.Unscoped().Where("sys_base_menu_id = ?", menu.ID).Delete(&menuParameterPO{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -37,7 +36,7 @@ func replaceMenuRelations(tx *gorm.DB, menu *biz.Menu) error {
|
||||
return createMenuRelations(tx, menu)
|
||||
}
|
||||
|
||||
func createMenuRelations(tx *gorm.DB, menu *biz.Menu) error {
|
||||
func createMenuRelations(tx *gorm.DB, menu *system.Menu) error {
|
||||
parameters := make([]menuParameterPO, 0, len(menu.Parameters))
|
||||
for _, parameter := range menu.Parameters {
|
||||
parameters = append(parameters, menuParameterPO{ID: parameter.ID, CreatedAt: parameter.CreatedAt, UpdatedAt: parameter.UpdatedAt, MenuID: menu.ID, Type: parameter.Type, Key: parameter.Key, Value: parameter.Value})
|
||||
@@ -57,7 +56,7 @@ func createMenuRelations(tx *gorm.DB, menu *biz.Menu) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *menuRepo) Create(ctx context.Context, v *biz.Menu) error {
|
||||
func (r *menuRepo) Create(ctx context.Context, v *system.Menu) error {
|
||||
return r.data.DB().WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
var count int64
|
||||
if err := tx.Model(&menuPO{}).Where("name = ?", v.Name).Count(&count).Error; err != nil {
|
||||
@@ -98,7 +97,7 @@ func (r *menuRepo) Create(ctx context.Context, v *biz.Menu) error {
|
||||
})
|
||||
}
|
||||
|
||||
func (r *menuRepo) Update(ctx context.Context, v *biz.Menu) error {
|
||||
func (r *menuRepo) Update(ctx context.Context, v *system.Menu) error {
|
||||
return r.data.DB().WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
var old menuPO
|
||||
if err := tx.First(&old, v.ID).Error; err != nil {
|
||||
@@ -156,12 +155,12 @@ func (r *menuRepo) Delete(ctx context.Context, id uint) error {
|
||||
})
|
||||
}
|
||||
|
||||
func (r *menuRepo) loadRelations(ctx context.Context, menu *biz.Menu) error {
|
||||
func (r *menuRepo) loadRelations(ctx context.Context, menu *system.Menu) error {
|
||||
var parameters []menuParameterPO
|
||||
if err := r.data.DB().WithContext(ctx).Where("sys_base_menu_id = ?", menu.ID).Find(¶meters).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
menu.Parameters = make([]*biz.MenuParameter, 0, len(parameters))
|
||||
menu.Parameters = make([]*system.MenuParameter, 0, len(parameters))
|
||||
for _, parameter := range parameters {
|
||||
menu.Parameters = append(menu.Parameters, menuParameterFromPO(parameter))
|
||||
}
|
||||
@@ -169,14 +168,14 @@ func (r *menuRepo) loadRelations(ctx context.Context, menu *biz.Menu) error {
|
||||
if err := r.data.DB().WithContext(ctx).Where("sys_base_menu_id = ?", menu.ID).Find(&buttons).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
menu.Buttons = make([]*biz.MenuButton, 0, len(buttons))
|
||||
menu.Buttons = make([]*system.MenuButton, 0, len(buttons))
|
||||
for _, button := range buttons {
|
||||
menu.Buttons = append(menu.Buttons, menuButtonFromPO(button))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *menuRepo) Find(ctx context.Context, id uint) (*biz.Menu, error) {
|
||||
func (r *menuRepo) Find(ctx context.Context, id uint) (*system.Menu, error) {
|
||||
var po menuPO
|
||||
if err := r.data.DB().WithContext(ctx).First(&po, id).Error; err != nil {
|
||||
return nil, err
|
||||
@@ -188,11 +187,11 @@ func (r *menuRepo) Find(ctx context.Context, id uint) (*biz.Menu, error) {
|
||||
return menu, nil
|
||||
}
|
||||
|
||||
func (r *menuRepo) List(ctx context.Context) ([]*biz.Menu, error) {
|
||||
func (r *menuRepo) List(ctx context.Context) ([]*system.Menu, error) {
|
||||
var pos []menuPO
|
||||
db := r.data.DB().WithContext(ctx).Model(&menuPO{}).Order("sort")
|
||||
config := r.data.Runtime().Admin()
|
||||
if actor, ok := biz.ActorFromContext(ctx); ok && config != nil && config.System != nil && config.System.UseStrictAuth {
|
||||
if actor, ok := system.ActorFromContext(ctx); ok && config != nil && config.System != nil && config.System.UseStrictAuth {
|
||||
var authority authorityPO
|
||||
if err := r.data.DB().WithContext(ctx).Where("authority_id = ?", actor.AuthorityID).First(&authority).Error; err != nil {
|
||||
return nil, err
|
||||
@@ -204,20 +203,20 @@ func (r *menuRepo) List(ctx context.Context) ([]*biz.Menu, error) {
|
||||
if err := db.Find(&pos).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]*biz.Menu, 0, len(pos))
|
||||
out := make([]*system.Menu, 0, len(pos))
|
||||
for _, po := range pos {
|
||||
out = append(out, menuFromPO(po))
|
||||
}
|
||||
if len(out) == 0 {
|
||||
return out, nil
|
||||
}
|
||||
byID := make(map[uint]*biz.Menu, len(out))
|
||||
byID := make(map[uint]*system.Menu, len(out))
|
||||
ids := make([]uint, 0, len(out))
|
||||
for _, menu := range out {
|
||||
byID[menu.ID] = menu
|
||||
ids = append(ids, menu.ID)
|
||||
menu.Parameters = []*biz.MenuParameter{}
|
||||
menu.Buttons = []*biz.MenuButton{}
|
||||
menu.Parameters = []*system.MenuParameter{}
|
||||
menu.Buttons = []*system.MenuButton{}
|
||||
}
|
||||
var parameters []menuParameterPO
|
||||
if err := r.data.DB().WithContext(ctx).Where("sys_base_menu_id IN ?", ids).Find(¶meters).Error; err != nil {
|
||||
@@ -238,22 +237,22 @@ func (r *menuRepo) List(ctx context.Context) ([]*biz.Menu, error) {
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func menuParameterFromPO(po menuParameterPO) *biz.MenuParameter {
|
||||
func menuParameterFromPO(po menuParameterPO) *system.MenuParameter {
|
||||
var deletedAt *time.Time
|
||||
if po.DeletedAt.Valid {
|
||||
value := po.DeletedAt.Time
|
||||
deletedAt = &value
|
||||
}
|
||||
return &biz.MenuParameter{CreatedAt: po.CreatedAt, UpdatedAt: po.UpdatedAt, DeletedAt: deletedAt, ID: po.ID, MenuID: po.MenuID, Type: po.Type, Key: po.Key, Value: po.Value}
|
||||
return &system.MenuParameter{CreatedAt: po.CreatedAt, UpdatedAt: po.UpdatedAt, DeletedAt: deletedAt, ID: po.ID, MenuID: po.MenuID, Type: po.Type, Key: po.Key, Value: po.Value}
|
||||
}
|
||||
|
||||
func menuButtonFromPO(po menuButtonPO) *biz.MenuButton {
|
||||
func menuButtonFromPO(po menuButtonPO) *system.MenuButton {
|
||||
var deletedAt *time.Time
|
||||
if po.DeletedAt.Valid {
|
||||
value := po.DeletedAt.Time
|
||||
deletedAt = &value
|
||||
}
|
||||
return &biz.MenuButton{CreatedAt: po.CreatedAt, UpdatedAt: po.UpdatedAt, DeletedAt: deletedAt, ID: po.ID, Name: po.Name, Description: po.Description, MenuID: po.MenuID}
|
||||
return &system.MenuButton{CreatedAt: po.CreatedAt, UpdatedAt: po.UpdatedAt, DeletedAt: deletedAt, ID: po.ID, Name: po.Name, Description: po.Description, MenuID: po.MenuID}
|
||||
}
|
||||
|
||||
func (r *menuRepo) SetAuthorityMenus(ctx context.Context, id uint, ids []uint) error {
|
||||
@@ -292,7 +291,7 @@ func (r *menuRepo) AuthorityMenuIDs(ctx context.Context, id uint) ([]uint, error
|
||||
// menus. In particular, it deliberately does not call List: List applies the
|
||||
// current request actor's strict-auth filter, which would incorrectly hide
|
||||
// menus when an administrator inspects a different (child) authority.
|
||||
func (r *menuRepo) ListAuthorityMenus(ctx context.Context, authorityID uint) ([]*biz.Menu, error) {
|
||||
func (r *menuRepo) ListAuthorityMenus(ctx context.Context, authorityID uint) ([]*system.Menu, error) {
|
||||
var pos []menuPO
|
||||
db := r.data.DB().WithContext(ctx).Model(&menuPO{}).
|
||||
Where("id IN (?)", r.data.DB().WithContext(ctx).Model(&authorityMenuPO{}).
|
||||
@@ -301,7 +300,7 @@ func (r *menuRepo) ListAuthorityMenus(ctx context.Context, authorityID uint) ([]
|
||||
if err := db.Find(&pos).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]*biz.Menu, 0, len(pos))
|
||||
out := make([]*system.Menu, 0, len(pos))
|
||||
for _, po := range pos {
|
||||
// This endpoint does not preload Parameters or MenuBtn. Keep
|
||||
// both relations nil so the response shape remains compatible.
|
||||
|
||||
Reference in New Issue
Block a user