refactor backend packages and global db service
This commit is contained in:
18
backend/internal/models/ai_call_log.go
Normal file
18
backend/internal/models/ai_call_log.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package models
|
||||
|
||||
import "time"
|
||||
|
||||
type SenlinAgentAICallLog struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
UserID uint `gorm:"index;not null"`
|
||||
Provider string `gorm:"not null"`
|
||||
UsedKeyType string `gorm:"not null"`
|
||||
Action string `gorm:"not null"`
|
||||
Status string `gorm:"not null"`
|
||||
Error string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
func (SenlinAgentAICallLog) TableName() string {
|
||||
return "senlin_agent_ai_call_logs"
|
||||
}
|
||||
16
backend/internal/models/ai_key.go
Normal file
16
backend/internal/models/ai_key.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package models
|
||||
|
||||
import "time"
|
||||
|
||||
type SenlinAgentAIKey struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
UserID uint `gorm:"uniqueIndex;not null"`
|
||||
Provider string `gorm:"not null"`
|
||||
EncryptedAPIKey string `gorm:"not null"`
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
func (SenlinAgentAIKey) TableName() string {
|
||||
return "senlin_agent_ai_keys"
|
||||
}
|
||||
17
backend/internal/models/ai_session.go
Normal file
17
backend/internal/models/ai_session.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package models
|
||||
|
||||
import "time"
|
||||
|
||||
type SenlinAgentAISession struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
ProjectID uint `gorm:"index;not null"`
|
||||
CreatedBy uint `gorm:"index;not null"`
|
||||
Title string `gorm:"not null"`
|
||||
Context string `gorm:"type:text"`
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
func (SenlinAgentAISession) TableName() string {
|
||||
return "senlin_agent_ai_sessions"
|
||||
}
|
||||
19
backend/internal/models/inbox_item.go
Normal file
19
backend/internal/models/inbox_item.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package models
|
||||
|
||||
import "time"
|
||||
|
||||
type SenlinAgentInboxItem struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
ProjectID uint `gorm:"index;not null"`
|
||||
CreatedBy uint `gorm:"index;not null"`
|
||||
SourceType string `gorm:"not null"`
|
||||
Title string
|
||||
Body string
|
||||
Status string `gorm:"not null;default:open"`
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
func (SenlinAgentInboxItem) TableName() string {
|
||||
return "senlin_agent_inbox_items"
|
||||
}
|
||||
39
backend/internal/models/new.go
Normal file
39
backend/internal/models/new.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var DBService *gorm.DB
|
||||
|
||||
func New(dsn string) error {
|
||||
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := AutoMigrate(db); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
DBService = db
|
||||
return nil
|
||||
}
|
||||
|
||||
func AutoMigrate(database *gorm.DB) error {
|
||||
return database.AutoMigrate(
|
||||
&SenlinAgentUser{},
|
||||
&SenlinAgentProject{},
|
||||
&SenlinAgentInboxItem{},
|
||||
&SenlinAgentTask{},
|
||||
&SenlinAgentNote{},
|
||||
&SenlinAgentSource{},
|
||||
&SenlinAgentAISession{},
|
||||
&SenlinAgentTag{},
|
||||
&SenlinAgentProjectEvent{},
|
||||
&SenlinAgentAIKey{},
|
||||
&SenlinAgentAICallLog{},
|
||||
&SenlinAgentTaskShare{},
|
||||
)
|
||||
}
|
||||
18
backend/internal/models/note.go
Normal file
18
backend/internal/models/note.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package models
|
||||
|
||||
import "time"
|
||||
|
||||
type SenlinAgentNote struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
ProjectID uint `gorm:"index;not null"`
|
||||
CreatedBy uint `gorm:"index;not null"`
|
||||
SourceInboxItemID *uint `gorm:"index"`
|
||||
Title string `gorm:"not null"`
|
||||
Markdown string `gorm:"type:text"`
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
func (SenlinAgentNote) TableName() string {
|
||||
return "senlin_agent_notes"
|
||||
}
|
||||
24
backend/internal/models/postgres_integration_test.go
Normal file
24
backend/internal/models/postgres_integration_test.go
Normal file
@@ -0,0 +1,24 @@
|
||||
//go:build integration
|
||||
|
||||
package models
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestPostgresPing(t *testing.T) {
|
||||
dsn := os.Getenv("DATABASE_DSN")
|
||||
if dsn == "" {
|
||||
dsn = os.Getenv("DATABASE_URL")
|
||||
}
|
||||
require.NotEmpty(t, dsn, "DATABASE_DSN or DATABASE_URL is required for integration tests")
|
||||
|
||||
database, err := Open(dsn)
|
||||
require.NoError(t, err)
|
||||
sqlDB, err := database.DB()
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, sqlDB.Ping())
|
||||
}
|
||||
16
backend/internal/models/project.go
Normal file
16
backend/internal/models/project.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package models
|
||||
|
||||
import "time"
|
||||
|
||||
type SenlinAgentProject struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
OwnerID uint `gorm:"index;not null"`
|
||||
Name string `gorm:"not null"`
|
||||
Description string
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
func (SenlinAgentProject) TableName() string {
|
||||
return "senlin_agent_projects"
|
||||
}
|
||||
18
backend/internal/models/project_event.go
Normal file
18
backend/internal/models/project_event.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package models
|
||||
|
||||
import "time"
|
||||
|
||||
type SenlinAgentProjectEvent struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
ProjectID uint `gorm:"index;not null"`
|
||||
ActorID uint `gorm:"index;not null"`
|
||||
EventType string `gorm:"not null"`
|
||||
EntityType string `gorm:"not null"`
|
||||
EntityID uint `gorm:"not null"`
|
||||
Summary string `gorm:"not null"`
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
func (SenlinAgentProjectEvent) TableName() string {
|
||||
return "senlin_agent_project_events"
|
||||
}
|
||||
21
backend/internal/models/source.go
Normal file
21
backend/internal/models/source.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package models
|
||||
|
||||
import "time"
|
||||
|
||||
type SenlinAgentSource struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
ProjectID uint `gorm:"index;not null"`
|
||||
CreatedBy uint `gorm:"index;not null"`
|
||||
SourceInboxItemID *uint `gorm:"index"`
|
||||
Kind string `gorm:"not null"`
|
||||
Title string `gorm:"not null"`
|
||||
URL string
|
||||
FilePath string
|
||||
ContentText string `gorm:"type:text"`
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
func (SenlinAgentSource) TableName() string {
|
||||
return "senlin_agent_sources"
|
||||
}
|
||||
14
backend/internal/models/tag.go
Normal file
14
backend/internal/models/tag.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package models
|
||||
|
||||
import "time"
|
||||
|
||||
type SenlinAgentTag struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
ProjectID uint `gorm:"index;not null"`
|
||||
Name string `gorm:"not null"`
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
func (SenlinAgentTag) TableName() string {
|
||||
return "senlin_agent_tags"
|
||||
}
|
||||
22
backend/internal/models/task.go
Normal file
22
backend/internal/models/task.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package models
|
||||
|
||||
import "time"
|
||||
|
||||
type SenlinAgentTask struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
ProjectID uint `gorm:"index;not null"`
|
||||
CreatedBy uint `gorm:"index;not null"`
|
||||
AssigneeID *uint `gorm:"index"`
|
||||
SourceInboxItemID *uint `gorm:"index"`
|
||||
Title string `gorm:"not null"`
|
||||
Description string
|
||||
Status string `gorm:"not null;default:open"`
|
||||
SortOrder int `gorm:"not null;default:0"`
|
||||
DueAt *time.Time
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
func (SenlinAgentTask) TableName() string {
|
||||
return "senlin_agent_tasks"
|
||||
}
|
||||
15
backend/internal/models/task_share.go
Normal file
15
backend/internal/models/task_share.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package models
|
||||
|
||||
import "time"
|
||||
|
||||
type SenlinAgentTaskShare struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
TaskID uint `gorm:"index;not null"`
|
||||
ObjectType string `gorm:"not null"`
|
||||
ObjectID uint `gorm:"not null"`
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
func (SenlinAgentTaskShare) TableName() string {
|
||||
return "senlin_agent_task_shares"
|
||||
}
|
||||
17
backend/internal/models/user.go
Normal file
17
backend/internal/models/user.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package models
|
||||
|
||||
import "time"
|
||||
|
||||
type SenlinAgentUser struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
Email string `gorm:"uniqueIndex;not null"`
|
||||
DisplayName string `gorm:"not null"`
|
||||
PasswordHash string `gorm:"not null"`
|
||||
Role string `gorm:"not null;default:user"`
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
func (SenlinAgentUser) TableName() string {
|
||||
return "senlin_agent_users"
|
||||
}
|
||||
Reference in New Issue
Block a user