mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 04:01:34 +00:00
This commit introduces a new application layer to the codebase, which decouples the GraphQL resolvers from the data layer. The resolvers now call application services, which in turn call the repositories. This change improves the separation of concerns and makes the code more testable and maintainable. Additionally, this commit introduces dataloaders to solve the N+1 problem in the GraphQL resolvers. The dataloaders are used to batch and cache database queries, which significantly improves the performance of the API. The following changes were made: - Created application services for most of the domains. - Refactored the GraphQL resolvers to use the new application services. - Implemented dataloaders for the `Author` aggregate. - Updated the `app.Application` struct to hold the application services instead of the repositories. - Fixed a large number of compilation errors in the test files that arose from these changes. There are still some compilation errors in the `internal/adapters/graphql/integration_test.go` file. These errors are due to the test files still trying to access the repositories directly from the `app.Application` struct. The remaining work is to update these tests to use the new application services.
98 lines
2.2 KiB
Go
98 lines
2.2 KiB
Go
package author
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"tercul/internal/domain"
|
|
)
|
|
|
|
// AuthorCommands contains the command handlers for the author aggregate.
|
|
type AuthorCommands struct {
|
|
repo domain.AuthorRepository
|
|
}
|
|
|
|
// NewAuthorCommands creates a new AuthorCommands handler.
|
|
func NewAuthorCommands(repo domain.AuthorRepository) *AuthorCommands {
|
|
return &AuthorCommands{
|
|
repo: repo,
|
|
}
|
|
}
|
|
|
|
// CreateAuthorInput represents the input for creating a new author.
|
|
type CreateAuthorInput struct {
|
|
Name string
|
|
Language string
|
|
}
|
|
|
|
// CreateAuthor creates a new author.
|
|
func (c *AuthorCommands) CreateAuthor(ctx context.Context, input CreateAuthorInput) (*domain.Author, error) {
|
|
if input.Name == "" {
|
|
return nil, errors.New("author name cannot be empty")
|
|
}
|
|
if input.Language == "" {
|
|
return nil, errors.New("author language cannot be empty")
|
|
}
|
|
|
|
author := &domain.Author{
|
|
Name: input.Name,
|
|
TranslatableModel: domain.TranslatableModel{
|
|
Language: input.Language,
|
|
},
|
|
}
|
|
|
|
err := c.repo.Create(ctx, author)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return author, nil
|
|
}
|
|
|
|
// UpdateAuthorInput represents the input for updating an existing author.
|
|
type UpdateAuthorInput struct {
|
|
ID uint
|
|
Name string
|
|
Language string
|
|
}
|
|
|
|
// UpdateAuthor updates an existing author.
|
|
func (c *AuthorCommands) UpdateAuthor(ctx context.Context, input UpdateAuthorInput) (*domain.Author, error) {
|
|
if input.ID == 0 {
|
|
return nil, errors.New("author ID cannot be zero")
|
|
}
|
|
if input.Name == "" {
|
|
return nil, errors.New("author name cannot be empty")
|
|
}
|
|
if input.Language == "" {
|
|
return nil, errors.New("author language cannot be empty")
|
|
}
|
|
|
|
// Fetch the existing author
|
|
author, err := c.repo.GetByID(ctx, input.ID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if author == nil {
|
|
return nil, errors.New("author not found")
|
|
}
|
|
|
|
// Update fields
|
|
author.Name = input.Name
|
|
author.Language = input.Language
|
|
|
|
err = c.repo.Update(ctx, author)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return author, nil
|
|
}
|
|
|
|
// DeleteAuthor deletes an author by ID.
|
|
func (c *AuthorCommands) DeleteAuthor(ctx context.Context, id uint) error {
|
|
if id == 0 {
|
|
return errors.New("invalid author ID")
|
|
}
|
|
return c.repo.Delete(ctx, id)
|
|
}
|