mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 05:11:34 +00:00
This commit addresses several high-priority tasks from the TASKS.md file, including: - **Fix Background Job Panic:** Replaced `log.Fatalf` with `log.Printf` in the `asynq` server to prevent crashes. - **Refactor API Server Setup:** Consolidated the GraphQL Playground and Prometheus metrics endpoints into the main API server. - **Implement `DeleteUser` Mutation:** Implemented the `DeleteUser` resolver. - **Implement `CreateContribution` Mutation:** Implemented the `CreateContribution` resolver and its required application service. Additionally, this commit includes a major refactoring of the configuration management system to fix a broken build. The global `config.Cfg` variable has been removed and replaced with a dependency injection approach, where the configuration object is passed to all components that require it. This change has been applied across the entire codebase, including the test suite, to ensure a stable and testable application.
86 lines
3.2 KiB
Go
86 lines
3.2 KiB
Go
package sql
|
|
|
|
import (
|
|
"context"
|
|
"tercul/internal/domain"
|
|
"tercul/internal/platform/config"
|
|
|
|
"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, cfg *config.Config) domain.TranslationRepository {
|
|
return &translationRepository{
|
|
BaseRepository: NewBaseRepositoryImpl[domain.Translation](db, cfg),
|
|
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
|
|
}
|