Beispiel #1
0
def add_beer(beer_name):
    session = Session()
    incoming_data = request.get_json()
    beer = session.query(Beer).filter_by(name=beer_name.lower()).first()
    if not beer:
        beer = Beer(
            beer_name,
            int(incoming_data["rating"]),
            incoming_data["brewerydb_id"],
            comment=incoming_data.get("comment")
        )
        session.add(beer)
    else:
        beer.rating = int(incoming_data["rating"])
        beer.brewerydb_id = incoming_data["brewerydb_id"]
        beer.comment = incoming_data.get("comment")
        beer.validate()
    session.commit()
    response = jsonify(ref="/beer/{0}".format(beer.name))
    response.status_code = 201
    return response