본문 바로가기

스파르타코딩클럽

내일배움캠프 19일차 기록

1차 프로젝트 끝! 그동안 작성한 코드를 올려본다 :)

# board 하나 제거하는 기능
@app.route('/api/delete', methods=['POST'])
def delete_board():
    title_receive = request.form['title_give'] #이름 받아오기
    db.board.delete_one({'title': title_receive}) # 받아온 이름으로 db 삭제하기
    return jsonify({'msg': '삭제 완료'}) #메세지 리턴해주기
    
   #서버에서 ajax 통신으로 이름값을 받아서 db에서 검색하고 삭제해주는 기능
@app.route('/api/board-update', methods=['POST'])
def update_board_content():
    content_receive = request.form['content_give']
    update_receive = request.form['update_content_give'] #수정할 텍스트를 받아왔음
    title_receive = request.form['title_give'] #받아온타이틀
    print(content_receive, update_receive, title_receive)
    db.board.update_one({'content': content_receive}, {"$set": {'title': title_receive}})
    db.board.update_one({'content': content_receive}, {"$set": {'content': update_receive}})
    return jsonify({'msg': '완료'})

# 수정할 텍스트와 내용을 받아서 고유값(여기서는 타이틀)로 db에서 그 내용을 찾고 수정해준다
// URL파라미터에서 데이터 추출
    $(document).ready(function () {
        url_data();
    });

    function url_data() {
        var search = location.search
        console.log("search: ", search)
        var params = new URLSearchParams(search);
        console.log("params: ", params)
        var getType = params.get('content');
        console.log("getType: ", getType)

        $.ajax({
            type: "GET",
            url: "/api/board-update",
            data: {content_give: getType},
            success: function (response) {
                    let title = response['title']
                    let writer = response['writer']
                    let content = response['content']


                    let temp_html = `<form name="update" action="/api/board-update" method="post">
                                            <input type="hidden" name="idx" value="${content}"/>
                                            제목 : <input type="text" id="title" name="update_title" value="${title}"/></br>
                                            작성자 : <br/>${writer}<br/>
                                        =============================================<br/>
                                            <textarea rows="10" cols="50" name="update_content" id="update_content">${content}</textarea>
                                            <div>
                                                <input type=submit value="수정" onclick="go_list_page(event, '${content}')">
                                                <input type=button value="삭제" onclick="delete_board()"/>
                                            </div>
                                             </form>`

                    $('#form-box'). append(temp_html)
               
             }
        })
    }

    function delete_board(){
        let title = $('#title').val()
        $.ajax({
            type: "POST",
            url: "api/delete",
            data: {title_give: title},
            success: function (response){
                alert(response['msg']);
                window.location.href="/board-list"
            }
        })
    }

    function go_list_page(event, content){
        event.preventDefault()
        let update_content = $('#update_content').val()
        let update_title = $('#title').val()
        console.log(content +'첫 컨텐츠 내용')
        console.log(update_content + '바뀐 컨텐츠 내용')
        console.log(update_title + '바뀐 타이틀')
        // window.location.href="/board-list"
        $.ajax({
            type:"POST",
            url:"/api/board-update",
            data: {title_give: update_title, update_content_give: update_content, content_give: content},
            success: function (response){
                alert(response['msg']);
                window.location.href='/board-list'
            }
        })
    }

오늘 다른 팀들 1차 프로젝트 발표하는걸 보니 진짜 잘하는 팀들도 많고, 발표하는걸 보고 튜터님과 Q&A에서도 또 많은걸 느낀 시간이였다. 다른 팀에서 구현한 기능이 많아서 상대적으로 좀 불안한 느낌이 들기는 했지만 튜터님 말씀을 듣고 보니까 그래도 우리팀이 잘 가고 있다는 걸 또 한번 확인해보기도 한 시간이였다. 앞으로도 열심히해서 발전할줄 아는 개발자가 될 수 있기를 :)