fix: 添加角色名重复检查
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 6m46s

- 创建角色时检查角色名是否已存在
- 更新角色时检查角色名是否与其他角色重复

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-02-26 14:55:46 +08:00
parent 1382cbbf47
commit 4ba1f5b99d
2 changed files with 34 additions and 2 deletions

View File

@@ -42,6 +42,20 @@ func (s *RoleStore) GetByName(ctx context.Context, name string) (*model.Role, er
return &role, nil
}
// ExistsByName 检查角色名是否已存在
// excludeID: 排除的角色 ID用于更新时排除自身传 0 表示不排除
func (s *RoleStore) ExistsByName(ctx context.Context, name string, excludeID uint) (bool, error) {
var count int64
query := s.db.WithContext(ctx).Model(&model.Role{}).Where("role_name = ?", name)
if excludeID > 0 {
query = query.Where("id != ?", excludeID)
}
if err := query.Count(&count).Error; err != nil {
return false, err
}
return count > 0, nil
}
// Update 更新角色
func (s *RoleStore) Update(ctx context.Context, role *model.Role) error {
return s.db.WithContext(ctx).Save(role).Error