Files
gochat/internal/channel/tiktok/repository.go
T
2026-06-04 15:44:48 +08:00

93 lines
3.5 KiB
Go

package tiktok
// Repository provides GORM-based persistence for TikTok channel data.
// Reference: gochat's repository pattern (Telegram repository, WhatsApp repository)
//
// Design: Repository pattern abstracts GORM operations, matching gochat's
// existing repository pattern used by other channel providers.
import (
"context"
"fmt"
channelmodel "github.com/gochat/gochat/internal/model/channel"
"gorm.io/gorm"
applogger "github.com/gochat/gochat/pkg/logger"
)
// Repository handles TikTok channel database operations.
type Repository struct {
db *gorm.DB
}
// NewRepository creates a TikTok repository with GORM DB.
func NewRepository(db *gorm.DB) *Repository {
return &Repository{db: db}
}
// === CRUD Operations ===
// Create persists a new ChannelTikTok record.
func (r *Repository) Create(ctx context.Context, channel *channelmodel.ChannelTikTok) error {
if err := r.db.WithContext(ctx).Create(channel).Error; err != nil {
return fmt.Errorf("failed to create TikTok channel: %w", err)
}
applogger.L().Infof("TikTok channel persisted: id=%d, business_id=%s",
channel.ID, channel.TikTokBusinessID)
return nil
}
// Update modifies an existing ChannelTikTok record.
func (r *Repository) Update(ctx context.Context, channel *channelmodel.ChannelTikTok) error {
if err := r.db.WithContext(ctx).Save(channel).Error; err != nil {
return fmt.Errorf("failed to update TikTok channel %d: %w", channel.ID, err)
}
return nil
}
// UpdateFields updates specific fields of a ChannelTikTok by ID.
func (r *Repository) UpdateFields(ctx context.Context, channelID uint, updates map[string]interface{}) error {
if err := r.db.WithContext(ctx).Model(&channelmodel.ChannelTikTok{}).
Where("id = ?", channelID).
Updates(updates).Error; err != nil {
return fmt.Errorf("failed to update TikTok channel fields %d: %w", channelID, err)
}
return nil
}
// Delete soft-deletes a ChannelTikTok record.
func (r *Repository) Delete(ctx context.Context, channelID uint) error {
if err := r.db.WithContext(ctx).Delete(&channelmodel.ChannelTikTok{}, channelID).Error; err != nil {
return fmt.Errorf("failed to delete TikTok channel %d: %w", channelID, err)
}
applogger.L().Infof("TikTok channel soft-deleted: id=%d", channelID)
return nil
}
// GetByID retrieves a ChannelTikTok by primary key.
func (r *Repository) GetByID(ctx context.Context, channelID uint) (*channelmodel.ChannelTikTok, error) {
var channel channelmodel.ChannelTikTok
if err := r.db.WithContext(ctx).First(&channel, channelID).Error; err != nil {
return nil, fmt.Errorf("failed to get TikTok channel %d: %w", channelID, err)
}
return &channel, nil
}
// GetByInboxID retrieves a ChannelTikTok by the associated Inbox ID.
func (r *Repository) GetByInboxID(ctx context.Context, inboxID uint) (*channelmodel.ChannelTikTok, error) {
var channel channelmodel.ChannelTikTok
if err := r.db.WithContext(ctx).Where("inbox_id = ?", inboxID).First(&channel).Error; err != nil {
return nil, fmt.Errorf("failed to get TikTok channel by inbox %d: %w", inboxID, err)
}
return &channel, nil
}
// GetByBusinessID retrieves a ChannelTikTok by TikTok Business ID.
func (r *Repository) GetByBusinessID(ctx context.Context, businessID string) (*channelmodel.ChannelTikTok, error) {
var channel channelmodel.ChannelTikTok
if err := r.db.WithContext(ctx).Where("tiktok_business_id = ?", businessID).First(&channel).Error; err != nil {
return nil, fmt.Errorf("failed to get TikTok channel by business_id %s: %w", businessID, err)
}
return &channel, nil
}