fix(api): enforce project identifier uniqueness
This commit is contained in:
@@ -77,17 +77,18 @@ func upsertUser(tx *gorm.DB, email string, displayName string, password string)
|
||||
func seedProjects(tx *gorm.DB, ownerID uint) ([]models.SenlinAgentProject, error) {
|
||||
definitions := []struct {
|
||||
Name string
|
||||
Identifier string
|
||||
Description string
|
||||
}{
|
||||
{Name: "项目 A1", Description: "企业智能协作平台的核心工作空间"},
|
||||
{Name: "项目 A2", Description: "客户成功与交付跟踪"},
|
||||
{Name: "数据中台", Description: "内部数据治理与指标分析"},
|
||||
{Name: "运营自动化", Description: "定时任务和业务流程自动化"},
|
||||
{Name: "项目 A1", Identifier: "A1", Description: "企业智能协作平台的核心工作空间"},
|
||||
{Name: "项目 A2", Identifier: "A2", Description: "客户成功与交付跟踪"},
|
||||
{Name: "数据中台", Identifier: "DATA", Description: "内部数据治理与指标分析"},
|
||||
{Name: "运营自动化", Identifier: "AUTO", Description: "定时任务和业务流程自动化"},
|
||||
}
|
||||
|
||||
projects := make([]models.SenlinAgentProject, 0, len(definitions))
|
||||
for _, definition := range definitions {
|
||||
project, err := firstOrCreateProject(tx, ownerID, definition.Name, definition.Description)
|
||||
project, err := firstOrCreateProject(tx, ownerID, definition.Name, definition.Identifier, definition.Description)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -107,7 +108,7 @@ func seedProjects(tx *gorm.DB, ownerID uint) ([]models.SenlinAgentProject, error
|
||||
return projects, nil
|
||||
}
|
||||
|
||||
func firstOrCreateProject(tx *gorm.DB, ownerID uint, name string, description string) (models.SenlinAgentProject, error) {
|
||||
func firstOrCreateProject(tx *gorm.DB, ownerID uint, name string, identifier string, description string) (models.SenlinAgentProject, error) {
|
||||
var project models.SenlinAgentProject
|
||||
err := tx.Where("owner_id = ? AND name = ?", ownerID, name).First(&project).Error
|
||||
if err == nil {
|
||||
@@ -116,7 +117,7 @@ func firstOrCreateProject(tx *gorm.DB, ownerID uint, name string, description st
|
||||
if err != gorm.ErrRecordNotFound {
|
||||
return project, err
|
||||
}
|
||||
project = models.SenlinAgentProject{OwnerID: ownerID, Name: name, Description: description}
|
||||
project = models.SenlinAgentProject{OwnerID: ownerID, Name: name, Identifier: identifier, Description: description}
|
||||
return project, tx.Create(&project).Error
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user