-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
147 lines (123 loc) · 4.19 KB
/
Copy pathserver.py
File metadata and controls
147 lines (123 loc) · 4.19 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
from datetime import datetime
from flask import Flask, request, jsonify, render_template
import sqlite3
# Check this seems interesting: TODO https://jokeapi.dev/#getting-started
app = Flask(__name__)
DB_PATH = "todo.db"
# Initialize the database
def init_db():
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
# Create users table
cursor.execute('''
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE NOT NULL,
token TEXT UNIQUE NOT NULL
)
''')
# Create todo table
cursor.execute('''
CREATE TABLE IF NOT EXISTS todo (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
time TEXT,
title TEXT,
body TEXT,
neededTime TEXT,
FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE
)
''')
conn.commit()
conn.close()
init_db()
@app.route("/")
def homepage():
return render_template("homepage.html")
# User registration
@app.route("/register", methods=["POST"])
def register_user():
data = request.json
username = data.get("username")
token = data.get("token")
if not username or not token:
return jsonify({"message": "Missing username or token"}), 400
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
try:
cursor.execute("INSERT INTO users (username, token) VALUES (?, ?)", (username, token))
conn.commit()
conn.close()
return jsonify({"message": "User registered successfully"}), 201
except sqlite3.IntegrityError:
conn.close()
return jsonify({"message": "Username already exists"}), 400
# User authentication
@app.route("/auth", methods=["POST"])
def authenticate_user():
data = request.json
token = data.get("token")
if not token:
return jsonify({"message": "Missing token"}), 400
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
cursor.execute("SELECT id, username FROM users WHERE token = ?", (token,))
user = cursor.fetchone()
conn.close()
if user:
return jsonify({"user": user, "user_id": user[0], "username": user[1]}), 200
return jsonify({"message": "Invalid token"}), 401
# Add a note
@app.route("/notes", methods=["POST"])
def add_note():
data = request.json
user_id = data.get("user_id")
title = data.get("title")
body = data.get("body")
needed_time = data.get("neededTime")
time = data.get("time")
if not user_id or not title or not needed_time:
return jsonify({"message": "Missing required fields"}), 400
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
cursor.execute('''
INSERT INTO todo (user_id, time, title, body, neededTime)
VALUES (?, ?, ?, ?, ?)
''', (user_id, time, title, body, needed_time))
conn.commit()
conn.close()
return jsonify({"message": "Note added successfully"}), 201
# Get notes for a user
@app.route("/notes/<int:user_id>", methods=["GET"])
def get_notes(user_id):
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
cursor.execute("SELECT * FROM todo WHERE user_id = ?", (user_id,))
notes = cursor.fetchall()
conn.close()
return jsonify({"notes": notes}), 200
# Get today's notes for a user
@app.route("/notes/today/<int:user_id>", methods=["GET"])
def get_today_notes(user_id):
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
cursor.execute("SELECT * FROM todo WHERE user_id = ? AND neededTime LIKE ?", (user_id, f"{datetime.now().strftime('%Y-%m-%d')}%"))
notes = cursor.fetchall()
conn.close()
return jsonify({"notes": notes}), 200
# Delete a note
@app.route("/notes", methods=["DELETE"])
def delete_note():
data = request.json
user_id = data.get("user_id")
note_id = data.get("note_id")
if not user_id or not note_id:
return jsonify({"message": "Missing required fields"}), 400
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
cursor.execute("DELETE FROM todo WHERE id = ? AND user_id = ?", (note_id, user_id))
conn.commit()
conn.close()
return jsonify({"message": "Note deleted successfully"}), 200
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000, debug=True)