73 lines
2.3 KiB
Go
73 lines
2.3 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
channelmodel "github.com/gochat/gochat/internal/model/channel"
|
|
)
|
|
|
|
// ChannelEmailRepo implements GORM repository for ChannelEmail.
|
|
// Reference: Chatwoot app/models/channel/email.rb
|
|
type ChannelEmailRepo struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewChannelEmailRepo creates a new ChannelEmail repository.
|
|
func NewChannelEmailRepo(db *gorm.DB) *ChannelEmailRepo {
|
|
return &ChannelEmailRepo{db: db}
|
|
}
|
|
|
|
// FindByID retrieves a ChannelEmail by primary key.
|
|
func (r *ChannelEmailRepo) FindByID(ctx context.Context, id uint) (*channelmodel.ChannelEmail, error) {
|
|
var ch channelmodel.ChannelEmail
|
|
err := r.db.WithContext(ctx).First(&ch, id).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &ch, nil
|
|
}
|
|
|
|
// FindByInboxID retrieves a ChannelEmail by inbox ID.
|
|
func (r *ChannelEmailRepo) FindByInboxID(ctx context.Context, inboxID uint) (*channelmodel.ChannelEmail, error) {
|
|
var ch channelmodel.ChannelEmail
|
|
err := r.db.WithContext(ctx).Where("inbox_id = ?", inboxID).First(&ch).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &ch, nil
|
|
}
|
|
|
|
// FindByEmail retrieves a ChannelEmail by email address.
|
|
func (r *ChannelEmailRepo) FindByEmail(ctx context.Context, email string) (*channelmodel.ChannelEmail, error) {
|
|
var ch channelmodel.ChannelEmail
|
|
err := r.db.WithContext(ctx).Where("email = ?", email).First(&ch).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &ch, nil
|
|
}
|
|
|
|
// Create inserts a new ChannelEmail record.
|
|
func (r *ChannelEmailRepo) Create(ctx context.Context, ch *channelmodel.ChannelEmail) error {
|
|
return r.db.WithContext(ctx).Create(ch).Error
|
|
}
|
|
|
|
// Update modifies an existing ChannelEmail.
|
|
func (r *ChannelEmailRepo) Update(ctx context.Context, ch *channelmodel.ChannelEmail) error {
|
|
return r.db.WithContext(ctx).Save(ch).Error
|
|
}
|
|
|
|
// Delete soft-deletes a ChannelEmail.
|
|
func (r *ChannelEmailRepo) Delete(ctx context.Context, id uint) error {
|
|
return r.db.WithContext(ctx).Delete(&channelmodel.ChannelEmail{}, id).Error
|
|
}
|
|
|
|
// ListByAccount retrieves all Email channels for an account.
|
|
func (r *ChannelEmailRepo) ListByAccount(ctx context.Context, accountID uint) ([]channelmodel.ChannelEmail, error) {
|
|
var channels []channelmodel.ChannelEmail
|
|
err := r.db.WithContext(ctx).Where("account_id = ?", accountID).Order("id DESC").Find(&channels).Error
|
|
return channels, err
|
|
}
|