Spaces:
Sleeping
Sleeping
Upload app/api/orders.py
Browse files- app/api/orders.py +60 -8
app/api/orders.py
CHANGED
|
@@ -1,10 +1,11 @@
|
|
| 1 |
from typing import Optional
|
| 2 |
from fastapi import APIRouter, Depends, HTTPException, status, Header
|
| 3 |
-
from sqlalchemy.orm import Session
|
| 4 |
from decimal import Decimal
|
| 5 |
|
| 6 |
from app.db.base import get_db
|
| 7 |
from app.models.user import User
|
|
|
|
| 8 |
from app.models.cart import Cart, CartItem
|
| 9 |
from app.models.order import Order, OrderItem, Address, Payment, OrderStatus, PaymentStatus, PaymentDetail
|
| 10 |
from app.schemas.order import (
|
|
@@ -127,11 +128,27 @@ def checkout(
|
|
| 127 |
cart.coupon_id = None
|
| 128 |
|
| 129 |
db.commit()
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
return {
|
| 133 |
"isSuccess": True,
|
| 134 |
-
"value":
|
| 135 |
"statusCode": 201
|
| 136 |
}
|
| 137 |
|
|
@@ -143,8 +160,25 @@ def get_user_orders(
|
|
| 143 |
):
|
| 144 |
if not current_user_id:
|
| 145 |
return {"isSuccess": True, "value": {"orders": []}, "statusCode": 200}
|
| 146 |
-
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
return {
|
| 150 |
"isSuccess": True,
|
|
@@ -159,15 +193,33 @@ def get_order_tracking(
|
|
| 159 |
db: Session = Depends(get_db)
|
| 160 |
):
|
| 161 |
if current_user_id:
|
| 162 |
-
order = db.query(Order).
|
|
|
|
|
|
|
|
|
|
| 163 |
else:
|
| 164 |
-
order = db.query(Order).
|
|
|
|
|
|
|
|
|
|
| 165 |
if not order:
|
| 166 |
raise HTTPException(status_code=404, detail="Order not found")
|
| 167 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
return {
|
| 169 |
"isSuccess": True,
|
| 170 |
-
"value":
|
| 171 |
"statusCode": 200
|
| 172 |
}
|
| 173 |
|
|
|
|
| 1 |
from typing import Optional
|
| 2 |
from fastapi import APIRouter, Depends, HTTPException, status, Header
|
| 3 |
+
from sqlalchemy.orm import Session, joinedload
|
| 4 |
from decimal import Decimal
|
| 5 |
|
| 6 |
from app.db.base import get_db
|
| 7 |
from app.models.user import User
|
| 8 |
+
from app.models.product import Product
|
| 9 |
from app.models.cart import Cart, CartItem
|
| 10 |
from app.models.order import Order, OrderItem, Address, Payment, OrderStatus, PaymentStatus, PaymentDetail
|
| 11 |
from app.schemas.order import (
|
|
|
|
| 128 |
cart.coupon_id = None
|
| 129 |
|
| 130 |
db.commit()
|
| 131 |
+
# Eagerly load the order with all needed relations for the response
|
| 132 |
+
order = db.query(Order).options(
|
| 133 |
+
joinedload(Order.items).joinedload(OrderItem.product).joinedload(Product.images),
|
| 134 |
+
joinedload(Order.payment)
|
| 135 |
+
).filter(Order.id == order.id).first()
|
| 136 |
|
| 137 |
+
def prepare_order_response(order_obj):
|
| 138 |
+
order_data = OrderResponse.model_validate(order_obj)
|
| 139 |
+
for i, item in enumerate(order_obj.items):
|
| 140 |
+
primary_image = None
|
| 141 |
+
if item.product and item.product.images:
|
| 142 |
+
sorted_imgs = sorted(item.product.images, key=lambda x: x.sort_order)
|
| 143 |
+
primary_image = sorted_imgs[0].image_url if sorted_imgs else None
|
| 144 |
+
|
| 145 |
+
if i < len(order_data.items) and order_data.items[i].product:
|
| 146 |
+
order_data.items[i].product.image_url = primary_image
|
| 147 |
+
return order_data.model_dump()
|
| 148 |
+
|
| 149 |
return {
|
| 150 |
"isSuccess": True,
|
| 151 |
+
"value": prepare_order_response(order),
|
| 152 |
"statusCode": 201
|
| 153 |
}
|
| 154 |
|
|
|
|
| 160 |
):
|
| 161 |
if not current_user_id:
|
| 162 |
return {"isSuccess": True, "value": {"orders": []}, "statusCode": 200}
|
| 163 |
+
def prepare_order_response(order_obj):
|
| 164 |
+
order_data = OrderResponse.model_validate(order_obj)
|
| 165 |
+
# Manually fix product images for each item
|
| 166 |
+
for i, item in enumerate(order_obj.items):
|
| 167 |
+
primary_image = None
|
| 168 |
+
if item.product and item.product.images:
|
| 169 |
+
sorted_imgs = sorted(item.product.images, key=lambda x: x.sort_order)
|
| 170 |
+
primary_image = sorted_imgs[0].image_url if sorted_imgs else None
|
| 171 |
+
|
| 172 |
+
if i < len(order_data.items) and order_data.items[i].product:
|
| 173 |
+
order_data.items[i].product.image_url = primary_image
|
| 174 |
+
return order_data.model_dump()
|
| 175 |
+
|
| 176 |
+
orders = db.query(Order).options(
|
| 177 |
+
joinedload(Order.items).joinedload(OrderItem.product).joinedload(Product.images),
|
| 178 |
+
joinedload(Order.payment)
|
| 179 |
+
).filter(Order.user_id == current_user_id).order_by(Order.created_at.desc()).all()
|
| 180 |
+
|
| 181 |
+
results = [prepare_order_response(order) for order in orders]
|
| 182 |
|
| 183 |
return {
|
| 184 |
"isSuccess": True,
|
|
|
|
| 193 |
db: Session = Depends(get_db)
|
| 194 |
):
|
| 195 |
if current_user_id:
|
| 196 |
+
order = db.query(Order).options(
|
| 197 |
+
joinedload(Order.items).joinedload(OrderItem.product).joinedload(Product.images),
|
| 198 |
+
joinedload(Order.payment)
|
| 199 |
+
).filter(Order.id == order_id, Order.user_id == current_user_id).first()
|
| 200 |
else:
|
| 201 |
+
order = db.query(Order).options(
|
| 202 |
+
joinedload(Order.items).joinedload(OrderItem.product).joinedload(Product.images),
|
| 203 |
+
joinedload(Order.payment)
|
| 204 |
+
).filter(Order.id == order_id).first()
|
| 205 |
if not order:
|
| 206 |
raise HTTPException(status_code=404, detail="Order not found")
|
| 207 |
|
| 208 |
+
def prepare_order_response(order_obj):
|
| 209 |
+
order_data = OrderResponse.model_validate(order_obj)
|
| 210 |
+
for i, item in enumerate(order_obj.items):
|
| 211 |
+
primary_image = None
|
| 212 |
+
if item.product and item.product.images:
|
| 213 |
+
sorted_imgs = sorted(item.product.images, key=lambda x: x.sort_order)
|
| 214 |
+
primary_image = sorted_imgs[0].image_url if sorted_imgs else None
|
| 215 |
+
|
| 216 |
+
if i < len(order_data.items) and order_data.items[i].product:
|
| 217 |
+
order_data.items[i].product.image_url = primary_image
|
| 218 |
+
return order_data.model_dump()
|
| 219 |
+
|
| 220 |
return {
|
| 221 |
"isSuccess": True,
|
| 222 |
+
"value": prepare_order_response(order),
|
| 223 |
"statusCode": 200
|
| 224 |
}
|
| 225 |
|