Files
gochat/backend/internal/repository/custom_attribute_definition_repo.go
T
rogee aeddedf2a3 Reorganize repo: backend/, deploy/, docs/ layout + AGENTS.md
Restructure the monorepo into clear top-level directories:
- backend/: Go module root (cmd, internal, pkg, configs, migrations,
  docs/swagger, scripts, tests, go.mod, Makefile, .air.toml)
- deploy/: Docker (Dockerfile, docker-compose*), quickstart, fluentd
- docs/: project documentation + reports/ (moved from repo root)
- AGENTS.md: new AI coding-agent guide at repo root

Update all references to the new layout:
- Dockerfile: COPY backend/go.mod, COPY backend/ (context = repo root)
- docker-compose files: context ../.., dockerfile deploy/docker/Dockerfile,
  env_file ../../.env, volume mounts ../../backend:/app
- deploy/quickstart/compose.yaml: dockerfile deploy/docker/Dockerfile
- CI: working-directory: backend for go commands, file deploy/docker/Dockerfile,
  coverage path backend/coverage.out, health_check backend/scripts/
- backend/Makefile: docker target uses -f ../deploy/docker/Dockerfile ../
- README: architecture tree, quickstart, config paths updated

Move root stray scripts (rename_models.*, run_m11_tests.sh, verify_build.sh,
gorm_bool_main.go) to backend/scripts/legacy/. All moves via git mv to
preserve history. Build, vet, SQLite tests, and docker compose config verified.
2026-07-07 14:44:12 +08:00

76 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
}
q = applyOffsetLimit(q, offset, limit)
err := q.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
}
q = applyOffsetLimit(q, offset, limit)
err := q.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
}