반응형
오늘도 어김없는 오전 9시가 다가왔다. 바로 출석부터 챙겨주는 센스가 필요하다.
오늘은 어제 하던 발표자료를 정리하고 프로젝트를 마무리 하는 시간을 가졌다. (해도해도 어려운 코딩...)
오늘은 계속 하다가 미루고 있던 CRUD의 마무리를 했다.
CREATE
@app.route("/comment", methods=["POST"])
def comment_post():
uuid_receive = request.form['uuid_give']
name_receive = request.form['name_give']
comment_receive = request.form['comment_give']
doc = {
"uuid": uuid_receive,
"name": name_receive,
"comment": comment_receive,
}
db.comment.insert_one(doc)
return jsonify({'msg': '작성 완료!'})
function save_comment() {
const uuid = Math.floor(Math.random() * 10000000)
const name = $("#name").val()
const comment = $("#comment").val()
$.ajax({
type: "POST",
url: "/comment",
data: { uuid_give: uuid, name_give: name, comment_give: comment },
success: function (response) {
alert(response["msg"])
window.location.reload()
},
})
}
READ
@app.route("/comment", methods=["GET"])
def comment_get():
post_list = list(db.comment.find({}, {'_id': False}))
return jsonify({"post": post_list})
JS는 너무 길어서 패스...
UPDATE
@app.route("/comment", methods=["PATCH"])
def comment_update():
uuid_receive = request.form["uuid_give"]
name_receive = request.form["name_give"]
comment_receive = request.form["comment_give"]
db.comment.update_one({"uuid": uuid_receive}, {"$set":{"name": name_receive, "comment": comment_receive} })
return jsonify({"msg": "수정 완료!"})
function update_comment(e) {
const uuid = e.target.parentElement.id
const name = $("#editName").val()
const comment = $("#editComment").val()
$.ajax({
type: "PATCH",
url: "/comment",
data: { uuid_give: uuid, name_give: name, comment_give: comment },
success: function (response) {
alert(response["msg"])
window.location.reload()
},
})
}
DELETE
@app.route("/comment", methods=["DELETE"])
def comment_delete():
uuid_receive = request.form["uuid_give"]
db.comment.delete_one({"uuid": uuid_receive})
return jsonify({"msg": "삭제 완료!"})
function delete_comment(uuid) {
$.ajax({
type: "DELETE",
url: "/comment",
data: { uuid_give: uuid },
success: function (response) {
alert(response["msg"])
window.location.reload()
},
})
}
이렇게 CRUD를 끝맞혔다 물론 저건만 작성한다고 잘 동작하는게 아니고 부가적인 코드도 많이 손봐야 한다.
CRUD에 핵심은 uuid를 잘 가져다 써야만 잘 동작한다! 오늘은 이것만 하다 끝난듯..
오늘은 정말 코드만 짜다가 하루가 끝나서 시간이 빨리 간건지 느리게 간건지 잘 모르겠다.
그럼 20000
반응형
'내일배움캠프' 카테고리의 다른 글
WIL 1주차 정리 (0) | 2022.11.06 |
---|---|
TIL 5일차 정리 (0) | 2022.11.04 |
TIL 3일차 정리 (0) | 2022.11.02 |
TIL 2일차 정리 (0) | 2022.11.01 |
TIL 1일차 정리 (0) | 2022.10.31 |