package collection import ( "context" "errors" "tercul/internal/domain" ) // 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 uint } // CreateCollection creates a new collection. func (c *CollectionCommands) CreateCollection(ctx context.Context, input CreateCollectionInput) (*domain.Collection, error) { if input.Name == "" { return nil, errors.New("collection name cannot be empty") } if input.UserID == 0 { return nil, errors.New("user ID cannot be zero") } collection := &domain.Collection{ Name: input.Name, Description: input.Description, UserID: input.UserID, } 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 uint Name string Description string UserID uint // for authorization } // UpdateCollection updates an existing collection. func (c *CollectionCommands) UpdateCollection(ctx context.Context, input UpdateCollectionInput) (*domain.Collection, error) { if input.ID == 0 { return nil, errors.New("collection ID cannot be zero") } if input.Name == "" { return nil, errors.New("collection name cannot be empty") } // Fetch the existing collection collection, err := c.repo.GetByID(ctx, input.ID) if err != nil { return nil, err } if collection == nil { return nil, errors.New("collection not found") } // Check ownership if collection.UserID != input.UserID { return nil, errors.New("unauthorized") } // Update fields collection.Name = input.Name collection.Description = input.Description err = c.repo.Update(ctx, collection) if err != nil { return nil, err } return collection, nil } // DeleteCollectionInput represents the input for deleting a collection. type DeleteCollectionInput struct { ID uint UserID uint // for authorization } // DeleteCollection deletes a collection by ID. func (c *CollectionCommands) DeleteCollection(ctx context.Context, input DeleteCollectionInput) error { if input.ID == 0 { return errors.New("invalid collection ID") } // Fetch the existing collection collection, err := c.repo.GetByID(ctx, input.ID) if err != nil { return err } if collection == nil { return errors.New("collection not found") } // Check ownership if collection.UserID != input.UserID { return errors.New("unauthorized") } return c.repo.Delete(ctx, input.ID) } // AddWorkToCollectionInput represents the input for adding a work to a collection. type AddWorkToCollectionInput struct { CollectionID uint WorkID uint UserID uint // for authorization } // AddWorkToCollection adds a work to a collection. func (c *CollectionCommands) AddWorkToCollection(ctx context.Context, input AddWorkToCollectionInput) error { if input.CollectionID == 0 { return errors.New("invalid collection ID") } if input.WorkID == 0 { return errors.New("invalid work ID") } // Fetch the existing collection collection, err := c.repo.GetByID(ctx, input.CollectionID) if err != nil { return err } if collection == nil { return errors.New("collection not found") } // Check ownership if collection.UserID != input.UserID { return errors.New("unauthorized") } return c.repo.AddWorkToCollection(ctx, input.CollectionID, input.WorkID) } // RemoveWorkFromCollectionInput represents the input for removing a work from a collection. type RemoveWorkFromCollectionInput struct { CollectionID uint WorkID uint UserID uint // for authorization } // RemoveWorkFromCollection removes a work from a collection. func (c *CollectionCommands) RemoveWorkFromCollection(ctx context.Context, input RemoveWorkFromCollectionInput) error { if input.CollectionID == 0 { return errors.New("invalid collection ID") } if input.WorkID == 0 { return errors.New("invalid work ID") } // Fetch the existing collection collection, err := c.repo.GetByID(ctx, input.CollectionID) if err != nil { return err } if collection == nil { return errors.New("collection not found") } // Check ownership if collection.UserID != input.UserID { return errors.New("unauthorized") } return c.repo.RemoveWorkFromCollection(ctx, input.CollectionID, input.WorkID) }