-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackend.py
More file actions
103 lines (83 loc) · 3.05 KB
/
backend.py
File metadata and controls
103 lines (83 loc) · 3.05 KB
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
from flask import Flask, jsonify, request, make_response
import secrets
import psycopg2
import signal
from psycopg2 import Error
from user_management import user_management, users_bp
app = Flask(__name__)
app.register_blueprint(users_bp)
db_conn = None
db_cursor = None
tasks = [()]
tasks[0] = (0, [''])
def create_connection():
conn = None
try:
conn = psycopg2.connect(
host="localhost",
port="5432", # default for PostgreSQL
database="todolist_database",
user="todolist_admin",
password="todolist_admin_password"
)
print("Connected to PostgreSQL database")
return conn
except (Exception, Error) as error:
print("Error while connecting to PostgreSQL database:", error)
if conn is not None:
conn.close()
def shutdown(signal, frame):
# Shutdown the application gracefully
print("Server shutting down...")
db_cursor.close()
db_conn.close()
raise SystemExit
signal.signal(signal.SIGINT, shutdown)
def find_row_id(user_id, tasks_list):
for i, (id_) in enumerate(tasks_list):
if id_[0] == user_id:
return i # Return the index of the tuple
return -1 # User ID not found
@app.route('/tasks', methods=['POST', 'GET', 'DELETE'])
def handle_tasks():
user_id = request.json.get('user_id')
if user_management.active_user.get_id() is not None and user_id == user_management.active_user.get_id():
list_id = find_row_id(user_id, tasks)
if request.method == 'POST':
task = request.json.get('task')
if list_id == -1:
tasks.append((user_id, []))
tasks[-1][1].append(task)
return jsonify({'message': 'Task added successfully'}), 201
elif request.method == 'GET':
list_id = find_row_id(user_id, tasks)
if list_id != -1:
tasks_dict = {'tasks': tasks[list_id][1]}
return jsonify(tasks_dict), 200
else:
return jsonify({'tasks': ''}), 200
elif request.method == 'DELETE':
list_id = find_row_id(user_id, tasks)
if list_id != -1:
del tasks[list_id]
return jsonify({'message': 'Tasks deleted'}), 200
else:
return jsonify({'message': 'Tasks deleted'}), 200
else:
return jsonify({'message': 'User not logged in'}), 401
if __name__ == '__main__':
db_conn = create_connection()
db_cursor = db_conn.cursor()
user_management.set_db(db_conn, db_cursor)
try:
# Check if the 'users' table exists
if user_management.table_exists():
print("The 'users' table already exists in the database")
else:
print("The 'users' table does not exist in the database")
print("Creating...")
user_management.table_create()
print("Table 'users' created successfully")
except (Exception, Error) as error:
print("Error while checking table existence:", error)
app.run(debug=True)