package db import ( "context" "database/sql" "encoding/json" "time" ) type Space struct { ID string `json:"id"` UserID string `json:"userId"` Name string `json:"name"` Description string `json:"description"` Icon string `json:"icon"` Color string `json:"color"` CustomInstructions string `json:"customInstructions"` DefaultFocusMode string `json:"defaultFocusMode"` DefaultModel string `json:"defaultModel"` IsPublic bool `json:"isPublic"` Settings map[string]interface{} `json:"settings"` CreatedAt time.Time `json:"createdAt"` UpdatedAt time.Time `json:"updatedAt"` ThreadCount int `json:"threadCount,omitempty"` } type SpaceRepository struct { db *PostgresDB } func NewSpaceRepository(db *PostgresDB) *SpaceRepository { return &SpaceRepository{db: db} } func (r *SpaceRepository) RunMigrations(ctx context.Context) error { migrations := []string{ `CREATE TABLE IF NOT EXISTS spaces ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), user_id UUID NOT NULL, name VARCHAR(255) NOT NULL, description TEXT, icon VARCHAR(50), color VARCHAR(20), custom_instructions TEXT, default_focus_mode VARCHAR(50) DEFAULT 'all', default_model VARCHAR(100), is_public BOOLEAN DEFAULT FALSE, settings JSONB DEFAULT '{}', created_at TIMESTAMPTZ DEFAULT NOW(), updated_at TIMESTAMPTZ DEFAULT NOW() )`, `CREATE INDEX IF NOT EXISTS idx_spaces_user ON spaces(user_id)`, } for _, m := range migrations { if _, err := r.db.db.ExecContext(ctx, m); err != nil { return err } } return nil } func (r *SpaceRepository) Create(ctx context.Context, s *Space) error { settingsJSON, _ := json.Marshal(s.Settings) query := ` INSERT INTO spaces (user_id, name, description, icon, color, custom_instructions, default_focus_mode, default_model, is_public, settings) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING id, created_at, updated_at ` return r.db.db.QueryRowContext(ctx, query, s.UserID, s.Name, s.Description, s.Icon, s.Color, s.CustomInstructions, s.DefaultFocusMode, s.DefaultModel, s.IsPublic, settingsJSON, ).Scan(&s.ID, &s.CreatedAt, &s.UpdatedAt) } func (r *SpaceRepository) GetByID(ctx context.Context, id string) (*Space, error) { query := ` SELECT id, user_id, name, description, icon, color, custom_instructions, default_focus_mode, default_model, is_public, settings, created_at, updated_at, (SELECT COUNT(*) FROM threads WHERE space_id = spaces.id) as thread_count FROM spaces WHERE id = $1 ` var s Space var settingsJSON []byte err := r.db.db.QueryRowContext(ctx, query, id).Scan( &s.ID, &s.UserID, &s.Name, &s.Description, &s.Icon, &s.Color, &s.CustomInstructions, &s.DefaultFocusMode, &s.DefaultModel, &s.IsPublic, &settingsJSON, &s.CreatedAt, &s.UpdatedAt, &s.ThreadCount, ) if err == sql.ErrNoRows { return nil, nil } if err != nil { return nil, err } json.Unmarshal(settingsJSON, &s.Settings) return &s, nil } func (r *SpaceRepository) GetByUserID(ctx context.Context, userID string) ([]*Space, error) { query := ` SELECT id, user_id, name, description, icon, color, custom_instructions, default_focus_mode, default_model, is_public, settings, created_at, updated_at, (SELECT COUNT(*) FROM threads WHERE space_id = spaces.id) as thread_count FROM spaces WHERE user_id = $1 ORDER BY updated_at DESC ` rows, err := r.db.db.QueryContext(ctx, query, userID) if err != nil { return nil, err } defer rows.Close() var spaces []*Space for rows.Next() { var s Space var settingsJSON []byte if err := rows.Scan( &s.ID, &s.UserID, &s.Name, &s.Description, &s.Icon, &s.Color, &s.CustomInstructions, &s.DefaultFocusMode, &s.DefaultModel, &s.IsPublic, &settingsJSON, &s.CreatedAt, &s.UpdatedAt, &s.ThreadCount, ); err != nil { return nil, err } json.Unmarshal(settingsJSON, &s.Settings) spaces = append(spaces, &s) } return spaces, nil } func (r *SpaceRepository) Update(ctx context.Context, s *Space) error { settingsJSON, _ := json.Marshal(s.Settings) query := ` UPDATE spaces SET name = $2, description = $3, icon = $4, color = $5, custom_instructions = $6, default_focus_mode = $7, default_model = $8, is_public = $9, settings = $10, updated_at = NOW() WHERE id = $1 ` _, err := r.db.db.ExecContext(ctx, query, s.ID, s.Name, s.Description, s.Icon, s.Color, s.CustomInstructions, s.DefaultFocusMode, s.DefaultModel, s.IsPublic, settingsJSON, ) return err } func (r *SpaceRepository) Delete(ctx context.Context, id string) error { _, err := r.db.db.ExecContext(ctx, "DELETE FROM spaces WHERE id = $1", id) return err }