79 lines
2.7 KiB
Go
79 lines
2.7 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
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
// 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
|
|
} |