/
app.py
192 lines (155 loc) · 4.15 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
from flask import Flask, jsonify, request, current_app
from flask.json import JSONEncoder
from sqlalchemy import create_engine, text
# Default JSON encoder는 set를 JSON으로 변환할 수 없다.
# 그러프로 커스텀 엔코더를 작성해서 set을 list로 변환하여
# JSON 으로 변환 가능하게 해주어야 한다.
class CustomJSONEncoder(JSONEncoder):
def default(self, o):
if isinstance(o, set):
return list(o)
return JSONEncoder.default(self, o)
# 유저 정보 가져오기
def get_user(user_id):
user = current_app.database.execute(
text(
"""
SELECT
id,
name,
email,
profile
FROM users
WHERE id = :user_id
"""
),
{"user_id": user_id},
).fetchone()
return (
{
"id": user["id"],
"name": user["name"],
"email": user["email"],
"profile": user["profile"],
}
if user
else None
)
def insert_user(user):
return current_app.database.execute(
text(
"""
INSERT INTO users (
name,
email,
profile,
hashed_password
) VALUES (
:name,
:email,
:profile,
:password
)
"""
),
user,
).lastrowid # lasrowid 란
def insert_tweet(user_tweet):
return current_app.database.execute(
text(
"""
INSERT INTO tweets (
user_id,
tweet
) VALUES (
:id,
:tweet
)
"""
),
user_tweet,
).rowcount # rowcount 란
def insert_follow(user_follow):
return current_app.database.execute(
text(
"""
INSERT INTO users_follow_list (
user_id,
follow_user_id
) VALUES (
:id,
:follow
)
"""
),
user_follow,
).rowcount
def insert_unfollow(user_unfollow):
return current_app.database.execute(
text(
"""
DELETE FROM users_follow_list
WHERE user_id = :id
AND follow_user_id = :unfollow
"""
),
user_unfollow,
).rowcount
def get_timeline(user_id):
timeline = current_app.database.execute(
text(
"""
SELECT
t.user_id,
t.tweet
FROM tweets t
LEFT JOIN users_follow_list ufl ON ufl.user_id = :user_id
WHERE t.user_id = :user_id
OR t.user_id = ufl.follow_user_id
"""
),
{"user_id": user_id},
).fetchall()
return [
{"user_id": tweet["user_id"], "tweet": tweet["tweet"]} for tweet in timeline
]
def create_app(test_config=None):
app = Flask(__name__)
app.json_encoder = CustomJSONEncoder
if test_config is None:
app.config.from_pyfile("config.py")
else:
app.config.update(test_config)
database = create_engine(app.config["DB_URL"], encoding="utf-8", max_overflow=0)
app.database = database
@app.route("/ping", methods=["GET"])
def ping():
return "pong"
@app.route("/sign-up", methods=["POST"])
def sign_up():
new_user = request.json
new_user_id = insert_user(new_user)
new_user = get_user(new_user_id)
return jsonify(new_user)
@app.route("/tweet", methods=["POST"])
def tweet():
user_tweet = request.json
tweet = user_tweet["tweet"]
if len(tweet) > 300:
return "300자를 초과 했습니다", 400
insert_tweet(user_tweet)
return "", 200
@app.route("/follow", methods=["POST"])
def follow():
payload = request.json
insert_follow(payload)
return "", 200
@app.route("/unfollow", methods=["POST"])
def unfollow():
payload = request.json
insert_unfollow(payload)
return "", 200
@app.route("/timeline/<int:user_id>", methods=["GET"])
def timeline(user_id):
return jsonify({"user_id": user_id, "timeline": get_timeline(user_id)})
return app