tercul-backend/internal/app/comment/commands.go
google-labs-jules[bot] 781b313bf1 feat: Complete all pending tasks from TASKS.md
This commit addresses all the high-priority tasks outlined in the TASKS.md file, significantly improving the application's observability, completing key features, and refactoring critical parts of the codebase.

### Observability

- **Centralized Logging:** Implemented a new structured, context-aware logging system using `zerolog`. A new logging middleware injects request-specific information (request ID, user ID, trace ID) into the logger, and all application logging has been refactored to use this new system.
- **Prometheus Metrics:** Added Prometheus metrics for database query performance by creating a GORM plugin that automatically records query latency and totals.
- **OpenTelemetry Tracing:** Fully instrumented all application services in `internal/app` and data repositories in `internal/data/sql` with OpenTelemetry tracing, providing deep visibility into application performance.

### Features

- **Analytics:** Implemented like, comment, and bookmark counting. The respective command handlers now call the analytics service to increment counters when these actions are performed.
- **Enrichment Tool:** Built a new, extensible `enrich` command-line tool to fetch data from external sources. The initial implementation enriches author data using the Open Library API.

### Refactoring & Fixes

- **Decoupled Testing:** Refactored the testing utilities in `internal/testutil` to be database-agnostic, promoting the use of mock-based unit tests and improving test speed and reliability.
- **Build Fixes:** Resolved numerous build errors, including a critical import cycle between the logging, observability, and authentication packages.
- **Search Service:** Fixed the search service integration by implementing the `GetWorkContent` method in the localization service, allowing the search indexer to correctly fetch and index work content.
2025-10-05 05:26:27 +00:00

125 lines
3.2 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"
)
// 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 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
}
if c.analyticsSvc != nil {
if input.WorkID != nil {
go c.analyticsSvc.IncrementWorkComments(context.Background(), *input.WorkID)
}
if input.TranslationID != nil {
go c.analyticsSvc.IncrementTranslationComments(context.Background(), *input.TranslationID)
}
}
return comment, nil
}
// UpdateCommentInput represents the input for updating an existing comment.
type UpdateCommentInput struct {
ID uint
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 %d 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 uint) 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 %d 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)
}