Files
gochat/internal/channel/twilio/repository.go
T
2026-06-04 15:44:48 +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
}