Repository navigation
Expand file tree
/
Copy pathhw-post-methode.py
More file actions
99 lines (85 loc) · 2.05 KB
/
Copy pathhw-post-methode.py
File metadata and controls
99 lines (85 loc) · 2.05 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
data = [
{
"title": "Nondito Noroke",
"author": "Humayun Ahmed",
"year": 1972,
"price": 2.50,
},
{
"title": "Hajar Bochor Dhore",
"author": "Zahir Raihan",
"year": 1964,
"price": 3.20,
},
{
"title": "A Golden Age",
"author": "Tahmima Anam",
"year": 2007,
"price": 14.95,
},
{
"title": "Chilerkothar Sepai",
"author": "Akhteruzzaman Elias",
"year": 1986,
"price": 6.50,
},
{
"title": "Lalsalu",
"author": "Syed Waliullah",
"year": 1948,
"price": 4.00,
},
{
"title": "Jochna O Jononir Golpo",
"author": "Humayun Ahmed",
"year": 2004,
"price": 8.50,
},
{
"title": "In the Light of What We Know",
"author": "Zia Haider Rahman",
"year": 2014,
"price": 16.99,
},
{
"title": "Amar Bondhu Rashed",
"author": "Muhammad Zafar Iqbal",
"year": 1996,
"price": 3.50,
},
{
"title": "Sultana's Dream",
"author": "Begum Rokeya Sakhawat Hossain",
"year": 1905,
"price": 1.99,
},
]
@app.get("/")
def home():
return {"Message": "Welcome to Book Collection API"}
# Schema Format
class BookCreate(BaseModel):
title: str
author: str
year: int
price: float
@app.post("/books")
def add_book(book: BookCreate):
data.append(book.model_dump())
return {"Message": "Book successfully added to the collection!"}
@app.get("/get/data")
def get_data():
return data
# Query parameter -> for year check ups
@app.get("/get_books_by_year")
def get_books_by_year(year: int):
books = []
for i in data:
if i["year"] == year:
books.append(i)
if len(books) == 0:
return {"Message": "Book not found!"}
return books