@@ -22,31 +22,44 @@ def add_new_book():
22
22
request_data = request .get_json ()
23
23
try :
24
24
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
+
28
31
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" )
30
33
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" )
49
39
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
+
50
63
51
64
def get_by_title (book_title ):
52
65
resp = token_validator (request .headers .get ('Authorization' ))
@@ -77,4 +90,4 @@ def get_by_title(book_title):
77
90
}
78
91
return Response (json .dumps (responseObject ), 200 , mimetype = "application/json" )
79
92
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