tercul-backend/internal/app/collection/commands.go
google-labs-jules[bot] 8ddc4a7986 This commit refactors the GraphQL layer to improve code quality and adhere to the project's target architecture.
Key changes include:
- Moved authorization logic for collection mutations from the GraphQL resolvers to the application service layer, ensuring that ownership checks are handled consistently within the business logic.
- Updated the `collection` command handlers and input structs to accept a user ID for authorization.
- Removed orphaned code, including unused resolver definitions (`workResolver`, `translationResolver`) and misplaced helper functions from `schema.resolvers.go`.
- Re-implemented the `Stats` resolvers for the `Work` and `Translation` types, ensuring they correctly call the `analytics` application service.
- Fixed several build errors related to type mismatches and redeclared functions by regenerating the GraphQL code and correcting helper function signatures.
- Updated integration tests to provide authenticated user context for collection mutations, ensuring that the new authorization checks pass.
2025-10-03 02:13:12 +00:00

123 lines
3.7 KiB
Go

package collection
import (
"context"
"fmt"
"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
UserID uint
}
// 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 %d cannot update collection %d", 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 uint, userID uint) error {
collection, err := c.repo.GetByID(ctx, id)
if err != nil {
return err
}
if collection.UserID != userID {
return fmt.Errorf("unauthorized: user %d cannot delete collection %d", userID, id)
}
return c.repo.Delete(ctx, id)
}
// AddWorkToCollectionInput represents the input for adding a work to a collection.
type AddWorkToCollectionInput struct {
CollectionID uint
WorkID uint
UserID uint
}
// 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 %d cannot add work to collection %d", 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 uint
WorkID uint
UserID uint
}
// 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 %d cannot remove work from collection %d", input.UserID, input.CollectionID)
}
return c.repo.RemoveWorkFromCollection(ctx, input.CollectionID, input.WorkID)
}