-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
71 lines (59 loc) · 1.63 KB
/
main.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
from fastapi import FastAPI
from pydantic import BaseModel
from typing import List #, Optional
app = FastAPI()
class Book(BaseModel):
title: str
id: int
author: str
description: str | None = None # description: Optional[str] = None
books = []
@app.get("/books/")
def initial():
"""
Initial homepage message
"""
return {"message": "Welcome to the Bookstore!"}
@app.post("/books/add/", response_model=Book)
def add_book(book: Book):
"""
Add a new book to the bookstore
"""
books.append(book)
return books
@app.get("books/all/", response_model=List[Book])
def get_books():
"""
Get all books in the bookstore
"""
return books
@app.get("/books/{book_id}/", response_model=Book)
def get_book(book_id: int):
"""
Get a book by its ID
"""
for book in books:
if book["id"] == book_id:
return book
return {"error": "Book not found!"}
@app.put("/books/{book_id}/", response_model=Book)
def update_book(book_id: int, book: Book):
"""
Update a book by its ID, given another book object
"""
for index, item in enumerate(books):
if item.id == book_id:
books[index] = book
return book
return {"error": "Book not found!"}
@app.delete("/books/{book_id}/")
def delete_book(book_id: int):
"""
Delete a book by its ID
"""
global books
books = [book for book in books if book.id != book_id]
return {"message": "Book deleted successfully!"}
# Run the app
# uvicorn main:app --reload
# to view the developer docs for this app, visit http://127.0.0.1:8000/docs or http://http://127.0.0.1:8000/redoc