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.
54 lines
1.8 KiB
Go
54 lines
1.8 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// CopilotMessageRepo provides data access for CopilotMessage.
|
|
type CopilotMessageRepo struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewCopilotMessageRepo(db *gorm.DB) *CopilotMessageRepo {
|
|
return &CopilotMessageRepo{db: db}
|
|
}
|
|
|
|
func (r *CopilotMessageRepo) DB() *gorm.DB {
|
|
return r.db
|
|
}
|
|
|
|
func (r *CopilotMessageRepo) Create(ctx context.Context, msg *model.CopilotMessage) error {
|
|
return r.db.WithContext(ctx).Create(msg).Error
|
|
}
|
|
|
|
func (r *CopilotMessageRepo) GetByID(ctx context.Context, id uint) (*model.CopilotMessage, error) {
|
|
var msg model.CopilotMessage
|
|
if err := r.db.WithContext(ctx).Preload("CopilotThread.User").Preload("CopilotThread.Assistant").First(&msg, id).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &msg, nil
|
|
}
|
|
|
|
func (r *CopilotMessageRepo) FindByThreadID(ctx context.Context, threadID uint, offset, limit int) ([]model.CopilotMessage, int64, error) {
|
|
var msgs []model.CopilotMessage
|
|
var count int64
|
|
db := r.db.WithContext(ctx).Model(&model.CopilotMessage{}).Where("copilot_thread_id = ?", threadID)
|
|
db.Count(&count)
|
|
if err := db.Preload("CopilotThread.User").Preload("CopilotThread.Assistant").Offset(offset).Limit(limit).Order("created_at ASC").Find(&msgs).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
return msgs, count, nil
|
|
}
|
|
|
|
// ListByThread is an alias for FindByThreadID for backward compatibility.
|
|
func (r *CopilotMessageRepo) ListByThread(ctx context.Context, threadID uint, offset, limit int) ([]model.CopilotMessage, int64, error) {
|
|
return r.FindByThreadID(ctx, threadID, offset, limit)
|
|
}
|
|
|
|
func (r *CopilotMessageRepo) DeleteByThread(ctx context.Context, threadID uint) error {
|
|
return r.db.WithContext(ctx).Where("copilot_thread_id = ?", threadID).Delete(&model.CopilotMessage{}).Error
|
|
}
|