-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
260 lines (214 loc) · 7.99 KB
/
api.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
from flask import Flask, request, jsonify
from flask_restful import Api
from database import Database
from bson import json_util, ObjectId
import json
from datetime import date, datetime
app = Flask(__name__)
api = Api(app)
db = Database()
collection = db.booksCollection()
@app.route('/books/<id>', methods = ['GET'])
def getBook(id):
try:
data = collection.find_one({'_id': ObjectId(str(id))})
return json.loads(json_util.dumps(data))
except:
return jsonify("404 Not Found")
@app.route('/books', methods = ['GET'])
def getFilteredBooks():
args = request.args
name = args.get('name')
min_price = args.get('min_price')
max_price = args.get('max_price')
category = args.get('category')
where = {}
if name:
where = {"name": {"$regex" : name, "$options" : 'i'} }
if min_price and max_price:
where["rentPerDay"] = { "$gte": int(min_price), "$lte": int(max_price) }
elif min_price:
where["rentPerDay"] = { "$gte": int(min_price) }
elif max_price:
where["rentPerDay"] = { "$lte": int(max_price) }
if category:
where["category"] = {"$regex" : category, "$options" :'i'}
try:
book = collection.find(where)
return json.loads(json_util.dumps(book))
except:
return jsonify("404 Not Found")
transactions_collection = db.transactionsCollection()
@app.route('/transactions/<id>', methods = ['GET'])
def getTransactionById(id):
try:
data = transactions_collection.find_one({'_id': ObjectId(str(id))})
result = json.loads(json_util.dumps(data))
return result
except:
return jsonify("404 Not Found")
@app.route('/transactions', methods = ['GET'])
def getTransactions():
args = request.args
book_name = args.get('book_name')
person_name = args.get('person_name')
start_from = args.get('start_from')
end_to = args.get('end_to')
where = {}
if book_name and person_name:
where = {"book_name": {"$regex" : book_name, "$options" : 'i'} }
where = {"person_name": {"$regex" : person_name, "$options" : 'i'} }
elif book_name :
where = {"book_name": {"$regex" : book_name, "$options" : 'i'} }
elif person_name:
where = {"person_name": {"$regex" : person_name, "$options" : 'i'} }
if start_from and end_to:
where["issued_date"] = { "$gte": start_from, "$lte": end_to }
elif start_from:
where["issued_date"] = { "$gte": int(start_from) }
elif end_to:
where["issued_date"] = { "$lte": int(end_to) }
try:
data = transactions_collection.find(where)
result = json.loads(json_util.dumps(data))
return result
except:
return jsonify("404 Not Found")
@app.route('/transactions', methods = ['POST'])
def issued():
data = request.get_json()
bookName = data['book_name']
personName = data['person_name']
currentDateTime = datetime.now()
data['issued_date'] = currentDateTime.strftime("%Y/%m/%d %H:%M:%S")
issuedDate = data['issued_date']
try:
if request.method == 'POST':
transactions_collection.insert_one(data)
response = jsonify(bookName, personName, issuedDate)
response.status_code = 201
return response
except:
return jsonify("404 Not Found")
@app.route('/transactions', methods = ["PATCH"])
def returnDate():
data = request.get_json()
book_name = data['book_name']
person_name = data['person_name']
filter = { 'book_name': book_name , 'person_name': person_name }
try:
outputs = getTransactions()
for output in outputs:
if output['book_name'] == book_name and output['person_name'] == person_name:
bookName = output['book_name']
personName = output['person_name']
issuedDate = output['issued_date']
returnDate = output['return_date']
break
issuedDate = issuedDate[0:10]
lst1 = issuedDate.split('/' or '/0')
year1 = int(lst1[0])
month1 = int(lst1[1])
day1 = int(lst1[2])
date1 = date(year1,month1,day1)
returnDate = returnDate[0:10]
lst2 = returnDate.split('/' or '/0')
year2 = int(lst2[0])
month2 = int(lst2[1])
day2 = int(lst2[2])
date2 = date(year2,month2,day2)
days = str(date2 - date1)
days = days.split(' ')
days = int(days[0])
days = days
books = getFilteredBooks()
for i in range(len(books)):
if books[i]['name'] == bookName:
rentPerDay = books[i]['rentPerDay']
rent = days * rentPerDay
data['total_rent'] = rent
total_rent = data['total_rent']
currentDateTime = datetime.now()
data['return_date'] = currentDateTime.strftime("%Y/%m/%d %H:%M:%S")
return_date = data['return_date']
newvalue = { "$set": { 'return_date': return_date, 'total_rent': total_rent } }
transactions_collection.update_one(filter, newvalue)
return jsonify({"book name" : bookName,
"person name": personName,
"issued date": issuedDate,
"return date": returnDate,
"days": days,
"rent per day": rentPerDay,
"total rent": rent})
except:
return jsonify('404 Not Found')
@app.route('/persons', methods = ['GET'])
def getPersonList():
args = request.args
book_name = args.get('book_name')
try:
where = {}
if book_name:
where = {"book_name": {"$regex" : book_name, "$options" : 'i'} }
data = transactions_collection.find(where)
results = json.loads(json_util.dumps(data))
book = results[0]['book_name']
count = 0
persons = []
currently_person_who_issued_book_count = 0
currently_person_who_issued_book = []
for result in results:
person = result['person_name']
persons.append(person)
count += 1
if 'return_date' not in result:
person = result['person_name']
currently_person_who_issued_book.append(person)
currently_person_who_issued_book_count += 1
return jsonify({
"Book Name": book,
"All Persons ": persons,
"Total Person Count": count,
"Currently Person Who Issued Book": currently_person_who_issued_book,
"Currently Total Person Who Issued Book": currently_person_who_issued_book_count
})
except:
return jsonify("404 Not Found")
@app.route('/rent', methods = ['GET'])
def getRent():
args = request.args
book_name = args.get('book_name')
try:
where = {}
if book_name:
where = {"book_name": {"$regex" : book_name, "$options" : 'i'} }
data = transactions_collection.find(where)
results = json.loads(json_util.dumps(data))
book = results[0]['book_name']
total_rent = 0
for result in results:
rent = result['total_rent']
total_rent += rent
return jsonify({"Book Name": book, "Total Rent Generated By Book": total_rent})
except:
return jsonify("404 Not Found")
@app.route('/issuedBooks', methods = ['GET'])
def getBooksByPersonName():
args = request.args
book_name = args.get('person_name')
try:
where = {}
if book_name:
where = {"person_name": {"$regex" : book_name, "$options" : 'i'} }
data = transactions_collection.find(where)
results = json.loads(json_util.dumps(data))
person = results[0]['person_name']
books = []
for result in results:
book = result['book_name']
books.append(book)
return jsonify({"Person Name": person, "All Issued Books": books})
except:
return jsonify("404 Not Found")
if __name__ == '__main__':
app.run(debug=True)