This repository has been archived by the owner on Oct 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusers.py
104 lines (81 loc) · 3.03 KB
/
users.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
104
"""
* users.py
*
* Copyright 2024, Filippini Giovanni
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
"""
from flask import abort, make_response, request
import sqlite3
from database import create_connection
DATABASE = "users.db"
def get_db_connection():
conn = sqlite3.connect(DATABASE)
conn.row_factory = sqlite3.Row
return conn
def read_all():
conn = get_db_connection()
users = conn.execute("SELECT * FROM users").fetchall()
conn.close()
return [dict(row) for row in users]
def create():
user = request.get_json()
name = user.get("name")
otp = user.get("otp")
vector = user.get("vector")
if not name or not otp or not vector:
abort(400, "Invalid input")
conn = get_db_connection()
conn.execute("INSERT INTO users (name, otp, vector) VALUES (?, ?, ?)", (name, otp, vector))
conn.commit()
user_id = conn.execute("SELECT last_insert_rowid()").fetchone()[0]
conn.close()
return {"id": user_id, "name": name, "otp": otp, "vector": vector}, 201
def read_one(userId):
conn = get_db_connection()
user = conn.execute("SELECT * FROM users WHERE id = ?", (userId,)).fetchone()
conn.close()
if user is None:
abort(404, f"User with id {userId} not found")
return dict(user)
def update(userId):
user = request.get_json()
name = user.get("name")
otp = user.get("otp")
vector = user.get("vector")
conn = get_db_connection()
existing_user = conn.execute("SELECT * FROM users WHERE id = ?", (userId,)).fetchone()
if existing_user is None:
conn.close()
abort(404, f"User with id {userId} not found")
conn.execute("UPDATE users SET name = ?, otp = ?, vector = ? WHERE id = ?", (name, otp, vector, userId))
conn.commit()
conn.close()
return {"id": userId, "name": name, "otp": otp, "vector": vector}
def delete(userId):
conn = get_db_connection()
existing_user = conn.execute("SELECT * FROM users WHERE id = ?", (userId,)).fetchone()
if existing_user is None:
conn.close()
abort(404, f"User with id {userId} not found")
conn.execute("DELETE FROM users WHERE id = ?", (userId,))
conn.commit()
conn.close()
return make_response(f"User with id {userId} successfully deleted", 200)
def read_by_otp(otp):
conn = get_db_connection()
user = conn.execute("SELECT * FROM users WHERE otp = ?", (otp,)).fetchone()
conn.close()
if user is None:
abort(404, f"User with OTP {otp} not found")
return dict(user)