PUT请求与子文档

huangapple go评论60阅读模式
英文:

PUT request with subdocuments

问题

我目前正在为我的后端API创建一个PUT请求,允许用户向数据集中添加餐厅。目前,在数据集中,餐厅的纬度和经度位于geometry > location下。我不确定如何在后端API中指定这一点。

以下是我目前的后端API代码:

@app.route("/api/v1.0/restaurant", methods=["POST"])
def add_new_restaurant():
    if "restaurant" in request.form and "city" in request.form:
        new_restaurant = {
            "restaurant": request.form["restaurant"],
            "city": request.form["city"],
            "reviews": []
        }
        new_restaurant_id = restaurants.insert_one(new_restaurant)
        new_restaurant_link = "http://localhost:5000/api/v1.0/restaurants/" + str(new_restaurant_id.inserted_id)
        return make_response(jsonify({"url": new_restaurant_link}), 201)

希望这对你有所帮助。

英文:

I'm currently creating a PUT request for my backend API to allow users to add restaurants to a dataset. Currently, in the dataset, the lat and lng of the restaurant is under geometry > location. Im unsure how I would specify this in the backend API.

PUT请求与子文档

Here is what I have for the backend API so far:

    @app.route("/api/v1.0/restaurant", methods = ["POST"])
    
    def add_new_restaurant():
        if "restaurant" in request.form and "city" in request.form:
            new_restaurant = {
                "restaurant": request.form["restaurant"],
                "city": request.form["city"],
                "reviews": []
            }
            new_restaurant_id = restaurants.insert_one(new_restaurant)
            new_restaurant_link = "http://localhost:5000/api/v1.0/restaurants/" \  +          str(new_restaurant_id.inserted_id)
            return make_response( jsonify(  { "url" : new_restaurant_link } ), 201 )

答案1

得分: 0

这是嵌套对象,因此您可以将它插入为如下所示。您可以用一个映射到一个 dict 值的 geometry 键表示它,该值具有映射到另一个指定经纬度的 dict 的 location 键。

new_restaurant = {
    "restaurant": request.form["restaurant"],
    "city": request.form["city"],
    "reviews": [],
    "geometry": {
        "location": {
            "lat": LAT_VALUE,
            "long": LONG_VALUE
        }
    }
}
英文:

It's a nested object so you can insert it as such. You can represent it in a dict with a geometry key that maps to a dict value, which has a location key that maps to another dict that specifies that lat/long.

new_restaurant = {
    "restaurant": request.form["restaurant"],
    "city": request.form["city"],
    "reviews": [],
    "geometry": {
        "location": {
            "lat": LAT_VALUE,
            "long": LONG_VALUE
        }
    }
}

huangapple
  • 本文由 发表于 2023年2月6日 19:58:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75361024.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定