feat: Semaine 8
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
import sqlite3
|
||||
|
||||
def get_database_connection():
|
||||
connection = sqlite3.connect('livrexpress.db')
|
||||
connection.row_factory = sqlite3.Row
|
||||
return connection
|
||||
@@ -0,0 +1,32 @@
|
||||
from data.database import get_database_connection
|
||||
from data.repositories.protocols.customer_repository_protocol import CustomerRepositoryProtocol
|
||||
from domain.entities.customer import Customer
|
||||
from domain.services.order_service import OrderService
|
||||
|
||||
class CustomerRepository(CustomerRepositoryProtocol):
|
||||
def __init__(self) -> None:
|
||||
self.db = get_database_connection()
|
||||
|
||||
def find_by_id(self, customer_id: int):
|
||||
row = self.db.execute(
|
||||
"""
|
||||
SELECT c.id, c.name, c.email, c.address
|
||||
FROM customers c
|
||||
LEFT JOIN orders o
|
||||
ON o.customer_id = c.id
|
||||
WHERE c.id = :id
|
||||
GROUP BY c.id
|
||||
""",
|
||||
{"id": customer_id}
|
||||
).fetchone()
|
||||
|
||||
if row is None: return None
|
||||
|
||||
return Customer(
|
||||
id= row['id'],
|
||||
name= row['name'],
|
||||
email= row['email'],
|
||||
address= row['address'],
|
||||
order_count= row['order_count'],
|
||||
is_premium= row['order_count'] >= OrderService.PREMIUM_THRESHOLD,
|
||||
)
|
||||
@@ -0,0 +1,90 @@
|
||||
from datetime import datetime
|
||||
|
||||
from data.database import get_database_connection
|
||||
from data.repositories.protocols.order_repository_protocol import OrderRepositoryProtocol
|
||||
from domain.entities.dish import Dish
|
||||
from domain.entities.order import Order
|
||||
|
||||
|
||||
class OrderRepository(OrderRepositoryProtocol):
|
||||
def __init__(self) -> None:
|
||||
self.db = get_database_connection()
|
||||
|
||||
def find_by_customer_id(self, customer_id: int) -> list[Order]:
|
||||
orders = self.db.execute( # list[tuple]
|
||||
"""
|
||||
SELECT o.id, o.customer_id, o.restaurant_id, r.name as restaurant_name, o.delivery_address, o.created_at, o.delivered_at
|
||||
FROM orders o
|
||||
JOIN restaurants r
|
||||
ON o.restaurant_id = r.id
|
||||
WHERE o.customer_id = :customer_id
|
||||
ORDER BY o.created_at DESC
|
||||
""",
|
||||
{"customer_id": customer_id}
|
||||
).fetchall()
|
||||
|
||||
return [
|
||||
Order(
|
||||
id= row['id'],
|
||||
customer_id= row['customer_id'],
|
||||
restaurant_id= row['restaurant_id'],
|
||||
restaurant_name= row['restaurant_name'],
|
||||
dishes= self.get_dishes(row['id']),
|
||||
delivery_address= row['delivery_address'],
|
||||
created_at= datetime.fromisoformat(row['created_at']),
|
||||
delivery_at= datetime.fromisoformat(row['delivered_at']) if row['delivered_at'] else None
|
||||
) for row in orders
|
||||
]
|
||||
|
||||
def find_by_id(self, order_id: int) -> Order | None:
|
||||
row = self.db.execute( # tuple
|
||||
"""
|
||||
SELECT o.*, r.name as restraurant_name
|
||||
FROM orders o
|
||||
JOIN restaurants r
|
||||
ON o.restaurant_id = r.id
|
||||
WHERE o.id = :id
|
||||
""",
|
||||
{"id": order_id}
|
||||
).fetchone()
|
||||
|
||||
if row is None: return None
|
||||
|
||||
return Order(
|
||||
id= row['id'],
|
||||
customer_id= row['customer_id'],
|
||||
restaurant_id= row['restaurant_id'],
|
||||
restaurant_name= row['restaurant_name'],
|
||||
dishes= self.get_dishes(order_id),
|
||||
delivery_address= row['delivery_address'],
|
||||
created_at= datetime.fromisoformat(row['created_at']),
|
||||
delivery_at= datetime.fromisoformat(row['delivered_at']) if row['delivered_at'] else None
|
||||
)
|
||||
|
||||
def get_dishes(self, order_id: int) -> list[Dish]:
|
||||
cursor = self.db.execute( # list[tuple]
|
||||
"""
|
||||
SELECT d.id, d.name, d.price, d.restaurant_id
|
||||
FROM dishes d
|
||||
JOIN order_dishes od
|
||||
ON d.id = od.dish_id
|
||||
WHERE od.order_id = :order_id
|
||||
""",
|
||||
{"order_id": order_id}
|
||||
)
|
||||
|
||||
return [
|
||||
Dish(
|
||||
id= row['id'],
|
||||
name= row['name'],
|
||||
price= row['price'],
|
||||
restaurant_id= row['restaurant_id']
|
||||
) for row in cursor
|
||||
]
|
||||
|
||||
|
||||
def save(self, order: Order) -> None:
|
||||
self.db.execute(
|
||||
"UPDATE orders SET delivered_at = :delivered_at WHERE id= :id",
|
||||
{"id": order.id, "delivered_at": order.delivery_at.isoformat() if order.delivery_at else None} # condition ternaire
|
||||
)
|
||||
@@ -0,0 +1,6 @@
|
||||
from typing import Protocol
|
||||
from domain.entities.customer import Customer
|
||||
|
||||
class CustomerRepositoryProtocol(Protocol):
|
||||
def find_by_id(self, customer_id: int) -> Customer: ...
|
||||
def save(self, customer): ...
|
||||
@@ -0,0 +1,8 @@
|
||||
from typing import Protocol
|
||||
from domain.entities.order import Order
|
||||
|
||||
class OrderRepositoryProtocol(Protocol):
|
||||
def find_by_customer_id(self, customer_id: int) -> list[Order]: ...
|
||||
def find_by_id(self, order_id: int) -> Order | None: ...
|
||||
def save(self, order: Order) -> None: ...
|
||||
def update(self, order: Order) -> None: ...
|
||||
Reference in New Issue
Block a user