감사쟁이야
감사쟁이의 성장기록
감사쟁이야
  • 분류 전체보기 (130)
    • Java-Spring (0)
    • ComputerScience (0)
    • Project (64)
      • TIL, WIL (57)
      • Project Retrospect (7)
    • Problem Solving (63)
    • Book Review (1)
    • Culture & Discovery (0)
    • Daily Log (2)

블로그 메뉴

  • 홈
  • 깃허브
  • 방명록
hELLO · Designed By 정상우.
감사쟁이야

감사쟁이의 성장기록

TIL(41) 21-11-20 : 조회수, comment 기능 구현 (Flask)
Project/TIL, WIL

TIL(41) 21-11-20 : 조회수, comment 기능 구현 (Flask)

2021. 11. 20. 17:18

Facts

✅ 메모장 사이트 조회수 기능 구현 [flask]

✅ 메모장 사이트 Comment 기능 구현 [flask]

 

조회수 기능 구현

게시글을 클릭할 때마다, 서버에서 게시글 컬럼의 read_count 필드값을 증가시키게 구현했다.

function readArticle(idx) {
            $.ajax({
                type: "PUT",
                url: `/article/${idx}`,
                data: {},
                success: function (response) {
                    console.log(response['article'])
                    let title = response['article']['title']
                    let content = response['article']['content']
                    $('#modal-title').html(title);
                    $('#modal-content').html(content);
                    $('#modal-idx').val(idx);
                    $('#articleModal').modal('show');
                    getComment();
                }
            })
}
@app.route('/article/<idx>', methods=['PUT'])
def update_read_count(idx):
    db.article.update_one({'idx': int(idx)}, {'$inc': {'read_count': 1}})
    article = db.article.find_one({'idx': int(idx)}, {'_id': False})
    return jsonify({"article": article})

Comment 기능 구현

게시글 CRUD와 같이, 서버에게 comment 관련 컬럼을 만들도록 요청했다.

 function saveComment() {
	let idx = $("#modal-idx").val();
	let comment = $("#comment").val()
	$.ajax({
		type: "POST",
		url: "/comment",
		data: {idx: idx, comment: comment},
		success: function (response) {
			alert("저장되었어요!!");
			comments = `<button type="button" class="list-group-item list-group-item-action">${$("#comment").val()}</button>`;
			$("#modal-comment").append(comments);
			$("#comment").val('');
		}
	})
}

function getComment() {
	$.ajax({
		type: "GET",
		url: `/comment?idx=${$("#modal-idx").val()}`,
		success: function (response) {
			let comments = '';
				for (let i = 0; i < response['comments'].length; i++) {
					comments += `<button type="button" class="list-group-item list-group-item-action">${response['comments'][i].comment}</button>`;
                 }
			$("#modal-comment").append(comments)
		}
	})
}
@app.route('/comment', methods=['POST'])
def save_comment():
    idx = request.form.get('idx')
    comment = request.form.get('comment')
    post = {'idx': int(idx), 'comment': comment, 'writer': ''}
    db.comment.insert_one(post)
    return {"result": "success"}


@app.route('/comment', methods=['GET'])
def get_comment():
    idx = request.args['idx']
    comments = list(db.comment.find({'idx': int(idx)}, {'_id': False}))
    return jsonify({"comments": comments})

'Project > TIL, WIL' 카테고리의 다른 글

TIL(43) 21-11-28 : Spring Secruity 사용하여, 인증 인가 구현하기  (0) 2021.12.25
TIL(42) 11/22 - 11/27 : TDP 사이트 Flask 👉🏻 Spring  (0) 2021.12.22
TIL(40) 21-11-17 : @Transactional / 스프링 Controller가 Client로 부터 파라미터를 받는 방법 / Lambda와 Stream  (0) 2021.11.17
TIL(39) 21-11-15 : 도커를 사용해서 컨테이너를 구성해보기  (0) 2021.11.15
TIL(38) 21-11-10 : Spring Security + OAuth2  (0) 2021.11.10
    'Project/TIL, WIL' 카테고리의 다른 글
    • TIL(43) 21-11-28 : Spring Secruity 사용하여, 인증 인가 구현하기
    • TIL(42) 11/22 - 11/27 : TDP 사이트 Flask 👉🏻 Spring
    • TIL(40) 21-11-17 : @Transactional / 스프링 Controller가 Client로 부터 파라미터를 받는 방법 / Lambda와 Stream
    • TIL(39) 21-11-15 : 도커를 사용해서 컨테이너를 구성해보기
    감사쟁이야
    감사쟁이야
    sunzero0116@gmail.com

    티스토리툴바