You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
2.1 KiB

3 years ago
  1. from flask import Flask, request, jsonify
  2. from flask_sqlalchemy import SQLAlchemy
  3. import time
  4. app = Flask(__name__)
  5. app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///primemeat.db"
  6. # DB
  7. db = SQLAlchemy(app)
  8. class Umsatz(db.Model):
  9. id = db.Column('id', db.Integer, primary_key = True)
  10. monat = db.Column(db.String(7))
  11. wert = db.Column(db.Integer)
  12. def __init__(self, monat, wert):
  13. self.monat = monat
  14. self.wert = wert
  15. @app.route("/")
  16. def hello():
  17. db.create_all()
  18. return "<h1 style='color:blue'>Hello, Prime Meat!<h1>"
  19. @app.route('/time')
  20. def get_current_time():
  21. return jsonify({'time': time.time()})
  22. @app.route("/api/umsatz", methods=["GET", "POST", "DELETE"])
  23. def umsatz():
  24. method = request.method
  25. if (method.lower() == "get"):
  26. umsatz = Umsatz.query.all()
  27. return jsonify([{"id": i.id, "monat": i.monat, "wert": i.wert} for i in umsatz])
  28. elif (method.lower() == "post"):
  29. try:
  30. monat = request.json["monat"]
  31. wert = request.json["wert"]
  32. if (monat and wert):
  33. try:
  34. umsatz = Umsatz(monat, wert)
  35. db.session.add(umsatz)
  36. db.session.commit()
  37. return jsonify({"success": True})
  38. except Exception as e:
  39. return ({"error": e})
  40. else:
  41. return jsonify({"error": "Invalid form"})
  42. except:
  43. return jsonify({"error": "Invalid form"})
  44. elif (method.lower() == "delete"):
  45. try:
  46. uid = request.json["id"]
  47. if (uid):
  48. try:
  49. umsatz = Umsatz.query.get(uid)
  50. db.session.delete(umsatz)
  51. db.session.commit()
  52. return jsonify({"success": True})
  53. except Exception as e:
  54. return jsonify({"error": e})
  55. else:
  56. return jsonify({"error": "Invalid form"})
  57. except:
  58. return jsonify({"error": "m"})
  59. if __name__ == "__main__":
  60. app.run(host='0.0.0.0')