feat: 优化web
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"giter.top/smart/internal/iam/entity"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// MenuRepository 菜单
|
||||
type MenuRepository interface {
|
||||
Create(ctx context.Context, m *entity.Menu) error
|
||||
Update(ctx context.Context, m *entity.Menu) error
|
||||
Delete(ctx context.Context, id string) error
|
||||
GetByID(ctx context.Context, id string) (*entity.Menu, error)
|
||||
ListAll(ctx context.Context) ([]entity.Menu, error)
|
||||
ListByType(ctx context.Context, menuType *int16) ([]entity.Menu, error)
|
||||
ExistsPerms(ctx context.Context, perms string, excludeID string) (bool, error)
|
||||
CountChildren(ctx context.Context, parentID string) (int64, error)
|
||||
CountRoleRefs(ctx context.Context, menuID string) (int64, error)
|
||||
ListByPerms(ctx context.Context, perms string) ([]entity.Menu, error)
|
||||
ListIDsByPermsIn(ctx context.Context, perms []string) ([]string, error)
|
||||
}
|
||||
|
||||
type menuRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewMenuRepository(db *gorm.DB) MenuRepository {
|
||||
return &menuRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *menuRepository) Create(ctx context.Context, m *entity.Menu) error {
|
||||
return r.db.WithContext(ctx).Create(m).Error
|
||||
}
|
||||
|
||||
func (r *menuRepository) Update(ctx context.Context, m *entity.Menu) error {
|
||||
return r.db.WithContext(ctx).Save(m).Error
|
||||
}
|
||||
|
||||
func (r *menuRepository) Delete(ctx context.Context, id string) error {
|
||||
return r.db.WithContext(ctx).Delete(&entity.Menu{}, "id = ?", id).Error
|
||||
}
|
||||
|
||||
func (r *menuRepository) GetByID(ctx context.Context, id string) (*entity.Menu, error) {
|
||||
var out entity.Menu
|
||||
err := r.db.WithContext(ctx).Where("id = ?", id).First(&out).Error
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &out, nil
|
||||
}
|
||||
|
||||
func (r *menuRepository) ListAll(ctx context.Context) ([]entity.Menu, error) {
|
||||
var rows []entity.Menu
|
||||
err := r.db.WithContext(ctx).Order("sort_order ASC, created_at ASC").Find(&rows).Error
|
||||
return rows, err
|
||||
}
|
||||
|
||||
func (r *menuRepository) ListByType(ctx context.Context, menuType *int16) ([]entity.Menu, error) {
|
||||
q := r.db.WithContext(ctx).Model(&entity.Menu{})
|
||||
if menuType != nil {
|
||||
q = q.Where("menu_type = ?", *menuType)
|
||||
}
|
||||
var rows []entity.Menu
|
||||
err := q.Order("sort_order ASC, created_at ASC").Find(&rows).Error
|
||||
return rows, err
|
||||
}
|
||||
|
||||
func (r *menuRepository) ExistsPerms(ctx context.Context, perms string, excludeID string) (bool, error) {
|
||||
if perms == "" {
|
||||
return false, nil
|
||||
}
|
||||
q := r.db.WithContext(ctx).Model(&entity.Menu{}).Where("perms = ?", perms)
|
||||
if excludeID != "" {
|
||||
q = q.Where("id <> ?", excludeID)
|
||||
}
|
||||
var n int64
|
||||
err := q.Count(&n).Error
|
||||
return n > 0, err
|
||||
}
|
||||
|
||||
func (r *menuRepository) CountChildren(ctx context.Context, parentID string) (int64, error) {
|
||||
var n int64
|
||||
err := r.db.WithContext(ctx).Model(&entity.Menu{}).Where("parent_id = ?", parentID).Count(&n).Error
|
||||
return n, err
|
||||
}
|
||||
|
||||
func (r *menuRepository) CountRoleRefs(ctx context.Context, menuID string) (int64, error) {
|
||||
var n int64
|
||||
err := r.db.WithContext(ctx).Model(&entity.RoleMenu{}).Where("menu_id = ?", menuID).Count(&n).Error
|
||||
return n, err
|
||||
}
|
||||
|
||||
func (r *menuRepository) ListByPerms(ctx context.Context, perms string) ([]entity.Menu, error) {
|
||||
var rows []entity.Menu
|
||||
err := r.db.WithContext(ctx).Where("perms = ?", perms).Find(&rows).Error
|
||||
return rows, err
|
||||
}
|
||||
|
||||
func (r *menuRepository) ListIDsByPermsIn(ctx context.Context, perms []string) ([]string, error) {
|
||||
if len(perms) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
var ids []string
|
||||
err := r.db.WithContext(ctx).Model(&entity.Menu{}).Where("perms IN ?", perms).Pluck("id", &ids).Error
|
||||
return ids, err
|
||||
}
|
||||
Reference in New Issue
Block a user