Skip to content

Commit 425bc70

Browse files
committed
after demolition
1 parent a4f0456 commit 425bc70

File tree

3 files changed

+112
-17
lines changed

3 files changed

+112
-17
lines changed

api/larslgate.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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')

api/lgate.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
from flask import Blueprint, request, jsonify, current_app, Response, g
2-
from flask_restful import Api, Resource # used for REST API building
3-
from __init__ import app
2+
from flask_restful import Api, Resource
3+
from __init__ import app, db # Ensure db is imported
44
from api.jwt_authorize import token_required
55
from model.lgatedata import lgate
66

77
# Blueprint setup for the API
88
lgate_api = Blueprint('lgate_api', __name__, url_prefix='/api/lgate')
9-
109
api = Api(lgate_api)
1110

12-
# test data
11+
# Wrap DB initialization in app context
12+
with app.app_context():
13+
if not lgate.query.first():
14+
sample_quiz = lgate(name="Basic Logic Gate Quiz", score=0, quiz_id=1)
15+
db.session.add(sample_quiz)
16+
db.session.commit()
1317

1418
@lgate_api.route('/test', methods=['GET'])
1519
def test_lgate():

model/lgatedata.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
11
from sqlite3 import IntegrityError
22
from sqlalchemy.exc import SQLAlchemyError
3-
from __init__ import app, db
3+
from __init__ import db
44

55
class lgate(db.Model):
6-
"""
7-
lgate Model
8-
9-
Represents a quiz with a question, score, and a quiz_id associated with a user.
10-
"""
11-
__tablename__ = 'lgate'
6+
__tablename__ = 'lgate' # Explicit table name
127

138
id = db.Column(db.Integer, primary_key=True)
14-
name = db.Column(db.String(255), nullable=False)
15-
score = db.Column(db.String(255), nullable=False)
16-
quiz_id = db.Column(db.Integer, nullable=False) # Remove ForeignKey for simplicity in SQLite
9+
name = db.Column(db.String(100), nullable=False)
10+
score = db.Column(db.Integer, nullable=False)
11+
quiz_id = db.Column(db.Integer, nullable=False)
1712

1813
def __init__(self, name, score, quiz_id):
19-
"""
20-
Constructor for lgate.
21-
"""
2214
self.name = name
2315
self.score = score
2416
self.quiz_id = quiz_id
2517

18+
def read(self):
19+
return {
20+
'id': self.id,
21+
'name': self.name,
22+
'score': self.score,
23+
'quiz_id': self.quiz_id
24+
}
2625
def __repr__(self):
2726
"""
2827
Represents the lgate object as a string for debugging.

0 commit comments

Comments
 (0)