-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
49 lines (41 loc) · 1.38 KB
/
Copy pathmain.py
File metadata and controls
49 lines (41 loc) · 1.38 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
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from typing import Optional
app = FastAPI()
# Allow browser requests from file:// and localhost (needed for the Python Bootcamp simulator)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
class Book(BaseModel):
title: str
price: float
category: str
books_db = [
{"id": 1, "title": "Clean Code", "price": 29.99, "category": "tech"},
{"id": 2, "title": "Dune", "price": 14.99, "category": "sci-fi"},
{"id": 3, "title": "The Pragmatic Programmer", "price": 34.99, "category": "tech"},
]
@app.get("/")
def root():
return {"message": "Book Store API", "total": len(books_db)}
@app.get("/books")
def get_books(category: Optional[str] = None):
if category:
return [b for b in books_db if b["category"] == category]
return books_db
@app.post("/books", status_code=201)
def create_book(book: Book):
new_book = {"id": len(books_db) + 1, **book.dict()}
books_db.append(new_book)
return new_book
@app.delete("/books/{book_id}")
def delete_book(book_id: int):
book = next((b for b in books_db if b["id"] == book_id), None)
if not book:
raise HTTPException(status_code=404, detail="Book not found")
books_db.remove(book)
return {"deleted": book_id}