-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
172 lines (133 loc) · 4.44 KB
/
server.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
from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from sqlite3 import Connection as SQLite3Connection
from datetime import datetime
from sqlalchemy import event
from sqlalchemy.engine import Engine
import linked_list
import hash_table
# app
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///sqlitedb.file"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = 0
# configure sqlite3 to enforce foreign key contraints
@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()
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)
# Routes
@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("/user/descending_id", methods=["GET"])
def get_all_users_descending():
users = User.query.all()
all_users_ll = linked_list.LinkedList()
for user in users:
all_users_ll.insert_beginning(
{
"id": user.id,
"name": user.name,
"email": user.email,
"address": user.address,
"phone": user.phone,
}
)
return jsonify(all_users_ll.to_list()), 200
@app.route("/user/ascending_id", methods=["GET"])
def get_all_users_ascending():
users = User.query.all()
all_users_ll = linked_list.LinkedList()
for user in users:
all_users_ll.insert_end(
{
"id": user.id,
"name": user.name,
"email": user.email,
"address": user.address,
"phone": user.phone,
}
)
return jsonify(all_users_ll.to_list()), 200
@app.route("/user/<user_id>", methods=["GET"])
def get_one_user(user_id):
users = User.query.all()
all_users_ll = linked_list.LinkedList()
for user in users:
all_users_ll.insert_beginning(
{
"id": user.id,
"name": user.name,
"email": user.email,
"address": user.address,
"phone": user.phone,
}
)
user = all_users_ll.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 = hash_table.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": "new blog post created"}), 200
@app.route("/user/<user_id>", methods=["GET"])
def get_all_blog_post(user_id):
pass
@app.route("/blog_post/<blog_post_id>", methods=["GET"])
def get_one_blog_post(blog_post_id):
pass
@app.route("/blog_post/<blog_post_id>", methods=["DELETE"])
def delete_blog_post(blog_post_id):
pass
if __name__ == "__main__":
app.run(debug=True)