明日学習団制作チャレンジ6期(5)


#コード開発ログの冒頭の一言
  • コメント(コメント)実施
  • #開発作業内容
    [今日の目標]
    1.コメント(コメント)機能の実現
    [ディスカッションの内容]
    1.レシピをユーザーが見たいレシピを追加する機能に変更する
    仕事中の話題/悩み...そして解決した問題
    詳細レシピページにアクセスして一番下にコメントする機能を実現!
    app.py
    
    @app.route("/review", methods=["POST"])
    def add_review():
        try:
            recipe_receive = request.form["recipe_give"]
            comment_receive = request.form["comment_give"]
            if session["user_id"] is None:
                return jsonify({"msg": "로그인이 필요합니다."})
            else:
                doc = {
                    "user_id": session["user_id"],
                    "name": recipe_receive,
                    "comment": comment_receive,
                    "datetime": time.strftime("%Y-%m-%d %H:%M"),
                }
    
                db.reviews.insert_one(doc)
        except Exception:
            return jsonify({"msg": "로그인이 필요합니다."})
    
        return jsonify({"user_id": session["user_id"]})
    
    
    @app.route("/review/show", methods=["POST"])
    def read_reviews():
        name_receive = request.form["name_give"]
        reviews = list(db.reviews.find({"name": name_receive}, {"_id": False}))
        return jsonify({"all_reviews": reviews})
    
    recipe.js
    
    function review_show(name){
        $.ajax({
            type: "POST",
            url: "/review/show",
            data: {name_give:name},
            success: function (response) {
                let reviews = response['all_reviews']
                for (let i = 0; i < reviews.length; i++) {
                    let comment = reviews[i]['comment']
                    let user_id = reviews[i]['user_id']
                    let datetime = reviews[i]['datetime']
                    let review_html = `<li> ${comment} / ${user_id} / ${datetime} </li>`
                    
                    $('#review-list').append(review_html)
                }
            }
        })
    }
    
    function add_review(name){
        let comment = $('#new-review').val();
    
        $.ajax({
            type: "POST",
            url: "/review",
            data: {recipe_give:name, comment_give:comment},
            success: function (response) {
                let user_id = response['user_id']
                let review_html = `<li> ${comment} / ${user_id} / ${date} </li>`
                if (user_id == undefined) {
                    alert('로그인 후 이용해주세요.')
                } else{
                    $('#review-list').append(review_html)
                    alert('추가 완료')
                }
            }
        })
    }
    #To-do List
    フォーミュラ要求機能の実装