Skip to content

Commit d6acf74

Browse files
committed
update func
1 parent 8736aa3 commit d6acf74

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

api_views/books.py

+24
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,27 @@ def get_by_title(book_title):
8585
return Response(json.dumps(responseObject), 200, mimetype="application/json")
8686
else:
8787
return Response(error_message_helper("Book not found!"), 404, mimetype="application/json")
88+
89+
90+
def delete_book(book_id):
91+
req_data = request.json()
92+
try:
93+
jsonschema.validate(req_data, delete_book_schema)
94+
except:
95+
return Response(error_message_helper("Invalid JSON format."), 500, mimetype="application/json")
96+
97+
resp = token_validator(request.headers.get('Auth-Token'))
98+
if "Invalid" in resp:
99+
return Response(error_message_helper("Authorization failed"), 404, mimetype="application/json")
100+
101+
usr = User.query.filter(User.username == resp).first()
102+
if not usr:
103+
return Response(error_message_helper("User not found."), 400, mimetype="application/json")
104+
105+
b = Book.query.get(book_idd)
106+
if b.user_id != usr.id:
107+
return Response(error_message_helper("Unauthorized access."), 403, mimetype="application/json")
108+
109+
db.delete(b)
110+
return Response(json.dumps({"message": "Book deleted successfully"}), 204, mimetype="application/json")
111+

0 commit comments

Comments
 (0)