tercul-backend/internal/app/comment/commands.go
Damir Mukimov d50722dad5
Some checks failed
Test / Integration Tests (push) Successful in 4s
Build / Build Binary (push) Failing after 2m9s
Docker Build / Build Docker Image (push) Failing after 2m32s
Test / Unit Tests (push) Failing after 3m12s
Lint / Go Lint (push) Failing after 1m0s
Refactor ID handling to use UUIDs across the application
- Updated database models and repositories to replace uint IDs with UUIDs.
- Modified test fixtures to generate and use UUIDs for authors, translations, users, and works.
- Adjusted mock implementations to align with the new UUID structure.
- Ensured all relevant functions and methods are updated to handle UUIDs correctly.
- Added necessary imports for UUID handling in various files.
2025-12-27 00:33:34 +01:00

137 lines
3.5 KiB
Go

package comment
import (
"context"
"errors"
"fmt"
"tercul/internal/app/analytics"
"tercul/internal/app/authz"
"tercul/internal/domain"
platform_auth "tercul/internal/platform/auth"
"tercul/internal/platform/log"
"github.com/google/uuid"
)
// CommentCommands contains the command handlers for the comment aggregate.
type CommentCommands struct {
repo domain.CommentRepository
authzSvc *authz.Service
analyticsSvc analytics.Service
}
// NewCommentCommands creates a new CommentCommands handler.
func NewCommentCommands(repo domain.CommentRepository, authzSvc *authz.Service, analyticsSvc analytics.Service) *CommentCommands {
return &CommentCommands{
repo: repo,
authzSvc: authzSvc,
analyticsSvc: analyticsSvc,
}
}
// CreateCommentInput represents the input for creating a new comment.
type CreateCommentInput struct {
Text string
UserID uuid.UUID
WorkID *uuid.UUID
TranslationID *uuid.UUID
ParentID *uuid.UUID
}
// 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
}
if c.analyticsSvc != nil {
if input.WorkID != nil {
go func() {
if err := c.analyticsSvc.IncrementWorkComments(context.Background(), *input.WorkID); err != nil {
log.Error(err, "failed to increment work comments")
}
}()
}
if input.TranslationID != nil {
go func() {
if err := c.analyticsSvc.IncrementTranslationComments(context.Background(), *input.TranslationID); err != nil {
log.Error(err, "failed to increment translation comments")
}
}()
}
}
return comment, nil
}
// UpdateCommentInput represents the input for updating an existing comment.
type UpdateCommentInput struct {
ID uuid.UUID
Text string
}
// UpdateComment updates an existing comment after an authorization check.
func (c *CommentCommands) UpdateComment(ctx context.Context, input UpdateCommentInput) (*domain.Comment, error) {
userID, ok := platform_auth.GetUserIDFromContext(ctx)
if !ok {
return nil, domain.ErrUnauthorized
}
comment, err := c.repo.GetByID(ctx, input.ID)
if err != nil {
if errors.Is(err, domain.ErrEntityNotFound) {
return nil, fmt.Errorf("%w: comment with id %s not found", domain.ErrEntityNotFound, input.ID)
}
return nil, err
}
can, err := c.authzSvc.CanDeleteComment(ctx, userID, comment) // Using CanDeleteComment for editing as well
if err != nil {
return nil, err
}
if !can {
return nil, domain.ErrForbidden
}
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 after an authorization check.
func (c *CommentCommands) DeleteComment(ctx context.Context, id uuid.UUID) error {
userID, ok := platform_auth.GetUserIDFromContext(ctx)
if !ok {
return domain.ErrUnauthorized
}
comment, err := c.repo.GetByID(ctx, id)
if err != nil {
if errors.Is(err, domain.ErrEntityNotFound) {
return fmt.Errorf("%w: comment with id %s not found", domain.ErrEntityNotFound, id)
}
return err
}
can, err := c.authzSvc.CanDeleteComment(ctx, userID, comment)
if err != nil {
return err
}
if !can {
return domain.ErrForbidden
}
return c.repo.Delete(ctx, id)
}