1
+ import jwt
2
+ from flask import Blueprint , request , jsonify , current_app , Response , g
3
+ from flask_restful import Api , Resource
4
+ from datetime import datetime
5
+ from __init__ import app
6
+ from api .jwt_authorize import token_required
7
+ from model .lgate import lgate
8
+
9
+ """
10
+ This Blueprint object is used to define APIs for the Post model.
11
+ - Blueprint is used to modularize application files.
12
+ - This Blueprint is registered to the Flask app in main.py.
13
+ """
14
+ lgate = Blueprint ('lgate' , __name__ , url_prefix = '/api' )
15
+
16
+ """
17
+ The Api object is connected to the Blueprint object to define the API endpoints.
18
+ - The API object is used to add resources to the API.
19
+ - The objects added are mapped to code that contains the actions for the API.
20
+ - For more information, refer to the API docs: https://flask-restful.readthedocs.io/en/latest/api.html
21
+ """
22
+ api = Api (lgate_api )
23
+
24
+ class lgateAPI :
25
+ """
26
+ Define the API CRUD endpoints for the Post model.
27
+ There are four operations that correspond to common HTTP methods:
28
+ - post: create a new post
29
+ - get: read posts
30
+ - put: update a post
31
+ - delete: delete a post
32
+ """
33
+ class _CRUD (Resource ):
34
+ @token_required ()
35
+ def post (self ):
36
+ # Obtain the current user from the token required setting in the global context
37
+ current_user = g .current_user
38
+ # Obtain the request data sent by the RESTful client API
39
+ data = request .get_json ()
40
+ # Create a new post object using the data from the request
41
+ post = lgate (data ['name' ], data ['score' ], current_user .id )
42
+ # Save the post object using the Object Relational Mapper (ORM) method defined in the model
43
+ post .create ()
44
+ # Return response to the client in JSON format, converting Python dictionaries to JSON format
45
+ return jsonify (post .read ())
46
+
47
+ @token_required ()
48
+ def get (self ):
49
+ # Obtain the current user
50
+ current_user = g .current_user
51
+ # Find all the posts by the current user
52
+ posts = lgate .query .filter (lgate ._user_id == current_user .id ).all ()
53
+ # Prepare a JSON list of all the posts, uses for loop shortcut called list comprehension
54
+ json_ready = [post .read () for post in posts ]
55
+ # Return a JSON list, converting Python dictionaries to JSON format
56
+ return jsonify (json_ready )
57
+
58
+ @token_required ()
59
+ def put (self ):
60
+ # Obtain the current user
61
+ current_user = g .current_user
62
+ # Obtain the request data
63
+ data = request .get_json ()
64
+ # Find the current post from the database table(s)
65
+ post = lgate .query .get (data ['id' ])
66
+ # Update the post
67
+ post ._name = data ['name' ]
68
+ post ._score = data ['score' ]
69
+ # Save the post
70
+ post .update ()
71
+ # Return response
72
+ return jsonify (post .read ())
73
+
74
+ @token_required ()
75
+ def delete (self ):
76
+ # Obtain the current user
77
+ current_user = g .current_user
78
+ # Obtain the request data
79
+ data = request .get_json ()
80
+ # Find the current post from the database table(s)
81
+ post = lgate .query .get (data ['id' ])
82
+ # Delete the post using the ORM method defined in the model
83
+ post .delete ()
84
+ # Return response
85
+ return jsonify ({"message" : "Post deleted" })
86
+
87
+ """
88
+ Map the _CRUD class to the API endpoints for /post.
89
+ - The API resource class inherits from flask_restful.Resource.
90
+ - The _CRUD class defines the HTTP methods for the API.
91
+ """
92
+ api .add_resource (_CRUD , '/lgate' )
0 commit comments