add basic entity layer
parent
78fccb6a94
commit
b453880cf4
|
|
@ -0,0 +1,27 @@
|
|||
#python imports
|
||||
from typing import List
|
||||
from dataclasses import dataclass
|
||||
|
||||
#dependency imports
|
||||
from entities.cart_item import CartItem
|
||||
from entities.product import Product
|
||||
|
||||
@dataclass
|
||||
class Cart:
|
||||
items: List[CartItem]
|
||||
|
||||
def add_item(self, product: Product, quantity: int):
|
||||
for item in self.items:
|
||||
if item.product.id == product.id:
|
||||
item.quantity += quantity
|
||||
return
|
||||
self.items.append(CartItem(product=product, quantity=quantity))
|
||||
|
||||
def remove_item(self, product_id: str):
|
||||
self.items = [item for item in self.items if item.product.id != product_id]
|
||||
|
||||
def calculate_total_price(self) -> float:
|
||||
return sum(item.calculate_total_price() for item in self.items)
|
||||
|
||||
def list_items(self) -> List[str]:
|
||||
return [f"{item.quantity} x {item.product.name} - {item.calculate_total_price()} EUR" for item in self.items]
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
#python imports
|
||||
from typing import List
|
||||
from dataclasses import dataclass
|
||||
|
||||
@dataclass
|
||||
class CartItem:
|
||||
product_id: int
|
||||
name: str
|
||||
quantity: int
|
||||
price: float
|
||||
|
||||
def post_init(self):
|
||||
if self.quantity <= 0:
|
||||
raise ValueError("Quantity has to be atleast 1")
|
||||
|
||||
def calculate_total_price(self) -> float:
|
||||
return self.quantity * self.price
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
#python imports
|
||||
from dataclasses import dataclass
|
||||
|
||||
@dataclass
|
||||
class Product:
|
||||
id: str
|
||||
name: str
|
||||
description: str
|
||||
price: float
|
||||
|
||||
def post_init(self):
|
||||
if self.price < 0:
|
||||
raise ValueError("Der Preis darf nicht negativ sein.")
|
||||
Loading…
Reference in New Issue