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
|
|
} |