32 lines
1.0 KiB
Python
32 lines
1.0 KiB
Python
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,
|
|
) |