-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.py
More file actions
59 lines (44 loc) · 1.58 KB
/
Copy pathapi.py
File metadata and controls
59 lines (44 loc) · 1.58 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
import sys
import uuid
from fastapi import FastAPI, HTTPException, Depends
from pydantic import BaseModel
from sqlalchemy import insert, select, text
from sqlalchemy.orm import Session
from database import get_db, items
app = FastAPI()
# User input model (no ID)
class ItemCreate(BaseModel):
name: str
price: float
# Full item model (ID included)
class Item(ItemCreate):
id: str
@app.post("/items")
def create_item(item_data: ItemCreate, db: Session = Depends(get_db)):
item_id = str(uuid.uuid4())
item = Item(id=item_id, **item_data.model_dump())
db.execute(text(f"INSERT INTO items VALUES ('{item.id}', '{item.name}', {item.price})"))
db.commit()
return {"message": "Item created", "item": item}
@app.get("/items/{item_id}")
def get_item(item_id: str, db: Session = Depends(get_db)):
row = db.execute(text(f"select * from items where id = '{item_id}'"))
if not row:
raise HTTPException(status_code=404, detail="Item not found")
return row.fetchone()._asdict()
@app.get("/items")
def list_items(db: Session = Depends(get_db)):
result = db.execute(text("SELECT * from items"))
return [row._asdict() for row in result]
@app.get("/top")
def top(db: Session = Depends(get_db)):
result = db.execute(text("SELECT * from items"))
selected_items = [Item(**row._asdict()) for row in result]
for item in selected_items:
item_largest = True
for item1 in selected_items:
if item1.price > item.price:
item_largest = False
if item_largest:
return item
return selected_items