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.
63 lines
1.8 KiB
63 lines
1.8 KiB
package queries |
|
|
|
import ( |
|
"context" |
|
"fmt" |
|
|
|
"autostore/internal/application/interfaces" |
|
"autostore/internal/domain/entities" |
|
"autostore/internal/domain/value_objects" |
|
) |
|
|
|
var ( |
|
ErrItemNotFound = fmt.Errorf("item not found") |
|
ErrUnauthorizedAccess = fmt.Errorf("unauthorized access to item") |
|
) |
|
|
|
type GetItemQuery struct { |
|
itemRepo interfaces.IItemRepository |
|
logger interfaces.ILogger |
|
} |
|
|
|
func NewGetItemQuery( |
|
itemRepo interfaces.IItemRepository, |
|
logger interfaces.ILogger, |
|
) *GetItemQuery { |
|
return &GetItemQuery{ |
|
itemRepo: itemRepo, |
|
logger: logger, |
|
} |
|
} |
|
|
|
func (q *GetItemQuery) Execute(ctx context.Context, itemID string, userID string) (*entities.ItemEntity, error) { |
|
q.logger.Info(ctx, "Executing GetItemQuery", "itemID", itemID, "userID", userID) |
|
|
|
// Convert string IDs to value objects |
|
itemIDObj, err := value_objects.NewItemID(itemID) |
|
if err != nil { |
|
q.logger.Error(ctx, "Invalid item ID", "itemID", itemID, "error", err) |
|
return nil, fmt.Errorf("invalid item ID: %w", err) |
|
} |
|
|
|
userIDObj, err := value_objects.NewUserID(userID) |
|
if err != nil { |
|
q.logger.Error(ctx, "Invalid user ID", "userID", userID, "error", err) |
|
return nil, fmt.Errorf("invalid user ID: %w", err) |
|
} |
|
|
|
// Find item by ID |
|
item, err := q.itemRepo.FindByID(ctx, itemIDObj) |
|
if err != nil { |
|
q.logger.Error(ctx, "Failed to find item", "itemID", itemID, "error", err) |
|
return nil, fmt.Errorf("%w: %v", ErrItemNotFound, err) |
|
} |
|
|
|
// Validate ownership - only the item's owner can access it |
|
if !item.GetUserID().Equals(userIDObj) { |
|
q.logger.Warn(ctx, "Unauthorized access attempt", "itemID", itemID, "userID", userID, "ownerID", item.GetUserID().String()) |
|
return nil, ErrUnauthorizedAccess |
|
} |
|
|
|
q.logger.Info(ctx, "Item retrieved successfully", "itemID", itemID, "userID", userID) |
|
return item, nil |
|
}
|
|
|