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

107 lines
3.1 KiB
Plaintext

package service
import (
"context"
"github.com/gochat/gochat/internal/app"
"github.com/gochat/gochat/internal/model"
"github.com/gochat/gochat/internal/repository"
"gorm.io/gorm"
)
// AccountService provides business logic for account operations.
type AccountService struct {
app *app.App
}
func NewAccountService(application *app.App) *AccountService {
return &AccountService{app: application}
}
func (s *AccountService) GetByID(ctx context.Context, id uint) (*model.Account, error) {
var account model.Account
if err := s.app.DB().WithContext(ctx).First(&account, id).Error; err != nil {
return nil, err
}
return &account, nil
}
func (s *AccountService) Create(ctx context.Context, account *model.Account) error {
return s.app.DB().WithContext(ctx).Create(account).Error
}
func (s *AccountService) List(ctx context.Context, offset, limit int) ([]model.Account, int64, error) {
var accounts []model.Account
var count int64
db := s.app.DB().WithContext(ctx).Model(&model.Account{})
db.Count(&count)
if err := db.Offset(offset).Limit(limit).Find(&accounts).Error; err != nil {
return nil, 0, err
}
return accounts, count, nil
}
// ConversationService provides business logic for conversation operations.
type ConversationService struct {
app *app.App
}
func NewConversationService(application *app.App) *ConversationService {
return &ConversationService{app: application}
}
func (s *ConversationService) GetByID(ctx context.Context, id uint) (*model.Conversation, error) {
var conv model.Conversation
if err := s.app.DB().WithContext(ctx).First(&conv, id).Error; err != nil {
return nil, err
}
return &conv, nil
}
func (s *ConversationService) Create(ctx context.Context, conv *model.Conversation) error {
return s.app.DB().WithContext(ctx).Create(conv).Error
}
func (s *ConversationService) ListByAccount(ctx context.Context, accountID uint, offset, limit int) ([]model.Conversation, int64, error) {
var convs []model.Conversation
var count int64
db := s.app.DB().WithContext(ctx).Model(&model.Conversation{}).Where("account_id = ?", accountID)
db.Count(&count)
if err := db.Offset(offset).Limit(limit).Find(&convs).Error; err != nil {
return nil, 0, err
}
return convs, count, nil
}
// GenericRepository is a convenience wrapper around BaseRepository with typed models.
type GenericRepository[T any] struct {
db *gorm.DB
}
func NewGenericRepository[T any](db *gorm.DB) *GenericRepository[T] {
return &GenericRepository[T]{db: db}
}
func (r *GenericRepository[T]) Create(ctx context.Context, entity *T) error {
return r.db.WithContext(ctx).Create(entity).Error
}
func (r *GenericRepository[T]) GetByID(ctx context.Context, id uint) (*T, error) {
var entity T
if err := r.db.WithContext(ctx).First(&entity, id).Error; err != nil {
return nil, err
}
return &entity, nil
}
func (r *GenericRepository[T]) List(ctx context.Context, offset, limit int) ([]T, error) {
var entities []T
if err := r.db.WithContext(ctx).Offset(offset).Limit(limit).Find(&entities).Error; err != nil {
return nil, err
}
return entities, nil
}
func (r *GenericRepository[T]) DB() *gorm.DB {
return r.db
}