refactor: shorten model and table prefixes
This commit is contained in:
@@ -40,14 +40,14 @@ type ConfirmResult struct {
|
||||
}
|
||||
|
||||
type Analyzer interface {
|
||||
Analyze(item models.SenlinAgentInboxItem, userID uint) ([]Suggestion, error)
|
||||
Analyze(item models.SaInboxItem, userID uint) ([]Suggestion, error)
|
||||
}
|
||||
|
||||
type StaticAnalyzer struct {
|
||||
Suggestions []Suggestion
|
||||
}
|
||||
|
||||
func (a StaticAnalyzer) Analyze(item models.SenlinAgentInboxItem, userID uint) ([]Suggestion, error) {
|
||||
func (a StaticAnalyzer) Analyze(item models.SaInboxItem, userID uint) ([]Suggestion, error) {
|
||||
if len(a.Suggestions) > 0 {
|
||||
return a.Suggestions, nil
|
||||
}
|
||||
@@ -85,7 +85,7 @@ func (s *Service) database() *gorm.DB {
|
||||
}
|
||||
|
||||
// Capture 先以 owner + project identity 校验项目边界,再创建归属于当前用户的原始 Inbox 条目。
|
||||
func (s *Service) Capture(input CaptureInput) (*models.SenlinAgentInboxItem, error) {
|
||||
func (s *Service) Capture(input CaptureInput) (*models.SaInboxItem, error) {
|
||||
if strings.TrimSpace(input.ProjectIdentity) == "" || input.UserID == 0 {
|
||||
return nil, ErrProjectAndUserRequired
|
||||
}
|
||||
@@ -93,13 +93,13 @@ func (s *Service) Capture(input CaptureInput) (*models.SenlinAgentInboxItem, err
|
||||
if sourceType == "" {
|
||||
return nil, ErrSourceTypeRequired
|
||||
}
|
||||
var item models.SenlinAgentInboxItem
|
||||
var item models.SaInboxItem
|
||||
err := s.database().Transaction(func(tx *gorm.DB) error {
|
||||
var project models.SenlinAgentProject
|
||||
var project models.SaProject
|
||||
if err := tx.Where("owner_id = ? AND identity = ?", input.UserID, input.ProjectIdentity).First(&project).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
item = models.SenlinAgentInboxItem{
|
||||
item = models.SaInboxItem{
|
||||
ProjectID: project.ID, ProjectIdentity: project.Identity, CreatedBy: input.UserID,
|
||||
SourceType: sourceType, Title: strings.TrimSpace(input.Title), Body: strings.TrimSpace(input.Body), Status: "open",
|
||||
}
|
||||
@@ -138,11 +138,11 @@ func (s *Service) Analyze(itemIdentity string, userID uint) ([]Suggestion, error
|
||||
if locked.Status != "open" {
|
||||
return ErrInboxAlreadyConfirmed
|
||||
}
|
||||
if err := tx.Where("inbox_item_id = ?", locked.ID).Delete(&models.SenlinAgentInboxSuggestion{}).Error; err != nil {
|
||||
if err := tx.Where("inbox_item_id = ?", locked.ID).Delete(&models.SaInboxSuggestion{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
for _, suggestion := range normalized {
|
||||
draft := models.SenlinAgentInboxSuggestion{
|
||||
draft := models.SaInboxSuggestion{
|
||||
InboxItemID: locked.ID, InboxItemIdentity: locked.Identity, CreatedBy: userID,
|
||||
Kind: suggestion.Kind, Title: suggestion.Title, Body: suggestion.Body,
|
||||
}
|
||||
@@ -174,14 +174,14 @@ func (s *Service) Confirm(itemIdentity string, userID uint, selectedSuggestionId
|
||||
return err
|
||||
}
|
||||
|
||||
var saved []models.SenlinAgentInboxSuggestion
|
||||
var saved []models.SaInboxSuggestion
|
||||
if err := tx.Where("inbox_item_id = ? AND created_by = ? AND identity IN ?", item.ID, userID, selected).Find(&saved).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if len(saved) != len(selected) {
|
||||
return ErrSuggestionNotFound
|
||||
}
|
||||
byIdentity := make(map[string]models.SenlinAgentInboxSuggestion, len(saved))
|
||||
byIdentity := make(map[string]models.SaInboxSuggestion, len(saved))
|
||||
for _, suggestion := range saved {
|
||||
byIdentity[suggestion.Identity] = suggestion
|
||||
}
|
||||
@@ -203,11 +203,11 @@ func (s *Service) Confirm(itemIdentity string, userID uint, selectedSuggestionId
|
||||
return result, err
|
||||
}
|
||||
|
||||
func findOwnedInboxItem(tx *gorm.DB, identity string, userID uint, lock bool) (*models.SenlinAgentInboxItem, error) {
|
||||
var item models.SenlinAgentInboxItem
|
||||
query := tx.Model(&models.SenlinAgentInboxItem{}).
|
||||
Joins("JOIN senlin_agent_projects ON senlin_agent_projects.id = senlin_agent_inbox_items.project_id").
|
||||
Where("senlin_agent_inbox_items.identity = ? AND senlin_agent_inbox_items.created_by = ? AND senlin_agent_projects.owner_id = ?", identity, userID, userID)
|
||||
func findOwnedInboxItem(tx *gorm.DB, identity string, userID uint, lock bool) (*models.SaInboxItem, error) {
|
||||
var item models.SaInboxItem
|
||||
query := tx.Model(&models.SaInboxItem{}).
|
||||
Joins("JOIN sa_projects ON sa_projects.id = sa_inbox_items.project_id").
|
||||
Where("sa_inbox_items.identity = ? AND sa_inbox_items.created_by = ? AND sa_projects.owner_id = ?", identity, userID, userID)
|
||||
if lock {
|
||||
query = query.Clauses(clause.Locking{Strength: "UPDATE"})
|
||||
}
|
||||
@@ -230,7 +230,7 @@ func normalizeSuggestions(suggestions []Suggestion) ([]Suggestion, error) {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func suggestionSubject(item models.SenlinAgentInboxItem) string {
|
||||
func suggestionSubject(item models.SaInboxItem) string {
|
||||
value := strings.TrimSpace(item.Title)
|
||||
if value == "" {
|
||||
value = strings.TrimSpace(item.Body)
|
||||
@@ -266,22 +266,22 @@ func normalizeSelectedIdentities(values []string) ([]string, error) {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func createConfirmedObject(tx *gorm.DB, item models.SenlinAgentInboxItem, suggestion models.SenlinAgentInboxSuggestion) error {
|
||||
func createConfirmedObject(tx *gorm.DB, item models.SaInboxItem, suggestion models.SaInboxSuggestion) error {
|
||||
sourceInboxItemID := item.ID
|
||||
switch suggestion.Kind {
|
||||
case "task":
|
||||
return tx.Create(&models.SenlinAgentTask{
|
||||
return tx.Create(&models.SaTask{
|
||||
ProjectID: item.ProjectID, ProjectIdentity: item.ProjectIdentity, CreatedBy: item.CreatedBy,
|
||||
SourceInboxItemID: &sourceInboxItemID, Title: suggestion.Title, Description: suggestion.Body, Status: "open",
|
||||
}).Error
|
||||
case "note":
|
||||
return tx.Create(&models.SenlinAgentNote{
|
||||
return tx.Create(&models.SaNote{
|
||||
ProjectID: item.ProjectID, ProjectIdentity: item.ProjectIdentity, CreatedBy: item.CreatedBy,
|
||||
SourceInboxItemID: &sourceInboxItemID, Title: suggestion.Title, Markdown: suggestion.Body,
|
||||
}).Error
|
||||
case "source":
|
||||
// 文本型 Inbox 资料不是上传文件,不构造或伪造任何本地路径。
|
||||
return tx.Create(&models.SenlinAgentSource{
|
||||
return tx.Create(&models.SaSource{
|
||||
ProjectID: item.ProjectID, ProjectIdentity: item.ProjectIdentity, CreatedBy: item.CreatedBy,
|
||||
SourceInboxItemID: &sourceInboxItemID, Kind: "text", Title: suggestion.Title, ContentText: suggestion.Body,
|
||||
}).Error
|
||||
|
||||
Reference in New Issue
Block a user