
- Set up FastAPI project structure with SQLite and SQLAlchemy - Create models for users, books, authors, categories, and orders - Implement JWT authentication and authorization - Add CRUD endpoints for all resources - Set up Alembic for database migrations - Add health check endpoint - Add proper error handling and validation - Create comprehensive documentation
68 lines
1.3 KiB
Python
68 lines
1.3 KiB
Python
from pydantic import BaseModel, field_validator
|
|
from typing import List, Optional
|
|
from datetime import datetime
|
|
from app.models.order import OrderStatus
|
|
|
|
|
|
class OrderItemBase(BaseModel):
|
|
book_id: int
|
|
quantity: int
|
|
unit_price: float
|
|
|
|
@field_validator("quantity")
|
|
def quantity_must_be_positive(cls, v):
|
|
if v <= 0:
|
|
raise ValueError("Quantity must be greater than zero")
|
|
return v
|
|
|
|
@field_validator("unit_price")
|
|
def unit_price_must_be_positive(cls, v):
|
|
if v <= 0:
|
|
raise ValueError("Unit price must be greater than zero")
|
|
return v
|
|
|
|
|
|
class OrderItemCreate(OrderItemBase):
|
|
pass
|
|
|
|
|
|
class OrderItem(OrderItemBase):
|
|
id: int
|
|
order_id: int
|
|
created_at: datetime
|
|
updated_at: datetime
|
|
|
|
class Config:
|
|
from_attributes = True
|
|
|
|
|
|
class OrderBase(BaseModel):
|
|
shipping_address: str
|
|
|
|
|
|
class OrderCreate(OrderBase):
|
|
items: List[OrderItemCreate]
|
|
|
|
|
|
class OrderUpdate(BaseModel):
|
|
shipping_address: Optional[str] = None
|
|
status: Optional[OrderStatus] = None
|
|
|
|
|
|
class Order(OrderBase):
|
|
id: int
|
|
user_id: int
|
|
total_amount: float
|
|
status: OrderStatus
|
|
items: List[OrderItem]
|
|
created_at: datetime
|
|
updated_at: datetime
|
|
|
|
class Config:
|
|
from_attributes = True
|
|
|
|
|
|
class OrderList(BaseModel):
|
|
total: int
|
|
items: List[Order]
|