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