-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
74 lines (50 loc) · 1.46 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from flask import Flask, render_template, jsonify, request
from flask_sqlalchemy import SQLAlchemy
from dataclasses import dataclass
import os
db_uri = (
"postgresql://"
+ os.environ["POSTGRES_USER"]
+ ":"
+ os.environ["POSTGRES_PASSWORD"]
+ "@"
+ os.environ["POSTGRES_HOST"]
+ ":5432/"
+ os.environ["POSTGRES_DB"]
)
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = db_uri
print(db_uri)
db = SQLAlchemy(app)
@dataclass
class Item(db.Model):
id: int
title: str
description: str
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String, nullable=False)
description = db.Column(db.String, nullable=False)
with app.app_context():
db.create_all()
@app.route("/")
def index():
return render_template("index.html"), 200
@app.route("/get-items")
def get_items():
items = Item.query.all()
return jsonify(items), 200
@app.route("/add-item", methods=["POST"])
def add_item():
data = request.get_json()
item = Item(title=data["title"], description=data["description"])
db.session.add(item)
db.session.commit()
return jsonify({"success": True}), 200
@app.route("/delete-item/<int:id>", methods=["POST"])
def delete_item(id):
item = db.get_or_404(Item, id)
db.session.delete(item)
db.session.commit()
return jsonify({"success": True}), 200
if __name__ == "__main__":
app.run(host="0.0.0.0", port=os.environ.get("PORT", 8000), debug=False)