MCPcopy Create free account
hub / github.com/GoSimplicity/LinkMe / CreateMenu

Method CreateMenu

internal/repository/dao/menu.go:55–96  ·  view source on GitHub ↗

CreateMenu 创建菜单

(ctx context.Context, menu *Menu)

Source from the content-addressed store, hash-verified

53
54// CreateMenu 创建菜单
55func (m *menuDAO) CreateMenu(ctx context.Context, menu *Menu) error {
56 if menu == nil {
57 return ErrInvalidMenu
58 }
59
60 // 检查必填字段
61 if menu.Name == "" {
62 return errors.New("菜单名称不能为空")
63 }
64
65 if menu.Path == "" {
66 return errors.New("菜单路径不能为空")
67 }
68
69 return m.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
70 // 检查父菜单是否存在
71 if menu.ParentID != 0 {
72 var count int64
73 if err := tx.Model(&Menu{}).Where("id = ? AND is_deleted = ?", menu.ParentID, 0).Count(&count).Error; err != nil {
74 return fmt.Errorf("检查父菜单失败: %v", err)
75 }
76 if count == 0 {
77 return errors.New("父菜单不存在")
78 }
79 }
80
81 // 检查同级菜单名称是否重复
82 var count int64
83 if err := tx.Model(&Menu{}).Where("name = ? AND parent_id = ? AND is_deleted = ?", menu.Name, menu.ParentID, 0).Count(&count).Error; err != nil {
84 return fmt.Errorf("检查菜单名称失败: %v", err)
85 }
86 if count > 0 {
87 return errors.New("同级菜单名称已存在")
88 }
89
90 now := time.Now().Unix()
91 menu.CreateTime = now
92 menu.UpdateTime = now
93
94 return tx.Create(menu).Error
95 })
96}
97
98// GetMenuById 根据ID获取菜单
99func (m *menuDAO) GetMenuById(ctx context.Context, id int) (*Menu, error) {

Callers

nothing calls this directly

Calls 2

CountMethod · 0.65
CreateMethod · 0.65

Tested by

no test coverage detected