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.
62 lines
1.9 KiB
Go
62 lines
1.9 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
)
|
|
|
|
// ContactNoteRepo implements GORM repository for ContactNote.
|
|
// Reference: Chatwoot app/models/note.rb (notes attached to contacts)
|
|
type ContactNoteRepo struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewContactNoteRepo creates a new ContactNote repository.
|
|
func NewContactNoteRepo(db *gorm.DB) *ContactNoteRepo {
|
|
return &ContactNoteRepo{db: db}
|
|
}
|
|
|
|
// FindByContact retrieves all notes for a contact.
|
|
func (r *ContactNoteRepo) FindByContact(ctx context.Context, contactID uint) ([]model.ContactNote, error) {
|
|
var notes []model.ContactNote
|
|
err := r.db.WithContext(ctx).Preload("User").
|
|
Where("contact_id = ?", contactID).
|
|
Order("id ASC").Find(¬es).Error
|
|
return notes, err
|
|
}
|
|
|
|
// FindByID retrieves a single note by primary key.
|
|
func (r *ContactNoteRepo) FindByID(ctx context.Context, id uint) (*model.ContactNote, error) {
|
|
var note model.ContactNote
|
|
err := r.db.WithContext(ctx).Preload("User").First(¬e, id).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return ¬e, nil
|
|
}
|
|
|
|
// Create inserts a new contact note.
|
|
func (r *ContactNoteRepo) Create(ctx context.Context, note *model.ContactNote) error {
|
|
return r.db.WithContext(ctx).Create(note).Error
|
|
}
|
|
|
|
// Delete removes a contact note.
|
|
func (r *ContactNoteRepo) Delete(ctx context.Context, id uint) error {
|
|
return r.db.WithContext(ctx).Delete(&model.ContactNote{}, id).Error
|
|
}
|
|
|
|
// Update updates a contact note.
|
|
func (r *ContactNoteRepo) Update(ctx context.Context, note *model.ContactNote) error {
|
|
return r.db.WithContext(ctx).Save(note).Error
|
|
}
|
|
|
|
// ReparentNotes changes all notes from one contact to another (used in contact merge).
|
|
func (r *ContactNoteRepo) ReparentNotes(ctx context.Context, fromContactID, toContactID uint) error {
|
|
return r.db.WithContext(ctx).
|
|
Model(&model.ContactNote{}).
|
|
Where("contact_id = ?", fromContactID).
|
|
Update("contact_id", toContactID).Error
|
|
} |