-
Notifications
You must be signed in to change notification settings - Fork 0
/
books.py
46 lines (34 loc) · 1.36 KB
/
books.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
from typing import Dict, List
from fastapi import FastAPI
app = FastAPI()
BOOKS = [
{"title": "Title One", "author": "Author One", "category": "science"},
{"title": "Title Two", "author": "Author Two", "category": "science"},
{"title": "Title Three", "author": "Author Three", "category": "history"},
{"title": "Title Four", "author": "Author Four", "category": "math"},
{"title": "Title Five", "author": "Author Five", "category": "math"},
{"title": "Title Six", "author": "Author Two", "category": "math"},
]
@app.get("/books")
async def read_all_books():
return {"book_title": "My favourite Book"}
@app.get("/books/{book_title}")
async def read_all_books(book_title: str):
for book in BOOKS:
if book.get("title").casefold() == book_title.casefold():
return book
@app.get("/books/")
async def read_category_by_query(category: str):
books_to_return = [
book for book in BOOKS if book.get("category").casefold() == category.casefold()
]
return books_to_return
@app.get("/books/{book_author}/")
async def read_author_category_by_query(book_author: str, category: str) -> List[Dict]:
book_to_return = [
book
for book in BOOKS
if book.get("author").casefold() == book_author.casefold()
and book.get("category").casefold() == category.casefold()
]
return book_to_return