tercul-backend/internal/app/collection/commands.go
google-labs-jules[bot] 1c4dcbcf99 Refactor: Introduce service layer for application logic
This change introduces a service layer to encapsulate the business logic
for each domain aggregate. This will make the code more modular,
testable, and easier to maintain.

The following services have been created:
- author
- bookmark
- category
- collection
- comment
- like
- tag
- translation
- user

The main Application struct has been updated to use these new services.
The integration test suite has also been updated to use the new
Application struct and services.

This is a work in progress. The next step is to fix the compilation
errors and then refactor the resolvers to use the new services.
2025-09-09 02:28:25 +00:00

95 lines
2.8 KiB
Go

package collection
import (
"context"
"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
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 uint
Name string
Description string
IsPublic bool
CoverImageURL string
}
// 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
}
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 uint) error {
return c.repo.Delete(ctx, id)
}
// AddWorkToCollectionInput represents the input for adding a work to a collection.
type AddWorkToCollectionInput struct {
CollectionID uint
WorkID uint
}
// AddWorkToCollection adds a work to a collection.
func (c *CollectionCommands) AddWorkToCollection(ctx context.Context, input AddWorkToCollectionInput) error {
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
}
// RemoveWorkFromCollection removes a work from a collection.
func (c *CollectionCommands) RemoveWorkFromCollection(ctx context.Context, input RemoveWorkFromCollectionInput) error {
return c.repo.RemoveWorkFromCollection(ctx, input.CollectionID, input.WorkID)
}