-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfastapi_main.py
More file actions
149 lines (125 loc) · 4.55 KB
/
fastapi_main.py
File metadata and controls
149 lines (125 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
from fastapi import FastAPI, Depends, HTTPException, status
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from typing import List, Optional
import uvicorn
import os
import sys
# Add the project directory to the Python path
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
# Import Django models and settings
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'bazaarmate.settings')
import django
django.setup()
from accounts.models import User
from products.models import Category, Product
from orders.models import Order
from reviews.models import Review
app = FastAPI(
title="BazaarMate FastAPI",
description="FastAPI endpoints for BazaarMate e-commerce platform",
version="1.0.0"
)
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# OAuth2 scheme
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
# Pydantic models
class UserBase(BaseModel):
username: str
email: str
role: str
class UserCreate(UserBase):
password: str
class UserResponse(UserBase):
id: int
class CategoryBase(BaseModel):
name: str
description: Optional[str] = None
class CategoryCreate(CategoryBase):
pass
class CategoryResponse(CategoryBase):
id: int
class ProductBase(BaseModel):
title: str
description: str
price: float
stock: int
category_id: int
image_url: Optional[str] = None
class ProductCreate(ProductBase):
pass
class ProductResponse(ProductBase):
id: int
category_name: str
seller_name: str
# Authentication dependency
async def get_current_user(token: str = Depends(oauth2_scheme)):
# In a real implementation, you would verify the token
# For now, we'll just return a mock user
return UserResponse(id=1, username="testuser", email="test@example.com", role="buyer")
# Auth endpoints
@app.post("/auth/register/", response_model=UserResponse)
async def register_user(user: UserCreate):
# In a real implementation, you would create the user in the database
return UserResponse(id=1, username=user.username, email=user.email, role="buyer")
@app.post("/auth/login/")
async def login(form_data: OAuth2PasswordRequestForm = Depends()):
# In a real implementation, you would verify credentials and return a token
return {"access_token": "mock_token", "token_type": "bearer"}
# Category endpoints
@app.get("/categories/", response_model=List[CategoryResponse])
async def list_categories(skip: int = 0, limit: int = 100):
# In a real implementation, you would query the database
categories = [
CategoryResponse(id=1, name="Electronics", description="Electronic devices"),
CategoryResponse(id=2, name="Clothing", description="Apparel and accessories"),
]
return categories
@app.post("/categories/", response_model=CategoryResponse)
async def create_category(category: CategoryCreate, current_user: UserResponse = Depends(get_current_user)):
# In a real implementation, you would save to the database
return CategoryResponse(id=3, name=category.name, description=category.description)
# Product endpoints
@app.get("/products/", response_model=List[ProductResponse])
async def list_products(skip: int = 0, limit: int = 100, category_id: Optional[int] = None):
# In a real implementation, you would query the database
products = [
ProductResponse(
id=1,
title="Smartphone",
description="Latest model smartphone",
price=699.99,
stock=50,
category_id=1,
category_name="Electronics",
seller_name="Tech Store",
image_url="https://example.com/smartphone.jpg"
),
]
return products
@app.post("/products/", response_model=ProductResponse)
async def create_product(product: ProductCreate, current_user: UserResponse = Depends(get_current_user)):
# In a real implementation, you would save to the database
category = CategoryResponse(id=product.category_id, name="Electronics", description="Electronic devices")
return ProductResponse(
id=2,
title=product.title,
description=product.description,
price=product.price,
stock=product.stock,
category_id=product.category_id,
category_name=category.name,
seller_name="Tech Store",
image_url=product.image_url
)
# Run the application
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8001)