from os import name
from flask import Flask, jsonify, request, render_template

# virtualenv venv --python=python3.8.5
# simple Flask application to save and retrive different types of stores and items

stores = [
    {
        "name": "Unique furnitures",
        "items": [
            {"name": "chair", "price": 999}
        ]
    }
]

app  = Flask(__name__)

#  endpoints

# GET /
@app.route('/')
def home():
    return render_template('index.html')

# POST /store  {name: } 
@app.route('/store', methods=["POST"])
def save_store():
    data = request.get_json()
    new_store = {
        "name": data['name'],
        "items": []
    }
    stores.append(new_store)
    return jsonify(new_store)
    

# GET /store/<string:name>
@app.route('/store/<string:name>')
def get_store(name):
    for store in stores:
        if store["name"] == name:
            return jsonify(store)
    return jsonify({"message": "Store not found"})

# GET /store  -- get all stores
@app.route('/store')
def get_all_stores():
    return jsonify({"stores": stores})

# POST /store/<string:name>/item {name:, price: }
@app.route('/store/<string:name>/item', methods=["POST"])
def save_store_item(name):
    data = request.get_json()
    for store in stores:
        if store['name'] == name:
            new_item = {
                "name": data['name'],
                "price": data["price"]
            }
            store['items'].append(new_item)
            return jsonify(store)
    return jsonify({"message": "Store not found"})

            

# GET /store/<string:store_name>/item/<string:item_name>
@app.route('/store/<string:store_name>/item/<string:item_name>')
def get_item(store_name, item_name):
    for store in stores:
        if store["name"] == store_name:
            items = store["items"]
            for item in items:
                if item["name"] == item_name:
                    return jsonify(item)  
      
            return jsonify({"message": "Item not found"})
    return jsonify({"message": "Store not found"})

# GET /store/<string:name>/item
@app.route('/store/<string:store_name>/item')
def get_all_item(store_name):
    for store in stores:
        if store["name"] == store_name:
            items = store["items"]
            return jsonify({"items":items})

    return jsonify({"message": "Store not found"})


app.run(port=4000)

from os import name
from flask import Flask, jsonify, request, render_template

# virtualenv venv --python=python3.8.5
# simple Flask application to save and retrive different types of stores and items

stores = [
    {
        "name": "Unique furnitures",
        "items": [
            {"name": "chair", "price": 999}
        ]
    }
]

app  = Flask(__name__)

#  endpoints

# GET /
@app.route('/')
def home():
    return render_template('index.html')

# POST /store  {name: } 
@app.route('/store', methods=["POST"])
def save_store():
    data = request.get_json()
    new_store = {
        "name": data['name'],
        "items": []
    }
    stores.append(new_store)
    return jsonify(new_store)
    

# GET /store/<string:name>
@app.route('/store/<string:name>')
def get_store(name):
    for store in stores:
        if store["name"] == name:
            return jsonify(store)
    return jsonify({"message": "Store not found"})

# GET /store  -- get all stores
@app.route('/store')
def get_all_stores():
    return jsonify({"stores": stores})

# POST /store/<string:name>/item {name:, price: }
@app.route('/store/<string:name>/item', methods=["POST"])
def save_store_item(name):
    data = request.get_json()
    for store in stores:
        if store['name'] == name:
            new_item = {
                "name": data['name'],
                "price": data["price"]
            }
            store['items'].append(new_item)
            return jsonify(store)
    return jsonify({"message": "Store not found"})

            

# GET /store/<string:store_name>/item/<string:item_name>
@app.route('/store/<string:store_name>/item/<string:item_name>')
def get_item(store_name, item_name):
    for store in stores:
        if store["name"] == store_name:
            items = store["items"]
            for item in items:
                if item["name"] == item_name:
                    return jsonify(item)  
      
            return jsonify({"message": "Item not found"})
    return jsonify({"message": "Store not found"})

# GET /store/<string:name>/item
@app.route('/store/<string:store_name>/item')
def get_all_item(store_name):
    for store in stores:
        if store["name"] == store_name:
            items = store["items"]
            return jsonify({"items":items})

    return jsonify({"message": "Store not found"})


app.run(port=4000)

# virtualenv venv --python=python3.8.5 # simple Flask application to save and retrive different types of stores and items #  endpoints # GET / # POST /store  {name: } # GET /store/<string:name> # GET /store  -- get all stores # POST /store/<string:name>/item {name:, price: } # GET /store/<string:store_name>/item/<string:item_name> # GET /store/<string:name>/item