from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
import time
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///primemeat.db"
# DB
db = SQLAlchemy(app)
class Umsatz(db.Model):
id = db.Column('id', db.Integer, primary_key = True)
monat = db.Column(db.String(7))
wert = db.Column(db.Integer)
def __init__(self, monat, wert):
self.monat = monat
self.wert = wert
@app.route("/")
def hello():
db.create_all()
return "
Hello, Prime Meat!"
@app.route('/time')
def get_current_time():
return jsonify({'time': time.time()})
@app.route("/api/umsatz", methods=["GET", "POST", "DELETE"])
def umsatz():
method = request.method
if (method.lower() == "get"):
umsatz = Umsatz.query.all()
return jsonify([{"id": i.id, "monat": i.monat, "wert": i.wert} for i in umsatz])
elif (method.lower() == "post"):
try:
monat = request.json["monat"]
wert = request.json["wert"]
if (monat and wert):
try:
umsatz = Umsatz(monat, wert)
db.session.add(umsatz)
db.session.commit()
return jsonify({"success": True})
except Exception as e:
return ({"error": e})
else:
return jsonify({"error": "Invalid form"})
except:
return jsonify({"error": "Invalid form"})
elif (method.lower() == "delete"):
try:
uid = request.json["id"]
if (uid):
try:
umsatz = Umsatz.query.get(uid)
db.session.delete(umsatz)
db.session.commit()
return jsonify({"success": True})
except Exception as e:
return jsonify({"error": e})
else:
return jsonify({"error": "Invalid form"})
except:
return jsonify({"error": "m"})
if __name__ == "__main__":
app.run(host='0.0.0.0')