[pricelist] new api to add, delete and modify ingredients

This commit is contained in:
Tim Gröger 2021-03-16 23:27:54 +01:00
parent 0b60c27f32
commit 5e0e5edf6f
3 changed files with 107 additions and 14 deletions

View File

@ -161,6 +161,12 @@ def set_volume(identifier):
return jsonify(pricelist_controller.set_volume(identifier, data))
@pricelist_bp.route("/volumes/<int:identifier>/ingredients", methods=["POST"])
def set_ingredient(identifier):
data = request.get_json()
return jsonify(pricelist_controller.set_ingredient(data, identifier))
@pricelist_bp.route("/volumes/<int:identifier>", methods=["PUT"])
def update_volume(identifier):
data = request.get_json()
@ -171,3 +177,20 @@ def update_volume(identifier):
def delete_volume(identifier):
pricelist_controller.delete_volume(identifier)
return "", NO_CONTENT
@pricelist_bp.route("/ingredients/extraIngredients", methods=["GET"])
def get_extraIngredients():
return jsonify(pricelist_controller.get_extra_ingredients())
@pricelist_bp.route("/ingredients/<int:identifier>", methods=["PUT"])
def update_ingredient(identifier):
data = request.get_json()
return jsonify(pricelist_controller.update_ingredient(identifier, data))
@pricelist_bp.route("/ingredients/<int:identifier>", methods=["DELETE"])
def delete_ingredient(identifier):
pricelist_controller.delete_ingredient(identifier)
return "", NO_CONTENT

View File

@ -75,7 +75,10 @@ class DrinkIngredient(db.Model, ModelSerializeMixin):
@property
def price(self):
try:
return self.drink_ingredient.cost_price_pro_volume * self.volume
except AttributeError:
pass
class Ingredient(db.Model, ModelSerializeMixin):
@ -101,21 +104,10 @@ class DrinkPriceVolume(db.Model, ModelSerializeMixin):
id: int = db.Column("id", db.Integer, primary_key=True)
volume: float = db.Column(db.Numeric(precision=5, scale=2, asdecimal=False))
prices: [DrinkPrice] = db.relationship(DrinkPrice, back_populates="volume", cascade="all,delete,delete-orphan")
ingredients: [DrinkIngredient or ExtraIngredient] = []
_ingredients: [Ingredient] = db.relationship("Ingredient", foreign_keys=Ingredient.volume_id)
ingredients: [Ingredient] = db.relationship("Ingredient", foreign_keys=Ingredient.volume_id)
drink_id = db.Column(db.Integer, db.ForeignKey("drink.id"), nullable=False)
@property
def ingredients(self):
retVal = []
for ingredient in self._ingredients:
if ingredient.drink_ingredient_id != None:
retVal.append(ingredient.drink_ingredient)
if ingredient.extra_ingredient_id != None:
retVal.append(ingredient.extra_ingredient)
return retVal
class Drink(db.Model, ModelSerializeMixin):
"""

View File

@ -160,7 +160,7 @@ def update_volume(identifier, data):
ingredients = values.pop("ingredients")
volume = get_volume(identifier)
for key, value in values.items():
setattr(volume, key, value)
setattr(volume, key, value if value != "" else None)
db.session.commit()
return volume
@ -214,3 +214,81 @@ def delete_price(identifier):
price = get_price(identifier)
db.session.delete(price)
db.session.commit()
def set_drink_ingredient(data):
allowedKeys = DrinkIngredient().serialize().keys()
if "id" in data:
data.pop("id")
if "drink_ingredient" in data:
drink_ingredient_ = data.pop("drink_ingredient")
values = {key: value for key, value in data.items() if key in allowedKeys}
drink_ingredient = DrinkIngredient(**values)
if "id" in drink_ingredient_:
drink = get_drink(drink_ingredient_.get("id"))
if drink:
drink_ingredient.drink_ingredient = drink
db.session.add(drink_ingredient)
db.session.commit()
return drink_ingredient
def get_ingredient(identifier):
return Ingredient.query.get(identifier)
def set_ingredient(data, volume_id):
allowedKeys = Ingredient().serialize().keys()
if "id" in data:
data.pop("id")
values = {key: value for key, value in data.items() if key in allowedKeys}
if "drink_ingredient" in values:
drink_ingredient_values = values.get("drink_ingredient")
if "extra_ingredient" in values:
extra_ingredient_value = values.get("extra_ingredient")
ingredient = Ingredient(**values)
volume = get_volume(volume_id)
if drink_ingredient_values:
ingredient.drink_ingredient = set_drink_ingredient(drink_ingredient_values)
if extra_ingredient_value:
if "id" in extra_ingredient_value:
ingredient.extra_ingredient = get_extra_ingredient(extra_ingredient_value.get("id"))
volume.ingredients.append(ingredient)
db.session.add(ingredient)
db.session.commit()
return ingredient
def update_ingredient(identifier, data):
ingredient = get_ingredient(identifier)
if "extra_ingredient" in data and isinstance(data.get("extra_ingredient"), dict):
if "id" in data.get("extra_ingredient"):
ingredient.extra_ingredient = get_extra_ingredient(data.get("extra_ingredient").get("id"))
if "drink_ingredient" in data and ingredient.drink_ingredient and isinstance(data.get("drink_ingredient"), dict):
if "drink_ingredient" in data.get("drink_ingredient"):
if "id" in data.get("drink_ingredient").get("drink_ingredient"):
ingredient.drink_ingredient.drink_ingredient = get_drink(
data.get("drink_ingredient").get("drink_ingredient").get("id")
)
if "volume" in data.get("drink_ingredient"):
ingredient.drink_ingredient.volume = data.get("drink_ingredient").get("volume")
db.session.commit()
return ingredient
def delete_ingredient(identifier):
ingredient = get_ingredient(identifier)
if ingredient.drink_ingredient:
db.session.delete(ingredient.drink_ingredient)
db.session.delete(ingredient)
db.session.commit()
def get_extra_ingredients():
return ExtraIngredient.query.all()
def get_extra_ingredient(identifier):
return ExtraIngredient.query.get(identifier)