59 lines
1.8 KiB
Go
59 lines
1.8 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
)
|
|
|
|
// PreChatFormRepo implements GORM repository for PreChatForm.
|
|
type PreChatFormRepo struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewPreChatFormRepo creates a new PreChatForm repository.
|
|
func NewPreChatFormRepo(db *gorm.DB) *PreChatFormRepo {
|
|
return &PreChatFormRepo{db: db}
|
|
}
|
|
|
|
// FindByInboxID retrieves the pre-chat form config for a specific inbox.
|
|
func (r *PreChatFormRepo) FindByInboxID(ctx context.Context, inboxID uint) (*model.PreChatForm, error) {
|
|
var form model.PreChatForm
|
|
err := r.db.WithContext(ctx).Where("inbox_id = ?", inboxID).First(&form).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &form, nil
|
|
}
|
|
|
|
// FindByID retrieves a pre-chat form by primary key.
|
|
func (r *PreChatFormRepo) FindByID(ctx context.Context, id uint) (*model.PreChatForm, error) {
|
|
var form model.PreChatForm
|
|
err := r.db.WithContext(ctx).First(&form, id).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &form, nil
|
|
}
|
|
|
|
// Create inserts a new pre-chat form config.
|
|
func (r *PreChatFormRepo) Create(ctx context.Context, form *model.PreChatForm) error {
|
|
return r.db.WithContext(ctx).Create(form).Error
|
|
}
|
|
|
|
// Update modifies an existing pre-chat form config.
|
|
func (r *PreChatFormRepo) Update(ctx context.Context, form *model.PreChatForm) error {
|
|
return r.db.WithContext(ctx).Save(form).Error
|
|
}
|
|
|
|
// Delete soft-deletes a pre-chat form config.
|
|
func (r *PreChatFormRepo) Delete(ctx context.Context, id uint) error {
|
|
return r.db.WithContext(ctx).Delete(&model.PreChatForm{}, id).Error
|
|
}
|
|
|
|
// DeleteByInboxID soft-deletes the pre-chat form for a specific inbox.
|
|
func (r *PreChatFormRepo) DeleteByInboxID(ctx context.Context, inboxID uint) error {
|
|
return r.db.WithContext(ctx).Where("inbox_id = ?", inboxID).Delete(&model.PreChatForm{}).Error
|
|
} |