from flask import Flask, render_template, jsonify, request
from pymongo import MongoClient

app = Flask(__name__)

client = MongoClient('localhost', 27017)
db = client.dbhomework


## HTML 화면 보여주기
@app.route('/')
def homework():
    return render_template('index.html')


# 주문하기(POST) API
@app.route('/order', methods=['POST'])
def save_order():

    name_receive = request.form['name_give']
    period_receive = request.form['period_give']
    address_receive = request.form['address_give']
    phone_receive = request.form['phone_give']

    doc = {
            'name': name_receive,
            'period': period_receive,
            'address': address_receive,
            'phone_number': phone_receive,
        }

    db.alonemall.insert_one(doc)

    return jsonify({'result': 'success', 'msg': '저장 완료!'})

# 주문 목록보기(Read) API
@app.route('/order', methods=['GET'])
def view_orders():
# 여길 채워나가세요!

    orders = list(db.alonemall.find({}, {"_id": False}))
    # print(alonemall)

    return_val = { 'result': 'success', 'orderList': orders }

    return jsonify(return_val)

if __name__ == '__main__':
    app.run('0.0.0.0', port=5000, debug=True)