mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 02:51:34 +00:00
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.
169 lines
7.0 KiB
Go
169 lines
7.0 KiB
Go
package copyright
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"tercul/internal/domain"
|
|
"tercul/internal/platform/log"
|
|
)
|
|
|
|
// CopyrightCommands contains the command handlers for copyright.
|
|
type CopyrightCommands struct {
|
|
repo domain.CopyrightRepository
|
|
}
|
|
|
|
// NewCopyrightCommands creates a new CopyrightCommands handler.
|
|
func NewCopyrightCommands(repo domain.CopyrightRepository) *CopyrightCommands {
|
|
return &CopyrightCommands{repo: repo}
|
|
}
|
|
|
|
// CreateCopyright creates a new copyright.
|
|
func (c *CopyrightCommands) CreateCopyright(ctx context.Context, copyright *domain.Copyright) error {
|
|
if copyright == nil {
|
|
return errors.New("copyright cannot be nil")
|
|
}
|
|
if copyright.Name == "" {
|
|
return errors.New("copyright name cannot be empty")
|
|
}
|
|
if copyright.Identificator == "" {
|
|
return errors.New("copyright identificator cannot be empty")
|
|
}
|
|
log.FromContext(ctx).With("name", copyright.Name).Debug("Creating copyright")
|
|
return c.repo.Create(ctx, copyright)
|
|
}
|
|
|
|
// UpdateCopyright updates an existing copyright.
|
|
func (c *CopyrightCommands) UpdateCopyright(ctx context.Context, copyright *domain.Copyright) error {
|
|
if copyright == nil {
|
|
return errors.New("copyright cannot be nil")
|
|
}
|
|
if copyright.ID == 0 {
|
|
return errors.New("copyright ID cannot be zero")
|
|
}
|
|
if copyright.Name == "" {
|
|
return errors.New("copyright name cannot be empty")
|
|
}
|
|
if copyright.Identificator == "" {
|
|
return errors.New("copyright identificator cannot be empty")
|
|
}
|
|
log.FromContext(ctx).With("id", copyright.ID).Debug("Updating copyright")
|
|
return c.repo.Update(ctx, copyright)
|
|
}
|
|
|
|
// DeleteCopyright deletes a copyright.
|
|
func (c *CopyrightCommands) DeleteCopyright(ctx context.Context, id uint) error {
|
|
if id == 0 {
|
|
return errors.New("invalid copyright ID")
|
|
}
|
|
log.FromContext(ctx).With("id", id).Debug("Deleting copyright")
|
|
return c.repo.Delete(ctx, id)
|
|
}
|
|
|
|
// AddCopyrightToWork adds a copyright to a work.
|
|
func (c *CopyrightCommands) AddCopyrightToWork(ctx context.Context, workID uint, copyrightID uint) error {
|
|
if workID == 0 || copyrightID == 0 {
|
|
return errors.New("invalid work ID or copyright ID")
|
|
}
|
|
log.FromContext(ctx).With("work_id", workID).With("copyright_id", copyrightID).Debug("Adding copyright to work")
|
|
return c.repo.AddCopyrightToWork(ctx, workID, copyrightID)
|
|
}
|
|
|
|
// RemoveCopyrightFromWork removes a copyright from a work.
|
|
func (c *CopyrightCommands) RemoveCopyrightFromWork(ctx context.Context, workID uint, copyrightID uint) error {
|
|
if workID == 0 || copyrightID == 0 {
|
|
return errors.New("invalid work ID or copyright ID")
|
|
}
|
|
log.FromContext(ctx).With("work_id", workID).With("copyright_id", copyrightID).Debug("Removing copyright from work")
|
|
return c.repo.RemoveCopyrightFromWork(ctx, workID, copyrightID)
|
|
}
|
|
|
|
// AddCopyrightToAuthor adds a copyright to an author.
|
|
func (c *CopyrightCommands) AddCopyrightToAuthor(ctx context.Context, authorID uint, copyrightID uint) error {
|
|
if authorID == 0 || copyrightID == 0 {
|
|
return errors.New("invalid author ID or copyright ID")
|
|
}
|
|
log.FromContext(ctx).With("author_id", authorID).With("copyright_id", copyrightID).Debug("Adding copyright to author")
|
|
return c.repo.AddCopyrightToAuthor(ctx, authorID, copyrightID)
|
|
}
|
|
|
|
// RemoveCopyrightFromAuthor removes a copyright from an author.
|
|
func (c *CopyrightCommands) RemoveCopyrightFromAuthor(ctx context.Context, authorID uint, copyrightID uint) error {
|
|
if authorID == 0 || copyrightID == 0 {
|
|
return errors.New("invalid author ID or copyright ID")
|
|
}
|
|
log.FromContext(ctx).With("author_id", authorID).With("copyright_id", copyrightID).Debug("Removing copyright from author")
|
|
return c.repo.RemoveCopyrightFromAuthor(ctx, authorID, copyrightID)
|
|
}
|
|
|
|
// AddCopyrightToBook adds a copyright to a book.
|
|
func (c *CopyrightCommands) AddCopyrightToBook(ctx context.Context, bookID uint, copyrightID uint) error {
|
|
if bookID == 0 || copyrightID == 0 {
|
|
return errors.New("invalid book ID or copyright ID")
|
|
}
|
|
log.FromContext(ctx).With("book_id", bookID).With("copyright_id", copyrightID).Debug("Adding copyright to book")
|
|
return c.repo.AddCopyrightToBook(ctx, bookID, copyrightID)
|
|
}
|
|
|
|
// RemoveCopyrightFromBook removes a copyright from a book.
|
|
func (c *CopyrightCommands) RemoveCopyrightFromBook(ctx context.Context, bookID uint, copyrightID uint) error {
|
|
if bookID == 0 || copyrightID == 0 {
|
|
return errors.New("invalid book ID or copyright ID")
|
|
}
|
|
log.FromContext(ctx).With("book_id", bookID).With("copyright_id", copyrightID).Debug("Removing copyright from book")
|
|
return c.repo.RemoveCopyrightFromBook(ctx, bookID, copyrightID)
|
|
}
|
|
|
|
// AddCopyrightToPublisher adds a copyright to a publisher.
|
|
func (c *CopyrightCommands) AddCopyrightToPublisher(ctx context.Context, publisherID uint, copyrightID uint) error {
|
|
if publisherID == 0 || copyrightID == 0 {
|
|
return errors.New("invalid publisher ID or copyright ID")
|
|
}
|
|
log.FromContext(ctx).With("publisher_id", publisherID).With("copyright_id", copyrightID).Debug("Adding copyright to publisher")
|
|
return c.repo.AddCopyrightToPublisher(ctx, publisherID, copyrightID)
|
|
}
|
|
|
|
// RemoveCopyrightFromPublisher removes a copyright from a publisher.
|
|
func (c *CopyrightCommands) RemoveCopyrightFromPublisher(ctx context.Context, publisherID uint, copyrightID uint) error {
|
|
if publisherID == 0 || copyrightID == 0 {
|
|
return errors.New("invalid publisher ID or copyright ID")
|
|
}
|
|
log.FromContext(ctx).With("publisher_id", publisherID).With("copyright_id", copyrightID).Debug("Removing copyright from publisher")
|
|
return c.repo.RemoveCopyrightFromPublisher(ctx, publisherID, copyrightID)
|
|
}
|
|
|
|
// AddCopyrightToSource adds a copyright to a source.
|
|
func (c *CopyrightCommands) AddCopyrightToSource(ctx context.Context, sourceID uint, copyrightID uint) error {
|
|
if sourceID == 0 || copyrightID == 0 {
|
|
return errors.New("invalid source ID or copyright ID")
|
|
}
|
|
log.FromContext(ctx).With("source_id", sourceID).With("copyright_id", copyrightID).Debug("Adding copyright to source")
|
|
return c.repo.AddCopyrightToSource(ctx, sourceID, copyrightID)
|
|
}
|
|
|
|
// RemoveCopyrightFromSource removes a copyright from a source.
|
|
func (c *CopyrightCommands) RemoveCopyrightFromSource(ctx context.Context, sourceID uint, copyrightID uint) error {
|
|
if sourceID == 0 || copyrightID == 0 {
|
|
return errors.New("invalid source ID or copyright ID")
|
|
}
|
|
log.FromContext(ctx).With("source_id", sourceID).With("copyright_id", copyrightID).Debug("Removing copyright from source")
|
|
return c.repo.RemoveCopyrightFromSource(ctx, sourceID, copyrightID)
|
|
}
|
|
|
|
// AddTranslation adds a translation to a copyright.
|
|
func (c *CopyrightCommands) AddTranslation(ctx context.Context, translation *domain.CopyrightTranslation) error {
|
|
if translation == nil {
|
|
return errors.New("translation cannot be nil")
|
|
}
|
|
if translation.CopyrightID == 0 {
|
|
return errors.New("copyright ID cannot be zero")
|
|
}
|
|
if translation.LanguageCode == "" {
|
|
return errors.New("language code cannot be empty")
|
|
}
|
|
if translation.Message == "" {
|
|
return errors.New("translation message cannot be empty")
|
|
}
|
|
log.FromContext(ctx).With("copyright_id", translation.CopyrightID).With("language", translation.LanguageCode).Debug("Adding translation to copyright")
|
|
return c.repo.AddTranslation(ctx, translation)
|
|
}
|