74 lines
2.7 KiB
Go
74 lines
2.7 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
)
|
|
|
|
// CustomAttributeDefinitionRepo implements GORM repository for CustomAttributeDefinition.
|
|
// Reference: Chatwoot app/models/custom_attribute_definition.rb
|
|
type CustomAttributeDefinitionRepo struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewCustomAttributeDefinitionRepo creates a new repository.
|
|
func NewCustomAttributeDefinitionRepo(db *gorm.DB) *CustomAttributeDefinitionRepo {
|
|
return &CustomAttributeDefinitionRepo{db: db}
|
|
}
|
|
|
|
// Create inserts a new custom attribute definition.
|
|
func (r *CustomAttributeDefinitionRepo) Create(ctx context.Context, def *model.CustomAttributeDefinition) error {
|
|
return r.db.WithContext(ctx).Create(def).Error
|
|
}
|
|
|
|
// GetByID retrieves a definition by primary key.
|
|
func (r *CustomAttributeDefinitionRepo) GetByID(ctx context.Context, id uint) (*model.CustomAttributeDefinition, error) {
|
|
var def model.CustomAttributeDefinition
|
|
err := r.db.WithContext(ctx).First(&def, id).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &def, nil
|
|
}
|
|
|
|
// FindByAccount retrieves all definitions for an account with pagination.
|
|
func (r *CustomAttributeDefinitionRepo) FindByAccount(ctx context.Context, accountID uint, offset, limit int) ([]model.CustomAttributeDefinition, int64, error) {
|
|
var defs []model.CustomAttributeDefinition
|
|
var total int64
|
|
|
|
q := r.db.WithContext(ctx).Where("account_id = ?", accountID)
|
|
if err := q.Model(&model.CustomAttributeDefinition{}).Count(&total).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
err := q.Offset(offset).Limit(limit).Order("created_at DESC").Find(&defs).Error
|
|
return defs, total, err
|
|
}
|
|
|
|
// FindByAccountAndModel retrieves definitions filtered by attribute_model (conversation/contact).
|
|
func (r *CustomAttributeDefinitionRepo) FindByAccountAndModel(ctx context.Context, accountID uint, attributeModel string, offset, limit int) ([]model.CustomAttributeDefinition, int64, error) {
|
|
var defs []model.CustomAttributeDefinition
|
|
var total int64
|
|
|
|
q := r.db.WithContext(ctx).Where("account_id = ? AND attribute_model = ?", accountID, attributeModel)
|
|
if err := q.Model(&model.CustomAttributeDefinition{}).Count(&total).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
err := q.Offset(offset).Limit(limit).Order("created_at DESC").Find(&defs).Error
|
|
return defs, total, err
|
|
}
|
|
|
|
// Update modifies a definition by primary key.
|
|
func (r *CustomAttributeDefinitionRepo) Update(ctx context.Context, def *model.CustomAttributeDefinition) error {
|
|
return r.db.WithContext(ctx).Save(def).Error
|
|
}
|
|
|
|
// Delete soft-deletes a definition by primary key.
|
|
func (r *CustomAttributeDefinitionRepo) Delete(ctx context.Context, id uint) error {
|
|
return r.db.WithContext(ctx).Delete(&model.CustomAttributeDefinition{}, id).Error
|
|
}
|