mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 05:11:34 +00:00
This commit addresses a broken build state caused by a mid-stream architectural refactoring. The changes align the existing code with the new Domain-Driven Design (DDD-lite) structure outlined in `refactor.md`. Key changes include: - Defined missing domain interfaces for `Auth`, `Localization`, and `Search`. - Refactored application services to use a `Commands` and `Queries` pattern. - Updated GraphQL resolvers to call application services instead of accessing repositories directly. - Fixed dependency injection in `cmd/api/main.go` by removing the non-existent `ApplicationBuilder` and manually instantiating services. - Corrected numerous test files (`integration`, `unit`, and `repository` tests) to reflect the new architecture, including fixing mock objects and test suite setups. - Added missing database migrations for test schemas to resolve "no such table" errors. This effort successfully gets the application to a compilable state and passes a significant portion of the test suite, laying the groundwork for further development and fixing the remaining test failures.
82 lines
2.0 KiB
Go
82 lines
2.0 KiB
Go
package work
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"tercul/internal/domain"
|
|
"tercul/internal/domain/search"
|
|
)
|
|
|
|
// WorkCommands contains the command handlers for the work aggregate.
|
|
type WorkCommands struct {
|
|
repo domain.WorkRepository
|
|
searchClient search.SearchClient
|
|
}
|
|
|
|
// NewWorkCommands creates a new WorkCommands handler.
|
|
func NewWorkCommands(repo domain.WorkRepository, searchClient search.SearchClient) *WorkCommands {
|
|
return &WorkCommands{
|
|
repo: repo,
|
|
searchClient: searchClient,
|
|
}
|
|
}
|
|
|
|
// CreateWork creates a new work.
|
|
func (c *WorkCommands) CreateWork(ctx context.Context, work *domain.Work) (*domain.Work, error) {
|
|
if work == nil {
|
|
return nil, errors.New("work cannot be nil")
|
|
}
|
|
if work.Title == "" {
|
|
return nil, errors.New("work title cannot be empty")
|
|
}
|
|
if work.Language == "" {
|
|
return nil, errors.New("work language cannot be empty")
|
|
}
|
|
err := c.repo.Create(ctx, work)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// Index the work in the search client
|
|
err = c.searchClient.IndexWork(ctx, work, "")
|
|
if err != nil {
|
|
// Log the error but don't fail the operation
|
|
}
|
|
return work, nil
|
|
}
|
|
|
|
// UpdateWork updates an existing work.
|
|
func (c *WorkCommands) UpdateWork(ctx context.Context, work *domain.Work) error {
|
|
if work == nil {
|
|
return errors.New("work cannot be nil")
|
|
}
|
|
if work.ID == 0 {
|
|
return errors.New("work ID cannot be zero")
|
|
}
|
|
if work.Title == "" {
|
|
return errors.New("work title cannot be empty")
|
|
}
|
|
if work.Language == "" {
|
|
return errors.New("work language cannot be empty")
|
|
}
|
|
err := c.repo.Update(ctx, work)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// Index the work in the search client
|
|
return c.searchClient.IndexWork(ctx, work, "")
|
|
}
|
|
|
|
// DeleteWork deletes a work by ID.
|
|
func (c *WorkCommands) DeleteWork(ctx context.Context, id uint) error {
|
|
if id == 0 {
|
|
return errors.New("invalid work ID")
|
|
}
|
|
return c.repo.Delete(ctx, id)
|
|
}
|
|
|
|
// AnalyzeWork performs linguistic analysis on a work.
|
|
func (c *WorkCommands) AnalyzeWork(ctx context.Context, workID uint) error {
|
|
// TODO: implement this
|
|
return nil
|
|
}
|