[pricelist] first step to add, modify and delete volumes

This commit is contained in:
Tim Gröger 2021-03-15 23:53:21 +01:00
parent df1610557f
commit e26b7b8c96
2 changed files with 67 additions and 3 deletions

View File

@ -137,7 +137,7 @@ def get_prices(identifier=None):
return jsonify(result)
@pricelist_bp.route("/prices/volumes/<int:identifier>", methods=["POST"])
@pricelist_bp.route("/volumes/<int:identifier>/prices", methods=["POST"])
def create_price(identifier):
data = request.get_json()
return jsonify(pricelist_controller.set_price(identifier, data))
@ -152,4 +152,22 @@ def modify_price(identifier):
@pricelist_bp.route("/prices/<int:identifier>", methods=["DELETE"])
def delete_price(identifier):
pricelist_controller.delete_price(identifier)
return NO_CONTENT
return "", NO_CONTENT
@pricelist_bp.route("/drinks/<int:identifier>/volumes", methods=["POST"])
def set_volume(identifier):
data = request.get_json()
return jsonify(pricelist_controller.set_volume(identifier, data))
@pricelist_bp.route("/volumes/<int:identifier>", methods=["PUT"])
def update_volume(identifier):
data = request.get_json()
return jsonify(pricelist_controller.update_volume(identifier, data))
@pricelist_bp.route("/volumes/<int:identifier>", methods=["DELETE"])
def delete_volume(identifier):
pricelist_controller.delete_volume(identifier)
return "", NO_CONTENT

View File

@ -124,6 +124,50 @@ def get_volume(identifier):
return DrinkPriceVolume.query.get(identifier)
def get_volumes(drink_id=None):
if drink_id:
return DrinkPriceVolume.query.filter(DrinkPriceVolume.drink_id == drink_id).all()
return DrinkPriceVolume.query.all()
def set_volume(identifier, data):
allowed_keys = DrinkPriceVolume().serialize().keys()
if "id" in data:
data.pop("id")
values = {key: value for key, value in data.items() if key in allowed_keys}
volume = DrinkPriceVolume(**values)
drink = get_drink(identifier)
if not drink:
raise BadRequest
drink.volumes.append(volume)
db.session.add(volume)
db.session.commit()
return volume
def update_volume(identifier, data):
allowed_keys = DrinkPriceVolume().serialize().keys()
if "id" in data:
data.pop("id")
values = {key: value for key, value in data.items() if key in allowed_keys}
if "prices" in values:
prices = values.pop("prices")
if "ingredients" in values:
ingredients = values.pop("ingredients")
volume = get_volume(identifier)
for key, value in values.items():
setattr(volume, key, value)
db.session.commit()
return volume
def delete_volume(identifier):
volume = get_volume(identifier)
db.session.delete(volume)
db.session.commit()
def get_price(identifier):
if isinstance(identifier, int):
return DrinkPrice.query.get(identifier)
@ -138,6 +182,8 @@ def get_prices(volume_id=None):
def set_price(identifier, data):
allowed_keys = DrinkPrice().serialize().keys()
if "id" in data:
data.pop("id")
values = {key: value for key, value in data.items() if key in allowed_keys}
price = DrinkPrice(**values)
volume = get_volume(identifier)
@ -155,7 +201,7 @@ def update_price(identifier, data):
data.pop("id")
values = {key: value for key, value in data.items() if key in allowed_keys}
price = get_price(identifier)
for key, value in values:
for key, value in values.items():
setattr(price, key, value)
db.session.commit()
return price