-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
234 lines (177 loc) · 5.93 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import random
from sqlite3 import Connection as SQLite3Connection
from datetime import datetime
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import event
from sqlalchemy.engine import Engine
from flask import Flask, request, jsonify
from DataStructures.BinarySearchTree import BinarySearchTree
from DataStructures.LinkedList import LinkedList
from DataStructures.HashTable import HashTable
from DataStructures.Queue import Queue
from DataStructures.Stack import Stack
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///sqlitedb.file"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = 0
# configure sqlite3 to enfore foreign key constraints
# without this config, sqlite would allow blog posts without valid users
@event.listens_for(Engine, "connect")
def _set_sqlite_pragma(dbapi_connection, connection_record):
if isinstance(dbapi_connection, SQLite3Connection):
cursor = dbapi_connection.cursor()
cursor.execute("PRAGMA foreign_keys=ON;")
cursor.close()
# get an instance of the SQLAlchemy class for app
db = SQLAlchemy(app)
now = datetime.now()
# models
class User(db.Model):
__tablename__ = "user"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50))
email = db.Column(db.String(50))
address = db.Column(db.String(200))
phone = db.Column(db.String(50))
posts = db.relationship("BlogPost", cascade="all, delete")
class BlogPost(db.Model):
__tablename__ = "blog_post"
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(50))
body = db.Column(db.String(200))
date = db.Column(db.Date)
user_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)
@app.route("/user", methods=["POST"])
def create_user():
data = request.get_json()
new_user = User(
name=data["name"],
email=data["email"],
address=data["address"],
phone=data["phone"]
)
db.session.add(new_user)
db.session.commit()
return jsonify({"message": "User created"}), 200
@app.route("/users/descending", methods=["GET"])
def get_all_users_descending():
users = User.query.all()
all_users = LinkedList.LinkedList()
for user in users:
all_users.insert_head(
{
"id": user.id,
"name": user.name,
"email": user.email,
"address": user.address,
"phone": user.phone
}
)
return jsonify(all_users.to_list()), 200
@app.route("/users/ascending", methods=["GET"])
def get_all_users_ascending():
users = User.query.all()
all_users = LinkedList.LinkedList()
for user in users:
all_users.insert_tail(
{
"id": user.id,
"name": user.name,
"email": user.email,
"address": user.address,
"phone": user.phone
}
)
return jsonify(all_users.to_list()), 200
@app.route("/user/<user_id>", methods=["GET"])
def get_one_user(user_id):
users = User.query.all()
all_users = LinkedList.LinkedList()
for user in users:
all_users.insert_head(
{
"id": user.id,
"name": user.name,
"email": user.email,
"address": user.address,
"phone": user.phone
}
)
user = all_users.get_user_by_id(user_id)
return jsonify(user), 200
@app.route("/user/<user_id>", methods=["DELETE"])
def delete_user(user_id):
user = User.query.filter_by(id=user_id).first()
db.session.delete(user)
db.session.commit()
return jsonify({}), 200
@app.route("/blog_post/<user_id>", methods=["POST"])
def create_blog_post(user_id):
data = request.get_json()
user = User.query.filter_by(id=user_id).first()
if not user:
return jsonify({"message": "user does not exist"}), 400
ht = HashTable.HashTable(10)
ht.add_key_value("title", data["title"])
ht.add_key_value("body", data["body"])
ht.add_key_value("date", now)
ht.add_key_value("user_id", user_id)
new_blog_post = BlogPost(
title=ht.get_value("title"),
body=ht.get_value("body"),
date=ht.get_value("date"),
user_id=ht.get_value("user_id")
)
db.session.add(new_blog_post)
db.session.commit()
return jsonify({"message": "Blog Post created"}), 200
@app.route("/blog/<blog_post_id>", methods=["GET"])
def get_all_blog_posts(blog_post_id):
posts = BlogPost.query.all()
random.shuffle(posts)
bst = BinarySearchTree.BinarySearchTree()
for post in posts:
bst.insert({
"id": post.id,
"title": post.title,
"body": post.body,
"user_id": post.user_id
})
post = bst.search(blog_post_id)
if not post:
return jsonify({"message": "Post not found"})
return jsonify(post), 200
@app.route("/blog_post/numeric_body", methods=["GET"])
def get_numeric_post_bodies():
posts = BlogPost.query.all()
q = Queue.Queue()
for post in posts:
q.enqueue(post)
return_list = []
for _ in range(len(posts)):
post = q.dequeue()
numeric_body = 0
for char in post.data.body:
numeric_body += ord(char)
post.data.body = numeric_body
return_list.append(
{
"id": post.data.id,
"title": post.data.title,
"body": post.data.body,
"user_id": post.data.user_id
}
)
return jsonify(return_list), 200
@app.route("/blog_post/delete_last_10", methods=["DELETE"])
def delete_blog_post():
posts = BlogPost.query.all()
s = Stack.Stack()
for post in posts:
s.push(post)
for _ in range(10):
post_delete = s.pop()
db.session.delete(post_delete.data)
db.session.commit()
return jsonify({"message": "success"}), 200
if __name__ == '__main__':
app.run(debug=True)