83 lines
2.8 KiB
Go
83 lines
2.8 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
channelmodel "github.com/gochat/gochat/internal/model/channel"
|
|
)
|
|
|
|
// ChannelTwitterRepo implements GORM repository for ChannelTwitter.
|
|
// Reference: Chatwoot app/models/channel/twitter_profile.rb
|
|
type ChannelTwitterRepo struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewChannelTwitterRepo creates a new ChannelTwitter repository.
|
|
func NewChannelTwitterRepo(db *gorm.DB) *ChannelTwitterRepo {
|
|
return &ChannelTwitterRepo{db: db}
|
|
}
|
|
|
|
// FindByID retrieves a ChannelTwitter by primary key.
|
|
func (r *ChannelTwitterRepo) FindByID(ctx context.Context, id uint) (*channelmodel.ChannelTwitter, error) {
|
|
var ch channelmodel.ChannelTwitter
|
|
err := r.db.WithContext(ctx).First(&ch, id).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &ch, nil
|
|
}
|
|
|
|
// FindByInboxID retrieves a ChannelTwitter by inbox ID.
|
|
func (r *ChannelTwitterRepo) FindByInboxID(ctx context.Context, inboxID uint) (*channelmodel.ChannelTwitter, error) {
|
|
var ch channelmodel.ChannelTwitter
|
|
err := r.db.WithContext(ctx).Where("inbox_id = ?", inboxID).First(&ch).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &ch, nil
|
|
}
|
|
|
|
// FindByAccountAndInboxID retrieves a ChannelTwitter scoped to an account and inbox.
|
|
func (r *ChannelTwitterRepo) FindByAccountAndInboxID(ctx context.Context, accountID, inboxID uint) (*channelmodel.ChannelTwitter, error) {
|
|
var ch channelmodel.ChannelTwitter
|
|
err := r.db.WithContext(ctx).Where("account_id = ? AND inbox_id = ?", accountID, inboxID).First(&ch).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &ch, nil
|
|
}
|
|
|
|
// FindByTwitterUserID retrieves a ChannelTwitter by Twitter user ID.
|
|
func (r *ChannelTwitterRepo) FindByTwitterUserID(ctx context.Context, twitterUserID string) (*channelmodel.ChannelTwitter, error) {
|
|
var ch channelmodel.ChannelTwitter
|
|
err := r.db.WithContext(ctx).Where("twitter_user_id = ?", twitterUserID).First(&ch).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &ch, nil
|
|
}
|
|
|
|
// Create inserts a new ChannelTwitter record.
|
|
func (r *ChannelTwitterRepo) Create(ctx context.Context, ch *channelmodel.ChannelTwitter) error {
|
|
return r.db.WithContext(ctx).Create(ch).Error
|
|
}
|
|
|
|
// Update updates a ChannelTwitter record.
|
|
func (r *ChannelTwitterRepo) Update(ctx context.Context, ch *channelmodel.ChannelTwitter) error {
|
|
return r.db.WithContext(ctx).Save(ch).Error
|
|
}
|
|
|
|
// Delete soft-deletes a ChannelTwitter record.
|
|
func (r *ChannelTwitterRepo) Delete(ctx context.Context, id uint) error {
|
|
return r.db.WithContext(ctx).Delete(&channelmodel.ChannelTwitter{}, id).Error
|
|
}
|
|
|
|
// ListByAccount retrieves all ChannelTwitter records for an account.
|
|
func (r *ChannelTwitterRepo) ListByAccount(ctx context.Context, accountID uint) ([]channelmodel.ChannelTwitter, error) {
|
|
var channels []channelmodel.ChannelTwitter
|
|
err := r.db.WithContext(ctx).Where("account_id = ?", accountID).Find(&channels).Error
|
|
return channels, err
|
|
}
|