Files
gochat/backend/internal/repository/draft_message_repo.go
T
rogee aeddedf2a3 Reorganize repo: backend/, deploy/, docs/ layout + AGENTS.md
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.
2026-07-07 14:44:12 +08:00

95 lines
3.4 KiB
Go

package repository
import (
"context"
"gorm.io/gorm"
"github.com/gochat/gochat/internal/model"
)
// DraftMessageRepo implements GORM repository for DraftMessage.
// Reference: Chatwoot app/models/draft_message.rb
type DraftMessageRepo struct {
db *gorm.DB
}
// NewDraftMessageRepo creates a new DraftMessage repository.
func NewDraftMessageRepo(db *gorm.DB) *DraftMessageRepo {
return &DraftMessageRepo{db: db}
}
// FindByConversationID retrieves all draft messages for a conversation, optionally filtered by user.
func (r *DraftMessageRepo) FindByConversationID(ctx context.Context, conversationID uint, userID uint) ([]model.DraftMessage, error) {
var drafts []model.DraftMessage
query := r.db.WithContext(ctx).Where("conversation_id = ?", conversationID)
if userID > 0 {
query = query.Where("user_id = ?", userID)
}
err := query.Order("id DESC").Find(&drafts).Error
return drafts, err
}
// FindLatestByConversationID retrieves the single Chatwoot-style draft for a conversation.
func (r *DraftMessageRepo) FindLatestByConversationID(ctx context.Context, conversationID uint) (*model.DraftMessage, error) {
var draft model.DraftMessage
err := r.db.WithContext(ctx).Where("conversation_id = ?", conversationID).Order("id DESC").First(&draft).Error
if err != nil {
return nil, err
}
return &draft, nil
}
// FindByID retrieves a draft message by ID.
func (r *DraftMessageRepo) FindByID(ctx context.Context, id uint) (*model.DraftMessage, error) {
var draft model.DraftMessage
err := r.db.WithContext(ctx).First(&draft, id).Error
if err != nil {
return nil, err
}
return &draft, nil
}
// Create creates a new draft message.
func (r *DraftMessageRepo) Create(ctx context.Context, draft *model.DraftMessage) error {
return r.db.WithContext(ctx).Create(draft).Error
}
// Update updates an existing draft message.
func (r *DraftMessageRepo) Update(ctx context.Context, draft *model.DraftMessage) error {
return r.db.WithContext(ctx).Save(draft).Error
}
// Delete deletes a draft message.
func (r *DraftMessageRepo) Delete(ctx context.Context, id uint) error {
return r.db.WithContext(ctx).Delete(&model.DraftMessage{}, id).Error
}
// DeleteByConversationID removes all persisted drafts for a conversation.
func (r *DraftMessageRepo) DeleteByConversationID(ctx context.Context, conversationID uint) error {
return r.db.WithContext(ctx).Where("conversation_id = ?", conversationID).Delete(&model.DraftMessage{}).Error
}
// SearchByContent retrieves draft messages whose content matches the given query string (LIKE).
func (r *DraftMessageRepo) SearchByContent(ctx context.Context, accountID uint, query string) ([]model.DraftMessage, error) {
var drafts []model.DraftMessage
// Join through conversations to filter by account_id
err := r.db.WithContext(ctx).
Joins("JOIN conversations ON conversations.id = draft_messages.conversation_id").
Where("conversations.account_id = ?", accountID).
Where("draft_messages.content LIKE ?", "%"+query+"%").
Order("draft_messages.id DESC").
Find(&drafts).Error
return drafts, err
}
// CountByAccount returns the total number of draft messages for an account.
func (r *DraftMessageRepo) CountByAccount(ctx context.Context, accountID uint) (int64, error) {
var count int64
err := r.db.WithContext(ctx).
Joins("JOIN conversations ON conversations.id = draft_messages.conversation_id").
Where("conversations.account_id = ?", accountID).
Count(&count).Error
return count, err
}