22 lines
719 B
Python
22 lines
719 B
Python
#python imports
|
|
from typing import Optional
|
|
|
|
#dependency imports
|
|
from entities.cart import Cart, CartItem
|
|
from use_cases.cart_repository_interface import CartRepositoryInterface
|
|
|
|
class ViewCart:
|
|
def __init__(self, cart_repository: CartRepositoryInterface):
|
|
self.cart_repository = cart_repository
|
|
|
|
def execute(self, user_id: int) -> Optional[Cart]:
|
|
"""
|
|
Fetches the cart data from the repository, converts it to Cart entity.
|
|
"""
|
|
cart_data = self.cart_repository.fetch_cart_by_user_id(user_id)
|
|
if not cart_data:
|
|
return None
|
|
|
|
# Convert raw data to domain entities
|
|
items = [CartItem(**item) for item in cart_data]
|
|
return Cart(items) |