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.
93 lines
3.5 KiB
Go
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
|
|
} |