Files
gochat/backend/internal/service/installation_config_service.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

126 lines
4.0 KiB
Go

package service
import (
"context"
"errors"
"github.com/gochat/gochat/internal/model"
"github.com/gochat/gochat/internal/repository"
applogger "github.com/gochat/gochat/pkg/logger"
pkgvalidator "github.com/gochat/gochat/pkg/validator"
)
// InstallationConfigService implements business logic for InstallationConfig CRUD.
// Reference: Chatwoot app/controllers/api/v1/platform/installation_configs_controller.rb
type InstallationConfigService struct {
repo *repository.InstallationConfigRepo
}
// NewInstallationConfigService creates a new InstallationConfig service.
func NewInstallationConfigService(repo *repository.InstallationConfigRepo) *InstallationConfigService {
return &InstallationConfigService{repo: repo}
}
// --- DTOs for InstallationConfig CRUD ---
// CreateInstallationConfigRequest is the DTO for creating an installation config.
type CreateInstallationConfigRequest struct {
Name string `json:"name" validate:"required,min=1,max=100"`
Value string `json:"value" validate:"required"`
}
// UpdateInstallationConfigRequest is the DTO for updating an installation config.
type UpdateInstallationConfigRequest struct {
Name string `json:"name,omitempty" validate:"omitempty,min=1,max=100"`
Value string `json:"value,omitempty" validate:"omitempty,min=1"`
}
// --- Service methods ---
// Get retrieves an InstallationConfig by ID.
func (s *InstallationConfigService) Get(ctx context.Context, id uint) (*model.InstallationConfig, error) {
cfg, err := s.repo.FindByID(ctx, id)
if err != nil {
applogger.L().Errorf("InstallationConfigService.Get error: %v", err)
return nil, err
}
return cfg, nil
}
// GetByName retrieves an InstallationConfig by name.
func (s *InstallationConfigService) GetByName(ctx context.Context, name string) (*model.InstallationConfig, error) {
cfg, err := s.repo.FindByName(ctx, name)
if err != nil {
applogger.L().Errorf("InstallationConfigService.GetByName error: %v", err)
return nil, err
}
return cfg, nil
}
// List retrieves all installation configs with pagination.
func (s *InstallationConfigService) List(ctx context.Context, offset, limit int) ([]model.InstallationConfig, int64, error) {
configs, total, err := s.repo.List(ctx, offset, limit)
if err != nil {
applogger.L().Errorf("InstallationConfigService.List error: %v", err)
return nil, 0, err
}
return configs, total, nil
}
// Create creates a new installation config.
func (s *InstallationConfigService) Create(ctx context.Context, req *CreateInstallationConfigRequest) (*model.InstallationConfig, error) {
if err := pkgvalidator.ValidateStruct(req); err != nil {
return nil, errors.New("validation error: " + err.Error())
}
// Check for duplicate name
existing, err := s.repo.FindByName(ctx, req.Name)
if err == nil && existing != nil {
return nil, errors.New("installation config with this name already exists")
}
cfg := &model.InstallationConfig{
Name: req.Name,
Value: req.Value,
}
if err := s.repo.Create(ctx, cfg); err != nil {
applogger.L().Errorf("InstallationConfigService.Create error: %v", err)
return nil, err
}
return cfg, nil
}
// Update modifies an existing installation config.
func (s *InstallationConfigService) Update(ctx context.Context, id uint, req *UpdateInstallationConfigRequest) (*model.InstallationConfig, error) {
if err := pkgvalidator.ValidateStruct(req); err != nil {
return nil, errors.New("validation error: " + err.Error())
}
cfg, err := s.repo.FindByID(ctx, id)
if err != nil {
return nil, err
}
if req.Name != "" {
cfg.Name = req.Name
}
if req.Value != "" {
cfg.Value = req.Value
}
if err := s.repo.Update(ctx, cfg); err != nil {
applogger.L().Errorf("InstallationConfigService.Update error: %v", err)
return nil, err
}
return cfg, nil
}
// Delete removes an installation config by ID.
func (s *InstallationConfigService) Delete(ctx context.Context, id uint) error {
if err := s.repo.Delete(ctx, id); err != nil {
applogger.L().Errorf("InstallationConfigService.Delete error: %v", err)
return err
}
return nil
}