-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfast_api.py
74 lines (61 loc) · 2.88 KB
/
fast_api.py
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
from fastapi import FastAPI, HTTPException,Body
from typing import List,Optional
from models import Book
import database
from pydantic import BaseModel
class SellBook(BaseModel):
quantity: int
app = FastAPI()
@app.get("/")
async def read_root():
return {"message": "Welcome to my online bookstore API!"}
@app.post("/books", response_description="Add new book", response_model=Book)
async def create_book(book: Book):
book = await database.add_book(book.dict())
return book
@app.get("/books", response_description="List all books", response_model=List[Book])
async def list_books():
books = await database.retrieve_books()
return books
@app.get("/books/{id}", response_description="Get a single book", response_model=Book)
async def show_book(id: str):
book = await database.retrieve_book(id)
if book is None:
raise HTTPException(status_code=404, detail=f"Book {id} not found")
return book
@app.put("/books/{id}", response_description="Update a book", response_model=Book)
async def update_book(id: str, book: Book):
book = {k: v for k, v in book.dict().items() if v is not None}
updated_book = await database.update_book(id, book)
if updated_book:
return await database.retrieve_book(id)
else:
raise HTTPException(status_code=404, detail=f"Book {id} not found")
@app.delete("/books/{id}", response_description="Delete a book")
async def delete_book(id: str):
delete_result = await database.delete_book(id)
if delete_result:
return {"msg": f"Book {id} deleted"}
else:
raise HTTPException(status_code=404, detail=f"Book {id} not found")
@app.get("/search", response_description="Search books", response_model=List[Book])
async def search_books(title: Optional[str] = None, author: Optional[str] = None, min_price: Optional[float] = None, max_price: Optional[float] = None):
books = await database.search_books(title, author, min_price, max_price)
return books
@app.put("/sell_book/{id}", response_description="Sell a book")
async def sell_book(id: str, sell_book: SellBook):
print(f"Received request to sell {sell_book.quantity} of book {id}")
sell_result = await database.sell_book(id, sell_book.quantity)
if sell_result:
return {"msg": f"Sold {sell_book.quantity} copies of book {id}"}
else:
raise HTTPException(status_code=404, detail=f"Book {id} not found or not enough stock")
@app.get("/books_count", response_description="Count all books")
async def count_books():
return {"total_books": await database.count_books()}
@app.get("/best_selling_book", response_description="Get the best selling book", response_model=Book)
async def get_best_selling_book():
book = await database.best_selling_book()
if book is None:
raise HTTPException(status_code=404, detail="No books found")
return book