Compare commits
3 Commits
f44a48a33a
...
c49874c572
| Author | SHA1 | Date |
|---|---|---|
|
|
c49874c572 | |
|
|
1384c83864 | |
|
|
f6c3d98cf1 |
|
|
@ -5,6 +5,12 @@ Clean Architecture - Architekturstil am praktischen Beispiel z.B. in Python.
|
|||
|
||||
run python script as a module e.g.: python -m framework_driver.db.db_cart
|
||||
|
||||
## Fast API:
|
||||
- 0: open Terminal
|
||||
- 1: navigate to src: cd .\src\
|
||||
- 2: run main app: uvicorn main:app --reload
|
||||
- 3 go to url: [http://127.0.0.1:8000/docs](http://127.0.0.1:8000/docs)
|
||||
- 4: close with: strg + c
|
||||
|
||||
## Layers
|
||||
- entities
|
||||
|
|
|
|||
|
|
@ -1,18 +1,18 @@
|
|||
import sqlite3
|
||||
from typing import Optional, List
|
||||
from interface_adapters.cart_database_interface import CartDatabaseInterface
|
||||
from interface_adapters.repositories.cart_database_interface import CartDatabaseInterface
|
||||
|
||||
class CartDatabase(CartDatabaseInterface):
|
||||
def __init__(self, db_file):
|
||||
def __init__(self, db_file="framework_driver/db/shop.db"):
|
||||
"""Initialize the CartDatabase with a connection to the SQLite database."""
|
||||
self.conn = sqlite3.connect(db_file)
|
||||
|
||||
def add_to_cart(self, user_id, product_id, quantity):
|
||||
def add_to_cart(self, user_id, product_id, name, quantity, price):
|
||||
"""Add a product to the user's cart."""
|
||||
sql = "INSERT INTO cart (user_id, product_id, quantity) VALUES (?, ?, ?)"
|
||||
sql = "INSERT INTO cart (user_id, product_id, name, quantity, price) VALUES (?, ?, ?, ?, ?)"
|
||||
try:
|
||||
c = self.conn.cursor()
|
||||
c.execute(sql, (user_id, product_id, quantity))
|
||||
c.execute(sql, (user_id, product_id, name, quantity, price))
|
||||
self.conn.commit()
|
||||
return c.lastrowid
|
||||
except sqlite3.Error as e:
|
||||
|
|
@ -20,12 +20,12 @@ class CartDatabase(CartDatabaseInterface):
|
|||
|
||||
def fetch_cart_items(self, user_id: int) -> Optional[List[dict]]:
|
||||
"""Fetch cart items from the database for a given user ID."""
|
||||
sql = "SELECT * FROM cart WHERE user_id = ?"
|
||||
sql = "SELECT product_id, name, quantity, price FROM cart WHERE user_id = ?"
|
||||
try:
|
||||
c = self.conn.cursor()
|
||||
c.execute(sql, (user_id,))
|
||||
rows = c.fetchall()
|
||||
return [{"id": row[0], "user_id": row[1], "product_id": row[2], "quantity": row[3]} for row in rows]
|
||||
return [{'product_id':r[0], 'name':r[1], 'quantity':r[2], 'price':r[3]} for r in rows]
|
||||
except sqlite3.Error as e:
|
||||
print(e)
|
||||
return None
|
||||
|
|
@ -60,7 +60,6 @@ class CartDatabase(CartDatabaseInterface):
|
|||
except sqlite3.Error as e:
|
||||
print(e)
|
||||
|
||||
|
||||
def main():
|
||||
database = "framework_driver/db/shop.db"
|
||||
|
||||
|
|
@ -71,13 +70,13 @@ def main():
|
|||
# delete all cart items
|
||||
cart_db.clear_cart(ex_user_id)
|
||||
|
||||
# user id, product id, quantity
|
||||
cart_db.add_to_cart(ex_user_id, 1, 2)
|
||||
cart_db.add_to_cart(ex_user_id, 3, 5)
|
||||
# user id, product id, name, quantity, price
|
||||
cart_db.add_to_cart(ex_user_id, 1, "Product 1", 2, 10.0)
|
||||
cart_db.add_to_cart(ex_user_id, 2, "Product 2", 5, 20.0)
|
||||
print(f"Cart items for user {ex_user_id}:", cart_db.fetch_cart_items(ex_user_id))
|
||||
|
||||
# user id, product id, quantity
|
||||
cart_db.update_cart_item(ex_user_id, 3, 3)
|
||||
cart_db.update_cart_item(ex_user_id, 2, 3)
|
||||
print(f"Cart items for user {ex_user_id}:", cart_db.fetch_cart_items(ex_user_id))
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
|||
|
|
@ -48,7 +48,9 @@ def main():
|
|||
id INTEGER PRIMARY KEY,
|
||||
user_id INTEGER NOT NULL,
|
||||
product_id INTEGER NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
quantity INTEGER NOT NULL,
|
||||
price REAL NOT NULL,
|
||||
FOREIGN KEY (user_id) REFERENCES users (id),
|
||||
FOREIGN KEY (product_id) REFERENCES products (id)
|
||||
)"""
|
||||
|
|
@ -56,6 +58,12 @@ def main():
|
|||
conn = create_connection(database)
|
||||
|
||||
if conn is not None:
|
||||
# Delete tables if they exist
|
||||
delete_table(conn, "cart")
|
||||
delete_table(conn, "products")
|
||||
delete_table(conn, "users")
|
||||
|
||||
# Create tables
|
||||
create_table(conn, sql_user_table)
|
||||
create_table(conn, sql_product_table)
|
||||
create_table(conn, sql_cart_table)
|
||||
|
|
|
|||
Binary file not shown.
|
|
@ -7,8 +7,8 @@ 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
|
||||
|
||||
#from interface_adapters.mocks.cart_db import CartDatabase
|
||||
from framework_driver.db.db_cart import CartDatabase
|
||||
# Initialize components
|
||||
cart_database = CartDatabase() # Concrete database implementation
|
||||
cart_repository = CartRepository(cart_database) # Repository depends on gateway
|
||||
|
|
|
|||
Loading…
Reference in New Issue