mirror of
https://github.com/Kakune55/ComiPy.git
synced 2025-05-07 02:39:26 +08:00
feat:添加评论删除功能
This commit is contained in:
parent
c8bae04145
commit
be5d9a8146
@ -2,6 +2,14 @@ import time
|
|||||||
import db.util as util
|
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):
|
def listByBookid(id: str):
|
||||||
"通过bookid查找所有评论"
|
"通过bookid查找所有评论"
|
||||||
@ -74,3 +82,16 @@ def new(bookid: str, from_uid: int, score: str, content=""):
|
|||||||
conn.commit()
|
conn.commit()
|
||||||
conn.close()
|
conn.close()
|
||||||
return
|
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
|
||||||
|
@ -115,7 +115,9 @@
|
|||||||
<h2>👍{{socre["like"]}} 👎{{socre["dislike"]}}</h2>
|
<h2>👍{{socre["like"]}} 👎{{socre["dislike"]}}</h2>
|
||||||
<button class="btn btn-primary" onclick="window.location.href='/view/{{ id }}'">在线浏览</button>
|
<button class="btn btn-primary" onclick="window.location.href='/view/{{ id }}'">在线浏览</button>
|
||||||
<button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal">撰写评论</button>
|
<button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal">撰写评论</button>
|
||||||
|
{%if islogin == "admin"%}
|
||||||
<button class="btn btn-danger">删除资源</button>
|
<button class="btn btn-danger">删除资源</button>
|
||||||
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@ -139,9 +141,11 @@
|
|||||||
</h3>
|
</h3>
|
||||||
<h3>{{item["text"]}}</h3>
|
<h3>{{item["text"]}}</h3>
|
||||||
<small class="text-muted">id:{{item["id"]}} {{item["time"]}}</small>
|
<small class="text-muted">id:{{item["id"]}} {{item["time"]}}</small>
|
||||||
|
{% if islogin == item["from"] %}
|
||||||
|
<button class="btn btn-danger" id="{{item['id']}}" onclick="delComment(id)">删除</button>
|
||||||
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
<!-- 在此添加更多评论 -->
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@ -154,6 +158,26 @@
|
|||||||
function commentSubmit() {
|
function commentSubmit() {
|
||||||
comment.submit()
|
comment.submit()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function delComment(id) {
|
||||||
|
fetch("/api/comment/remove?id=" + id, {
|
||||||
|
method: 'GET'
|
||||||
|
})
|
||||||
|
.then(response => {
|
||||||
|
// 首先检查HTTP状态码
|
||||||
|
if (response.ok) {
|
||||||
|
// 请求成功,刷新页面
|
||||||
|
location.reload();
|
||||||
|
} else {
|
||||||
|
// 请求失败,抛出一个错误
|
||||||
|
throw new Error('网络请求失败,状态码:' + response.status);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
// 处理任何在请求过程中发生的错误
|
||||||
|
alert('请求失败:' + error.message);
|
||||||
|
});
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
|
@ -25,3 +25,20 @@ def comment_api(): # 概览
|
|||||||
request.form["text"],
|
request.form["text"],
|
||||||
)
|
)
|
||||||
return redirect("/book/" + request.form["bookid"])
|
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)
|
||||||
|
@ -48,6 +48,7 @@ def book(bookid): # 接口
|
|||||||
raw_com = db.comments.listByBookid(bookid)
|
raw_com = db.comments.listByBookid(bookid)
|
||||||
comments = []
|
comments = []
|
||||||
for i in raw_com:
|
for i in raw_com:
|
||||||
|
print(request.cookies.get("islogin"))
|
||||||
comments.append(
|
comments.append(
|
||||||
{
|
{
|
||||||
"id": i[0],
|
"id": i[0],
|
||||||
@ -65,6 +66,7 @@ def book(bookid): # 接口
|
|||||||
time=time.strftime("%Y-%m-%d %H:%M:%S", local_time),
|
time=time.strftime("%Y-%m-%d %H:%M:%S", local_time),
|
||||||
socre=db.comments.getScore(bookid),
|
socre=db.comments.getScore(bookid),
|
||||||
comments=comments,
|
comments=comments,
|
||||||
|
islogin=request.cookies.get("islogin")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user