-
Notifications
You must be signed in to change notification settings - Fork 0
/
Library_management.py
245 lines (184 loc) · 5.74 KB
/
Library_management.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
"""
Functional Requirements for a Library Management System
The following are the functional requirements for a Library Management System:
Ability to add and remove books from the library *
Ability to search for books in the library by title, author, or ISBN *
Ability to check out and return books*
Ability to display a list of all books in the library *
Ability to store and retrieve information about library patrons, including their name and ID number*
Ability to track which books are currently checked out and when they are due to be returned
Ability to generate reports on library usage and checkouts
"""
from fastapi import FastAPI
from pydantic import BaseModel
from fastapi.responses import Response
from datetime import datetime
class BookSchema(BaseModel):
book_id: int
book_title: str
book_author_name: str
book_isbn: str
book_available: bool
class BookDto(BaseModel):
book_id: int | None
book_title: str
book_author_name: str
book_isbn: str
book_available: bool
class PetronSchema(BaseModel):
petrons_id: int
petrons_name: str
petrons_mail: str
class PetronDto(BaseModel):
petrons_id: int | None
petrons_name: str
petrons_mail: str
class ParchaseSchema(BaseModel):
parchase_id: int
petrons_id: int
book_id: int
class ParchaseDto(BaseModel):
parchase_id: int
petrons_id: int
book_id: int
app = FastAPI()
book_repo: list[BookSchema] = [
BookSchema(
book_id=1,
book_author_name="Martin",
book_isbn="123456",
book_title="Clean Code",
book_available=True,
),
BookSchema(
book_id=2,
book_author_name="Robert Luis",
book_isbn="345678",
book_title="Treasure Island",
book_available=True,
),
]
petrons_repo: list[PetronSchema] = [
PetronSchema(
petrons_id=1,
petrons_mail="nafisa@gmail.com",
petrons_name="Nafisa Tabassum",
),
PetronSchema(
petrons_id=2,
petrons_mail="tanha@gmail.com",
petrons_name="Tanha Mahajabin",
),
]
parchase_repo: list[ParchaseSchema] = []
max_book_id = 2
max_petron_id = 2
max_parchase_id = 0
def book_schema_to_dto(book: BookSchema):
return BookDto(
book_id=book.book_id if book.book_id else None,
book_title=book.book_title,
book_author_name=book.book_author_name,
book_isbn=book.book_isbn,
book_available=book.book_available,
)
@app.post("/books")
def add_books(book_data: BookDto):
global max_book_id
max_book_id += 1
book_repo.append(
BookSchema(
book_id=max_book_id,
book_author_name=book_data.book_author_name,
book_isbn=book_data.book_isbn,
book_title=book_data.book_title,
book_available=book_data.book_available,
)
)
return Response(status_code=201)
@app.delete("/books/{book_id}")
def remove_book(book_id: int):
for book in book_repo:
if book.book_id != book_id:
continue
book_repo.remove(book)
return Response(status_code=204)
return Response(status_code=404)
@app.get("/books/{book_id}")
def get_single_book(book_id: int):
for book in book_repo:
if book.book_id != book_id:
continue
return book_schema_to_dto(book)
return Response(status_code=404)
@app.get("/books")
def show_all_books():
result: list[BookDto] = []
for book in book_repo:
result.append(book_schema_to_dto(book))
return result
@app.get("/books/search")
def search_books(search_query: str | None = None):
result: list[BookDto] = []
for book in book_repo:
if search_query == None:
result.append(book_schema_to_dto(book))
continue
if (
book.book_author_name.find(search_query) != -1
or book.book_title.find(search_query) != -1
or book.book_isbn.find(search_query) != -1
):
result.append(book_schema_to_dto(book))
return result
@app.post("/parchase/{petrons_id}")
def check_out(parchase_data: ParchaseDto):
global max_parchase_id
for user in petrons_repo:
if user.petrons_id != parchase_data.petrons_id:
continue
for book in book_repo:
if book.book_id != parchase_data.book_id and book.book_available == False:
continue
max_parchase_id += 1
book.book_available = False
parchase_repo.append(
ParchaseSchema(
book_id=parchase_data.book_id,
petrons_id=parchase_data.petrons_id,
parchase_id=max_parchase_id,
)
)
return Response(status_code=201)
return Response(status_code=404)
@app.delete("/parchase/{parchase_id}")
def check_out(parchase_id: int):
for parchase in parchase_repo:
if parchase.parchase_id != parchase_id:
continue
parchase_repo.remove(parchase)
return Response(status_code=204)
return Response(status_code=404)
@app.post("/petrons")
def add_petrons(petrons_data: PetronDto):
global max_petron_id
max_petron_id += 1
petrons_repo.append(
PetronSchema(
petrons_id=max_petron_id,
petrons_mail=petrons_data.petrons_mail,
petrons_name=petrons_data.petrons_mail,
)
)
return Response(status_code=201)
@app.get("/petrons/{petrons_id}")
def show_petron(petrons_id: int):
for petron in petrons_repo:
if petron.petrons_id != petrons_id:
continue
return PetronDto(
petrons_id=petron.petrons_id,
petrons_name=petron.petrons_name,
petrons_mail=petron.petrons_mail,
)
return Response(status_code=404)