-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbooks.py
69 lines (52 loc) · 1.99 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from fastapi import FastAPI
from fastapi import Body
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 BOOKS
@app.get('/books/{book_title}')
async def read_book(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 = []
for book in BOOKS:
if book.get('category').casefold() == category.casefold():
books_to_return.append(book)
return books_to_return
@app.get('/books/{book_author}/')
async def read_author_category_by_query(book_author: str, category: str):
books_to_return = []
for book in BOOKS:
if (
book.get('author').casefold() == book_author.casefold()
and book.get('category').casefold() == category.casefold()
):
books_to_return.append(book)
return books_to_return
@app.post('/books/create_book')
async def create(new_book=Body()):
BOOKS.append(new_book)
return new_book
@app.put('/books/update_book')
async def update_book(updated_book=Body()):
for i in range(len(BOOKS)):
if BOOKS[i].get('title').casefold() == updated_book.get('title').casefold():
BOOKS[i] = updated_book
return BOOKS[i]
@app.delete('/books/delete_book/{book_title}')
async def delete_book(book_title: str):
for book in BOOKS:
if book.get('title').casefold() == book_title.casefold():
BOOKS.remove(book)
return book