mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 04:01:34 +00:00
This change introduces a service layer to encapsulate the business logic for each domain aggregate. This will make the code more modular, testable, and easier to maintain. The following services have been created: - author - bookmark - category - collection - comment - like - tag - translation - user The main Application struct has been updated to use these new services. The integration test suite has also been updated to use the new Application struct and services. This is a work in progress. The next step is to fix the compilation errors and then refactor the resolvers to use the new services.
67 lines
1.7 KiB
Go
67 lines
1.7 KiB
Go
package comment
|
|
|
|
import (
|
|
"context"
|
|
"tercul/internal/domain"
|
|
)
|
|
|
|
// CommentCommands contains the command handlers for the comment aggregate.
|
|
type CommentCommands struct {
|
|
repo domain.CommentRepository
|
|
}
|
|
|
|
// NewCommentCommands creates a new CommentCommands handler.
|
|
func NewCommentCommands(repo domain.CommentRepository) *CommentCommands {
|
|
return &CommentCommands{repo: repo}
|
|
}
|
|
|
|
// 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) {
|
|
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
|
|
}
|
|
return comment, nil
|
|
}
|
|
|
|
// UpdateCommentInput represents the input for updating an existing comment.
|
|
type UpdateCommentInput struct {
|
|
ID uint
|
|
Text string
|
|
}
|
|
|
|
// UpdateComment updates an existing comment.
|
|
func (c *CommentCommands) UpdateComment(ctx context.Context, input UpdateCommentInput) (*domain.Comment, error) {
|
|
comment, err := c.repo.GetByID(ctx, input.ID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
comment.Text = input.Text
|
|
err = c.repo.Update(ctx, comment)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return comment, nil
|
|
}
|
|
|
|
// DeleteComment deletes a comment by ID.
|
|
func (c *CommentCommands) DeleteComment(ctx context.Context, id uint) error {
|
|
return c.repo.Delete(ctx, id)
|
|
}
|