diff --git a/db/comments.py b/db/comments.py
index 1cfcf57..ab2d928 100644
--- a/db/comments.py
+++ b/db/comments.py
@@ -2,6 +2,14 @@ import time
import db.util as util
+# 查找评论
+def getById(id: str):
+ "通过id查找所有评论"
+ conn = util.getConn()
+ c = conn.cursor()
+ cursor = c.execute("SELECT * FROM Comments WHERE id = ?", (id,))
+ return cursor.fetchone()
+
# 查找评论
def listByBookid(id: str):
"通过bookid查找所有评论"
@@ -74,3 +82,16 @@ def new(bookid: str, from_uid: int, score: str, content=""):
conn.commit()
conn.close()
return
+
+# 查找评论
+def remove(id:int)->bool:
+ "通过id删除评论"
+ conn = util.getConn()
+ c = conn.cursor()
+ c.execute("DELETE FROM Comments WHERE id = ?", (id,))
+ conn.commit()
+ changes = conn.total_changes
+ conn.close()
+ if changes == 0:
+ return False
+ return True
diff --git a/templates/book.html b/templates/book.html
index 1a84bd5..51d3b83 100644
--- a/templates/book.html
+++ b/templates/book.html
@@ -74,32 +74,32 @@
@@ -112,10 +112,12 @@
{{ data[0][2] }}
更新时间: {{time}}
- 👍{{socre["like"]}} 👎{{socre["dislike"]}}
+ 👍{{socre["like"]}} 👎{{socre["dislike"]}}
+ {%if islogin == "admin"%}
+ {% endif %}
@@ -130,18 +132,20 @@
{% for item in comments %}
{% endfor %}
-
@@ -151,9 +155,29 @@
diff --git a/web/api_comment.py b/web/api_comment.py
index 32680a0..c790b24 100644
--- a/web/api_comment.py
+++ b/web/api_comment.py
@@ -25,3 +25,20 @@ def comment_api(): # 概览
request.form["text"],
)
return redirect("/book/" + request.form["bookid"])
+
+@comment_api_bp.route("/api/comment/remove")
+def remove(): # 删除api
+ if request.cookies.get("islogin") is None: # 验证登录状态
+ return abort(403)
+ try:
+ id = int(request.args.get("id"))
+ except:
+ return abort(400)
+ commentInfo = db.comments.getById(id)
+ if commentInfo is None:
+ return abort(404)
+ if int(request.cookies.get("uid")) == commentInfo[3]:
+ if db.comments.remove(id):
+ return "OK"
+ return abort(404)
+ return abort(400)
diff --git a/web/page.py b/web/page.py
index 5ac9d7d..2b3fe17 100644
--- a/web/page.py
+++ b/web/page.py
@@ -48,6 +48,7 @@ def book(bookid): # 接口
raw_com = db.comments.listByBookid(bookid)
comments = []
for i in raw_com:
+ print(request.cookies.get("islogin"))
comments.append(
{
"id": i[0],
@@ -65,6 +66,7 @@ def book(bookid): # 接口
time=time.strftime("%Y-%m-%d %H:%M:%S", local_time),
socre=db.comments.getScore(bookid),
comments=comments,
+ islogin=request.cookies.get("islogin")
)
{{item["from"]}}: - {%if item["socre"] == "like"%} - 觉得很赞👍 - {%endif%} - {%if item["socre"] == "dislike"%} - 点了个踩👎 - {%endif%} + {%if item["socre"] == "like"%} + 觉得很赞👍 + {%endif%} + {%if item["socre"] == "dislike"%} + 点了个踩👎 + {%endif%}
{{item["text"]}}
- id:{{item["id"]}} {{item["time"]}} + id:{{item["id"]}} {{item["time"]}} + {% if islogin == item["from"] %} + + {% endif %}