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.
140 lines
3.3 KiB
Go
140 lines
3.3 KiB
Go
package comment
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"tercul/internal/domain"
|
|
)
|
|
|
|
// CommentCommands contains the command handlers for the comment aggregate.
|
|
type CommentCommands struct {
|
|
repo domain.CommentRepository
|
|
analyticsService AnalyticsService
|
|
}
|
|
|
|
// AnalyticsService defines the interface for analytics operations.
|
|
type AnalyticsService interface {
|
|
IncrementWorkComments(ctx context.Context, workID uint) error
|
|
IncrementTranslationComments(ctx context.Context, translationID uint) error
|
|
}
|
|
|
|
// NewCommentCommands creates a new CommentCommands handler.
|
|
func NewCommentCommands(repo domain.CommentRepository, analyticsService AnalyticsService) *CommentCommands {
|
|
return &CommentCommands{
|
|
repo: repo,
|
|
analyticsService: analyticsService,
|
|
}
|
|
}
|
|
|
|
// CreateCommentInput represents the input for creating a new comment.
|
|
type CreateCommentInput struct {
|
|
Text string
|
|
UserID uint
|
|
WorkID *uint
|
|
TranslationID *uint
|
|
ParentID *uint
|
|
}
|
|
|
|
// CreateComment creates a new comment.
|
|
func (c *CommentCommands) CreateComment(ctx context.Context, input CreateCommentInput) (*domain.Comment, error) {
|
|
if input.Text == "" {
|
|
return nil, errors.New("comment text cannot be empty")
|
|
}
|
|
if input.UserID == 0 {
|
|
return nil, errors.New("user ID cannot be zero")
|
|
}
|
|
|
|
comment := &domain.Comment{
|
|
Text: input.Text,
|
|
UserID: input.UserID,
|
|
WorkID: input.WorkID,
|
|
TranslationID: input.TranslationID,
|
|
ParentID: input.ParentID,
|
|
}
|
|
|
|
err := c.repo.Create(ctx, comment)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Increment analytics
|
|
if comment.WorkID != nil {
|
|
c.analyticsService.IncrementWorkComments(ctx, *comment.WorkID)
|
|
}
|
|
if comment.TranslationID != nil {
|
|
c.analyticsService.IncrementTranslationComments(ctx, *comment.TranslationID)
|
|
}
|
|
|
|
return comment, nil
|
|
}
|
|
|
|
// UpdateCommentInput represents the input for updating an existing comment.
|
|
type UpdateCommentInput struct {
|
|
ID uint
|
|
Text string
|
|
UserID uint // for authorization
|
|
}
|
|
|
|
// UpdateComment updates an existing comment.
|
|
func (c *CommentCommands) UpdateComment(ctx context.Context, input UpdateCommentInput) (*domain.Comment, error) {
|
|
if input.ID == 0 {
|
|
return nil, errors.New("comment ID cannot be zero")
|
|
}
|
|
if input.Text == "" {
|
|
return nil, errors.New("comment text cannot be empty")
|
|
}
|
|
|
|
// Fetch the existing comment
|
|
comment, err := c.repo.GetByID(ctx, input.ID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if comment == nil {
|
|
return nil, errors.New("comment not found")
|
|
}
|
|
|
|
// Check ownership
|
|
if comment.UserID != input.UserID {
|
|
return nil, errors.New("unauthorized")
|
|
}
|
|
|
|
// Update fields
|
|
comment.Text = input.Text
|
|
|
|
err = c.repo.Update(ctx, comment)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return comment, nil
|
|
}
|
|
|
|
// DeleteCommentInput represents the input for deleting a comment.
|
|
type DeleteCommentInput struct {
|
|
ID uint
|
|
UserID uint // for authorization
|
|
}
|
|
|
|
// DeleteComment deletes a comment by ID.
|
|
func (c *CommentCommands) DeleteComment(ctx context.Context, input DeleteCommentInput) error {
|
|
if input.ID == 0 {
|
|
return errors.New("invalid comment ID")
|
|
}
|
|
|
|
// Fetch the existing comment
|
|
comment, err := c.repo.GetByID(ctx, input.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if comment == nil {
|
|
return errors.New("comment not found")
|
|
}
|
|
|
|
// Check ownership
|
|
if comment.UserID != input.UserID {
|
|
return errors.New("unauthorized")
|
|
}
|
|
|
|
return c.repo.Delete(ctx, input.ID)
|
|
}
|