mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 05:11:34 +00:00
This commit introduces a significant refactoring to improve the application's quality, test coverage, and production readiness, focusing on core localization and business logic features. Key changes include: - Consolidated the `CreateTranslation` and `UpdateTranslation` commands into a single, more robust `CreateOrUpdateTranslation` command. This uses a database-level `Upsert` for atomicity. - Centralized authorization for translatable entities into a new `CanEditEntity` check within the application service layer. - Fixed a critical bug in the `MergeWork` command that caused a UNIQUE constraint violation when merging works with conflicting translations. The logic now intelligently handles language conflicts. - Implemented decrementing for "like" counts in the analytics service when a like is deleted, ensuring accurate statistics. - Stabilized the test suite by switching to a file-based database for integration tests, fixing test data isolation issues, and adding a unique index to the `Translation` model to enforce data integrity. - Refactored manual mocks to use the `testify/mock` library for better consistency and maintainability.
93 lines
2.5 KiB
Go
93 lines
2.5 KiB
Go
package like
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"tercul/internal/app/analytics"
|
|
"tercul/internal/domain"
|
|
)
|
|
|
|
// LikeCommands contains the command handlers for the like aggregate.
|
|
type LikeCommands struct {
|
|
repo domain.LikeRepository
|
|
analyticsSvc analytics.Service
|
|
}
|
|
|
|
// NewLikeCommands creates a new LikeCommands handler.
|
|
func NewLikeCommands(repo domain.LikeRepository, analyticsSvc analytics.Service) *LikeCommands {
|
|
return &LikeCommands{
|
|
repo: repo,
|
|
analyticsSvc: analyticsSvc,
|
|
}
|
|
}
|
|
|
|
// CreateLikeInput represents the input for creating a new like.
|
|
type CreateLikeInput struct {
|
|
UserID uint
|
|
WorkID *uint
|
|
TranslationID *uint
|
|
CommentID *uint
|
|
}
|
|
|
|
// CreateLike creates a new like and increments the relevant counter.
|
|
func (c *LikeCommands) CreateLike(ctx context.Context, input CreateLikeInput) (*domain.Like, error) {
|
|
like := &domain.Like{
|
|
UserID: input.UserID,
|
|
WorkID: input.WorkID,
|
|
TranslationID: input.TranslationID,
|
|
CommentID: input.CommentID,
|
|
}
|
|
err := c.repo.Create(ctx, like)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// After creating the like, increment the appropriate counter.
|
|
if c.analyticsSvc != nil {
|
|
if input.WorkID != nil {
|
|
go c.analyticsSvc.IncrementWorkLikes(context.Background(), *input.WorkID)
|
|
}
|
|
if input.TranslationID != nil {
|
|
go c.analyticsSvc.IncrementTranslationLikes(context.Background(), *input.TranslationID)
|
|
}
|
|
// Assuming there's a counter for comment likes, which is a reasonable feature to add.
|
|
// if input.CommentID != nil {
|
|
// go c.analyticsSvc.IncrementCommentLikes(context.Background(), *input.CommentID)
|
|
// }
|
|
}
|
|
|
|
return like, nil
|
|
}
|
|
|
|
// DeleteLike deletes a like by ID and decrements the relevant counter.
|
|
func (c *LikeCommands) DeleteLike(ctx context.Context, id uint) error {
|
|
// First, get the like to determine what it was attached to.
|
|
like, err := c.repo.GetByID(ctx, id)
|
|
if err != nil {
|
|
// If the like doesn't exist, we can't decrement anything, but we shouldn't fail.
|
|
// The end result (the like is gone) is the same.
|
|
if errors.Is(err, domain.ErrEntityNotFound) {
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
|
|
// Now, delete the like.
|
|
err = c.repo.Delete(ctx, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// After deleting the like, decrement the appropriate counter in the background.
|
|
if c.analyticsSvc != nil {
|
|
if like.WorkID != nil {
|
|
go c.analyticsSvc.DecrementWorkLikes(context.Background(), *like.WorkID)
|
|
}
|
|
if like.TranslationID != nil {
|
|
go c.analyticsSvc.DecrementTranslationLikes(context.Background(), *like.TranslationID)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|