mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 02:51: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.
119 lines
5.3 KiB
Go
119 lines
5.3 KiB
Go
package sql
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"tercul/internal/domain"
|
|
"tercul/internal/platform/config"
|
|
|
|
"go.opentelemetry.io/otel"
|
|
"go.opentelemetry.io/otel/trace"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type copyrightRepository struct {
|
|
domain.BaseRepository[domain.Copyright]
|
|
db *gorm.DB
|
|
tracer trace.Tracer
|
|
}
|
|
|
|
// NewCopyrightRepository creates a new CopyrightRepository.
|
|
func NewCopyrightRepository(db *gorm.DB, cfg *config.Config) domain.CopyrightRepository {
|
|
return ©rightRepository{
|
|
BaseRepository: NewBaseRepositoryImpl[domain.Copyright](db, cfg),
|
|
db: db,
|
|
tracer: otel.Tracer("copyright.repository"),
|
|
}
|
|
}
|
|
|
|
// AddTranslation adds a translation to a copyright
|
|
func (r *copyrightRepository) AddTranslation(ctx context.Context, translation *domain.CopyrightTranslation) error {
|
|
ctx, span := r.tracer.Start(ctx, "AddTranslation")
|
|
defer span.End()
|
|
return r.db.WithContext(ctx).Create(translation).Error
|
|
}
|
|
|
|
// GetTranslations gets all translations for a copyright
|
|
func (r *copyrightRepository) GetTranslations(ctx context.Context, copyrightID uint) ([]domain.CopyrightTranslation, error) {
|
|
ctx, span := r.tracer.Start(ctx, "GetTranslations")
|
|
defer span.End()
|
|
var translations []domain.CopyrightTranslation
|
|
err := r.db.WithContext(ctx).Where("copyright_id = ?", copyrightID).Find(&translations).Error
|
|
return translations, err
|
|
}
|
|
|
|
// GetTranslationByLanguage gets a specific translation by language code
|
|
func (r *copyrightRepository) GetTranslationByLanguage(ctx context.Context, copyrightID uint, languageCode string) (*domain.CopyrightTranslation, error) {
|
|
ctx, span := r.tracer.Start(ctx, "GetTranslationByLanguage")
|
|
defer span.End()
|
|
var translation domain.CopyrightTranslation
|
|
err := r.db.WithContext(ctx).Where("copyright_id = ? AND language_code = ?", copyrightID, languageCode).First(&translation).Error
|
|
if err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, ErrEntityNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
return &translation, nil
|
|
}
|
|
|
|
func (r *copyrightRepository) AddCopyrightToWork(ctx context.Context, workID uint, copyrightID uint) error {
|
|
ctx, span := r.tracer.Start(ctx, "AddCopyrightToWork")
|
|
defer span.End()
|
|
return r.db.WithContext(ctx).Exec("INSERT INTO work_copyrights (work_id, copyright_id) VALUES (?, ?) ON CONFLICT DO NOTHING", workID, copyrightID).Error
|
|
}
|
|
|
|
func (r *copyrightRepository) RemoveCopyrightFromWork(ctx context.Context, workID uint, copyrightID uint) error {
|
|
ctx, span := r.tracer.Start(ctx, "RemoveCopyrightFromWork")
|
|
defer span.End()
|
|
return r.db.WithContext(ctx).Exec("DELETE FROM work_copyrights WHERE work_id = ? AND copyright_id = ?", workID, copyrightID).Error
|
|
}
|
|
|
|
func (r *copyrightRepository) AddCopyrightToAuthor(ctx context.Context, authorID uint, copyrightID uint) error {
|
|
ctx, span := r.tracer.Start(ctx, "AddCopyrightToAuthor")
|
|
defer span.End()
|
|
return r.db.WithContext(ctx).Exec("INSERT INTO author_copyrights (author_id, copyright_id) VALUES (?, ?) ON CONFLICT DO NOTHING", authorID, copyrightID).Error
|
|
}
|
|
|
|
func (r *copyrightRepository) RemoveCopyrightFromAuthor(ctx context.Context, authorID uint, copyrightID uint) error {
|
|
ctx, span := r.tracer.Start(ctx, "RemoveCopyrightFromAuthor")
|
|
defer span.End()
|
|
return r.db.WithContext(ctx).Exec("DELETE FROM author_copyrights WHERE author_id = ? AND copyright_id = ?", authorID, copyrightID).Error
|
|
}
|
|
|
|
func (r *copyrightRepository) AddCopyrightToBook(ctx context.Context, bookID uint, copyrightID uint) error {
|
|
ctx, span := r.tracer.Start(ctx, "AddCopyrightToBook")
|
|
defer span.End()
|
|
return r.db.WithContext(ctx).Exec("INSERT INTO book_copyrights (book_id, copyright_id) VALUES (?, ?) ON CONFLICT DO NOTHING", bookID, copyrightID).Error
|
|
}
|
|
|
|
func (r *copyrightRepository) RemoveCopyrightFromBook(ctx context.Context, bookID uint, copyrightID uint) error {
|
|
ctx, span := r.tracer.Start(ctx, "RemoveCopyrightFromBook")
|
|
defer span.End()
|
|
return r.db.WithContext(ctx).Exec("DELETE FROM book_copyrights WHERE book_id = ? AND copyright_id = ?", bookID, copyrightID).Error
|
|
}
|
|
|
|
func (r *copyrightRepository) AddCopyrightToPublisher(ctx context.Context, publisherID uint, copyrightID uint) error {
|
|
ctx, span := r.tracer.Start(ctx, "AddCopyrightToPublisher")
|
|
defer span.End()
|
|
return r.db.WithContext(ctx).Exec("INSERT INTO publisher_copyrights (publisher_id, copyright_id) VALUES (?, ?) ON CONFLICT DO NOTHING", publisherID, copyrightID).Error
|
|
}
|
|
|
|
func (r *copyrightRepository) RemoveCopyrightFromPublisher(ctx context.Context, publisherID uint, copyrightID uint) error {
|
|
ctx, span := r.tracer.Start(ctx, "RemoveCopyrightFromPublisher")
|
|
defer span.End()
|
|
return r.db.WithContext(ctx).Exec("DELETE FROM publisher_copyrights WHERE publisher_id = ? AND copyright_id = ?", publisherID, copyrightID).Error
|
|
}
|
|
|
|
func (r *copyrightRepository) AddCopyrightToSource(ctx context.Context, sourceID uint, copyrightID uint) error {
|
|
ctx, span := r.tracer.Start(ctx, "AddCopyrightToSource")
|
|
defer span.End()
|
|
return r.db.WithContext(ctx).Exec("INSERT INTO source_copyrights (source_id, copyright_id) VALUES (?, ?) ON CONFLICT DO NOTHING", sourceID, copyrightID).Error
|
|
}
|
|
|
|
func (r *copyrightRepository) RemoveCopyrightFromSource(ctx context.Context, sourceID uint, copyrightID uint) error {
|
|
ctx, span := r.tracer.Start(ctx, "RemoveCopyrightFromSource")
|
|
defer span.End()
|
|
return r.db.WithContext(ctx).Exec("DELETE FROM source_copyrights WHERE source_id = ? AND copyright_id = ?", sourceID, copyrightID).Error
|
|
}
|