changed based on CartItem entity
parent
f44a48a33a
commit
f6c3d98cf1
|
|
@ -1,18 +1,18 @@
|
||||||
import sqlite3
|
import sqlite3
|
||||||
from typing import Optional, List
|
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):
|
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."""
|
"""Initialize the CartDatabase with a connection to the SQLite database."""
|
||||||
self.conn = sqlite3.connect(db_file)
|
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."""
|
"""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:
|
try:
|
||||||
c = self.conn.cursor()
|
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()
|
self.conn.commit()
|
||||||
return c.lastrowid
|
return c.lastrowid
|
||||||
except sqlite3.Error as e:
|
except sqlite3.Error as e:
|
||||||
|
|
@ -20,12 +20,12 @@ class CartDatabase(CartDatabaseInterface):
|
||||||
|
|
||||||
def fetch_cart_items(self, user_id: int) -> Optional[List[dict]]:
|
def fetch_cart_items(self, user_id: int) -> Optional[List[dict]]:
|
||||||
"""Fetch cart items from the database for a given user ID."""
|
"""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:
|
try:
|
||||||
c = self.conn.cursor()
|
c = self.conn.cursor()
|
||||||
c.execute(sql, (user_id,))
|
c.execute(sql, (user_id,))
|
||||||
rows = c.fetchall()
|
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:
|
except sqlite3.Error as e:
|
||||||
print(e)
|
print(e)
|
||||||
return None
|
return None
|
||||||
|
|
@ -60,7 +60,6 @@ class CartDatabase(CartDatabaseInterface):
|
||||||
except sqlite3.Error as e:
|
except sqlite3.Error as e:
|
||||||
print(e)
|
print(e)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
database = "framework_driver/db/shop.db"
|
database = "framework_driver/db/shop.db"
|
||||||
|
|
||||||
|
|
@ -71,13 +70,13 @@ def main():
|
||||||
# delete all cart items
|
# delete all cart items
|
||||||
cart_db.clear_cart(ex_user_id)
|
cart_db.clear_cart(ex_user_id)
|
||||||
|
|
||||||
# user id, product id, quantity
|
# user id, product id, name, quantity, price
|
||||||
cart_db.add_to_cart(ex_user_id, 1, 2)
|
cart_db.add_to_cart(ex_user_id, 1, "Product 1", 2, 10.0)
|
||||||
cart_db.add_to_cart(ex_user_id, 3, 5)
|
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))
|
print(f"Cart items for user {ex_user_id}:", cart_db.fetch_cart_items(ex_user_id))
|
||||||
|
|
||||||
# user id, product id, quantity
|
# 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))
|
print(f"Cart items for user {ex_user_id}:", cart_db.fetch_cart_items(ex_user_id))
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
||||||
|
|
@ -48,7 +48,9 @@ def main():
|
||||||
id INTEGER PRIMARY KEY,
|
id INTEGER PRIMARY KEY,
|
||||||
user_id INTEGER NOT NULL,
|
user_id INTEGER NOT NULL,
|
||||||
product_id INTEGER NOT NULL,
|
product_id INTEGER NOT NULL,
|
||||||
|
name TEXT NOT NULL,
|
||||||
quantity INTEGER NOT NULL,
|
quantity INTEGER NOT NULL,
|
||||||
|
price REAL NOT NULL,
|
||||||
FOREIGN KEY (user_id) REFERENCES users (id),
|
FOREIGN KEY (user_id) REFERENCES users (id),
|
||||||
FOREIGN KEY (product_id) REFERENCES products (id)
|
FOREIGN KEY (product_id) REFERENCES products (id)
|
||||||
)"""
|
)"""
|
||||||
|
|
@ -56,6 +58,12 @@ def main():
|
||||||
conn = create_connection(database)
|
conn = create_connection(database)
|
||||||
|
|
||||||
if conn is not None:
|
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_user_table)
|
||||||
create_table(conn, sql_product_table)
|
create_table(conn, sql_product_table)
|
||||||
create_table(conn, sql_cart_table)
|
create_table(conn, sql_cart_table)
|
||||||
|
|
|
||||||
Binary file not shown.
Loading…
Reference in New Issue