Multiple implementations of the same back-end application. The aim is to provide quick, side-by-side comparisons of different technologies (languages, frameworks, libraries) while preserving consistent business logic across all implementations.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

69 lines
2.0 KiB

package commands
import (
"context"
"fmt"
"autostore/internal/application/interfaces"
"autostore/internal/domain/value_objects"
)
var (
ErrItemDeletionFailed = fmt.Errorf("failed to delete item")
ErrItemNotFound = fmt.Errorf("item not found")
ErrUnauthorizedAccess = fmt.Errorf("unauthorized access to item")
)
type DeleteItemCommand struct {
itemRepo interfaces.IItemRepository
logger interfaces.ILogger
}
func NewDeleteItemCommand(
itemRepo interfaces.IItemRepository,
logger interfaces.ILogger,
) *DeleteItemCommand {
return &DeleteItemCommand{
itemRepo: itemRepo,
logger: logger,
}
}
func (c *DeleteItemCommand) Execute(ctx context.Context, itemID string, userID string) error {
c.logger.Info(ctx, "Executing DeleteItemCommand", "itemID", itemID, "userID", userID)
// Convert string IDs to value objects
itemIDObj, err := value_objects.NewItemID(itemID)
if err != nil {
c.logger.Error(ctx, "Invalid item ID", "itemID", itemID, "error", err)
return fmt.Errorf("invalid item ID: %w", err)
}
userIDObj, err := value_objects.NewUserID(userID)
if err != nil {
c.logger.Error(ctx, "Invalid user ID", "userID", userID, "error", err)
return fmt.Errorf("invalid user ID: %w", err)
}
// Find item by ID
item, err := c.itemRepo.FindByID(ctx, itemIDObj)
if err != nil {
c.logger.Error(ctx, "Failed to find item", "itemID", itemID, "error", err)
return fmt.Errorf("%w: %v", ErrItemNotFound, err)
}
// Validate ownership - only the item's owner can delete it
if !item.GetUserID().Equals(userIDObj) {
c.logger.Warn(ctx, "Unauthorized deletion attempt", "itemID", itemID, "userID", userID, "ownerID", item.GetUserID().String())
return ErrUnauthorizedAccess
}
// Delete the item
if err := c.itemRepo.Delete(ctx, itemIDObj); err != nil {
c.logger.Error(ctx, "Failed to delete item", "itemID", itemID, "error", err)
return fmt.Errorf("%w: %v", ErrItemDeletionFailed, err)
}
c.logger.Info(ctx, "Item deleted successfully", "itemID", itemID, "userID", userID)
return nil
}