Merge branch 'layer_interface_adapters' into layer_framework_driver
commit
f44a48a33a
|
|
@ -0,0 +1,32 @@
|
||||||
|
#python imports
|
||||||
|
from fastapi import APIRouter, HTTPException
|
||||||
|
from fastapi.responses import JSONResponse
|
||||||
|
|
||||||
|
#dependency imports
|
||||||
|
from use_cases.view_cart import ViewCart
|
||||||
|
from interface_adapters.dtos.view_cart_response import ViewCartResponseDTO, CartItemDTO
|
||||||
|
from interface_adapters.dtos.view_cart_request import ViewCartRequestDTO
|
||||||
|
from interface_adapters.repositories.cart_repository import CartRepository
|
||||||
|
from interface_adapters.mocks.cart_db import CartDatabase
|
||||||
|
|
||||||
|
# Initialize components
|
||||||
|
cart_database = CartDatabase() # Concrete database implementation
|
||||||
|
cart_repository = CartRepository(cart_database) # Repository depends on gateway
|
||||||
|
view_cart_use_case = ViewCart(cart_repository) # Use case depends on repository
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
@router.post("/view_cart", response_model=ViewCartResponseDTO)
|
||||||
|
async def view_cart(request_dto: ViewCartRequestDTO):
|
||||||
|
|
||||||
|
cart = view_cart_use_case.execute(request_dto.user_id)
|
||||||
|
|
||||||
|
if not cart:
|
||||||
|
raise HTTPException(status_code=404, detail="Cart not found")
|
||||||
|
|
||||||
|
response_dto = ViewCartResponseDTO(
|
||||||
|
items=[CartItemDTO(product_id=item.product_id, quantity=item.quantity, price=item.price) for item in cart.items],
|
||||||
|
total_price=sum(item.price * item.quantity for item in cart.items)
|
||||||
|
)
|
||||||
|
|
||||||
|
return response_dto
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
#python imports
|
||||||
|
from pydantic import BaseModel
|
||||||
|
from typing import List, Optional
|
||||||
|
|
||||||
|
class ViewCartRequestDTO(BaseModel):
|
||||||
|
user_id: int
|
||||||
|
|
@ -0,0 +1,12 @@
|
||||||
|
#python imports
|
||||||
|
from pydantic import BaseModel
|
||||||
|
from typing import List, Optional
|
||||||
|
|
||||||
|
class CartItemDTO(BaseModel):
|
||||||
|
product_id: int
|
||||||
|
quantity: int
|
||||||
|
price: float
|
||||||
|
|
||||||
|
class ViewCartResponseDTO(BaseModel):
|
||||||
|
items: List[CartItemDTO] = []
|
||||||
|
total_price: float = 0.0
|
||||||
|
|
@ -0,0 +1,21 @@
|
||||||
|
#python imports
|
||||||
|
from typing import Optional, List
|
||||||
|
|
||||||
|
#dependency imports
|
||||||
|
from interface_adapters.repositories.cart_database_interface import CartDatabaseInterface
|
||||||
|
|
||||||
|
class CartDatabase(CartDatabaseInterface):
|
||||||
|
def __init__(self):
|
||||||
|
# Mock database setup
|
||||||
|
self.mock_db = {
|
||||||
|
1: [
|
||||||
|
{"product_id": 101, "name": "Apple", "quantity": 2, "price": 0.5},
|
||||||
|
{"product_id": 102, "name": "Milk", "quantity": 1, "price": 1.5},
|
||||||
|
],
|
||||||
|
2: [
|
||||||
|
{"product_id": 201, "name": "Bread", "quantity": 1, "price": 2.0},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
|
||||||
|
def fetch_cart_items(self, user_id: int) -> Optional[List[dict]]:
|
||||||
|
return self.mock_db.get(user_id)
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
#python imports
|
||||||
from typing import Optional, List
|
from typing import Optional, List
|
||||||
|
|
||||||
class CartDatabaseInterface:
|
class CartDatabaseInterface:
|
||||||
|
|
@ -3,7 +3,7 @@ from typing import Optional, List
|
||||||
|
|
||||||
#dependency imports
|
#dependency imports
|
||||||
from use_cases.cart_repository_interface import CartRepositoryInterface
|
from use_cases.cart_repository_interface import CartRepositoryInterface
|
||||||
from interface_adapters.cart_database_interface import CartDatabaseInterface
|
from interface_adapters.repositories.cart_database_interface import CartDatabaseInterface
|
||||||
|
|
||||||
class CartRepository(CartRepositoryInterface):
|
class CartRepository(CartRepositoryInterface):
|
||||||
def __init__(self, database_gateway: CartDatabaseInterface):
|
def __init__(self, database_gateway: CartDatabaseInterface):
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
from interface_adapters.controllers.cart_controller import router as cart_router
|
||||||
|
|
||||||
|
from fastapi import FastAPI
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
|
app.include_router(cart_router)
|
||||||
|
|
||||||
|
@app.get("/")
|
||||||
|
async def root():
|
||||||
|
return {"message": "Hello World"}
|
||||||
|
|
||||||
|
|
@ -3,10 +3,10 @@ from typing import Optional
|
||||||
|
|
||||||
#dependency imports
|
#dependency imports
|
||||||
from entities.cart import Cart, CartItem
|
from entities.cart import Cart, CartItem
|
||||||
from interface_adapters.cart_repository import CartRepository
|
from use_cases.cart_repository_interface import CartRepositoryInterface
|
||||||
|
|
||||||
class ViewCart:
|
class ViewCart:
|
||||||
def __init__(self, cart_repository: CartRepository):
|
def __init__(self, cart_repository: CartRepositoryInterface):
|
||||||
self.cart_repository = cart_repository
|
self.cart_repository = cart_repository
|
||||||
|
|
||||||
def execute(self, user_id: int) -> Optional[Cart]:
|
def execute(self, user_id: int) -> Optional[Cart]:
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue