-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
79 lines (64 loc) · 2.11 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
72
73
74
75
76
77
78
79
from fastapi import FastAPI, HTTPException
from models import Book # Import the Book model
# Create an instance of FastAPI
app = FastAPI()
# In-memory data store for books (replace with a database in production)
books_db = {}
@app.get("/")
def read_root():
return {"message": "Welcome to the Book Store API"}
@app.post("/books/", response_model=Book)
def create_book(book: Book):
"""
Create a new book entry.
Parameters:
- book (Book) : The book data to be added.
Returns:
- book: A response message indicating success or failure.
"""
# FastAPI automatically validates the incoming 'book' data
# If the data is invalid, FastAPI will return a detailed error response
# If the data is valid, you can proceed to save it
books_db[book.isbn] = book
return book
@app.get("/books/{isbn}", response_model=Book)
def read_book(isbn: str):
"""
Retrieve book details by ISBN.
Parameters:
- isbn (str): The ISBN of the book to retrieve.
Returns:
- book: The book details or a message if the book is not found.
"""
# Fetch book data from your data store
book_data = books_db.get(isbn)
if book_data is None:
raise HTTPException(status_code=404, detail="Book not found")
return book_data
@app.put("/books/{isbn}", response_model=Book)
def update_book(isbn: str, updated_book: Book):
"""
Update book information by ISBN.
Parameters:
- isbn (str): The ISBN of the book to update.
- updated_book (Book): The updated book data.
Returns:
- dict: A response message indicating success or failure.
"""
if isbn not in books_db:
raise HTTPException(status_code=404, detail="Book not found")
books_db[isbn] = updated_book
return updated_book
@app.delete("/books/{isbn}")
def delete_book(isbn: str):
"""
Delete a book by ISBN.
Parameters:
- isbn (str): The ISBN of the book to delete.
Returns:
- Book: A detail of deleted book.
"""
if isbn not in books_db:
raise HTTPException(status_code=404, detail="Book not found")
deleted_book = books_db.pop(isbn)
return deleted_book