1
1
from flask import Blueprint , request , jsonify , g
2
2
from flask_restful import Api , Resource # used for REST API building
3
3
from api .jwt_authorize import token_required
4
- from model .binaryLearningGame import binaryLearningGameScores
4
+ from model .binaryLearningGame import BinaryLearningGameScores
5
5
6
6
"""
7
7
This Blueprint object is used to define APIs for the Post model.
@@ -28,28 +28,44 @@ class BinaryLearningGameScoresAPI:
28
28
- delete: delete a post
29
29
"""
30
30
class _CRUD (Resource ):
31
+
31
32
@token_required ()
32
33
def post (self ):
33
34
# Obtain the current user from the token
34
35
current_user = g .current_user
35
36
# Obtain the request data sent by the RESTful client API
36
37
data = request .get_json ()
37
38
# Create a new post object using the data from the request
38
- post = binaryLearningGameScores (data ['username' ], current_user .id , data ['score' ], data ['difficulty' ])
39
+ post = BinaryLearningGameScores (data ['username' ], current_user .id , data ['score' ], data ['difficulty' ])
39
40
# Save the post object using the ORM method defined in the model
40
41
post .create ()
41
42
# Return response to the client in JSON format
42
43
return jsonify (post .read ())
43
-
44
+
45
+ @token_required ()
44
46
def get (self ):
45
47
# Obtain the current user
46
48
# current_user = g.current_user
47
49
# Find all the posts by the current user
48
- posts = binaryLearningGameScores .query .all ()
50
+ posts = BinaryLearningGameScores .query .all ()
49
51
# Prepare a JSON list of all the posts, uses for loop shortcut called list comprehension
50
52
json_ready = [post .read () for post in posts ]
51
53
# Return a JSON list, converting Python dictionaries to JSON format
52
54
return jsonify (json_ready )
55
+
56
+ @token_required ("Admin" )
57
+ def put (self ):
58
+ """
59
+ Update a section.
60
+ """
61
+ # Obtain the request data sent by the RESTful client API
62
+ data = request .get_json ()
63
+ # Find the section to update
64
+ updatedScoreData = BinaryLearningGameScores .query .get (data ['id' ])
65
+ # Save the section object using the Object Relational Mapper (ORM) method defined in the model
66
+ updatedScoreData .update ({'user_score' : data ['user_score' ], 'user_difficulty' : data ['user_difficulty' ]})
67
+ # Return a JSON restful response to the client
68
+ return jsonify (updatedScoreData .read ())
53
69
54
70
@token_required ()
55
71
def delete (self ):
@@ -58,7 +74,7 @@ def delete(self):
58
74
# Obtain the request data
59
75
data = request .get_json ()
60
76
# Find the current post from the database table(s)
61
- post = binaryLearningGameScores .query .get (data ['id' ])
77
+ post = BinaryLearningGameScores .query .get (data ['id' ])
62
78
# Delete the post using the ORM method defined in the model
63
79
post .delete ()
64
80
# Return response
0 commit comments