mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 05:11:34 +00:00
Some checks failed
- Updated database models and repositories to replace uint IDs with UUIDs. - Modified test fixtures to generate and use UUIDs for authors, translations, users, and works. - Adjusted mock implementations to align with the new UUID structure. - Ensured all relevant functions and methods are updated to handle UUIDs correctly. - Added necessary imports for UUID handling in various files.
125 lines
3.8 KiB
Go
125 lines
3.8 KiB
Go
package collection
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"tercul/internal/domain"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
// CollectionCommands contains the command handlers for the collection aggregate.
|
|
type CollectionCommands struct {
|
|
repo domain.CollectionRepository
|
|
}
|
|
|
|
// NewCollectionCommands creates a new CollectionCommands handler.
|
|
func NewCollectionCommands(repo domain.CollectionRepository) *CollectionCommands {
|
|
return &CollectionCommands{repo: repo}
|
|
}
|
|
|
|
// CreateCollectionInput represents the input for creating a new collection.
|
|
type CreateCollectionInput struct {
|
|
Name string
|
|
Description string
|
|
UserID uuid.UUID
|
|
IsPublic bool
|
|
CoverImageURL string
|
|
}
|
|
|
|
// CreateCollection creates a new collection.
|
|
func (c *CollectionCommands) CreateCollection(ctx context.Context, input CreateCollectionInput) (*domain.Collection, error) {
|
|
collection := &domain.Collection{
|
|
Name: input.Name,
|
|
Description: input.Description,
|
|
UserID: input.UserID,
|
|
IsPublic: input.IsPublic,
|
|
CoverImageURL: input.CoverImageURL,
|
|
}
|
|
err := c.repo.Create(ctx, collection)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return collection, nil
|
|
}
|
|
|
|
// UpdateCollectionInput represents the input for updating an existing collection.
|
|
type UpdateCollectionInput struct {
|
|
ID uuid.UUID
|
|
Name string
|
|
Description string
|
|
IsPublic bool
|
|
CoverImageURL string
|
|
UserID uuid.UUID
|
|
}
|
|
|
|
// UpdateCollection updates an existing collection.
|
|
func (c *CollectionCommands) UpdateCollection(ctx context.Context, input UpdateCollectionInput) (*domain.Collection, error) {
|
|
collection, err := c.repo.GetByID(ctx, input.ID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if collection.UserID != input.UserID {
|
|
return nil, fmt.Errorf("unauthorized: user %s cannot update collection %s", input.UserID, input.ID)
|
|
}
|
|
collection.Name = input.Name
|
|
collection.Description = input.Description
|
|
collection.IsPublic = input.IsPublic
|
|
collection.CoverImageURL = input.CoverImageURL
|
|
err = c.repo.Update(ctx, collection)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return collection, nil
|
|
}
|
|
|
|
// DeleteCollection deletes a collection by ID.
|
|
func (c *CollectionCommands) DeleteCollection(ctx context.Context, id uuid.UUID, userID uuid.UUID) error {
|
|
collection, err := c.repo.GetByID(ctx, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if collection.UserID != userID {
|
|
return fmt.Errorf("unauthorized: user %s cannot delete collection %s", userID, id)
|
|
}
|
|
return c.repo.Delete(ctx, id)
|
|
}
|
|
|
|
// AddWorkToCollectionInput represents the input for adding a work to a collection.
|
|
type AddWorkToCollectionInput struct {
|
|
CollectionID uuid.UUID
|
|
WorkID uuid.UUID
|
|
UserID uuid.UUID
|
|
}
|
|
|
|
// AddWorkToCollection adds a work to a collection.
|
|
func (c *CollectionCommands) AddWorkToCollection(ctx context.Context, input AddWorkToCollectionInput) error {
|
|
collection, err := c.repo.GetByID(ctx, input.CollectionID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if collection.UserID != input.UserID {
|
|
return fmt.Errorf("unauthorized: user %s cannot add work to collection %s", input.UserID, input.CollectionID)
|
|
}
|
|
return c.repo.AddWorkToCollection(ctx, input.CollectionID, input.WorkID)
|
|
}
|
|
|
|
// RemoveWorkFromCollectionInput represents the input for removing a work from a collection.
|
|
type RemoveWorkFromCollectionInput struct {
|
|
CollectionID uuid.UUID
|
|
WorkID uuid.UUID
|
|
UserID uuid.UUID
|
|
}
|
|
|
|
// RemoveWorkFromCollection removes a work from a collection.
|
|
func (c *CollectionCommands) RemoveWorkFromCollection(ctx context.Context, input RemoveWorkFromCollectionInput) error {
|
|
collection, err := c.repo.GetByID(ctx, input.CollectionID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if collection.UserID != input.UserID {
|
|
return fmt.Errorf("unauthorized: user %s cannot remove work from collection %s", input.UserID, input.CollectionID)
|
|
}
|
|
return c.repo.RemoveWorkFromCollection(ctx, input.CollectionID, input.WorkID)
|
|
}
|