Skip to content

Commit

Permalink
fix: order
Browse files Browse the repository at this point in the history
  • Loading branch information
felixrodrigo19 committed Oct 16, 2024
1 parent ac658fb commit 57e884b
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/api/presentation/shared/dtos/order_response_dto.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class OrderProductResponseDto(BaseModel):

class OrderResponseDto(BaseModel):
id: str
user: int
user: str
total_order: float
order_status: str
payment_condition: str
Expand Down
38 changes: 23 additions & 15 deletions src/cart/adapters/postgres_gateway.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from typing import List

from cart.domain.entities.order_product import OrderProduct
from product.domain.entities.product import Product
from src.cart.domain.entities.order import Order
from src.cart.domain.entities.order_product import OrderProduct
from src.cart.domain.enums.paymentConditions import PaymentConditions
from src.cart.ports.cart_gateway import ICartGateway
from src.cart.ports.unit_of_work_interface import ICartUnitOfWork
Expand Down Expand Up @@ -32,7 +33,7 @@ def create_update_order(self, order: Order) -> Order:
'status': order.order_status.value,
'payment_condition': condition.name,
'products': [{'id': p.id,
'product_id': p.product.sku, # sku
'product_id': p.product.sku, # sku
'quantity': p.quantity,
'observation': p.observation} for p in order.products]
})
Expand All @@ -44,21 +45,28 @@ def delete_order(self, order_id: str) -> None:
self.uow.repository.delete(order_id)
self.uow.commit()

def build_order_entity(self, order):
@staticmethod
def build_order_entity(order):
if not order:
return None
products = [self.build_order_product_entity(p) for p in order['products']]

payment_condition = PaymentConditions[order['payment_condition']]
products = []
for p in order['products']:
products.append(
OrderProduct(
product=Product(_id=p['id'],
sku=p['sku'],
description=p['description'],
category=p['category'],
stock=p['stock'],
price=p['price']),
quantity=p['quantity'],
observation=p['observation']
)
)
return Order(_id=order['id'],
user=order['user_id'],
order_datetime=order['order_datetime'],
order_status=order['order_status'],
payment_condition=order['payment_condition'],
order_datetime=order['created_at'],
order_status=order['status'],
payment_condition=payment_condition.value,
products=products)

@staticmethod
def build_order_product_entity(product):
return OrderProduct(_id=product['id'],
product=product['product'], # sku
quantity=product['quantity'],
observation=product.get('observation', ''))
114 changes: 106 additions & 8 deletions src/cart/adapters/postgresql_repository.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from typing import Dict, List, Sequence, Any
from typing import Dict, List, Any

from sqlalchemy import select, Row, delete
from sqlalchemy import select, delete
from sqlalchemy.dialects.postgresql import insert
from sqlalchemy.orm import Session

from cart.domain.entities.order_product import OrderProduct
from product.adapters.product_table import ProductTable
from src.cart.adapters.order_table import OrderTable, OrderProductTable
from src.cart.ports.repository_interface import IRepository

Expand All @@ -14,20 +16,116 @@ def __init__(self, session: Session):
self.session = session

def get_all(self) -> List[Dict]:
stmt = select(OrderTable).order_by(OrderTable.status, OrderTable.created_at)
results: Sequence[Row[tuple[OrderTable]]] = self.session.execute(stmt).all()

stmt = (
select(
OrderTable.id,
OrderTable.user_id,
OrderTable.status,
OrderTable.payment_condition,
OrderTable.created_at,
OrderTable.updated_at,
OrderProductTable.quantity,
OrderProductTable.observation,
ProductTable.sku,
ProductTable.description,
ProductTable.price,
ProductTable.category,
ProductTable.stock,
ProductTable.id
)
.join(OrderProductTable, OrderTable.id == OrderProductTable.order_id)
.join(ProductTable, ProductTable.sku == OrderProductTable.product_id)
.order_by(OrderTable.status, OrderTable.created_at)
)

results = self.session.execute(stmt).all()

if not results:
return []

return [row.to_dict() for row in results]
# Processing the results into a more readable format
response = []
for row in results:
order_data = {
'id': row[0],
'user_id': row[1],
'status': row[2],
'payment_condition': row[3],
'created_at': row[4],
'updated_at': row[5],
'products': []
}

product_data = {
'sku': row[8],
'description': row[9],
'price': row[10],
'quantity': row[6],
'observation': row[7],
'category': row[11],
'stock': row[12],
'id': row[13]
}
order_data['products'].append(product_data)
response.append(order_data)

return response

def filter_by_id(self, order_id: str) -> Dict:
stmt = select(OrderTable).where(OrderTable.id == order_id)
results: Sequence[Row[tuple[OrderTable]]] = self.session.execute(stmt).first()

stmt = (
select(
OrderTable.id,
OrderTable.user_id,
OrderTable.status,
OrderTable.payment_condition,
OrderTable.created_at,
OrderTable.updated_at,
OrderProductTable.quantity,
OrderProductTable.observation,
ProductTable.sku,
ProductTable.description,
ProductTable.price,
ProductTable.category,
ProductTable.stock,
ProductTable.id
)
.join(OrderProductTable, OrderTable.id == OrderProductTable.order_id)
.join(ProductTable, ProductTable.sku == OrderProductTable.product_id)
.where(OrderTable.id == order_id)
)

results = self.session.execute(stmt).all()

if not results:
return {}

return results[0].to_dict()
# Processing the single result
order_data = {
'id': results[0][0],
'user_id': results[0][1],
'status': results[0][2],
'payment_condition': results[0][3],
'created_at': results[0][4],
'updated_at': results[0][5],
'products': []
}

for row in results:
product_data = {
'sku': row[8],
'description': row[9],
'price': row[10],
'quantity': row[6],
'observation': row[7],
'category': row[11],
'stock': row[12],
'id': row[13]
}
order_data['products'].append(product_data)

return order_data

def insert_update(self, values: Dict[str, Any]):
stmt_order = insert(OrderTable).values({key: values[key] for key in values if key != 'products'})
Expand Down
4 changes: 3 additions & 1 deletion src/cart/adapters/pydantic_presenter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import List

from cart.domain.enums.order_status import OrderStatus
from src.api.presentation.shared.dtos.order_response_dto import OrderResponseDto, OrderProductResponseDto
from src.cart.domain.entities.order import Order
from src.cart.ports.cart_presenter import ICartPresenter
Expand All @@ -16,10 +17,11 @@ def present(self, output: Order | List[Order]) -> OrderResponseDto | List[OrderR
def formater(order):
if not order:
return {}

return OrderResponseDto(id=order.id,
user=order.user,
total_order=order.total_order,
order_status=order.order_status.name,
order_status=OrderStatus(order.order_status),
payment_condition=order.payment_condition,
products=[OrderProductResponseDto(
product=p.product.id,
Expand Down
4 changes: 0 additions & 4 deletions src/cart/domain/validators/order_product_validator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from src.cart.domain.domain_exception import OrderProductDomainException
from src.product.domain.entities.product import Product

TEXT_MAX_SIZE = 100

Expand All @@ -10,9 +9,6 @@ def validate(order_product):
if order_product.quantity <= 0:
raise OrderProductDomainException("Quantidade do produto precisa ser maior que zero")

if not isinstance(order_product.product, Product):
raise OrderProductDomainException("Produto precisa ser do tipo `Product`")

if order_product.product.price <= 0:
raise OrderProductDomainException("Preço do produto precisa ser maior que zero")

Expand Down

0 comments on commit 57e884b

Please sign in to comment.