-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathposts.py
More file actions
148 lines (133 loc) · 4.07 KB
/
posts.py
File metadata and controls
148 lines (133 loc) · 4.07 KB
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
import db
def add_post(title, content_days, tag, user_id):
sql = """
INSERT INTO posts (
title, tag, content_day1, content_day2, content_day3,
content_day4, content_day5, content_day6, content_day7, user_id
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
"""
params = [title, tag] + content_days + [user_id]
db.execute(sql, params)
post_id = db.last_insert_id()
return post_id
def vote(user_id, post_id, vote):
sql = """
INSERT INTO votes (user_id, post_id, vote)
VALUES (?, ?, ?)
"""
db.execute(sql, [user_id, post_id, vote])
def fetch_post(post_id):
sql = """
SELECT id, title, tag, content_day1, content_day2, content_day3,
content_day4, content_day5, content_day6, content_day7, user_id
FROM posts
WHERE id = ?
"""
result = db.query(sql, [post_id])
return result[0] if result else None
def fetch_posts(offset=0, limit=10):
sql = """
SELECT id, title, tag, content_day1, content_day2, content_day3,
content_day4, content_day5, content_day6, content_day7, user_id
FROM posts
LIMIT ? OFFSET ?
"""
result = db.query(sql, [limit, offset])
posts_list = [
{
"id": row[0],
"title": row[1],
"tag": row[2],
"content_days": row[3:10],
"user_id": row[10]
}
for row in result
]
return posts_list
def remove_post(post_id):
sql = "DELETE FROM posts WHERE id = ?"
db.execute(sql, [post_id])
def update_post(title, tag, content_days, user_id, post_id):
sql = """
UPDATE posts
SET title = ?, tag = ?, content_day1 = ?, content_day2 = ?,
content_day3 = ?, content_day4 = ?, content_day5 = ?,
content_day6 = ?, content_day7 = ?, user_id = ?
WHERE id = ?
"""
params = [title, tag] + content_days + [user_id, post_id]
db.execute(sql, params)
def search(query, offset=0, limit=10):
sql = """
SELECT id, title, tag, content_day1, content_day2, content_day3,
content_day4, content_day5, content_day6, content_day7, user_id
FROM posts
WHERE tag = ?
LIMIT ? OFFSET ?
"""
result = db.query(sql, [query, limit, offset])
posts_list = [
{
"id": row[0],
"title": row[1],
"tag": row[2],
"content_days": row[3:10],
"user_id": row[10]
}
for row in result
]
return posts_list
def user_posts(user_id, offset=0, limit=10):
sql = """
SELECT id, title, tag, content_day1, content_day2, content_day3,
content_day4, content_day5, content_day6, content_day7, user_id
FROM posts
WHERE user_id = ?
LIMIT ? OFFSET ?
"""
result = db.query(sql, [user_id, limit, offset])
posts_list = [
{
"id": row[0],
"title": row[1],
"tag": row[2],
"content_days": row[3:10],
"user_id": row[10]
}
for row in result
]
return posts_list
def total_posts_count(user_id):
sql = "SELECT COUNT(*) FROM posts WHERE user_id = ?"
result = db.query(sql, [user_id])
return result[0][0]
def get_user_vote(user_id, post_id):
sql = "SELECT vote FROM votes WHERE user_id = ? AND post_id = ?"
result = db.query(sql, [user_id, post_id])
if result:
return result[0]
else:
return None
def get_post_votes(post_id):
sql = "SELECT SUM(vote) as total_votes FROM votes WHERE post_id = ?"
result = db.query(sql, [post_id])
if result:
return result[0]['total_votes']
return 0
def get_received_upvotes(user_id):
sql = """
SELECT SUM(vote) as received_upvotes
FROM votes
JOIN posts ON votes.post_id = posts.id
WHERE posts.user_id = ? AND vote > 0
"""
result = db.query(sql, [user_id])
if result:
return result[0]['received_upvotes']
return 0
def get_sent_votes(user_id):
sql = "SELECT COUNT(vote) as sent_votes FROM votes WHERE user_id = ?"
result = db.query(sql, [user_id])
if result:
return result[0]['sent_votes']
return 0