Digitale bierlijst

consumptions.py 943B

12345678910111213141516171819202122232425262728293031323334353637
  1. """
  2. Provides routes related to Consumption objects.
  3. """
  4. from flask import jsonify
  5. from sqlalchemy.exc import SQLAlchemyError
  6. from piket_server.flask import app, db
  7. from piket_server.models import Consumption
  8. @app.route("/consumptions/<int:consumption_id>", methods=["DELETE"])
  9. def reverse_consumption(consumption_id: int):
  10. """ Reverse a consumption. """
  11. consumption = Consumption.query.get_or_404(consumption_id)
  12. if consumption.reversed:
  13. return (
  14. jsonify(
  15. {
  16. "error": "Consumption already reversed",
  17. "consumption": consumption.as_dict,
  18. }
  19. ),
  20. 409,
  21. )
  22. try:
  23. consumption.reversed = True
  24. db.session.add(consumption)
  25. db.session.commit()
  26. except SQLAlchemyError:
  27. return jsonify({"error": "Database error."}), 500
  28. return jsonify(consumption=consumption.as_dict), 200