-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_manager.py
170 lines (143 loc) · 6.43 KB
/
data_manager.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
import connection
QUESTION_FILE_NAME = 'sample_data/question.csv'
ANSWERS_FILE_NAME = 'sample_data/answer.csv'
@connection.connection_handler
def list_all_questions_ordered_by_submission_time(cursor):
cursor.execute("""
SELECT q.id, q.title, q.submission_time, q.image, q.message, q.view_number,
q.vote_number, q.user_id, COALESCE(u.user_name, 'Anonymous') AS user_name
FROM question AS q
LEFT JOIN users AS u
ON q.user_id=u.id
ORDER BY submission_time DESC;
""")
all_questions = cursor.fetchall()
return all_questions
@connection.connection_handler
def get_question_by_id(cursor, question_id):
cursor.execute("""SELECT q.id, q.title, q.submission_time, q.image, q.message, q.view_number,
q.vote_number, q.user_id, COALESCE(u.user_name, 'Anonymous') AS user_name
FROM question AS q
LEFT JOIN users AS u
ON q.user_id=u.id
WHERE q.id =%(id)s;""", {'id': question_id})
return cursor.fetchone()
@connection.connection_handler
def get_questions_by_tag(cursor, tag):
"""
:param tag:
:return: List of dictionaries, where every dictionary contains a questions, with
the following keys: id, submission_time, title, view_number, vote_number
"""
cursor.execute("""
SELECT DISTINCT question.id, question.submission_time,
question.view_number, question.vote_number, question.title
FROM question
LEFT JOIN question_tag ON question.id = question_tag.question_id
LEFT JOIN tag ON tag.id = question_tag.tag_id
WHERE tag.name = %(tag)s;
""", {'tag': tag})
return cursor.fetchall()
@connection.connection_handler
def get_answer_by_id(cursor, answer_id):
cursor.execute("""SELECT * FROM answer
WHERE id = %(id)s;""", {'id': answer_id})
return cursor.fetchone()
@connection.connection_handler
def get_answers_by_question_id(cursor, id):
cursor.execute("""SELECT a.id, a.submission_time, a.vote_number, a.question_id, a.message, a.image,
COALESCE(u.user_name, 'Anonymous') AS user_name FROM answer AS a
LEFT JOIN users AS u
ON a.user_id = u.id
WHERE question_id = %(question_id)s
ORDER BY submission_time;""", {'question_id': id})
answers_for_question = cursor.fetchall()
return answers_for_question
@connection.connection_handler
def add_new_question(cursor, new_data):
cursor.execute("""INSERT INTO question(submission_time,
view_number,
vote_number,
title,
message,
image,
user_id)
VALUES (%(submission_time)s,
%(view_number)s,
%(vote_number)s,
%(title)s,
%(message)s,
%(image)s,
%(user_id)s);
""", new_data)
cursor.execute("""SELECT id FROM question
ORDER BY id DESC
LIMIT 1;
""")
return cursor.fetchone()['id']
@connection.connection_handler
def add_new_answer(cursor, new_data):
cursor.execute("""INSERT INTO answer(submission_time,
vote_number,
question_id,
message,
image,
user_id)
VALUES (%(submission_time)s,
%(vote_number)s,
%(question_id)s,
%(message)s,
%(image)s,
%(user_id)s);
""", new_data)
@connection.connection_handler
def update_question(cursor, data):
cursor.execute("""
UPDATE question
SET submission_time = %(submission_time)s,
view_number = %(view_number)s,
vote_number = %(vote_number)s,
title = %(title)s,
message = %(message)s,
image = %(image)s
WHERE id = %(id)s;
""",
data)
@connection.connection_handler
def update_answer(cursor, new_data):
cursor.execute("""
UPDATE answer
SET submission_time = %(submission_time)s,
vote_number = %(vote_number)s,
message = %(message)s,
image = %(image)s
WHERE id = %(id)s;
""", new_data)
@connection.connection_handler
def search_questions(cursor, pattern):
cursor.execute("""
SELECT DISTINCT question.vote_number, question.message, question.view_number,
question.title, question.submission_time, question.id, question.image
FROM question
LEFT JOIN answer ON question.id = answer.question_id
WHERE LOWER(question.title) LIKE %(pattern)s
OR
LOWER(question.message) LIKE %(pattern)s
OR
LOWER(answer.message) LIKE %(pattern)s
ORDER BY question.submission_time DESC;
""",
{'pattern': '%' + pattern.lower() + '%'})
return cursor.fetchall()
@connection.connection_handler
def search_answer(cursor, pattern):
cursor.execute("""
SELECT * FROM answer
WHERE LOWER(message) LIKE %(pattern)s
ORDER BY id DESC;
""",
{'pattern': '%' + pattern.lower() + '%'})
return cursor.fetchall()
@connection.connection_handler
def delete_comment(cursor, comment_id):
cursor.execute("""DELETE FROM comment WHERE id = %(cid)s;""", {'cid': comment_id})