-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbooks.py
71 lines (53 loc) · 2.13 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
70
from fastapi import Body, FastAPI
app = FastAPI()
BOOKS = [
{"title": "Zebra", "author":"Benn Hill", "category": "science"},
{"title": "Ptica", "author":"Bill Astron", "category": "science"},
{"title": "Hrana", "author":"Jen Pal", "category": "math"},
{"title": "Tisina", "author":"Ivo Jima", "category": "math"},
{"title": "Njiva", "author":"Cahtie Hill", "category": "math"},
{"title": "Sela", "author":"Jen Pal", "category": "math"}
]
@app.get("/books") #get all the books from the list
async def getAllBooks():
return BOOKS
@app.get("/books/{title}") #http://127.0.0.1:8000/books/njiva (get book by title)
async def getSingleBook(title: str):
for book in BOOKS:
if book.get('title').casefold() == title.casefold():
return book
@app.get("/books/") #http://127.0.0.1:8000/books/?category=science (query with parameters)
async def 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/{author}/") #http://127.0.0.1:8000/books/Jen%20Pal/?category=math (query author and category)
async def author_category_by_query(author: str, category: str):
books_to_return= []
for book in BOOKS:
if book.get('author').casefold() == author.casefold() and \
book.get('category').casefold() == category.casefold():
books_to_return.append(book)
return books_to_return
#Create new book
@app.post("/books/create")
async def create(new_book=Body()):
BOOKS.append(new_book)
return 200
#Update book by title
@app.put("/books/update")
async def update_book(updated_book = Body()):
print("usao sam")
for i in range(len(BOOKS)):
if updated_book.get('title').casefold() == BOOKS[i].get('title').casefold():
BOOKS[i] = updated_book
return 200
#Delete book by title
@app.delete("/books/delete/{title}")
async def detele_book(title: str):
for i in range(len(BOOKS)):
if BOOKS[i].get('title').casefold() == title.casefold():
BOOKS.pop(i)
return 200