feat:添加评论上传功能 未完全完成

This commit is contained in:
2024-04-28 20:21:36 +08:00
parent b5c58e1216
commit c8bae04145
12 changed files with 279 additions and 42 deletions

76
db/comments.py Normal file
View File

@@ -0,0 +1,76 @@
import time
import db.util as util
# 查找评论
def listByBookid(id: str):
"通过bookid查找所有评论"
conn = util.getConn()
c = conn.cursor()
cursor = c.execute("SELECT * FROM Comments WHERE bookid = ? ORDER BY time desc", (id,))
out = []
for row in cursor:
out.append(row)
conn.close()
return out
# 获取综合评分
def getScore(bookid: str):
"获取综合评分 返回一个字典 字典有两个key like和dislike分别记录不同评论的个数"
conn = util.getConn()
c = conn.cursor()
cursor = c.execute("SELECT * FROM Comments WHERE bookid = ? ", (bookid,))
num={'like':0,'dislike':0}
for row in cursor:
if row[4] == "like":
num["like"]+=1
elif row[4] == "dislike":
num["dislike"]+=1
conn.close()
return num
# 查找评论
def searchByUid(uid: str):
"通过用户查找所有评论"
conn = util.getConn()
c = conn.cursor()
cursor = c.execute("SELECT * FROM Comments WHERE from_uid = ? ORDER BY time desc", (uid,))
out = []
for row in cursor:
out.append(row)
conn.close()
return out
# 查找评论
def searchByAll(uid: str,bookid:str):
"通过用户和BookID查找所有评论"
conn = util.getConn()
c = conn.cursor()
cursor = c.execute("SELECT * FROM Comments WHERE from_uid = ? AND bookid= ? ORDER BY time desc", (uid,bookid))
out = []
for row in cursor:
out.append(row)
conn.close()
return out
# 在数据库中添加一个新的文件记录
def new(bookid: str, from_uid: int, score: str, content=""):
"添加一条新评论 score字段可选值为[like,none,dislike] content字段非必填"
conn = util.getConn()
c = conn.cursor()
c.execute(
"""
INSERT INTO Comments
(time, bookid, from_uid, score, content)
VALUES
(?, ?, ?, ?,?);
""",
(int(time.time()), bookid, from_uid, score, content),
)
conn.commit()
conn.close()
return

View File

@@ -32,4 +32,24 @@ def check(username: str, password: int):
cursor = c.execute("SELECT * FROM User WHERE username = ? AND password = ?", (username, password))
if cursor.fetchone() is None:
return False
return True
return True
def getUid(username: str):
"判断用户名是否存在 并获取用户uid 用户不存在则返回None"
conn = util.getConn()
c = conn.cursor()
cursor = c.execute("SELECT * FROM User WHERE username = ?", (username,))
out = cursor.fetchone()
if out is not None:
return out[0]
return None
def getUsername(uid:str):
"判断Uid是否存在 并获取用户名 用户不存在则返回None"
conn = util.getConn()
c = conn.cursor()
cursor = c.execute("SELECT * FROM User WHERE uid = ?", (uid,))
out = cursor.fetchone()
if out is not None:
return out[1]
return None

View File

@@ -31,5 +31,29 @@ def init():
);
"""
)
c.execute(
"""
CREATE TABLE IF NOT EXISTS Comments (
id INTEGER PRIMARY KEY AUTOINCREMENT,
time INT NOT NULL,
bookid TEXT NOT NULL,
from_uid INTEGAR NOT NULL,
score INT NOT NULL,
content TEXT
);
"""
)
c.execute(
"""
INSERT INTO User (username, password)
SELECT ?, ?
WHERE NOT EXISTS (SELECT 1 FROM User WHERE username = ?);
""",
(
conf.get("user", "username"),
conf.get("user", "password"),
conf.get("user", "username"),
),
)
conn.commit()
conn.close()