-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
103 lines (81 loc) · 2.9 KB
/
main.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
"""
CREATE USER myuser WITH PASSWORD 'mypassword';
ALTER USER myuser WITH SUPERUSER;
CREATE DATABASE mydatabase;
GRANT ALL PRIVILEGES ON DATABASE mydatabase TO myuser;
CREATE TABLE users (id SERIAL PRIMARY KEY, nome VARCHAR(100) NOT NULL, idade INT NOT NULL);
"""
from flask import Flask, render_template, redirect, jsonify, request, url_for, flash
import psycopg2
from dotenv import load_dotenv
import os
load_dotenv()
def connect_database():
conn = psycopg2.connect(
host=os.environ.get("DB_HOST"),
dbname=os.environ.get("DB_NAME"),
user=os.environ.get("DB_USER"),
password=os.environ.get("DB_PASSWORD")
)
return conn
app = Flask(__name__)
@app.route("/")
def index():
try:
conn = connect_database()
if conn:
cursor = conn.cursor()
search = request.args.get('search')
if search:
query = "SELECT * FROM users WHERE nome ILIKE %s;"
cursor.execute(query, ('%' + search + '%',))
else:
cursor.execute("SELECT * FROM users;")
users = cursor.fetchall()
print("Connection to database ok")
return render_template("index.html", users=users)
else:
return jsonify({"Error 01": "Failed to connect to the database"})
except Exception as e:
return jsonify({"Error 02": f"Failed to connect to the database: {str(e)}"})
@app.route("/add_person", methods=["GET", "POST"])
def add_person():
if request.method == "POST":
conn = connect_database()
cursor = conn.cursor()
nome = request.form["nome"]
idade = request.form["idade"]
cursor.execute('INSERT INTO users (nome, idade) VALUES (%s, %s)', (nome, idade))
cursor.close()
conn.commit()
conn.close()
return redirect(url_for("index"))
return render_template("add_person.html")
@app.route("/delete_person/<int:id>", methods=["POST"])
def delete_person(id):
conn = connect_database()
cursor = conn.cursor()
cursor.execute("DELETE FROM users WHERE id = %s", (id,))
cursor.close()
conn.commit()
conn.close()
return redirect(url_for("index"))
@app.route("/update_person/<int:id>", methods=["GET", "POST"])
def update_person(id):
conn = connect_database()
cursor = conn.cursor()
cursor.execute("SELECT * FROM users WHERE id = %s", (id,))
user = cursor.fetchone()
if request.method == "POST":
nome = request.form["nome"]
idade = request.form["idade"]
cursor.execute('UPDATE users SET nome = %s, idade = %s WHERE id = %s', (nome, idade, id))
cursor.close()
conn.commit()
conn.close()
return redirect(url_for("index"))
cursor.close()
conn.close()
return render_template("update_person.html", user=user)
if __name__ == "__main__":
app.run(debug=True)