[엘리스 AI 트랙] 10주차 - 2
- comment 권한이 있으면 수정하는 기능 추가
- comment 권한이 있으면 삭제하는 기능 추가
- UI 일부 정리
{% if g.user._id == comment.user_id %}
<div id="edit-delete-btns">
<a href="#" onclick="updateComment('{{comment._id}}', '{{book._id}}');return false;" id='editBtn{{comment._id}}'>수정하기</a>
<a href="#" onclick="deleteComment('{{comment._id}}', '{{book._id}}');return false;" id='deleteBtn{{comment._id}}'>삭제하기</a>
</div>
{% endif %}
function updateComment(commentId, bookId) {
let rating = $(`#user-rating${commentId}`).text()
let content = $(`#user-comment${commentId}`).text()
let tmpHtml = `<form class="row g-2" action="#"token interpolation">${commentId}, ${bookId}); return false;">
<div class="col-md-7">
<label for="inputCity" class="form-label">댓글 수정</label>
<input type="text" class="form-control" id="userEidContent${commentId}">
</div>
<div class="col-md-3">
<label for="rating" class="form-label">평가하기</label>
<select id="user-rating${commentId}" class="form-select">
<option selected disabled hidden>별점을 선택하세요.</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div>
<div class="col-12">
<button type="submit" class="btn btn-warning">수정 완료</button>
</div>
</form>`
$("#edit-area").empty()
$("#edit-area").append(tmpHtml)
$(`#editBtn${commentId}`).hide()
$(`#deleteBtn${commentId}`).hide()
$(`#userEidContent${commentId}`).val(content)
$(`#user-rating${commentId}`).children().first().text(rating.length)
}
function postUpdateComment(commentId, bookId) {
let star_rating = $(`#user-rating${commentId}`).val()
let comment = $(`#userEidContent${commentId}`).val()
if (comment == "" || star_rating == null) {
alert("댓글과 평점은 필수 항목입니다.");
return;
}
$.ajax({
url: '/info/'+bookId,
type: 'PATCH',
data: {
"comment_id": commentId,
"comment": comment,
"star_rating": star_rating,
},
success: function (res) {
if (res['result'] == 'success') {
alert("수정되었습니다.")
window.location.reload()
}
}
})
}
@board.route("/info/<int:book_id>", methods=["GET", "POST", "PATCH", "DELETE"])
def bookInfo(book_id):
# ... 중략 ...
elif method == "PATCH":
# 댓글 수정
comment_id = request.form['comment_id']
comment = request.form['comment']
star_rating = request.form['star_rating']
comment_to_edit = Comment.query.filter(Comment._id == comment_id).first()
comment_to_edit.comment = comment
comment_to_edit.star_rating = star_rating
comment_to_edit.created_at = datetime.now()
db.session.commit()
return jsonify({"result": "success"})
function deleteComment(commentId, bookId) {
$.ajax({
url: '/info/'+bookId,
type: 'DELETE',
data: {
'id': commentId,
},
success: function (res) {
if (res['result'] == 'success') {
alert("삭제되었습니다.")
window.location.reload()
} else {
alert("삭제 실패")
}
}
})
}
@board.route("/info/<int:book_id>", methods=["GET", "POST", "PATCH", "DELETE"])
def bookInfo(book_id):
# ... 중략 ...
else:
# 댓글 삭제
id = request.form["id"]
Comment.query.filter(Comment._id == id).delete()
db.session.commit()
return jsonify({"result": "success"})