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.
94 lines
2.7 KiB
Go
94 lines
2.7 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"
|
|
)
|
|
|
|
// BannerService implements business logic for Banner operations.
|
|
// Reference: Chatwoot Platform::Api::V1::BannersController — super-admin CRUD,
|
|
// and api/v1/accounts/:account_id/banners — read-only list of active banners.
|
|
type BannerService struct {
|
|
repo *repository.BannerRepo
|
|
}
|
|
|
|
// NewBannerService creates a new BannerService.
|
|
func NewBannerService(repo *repository.BannerRepo) *BannerService {
|
|
return &BannerService{repo: repo}
|
|
}
|
|
|
|
// List retrieves all banners with pagination (platform admin).
|
|
func (s *BannerService) List(ctx context.Context, offset, limit int) ([]model.Banner, int64, error) {
|
|
banners, total, err := s.repo.List(ctx, offset, limit)
|
|
if err != nil {
|
|
applogger.L().Errorf("BannerService.List error: %v", err)
|
|
return nil, 0, err
|
|
}
|
|
return banners, total, nil
|
|
}
|
|
|
|
// Get retrieves a single banner by ID.
|
|
func (s *BannerService) Get(ctx context.Context, id uint) (*model.Banner, error) {
|
|
banner, err := s.repo.FindByID(ctx, id)
|
|
if err != nil {
|
|
applogger.L().Errorf("BannerService.Get error: %v", err)
|
|
return nil, err
|
|
}
|
|
return banner, nil
|
|
}
|
|
|
|
// Create creates a new banner.
|
|
func (s *BannerService) Create(ctx context.Context, banner *model.Banner) error {
|
|
if banner.Title == "" {
|
|
return errors.New("title is required")
|
|
}
|
|
if banner.Content == "" {
|
|
return errors.New("content is required")
|
|
}
|
|
if banner.BannerType == "" {
|
|
return errors.New("banner_type is required")
|
|
}
|
|
if err := s.repo.Create(ctx, banner); err != nil {
|
|
applogger.L().Errorf("BannerService.Create error: %v", err)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Update patches an existing banner.
|
|
func (s *BannerService) Update(ctx context.Context, id uint, updates map[string]interface{}) error {
|
|
if id == 0 {
|
|
return errors.New("invalid banner ID")
|
|
}
|
|
if err := s.repo.Update(ctx, id, updates); err != nil {
|
|
applogger.L().Errorf("BannerService.Update error: %v", err)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Delete soft-deletes a banner by ID.
|
|
func (s *BannerService) Delete(ctx context.Context, id uint) error {
|
|
if id == 0 {
|
|
return errors.New("invalid banner ID")
|
|
}
|
|
if err := s.repo.Delete(ctx, id); err != nil {
|
|
applogger.L().Errorf("BannerService.Delete error: %v", err)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ListActive returns all active banners (for account-scoped read-only endpoint).
|
|
func (s *BannerService) ListActive(ctx context.Context) ([]model.Banner, error) {
|
|
banners, err := s.repo.FindActive(ctx)
|
|
if err != nil {
|
|
applogger.L().Errorf("BannerService.ListActive error: %v", err)
|
|
return nil, err
|
|
}
|
|
return banners, nil
|
|
} |