Restructure the monorepo into clear top-level directories: - backend/: Go module root (cmd, internal, pkg, configs, migrations, docs/swagger, scripts, tests, go.mod, Makefile, .air.toml) - deploy/: Docker (Dockerfile, docker-compose*), quickstart, fluentd - docs/: project documentation + reports/ (moved from repo root) - AGENTS.md: new AI coding-agent guide at repo root Update all references to the new layout: - Dockerfile: COPY backend/go.mod, COPY backend/ (context = repo root) - docker-compose files: context ../.., dockerfile deploy/docker/Dockerfile, env_file ../../.env, volume mounts ../../backend:/app - deploy/quickstart/compose.yaml: dockerfile deploy/docker/Dockerfile - CI: working-directory: backend for go commands, file deploy/docker/Dockerfile, coverage path backend/coverage.out, health_check backend/scripts/ - backend/Makefile: docker target uses -f ../deploy/docker/Dockerfile ../ - README: architecture tree, quickstart, config paths updated Move root stray scripts (rename_models.*, run_m11_tests.sh, verify_build.sh, gorm_bool_main.go) to backend/scripts/legacy/. All moves via git mv to preserve history. Build, vet, SQLite tests, and docker compose config verified.
92 lines
3.3 KiB
Go
92 lines
3.3 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// CopilotThreadRepo provides data access for CopilotThread.
|
|
type CopilotThreadRepo struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewCopilotThreadRepo(db *gorm.DB) *CopilotThreadRepo {
|
|
return &CopilotThreadRepo{db: db}
|
|
}
|
|
|
|
func (r *CopilotThreadRepo) DB() *gorm.DB {
|
|
return r.db
|
|
}
|
|
|
|
func (r *CopilotThreadRepo) Create(ctx context.Context, thread *model.CopilotThread) error {
|
|
return r.db.WithContext(ctx).Create(thread).Error
|
|
}
|
|
|
|
func (r *CopilotThreadRepo) GetByID(ctx context.Context, id uint) (*model.CopilotThread, error) {
|
|
var thread model.CopilotThread
|
|
if err := r.db.WithContext(ctx).Preload("Messages").Preload("User").Preload("Assistant").First(&thread, id).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &thread, nil
|
|
}
|
|
|
|
func (r *CopilotThreadRepo) GetByAccountUserAndID(ctx context.Context, accountID, userID, id uint) (*model.CopilotThread, error) {
|
|
var thread model.CopilotThread
|
|
if err := r.db.WithContext(ctx).
|
|
Preload("Messages").Preload("User").Preload("Assistant").
|
|
Where("account_id = ? AND user_id = ? AND id = ?", accountID, userID, id).
|
|
First(&thread).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &thread, nil
|
|
}
|
|
|
|
func (r *CopilotThreadRepo) Update(ctx context.Context, thread *model.CopilotThread) error {
|
|
return r.db.WithContext(ctx).Save(thread).Error
|
|
}
|
|
|
|
func (r *CopilotThreadRepo) Delete(ctx context.Context, id uint) error {
|
|
return r.db.WithContext(ctx).Delete(&model.CopilotThread{}, id).Error
|
|
}
|
|
|
|
func (r *CopilotThreadRepo) DeleteByAccountUser(ctx context.Context, accountID, userID, id uint) error {
|
|
return r.db.WithContext(ctx).Where("account_id = ? AND user_id = ? AND id = ?", accountID, userID, id).Delete(&model.CopilotThread{}).Error
|
|
}
|
|
|
|
func (r *CopilotThreadRepo) ListByUser(ctx context.Context, accountID, userID uint, offset, limit int) ([]model.CopilotThread, int64, error) {
|
|
var threads []model.CopilotThread
|
|
var count int64
|
|
db := r.db.WithContext(ctx).Model(&model.CopilotThread{}).Where("account_id = ? AND user_id = ?", accountID, userID)
|
|
db.Count(&count)
|
|
if err := db.Preload("User").Preload("Assistant").Order("created_at DESC").Offset(offset).Limit(limit).Find(&threads).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
return threads, count, nil
|
|
}
|
|
|
|
// FindByAccountID retrieves all threads for a given account with pagination.
|
|
func (r *CopilotThreadRepo) FindByAccountID(ctx context.Context, accountID uint, offset, limit int) ([]model.CopilotThread, int64, error) {
|
|
var threads []model.CopilotThread
|
|
var count int64
|
|
db := r.db.WithContext(ctx).Model(&model.CopilotThread{}).Where("account_id = ?", accountID)
|
|
db.Count(&count)
|
|
if err := db.Offset(offset).Limit(limit).Find(&threads).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
return threads, count, nil
|
|
}
|
|
|
|
// FindByAssistantID retrieves all threads linked to a given assistant.
|
|
func (r *CopilotThreadRepo) FindByAssistantID(ctx context.Context, assistantID uint, offset, limit int) ([]model.CopilotThread, int64, error) {
|
|
var threads []model.CopilotThread
|
|
var count int64
|
|
db := r.db.WithContext(ctx).Model(&model.CopilotThread{}).Where("assistant_id = ?", assistantID)
|
|
db.Count(&count)
|
|
if err := db.Offset(offset).Limit(limit).Find(&threads).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
return threads, count, nil
|
|
}
|