Files
gochat/internal/repository/channel_line_repo.go
T
2026-06-04 15:44:48 +08:00

69 lines
2.2 KiB
Go

package repository
import (
"context"
"gorm.io/gorm"
channelmodel "github.com/gochat/gochat/internal/model/channel"
)
// ChannelLINERepo implements GORM repository for ChannelLINE.
// Reference: LINE Messaging API — https://developers.line.biz/en/docs/messaging-api/
type ChannelLINERepo struct {
db *gorm.DB
}
// NewChannelLINERepo creates a new ChannelLINE repository.
func NewChannelLINERepo(db *gorm.DB) *ChannelLINERepo {
return &ChannelLINERepo{db: db}
}
// FindByID retrieves a ChannelLINE by primary key.
func (r *ChannelLINERepo) FindByID(ctx context.Context, id uint) (*channelmodel.ChannelLINE, error) {
var ch channelmodel.ChannelLINE
err := r.db.WithContext(ctx).First(&ch, id).Error
if err != nil {
return nil, err
}
return &ch, nil
}
// FindByChannelID retrieves a ChannelLINE by LINE Channel ID.
func (r *ChannelLINERepo) FindByChannelID(ctx context.Context, channelID string) (*channelmodel.ChannelLINE, error) {
var ch channelmodel.ChannelLINE
err := r.db.WithContext(ctx).Where("channel_id = ?", channelID).First(&ch).Error
if err != nil {
return nil, err
}
return &ch, nil
}
// Create inserts a new ChannelLINE record.
func (r *ChannelLINERepo) Create(ctx context.Context, ch *channelmodel.ChannelLINE) error {
return r.db.WithContext(ctx).Create(ch).Error
}
// Update modifies an existing ChannelLINE.
func (r *ChannelLINERepo) Update(ctx context.Context, ch *channelmodel.ChannelLINE) error {
return r.db.WithContext(ctx).Save(ch).Error
}
// Delete soft-deletes a ChannelLINE.
func (r *ChannelLINERepo) Delete(ctx context.Context, id uint) error {
return r.db.WithContext(ctx).Delete(&channelmodel.ChannelLINE{}, id).Error
}
// List retrieves all LINE channels.
func (r *ChannelLINERepo) List(ctx context.Context) ([]channelmodel.ChannelLINE, error) {
var channels []channelmodel.ChannelLINE
err := r.db.WithContext(ctx).Order("id DESC").Find(&channels).Error
return channels, err
}
// ListByAccount retrieves all LINE channels for a given account.
func (r *ChannelLINERepo) ListByAccount(ctx context.Context, accountID uint) ([]channelmodel.ChannelLINE, error) {
var channels []channelmodel.ChannelLINE
err := r.db.WithContext(ctx).Where("account_id = ?", accountID).Order("id DESC").Find(&channels).Error
return channels, err
}