Files
gochat/backend/internal/channel/twilio/repository.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

80 lines
2.9 KiB
Go

package twilio
// Repository provides GORM-based persistence for Twilio SMS channel data.
// Reference: gochat's repository pattern (TikTok, LINE repositories)
//
// Design: Repository pattern with GORM for Twilio SMS channel model persistence.
// - Create, FindByID, Update, Delete with soft-delete support
// - UpdateFields for partial updates using map[string]interface{}
// - FindByPhoneNumber for lookup by Twilio phone number
// - List with account-level filtering for multi-tenant isolation
import (
"context"
"gorm.io/gorm"
channelmodel "github.com/gochat/gochat/internal/model/channel"
)
// Repository handles Twilio SMS channel database operations.
type Repository struct {
db *gorm.DB
}
// NewRepository creates a Twilio SMS channel repository instance.
func NewRepository(db *gorm.DB) *Repository {
return &Repository{db: db}
}
// Create persists a new Twilio SMS channel record.
func (r *Repository) Create(ctx context.Context, channel *channelmodel.ChannelTwilioSMS) error {
return r.db.WithContext(ctx).Create(channel).Error
}
// FindByID retrieves a Twilio SMS channel by primary key.
func (r *Repository) FindByID(ctx context.Context, id uint) (*channelmodel.ChannelTwilioSMS, error) {
var ch channelmodel.ChannelTwilioSMS
if err := r.db.WithContext(ctx).First(&ch, id).Error; err != nil {
return nil, err
}
return &ch, nil
}
// FindByPhoneNumber retrieves a Twilio SMS channel by phone number.
func (r *Repository) FindByPhoneNumber(ctx context.Context, phoneNumber string) (*channelmodel.ChannelTwilioSMS, error) {
var ch channelmodel.ChannelTwilioSMS
if err := r.db.WithContext(ctx).Where("phone_number = ?", phoneNumber).First(&ch).Error; err != nil {
return nil, err
}
return &ch, nil
}
// FindByAccountSID retrieves a Twilio SMS channel by Account SID.
func (r *Repository) FindByAccountSID(ctx context.Context, accountSID string) (*channelmodel.ChannelTwilioSMS, error) {
var ch channelmodel.ChannelTwilioSMS
if err := r.db.WithContext(ctx).Where("account_sid = ?", accountSID).First(&ch).Error; err != nil {
return nil, err
}
return &ch, nil
}
// UpdateFields performs a partial update on a Twilio SMS channel record.
func (r *Repository) UpdateFields(ctx context.Context, id uint, fields map[string]interface{}) error {
return r.db.WithContext(ctx).Model(&channelmodel.ChannelTwilioSMS{}).Where("id = ?", id).Updates(fields).Error
}
// Delete soft-deletes a Twilio SMS channel record (GORM soft-delete).
func (r *Repository) Delete(ctx context.Context, id uint) error {
return r.db.WithContext(ctx).Delete(&channelmodel.ChannelTwilioSMS{}, id).Error
}
// List retrieves all Twilio SMS channels for a given account.
func (r *Repository) List(ctx context.Context, accountID uint) ([]channelmodel.ChannelTwilioSMS, error) {
var channels []channelmodel.ChannelTwilioSMS
if err := r.db.WithContext(ctx).Where("account_id = ?", accountID).Find(&channels).Error; err != nil {
return nil, err
}
return channels, nil
}