Skip to content

Commit 8536b23

Browse files
authored
Update books.py
1 parent cf49467 commit 8536b23

File tree

1 file changed

+36
-23
lines changed

1 file changed

+36
-23
lines changed

api_views/books.py

+36-23
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,44 @@ def add_new_book():
2222
request_data = request.get_json()
2323
try:
2424
jsonschema.validate(request_data, add_book_schema)
25-
except:
26-
return Response(error_message_helper("Please provide a proper JSON body."), 400, mimetype="application/json")
27-
resp = token_validator(request.headers.get('Authorization'))
25+
except jsonschema.exceptions.ValidationError as exc:
26+
return Response(error_message_helper(exc.message), 400, mimetype="application/json")
27+
28+
auth_header = request.headers.get('Authorization')
29+
resp = token_validator(auth_header)
30+
2831
if "expired" in resp:
29-
return Response(error_message_helper(resp), 401, mimetype="application/json")
32+
return Response(error_message_helper("Token expired. Please log in again."), 401, mimetype="application/json")
3033
elif "Invalid token" in resp:
31-
return Response(error_message_helper(resp), 401, mimetype="application/json")
32-
else:
33-
user = User.query.filter_by(username=resp).first()
34-
35-
# check if user already has this book title
36-
book = Book.query.filter_by(user=user, book_title=request_data.get('book_title')).first()
37-
if book:
38-
return Response(error_message_helper("Book Already exists!"), 400, mimetype="application/json")
39-
else:
40-
newBook = Book(book_title=request_data.get('book_title'), secret_content=request_data.get('secret'),
41-
user_id=user.id)
42-
db.session.add(newBook)
43-
db.session.commit()
44-
responseObject = {
45-
'status': 'success',
46-
'message': 'Book has been added.'
47-
}
48-
return Response(json.dumps(responseObject), 200, mimetype="application/json")
34+
return Response(error_message_helper("Invalid token. Please log in again."), 401, mimetype="application/json")
35+
36+
user = User.query.filter_by(username=resp).first()
37+
if not user:
38+
return Response(error_message_helper("User not found."), 404, mimetype="application/json")
4939

40+
# Check if the user already has this book title
41+
existing_book = Book.query.filter_by(user=user, book_title=request_data.get('book_title')).first()
42+
if existing_book:
43+
return Response(error_message_helper("Book already exists!"), 400, mimetype="application/json")
44+
45+
# Add the new book
46+
try:
47+
new_book = Book(
48+
book_title=request_data.get('book_title'),
49+
secret_content=request_data.get('secret'),
50+
user_id=user.id
51+
)
52+
db.session.add(new_book)
53+
db.session.commit()
54+
responseObject = {
55+
'status': 'success',
56+
'message': 'Book has been added.'
57+
}
58+
return Response(json.dumps(responseObject), 201, mimetype="application/json")
59+
except Exception as e:
60+
db.session.rollback()
61+
return Response(error_message_helper("An error occurred while adding the book."), 500, mimetype="application/json")
62+
5063

5164
def get_by_title(book_title):
5265
resp = token_validator(request.headers.get('Authorization'))
@@ -77,4 +90,4 @@ def get_by_title(book_title):
7790
}
7891
return Response(json.dumps(responseObject), 200, mimetype="application/json")
7992
else:
80-
return Response(error_message_helper("Book not found!"), 404, mimetype="application/json")
93+
return Response(error_message_helper("Book not found!"), 404, mimetype="application/json")

0 commit comments

Comments
 (0)