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.
85 lines
3.2 KiB
Go
85 lines
3.2 KiB
Go
package sql
|
|
|
|
import (
|
|
"context"
|
|
"tercul/internal/domain"
|
|
|
|
"go.opentelemetry.io/otel"
|
|
"go.opentelemetry.io/otel/trace"
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/clause"
|
|
)
|
|
|
|
type translationRepository struct {
|
|
domain.BaseRepository[domain.Translation]
|
|
db *gorm.DB
|
|
tracer trace.Tracer
|
|
}
|
|
|
|
// NewTranslationRepository creates a new TranslationRepository.
|
|
func NewTranslationRepository(db *gorm.DB) domain.TranslationRepository {
|
|
return &translationRepository{
|
|
BaseRepository: NewBaseRepositoryImpl[domain.Translation](db),
|
|
db: db,
|
|
tracer: otel.Tracer("translation.repository"),
|
|
}
|
|
}
|
|
|
|
// ListByWorkID finds translations by work ID
|
|
func (r *translationRepository) ListByWorkID(ctx context.Context, workID uint) ([]domain.Translation, error) {
|
|
ctx, span := r.tracer.Start(ctx, "ListByWorkID")
|
|
defer span.End()
|
|
var translations []domain.Translation
|
|
if err := r.db.WithContext(ctx).Where("translatable_id = ? AND translatable_type = ?", workID, "works").Find(&translations).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return translations, nil
|
|
}
|
|
|
|
// Upsert creates a new translation or updates an existing one based on the unique
|
|
// composite key of (translatable_id, translatable_type, language).
|
|
func (r *translationRepository) Upsert(ctx context.Context, translation *domain.Translation) error {
|
|
ctx, span := r.tracer.Start(ctx, "Upsert")
|
|
defer span.End()
|
|
|
|
// The unique key for a translation is (TranslatableID, TranslatableType, Language).
|
|
// If a translation with this combination exists, we update it. Otherwise, we create it.
|
|
return r.db.WithContext(ctx).Clauses(clause.OnConflict{
|
|
Columns: []clause.Column{{Name: "translatable_id"}, {Name: "translatable_type"}, {Name: "language"}},
|
|
DoUpdates: clause.AssignmentColumns([]string{"title", "content", "description", "status", "translator_id"}),
|
|
}).Create(translation).Error
|
|
}
|
|
|
|
// ListByEntity finds translations by entity type and ID
|
|
func (r *translationRepository) ListByEntity(ctx context.Context, entityType string, entityID uint) ([]domain.Translation, error) {
|
|
ctx, span := r.tracer.Start(ctx, "ListByEntity")
|
|
defer span.End()
|
|
var translations []domain.Translation
|
|
if err := r.db.WithContext(ctx).Where("translatable_id = ? AND translatable_type = ?", entityID, entityType).Find(&translations).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return translations, nil
|
|
}
|
|
|
|
// ListByTranslatorID finds translations by translator ID
|
|
func (r *translationRepository) ListByTranslatorID(ctx context.Context, translatorID uint) ([]domain.Translation, error) {
|
|
ctx, span := r.tracer.Start(ctx, "ListByTranslatorID")
|
|
defer span.End()
|
|
var translations []domain.Translation
|
|
if err := r.db.WithContext(ctx).Where("translator_id = ?", translatorID).Find(&translations).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return translations, nil
|
|
}
|
|
|
|
// ListByStatus finds translations by status
|
|
func (r *translationRepository) ListByStatus(ctx context.Context, status domain.TranslationStatus) ([]domain.Translation, error) {
|
|
ctx, span := r.tracer.Start(ctx, "ListByStatus")
|
|
defer span.End()
|
|
var translations []domain.Translation
|
|
if err := r.db.WithContext(ctx).Where("status = ?", status).Find(&translations).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return translations, nil
|
|
}
|