-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
105 lines (86 loc) · 2.85 KB
/
main.py
File metadata and controls
105 lines (86 loc) · 2.85 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
from fastapi import FastAPI, HTTPException
from database import cursor, db
from models import User
app = FastAPI()
# -------------------------------
# Helper function: format DB data
# -------------------------------
def format_users(data):
return [
{
"id": row[0],
"username": row[1],
"password": row[2],
"age": row[3]
}
for row in data
]
# -------------------------------
# 1. SIGNUP API (Create user)
# -------------------------------
@app.post("/signup")
def signup(user: User):
query = "INSERT INTO users (username, password, age) VALUES (%s, %s, %s)"
values = (user.username, user.password, user.age)
cursor.execute(query, values)
db.commit()
return {"message": "User created successfully"}
# -------------------------------
# 2. LOGIN API (Check user)
# -------------------------------
@app.post("/login")
def login(user: User):
query = "SELECT * FROM users WHERE username=%s AND password=%s"
values = (user.username, user.password)
cursor.execute(query, values)
result = cursor.fetchone()
if result:
return {"message": "Login successful"}
else:
raise HTTPException(status_code=401, detail="Invalid credentials")
# -------------------------------
# 3. GET ALL USERS
# -------------------------------
@app.get("/users")
def get_users():
cursor.execute("SELECT * FROM users")
result = cursor.fetchall()
return format_users(result)
# -------------------------------
# 4. GET USER BY ID
# -------------------------------
@app.get("/users/{id}")
def get_user(id: int):
cursor.execute("SELECT * FROM users WHERE id=%s", (id,))
result = cursor.fetchone()
if not result:
raise HTTPException(status_code=404, detail="User not found")
return {
"id": result[0],
"username": result[1],
"password": result[2],
"age": result[3]
}
# -------------------------------
# 5. UPDATE USER
# -------------------------------
@app.put("/users/{id}")
def update_user(id: int, user: User):
query = "UPDATE users SET username=%s, password=%s, age=%s WHERE id=%s"
values = (user.username, user.password, user.age, id)
cursor.execute(query, values)
db.commit()
if cursor.rowcount == 0:
raise HTTPException(status_code=404, detail="User not found")
return {"message": "User updated successfully"}
# -------------------------------
# 6. DELETE USER
# -------------------------------
@app.delete("/users/{id}")
def delete_user(id: int):
query = "DELETE FROM users WHERE id=%s"
cursor.execute(query, (id,))
db.commit()
if cursor.rowcount == 0:
raise HTTPException(status_code=404, detail="User not found")
return {"message": "User deleted successfully"}