-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
65 lines (58 loc) · 1.74 KB
/
main.py
File metadata and controls
65 lines (58 loc) · 1.74 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
from fastapi import FastAPI, HTTPException
from uuid import UUID, uuid4
from typing import List
from models import User, Gender, Role, UserUpdateRequest
app = FastAPI()
db: List[User]=[
User(
id=UUID("14917fea-9f67-465f-b108-84c9b283aab0"),
first_name="Praneeth",
last_name="Settipalli",
gender = Gender.male,
roles=[Role.student]
),
User(
id=uuid4(),
first_name="Vinusha",
last_name="Settipalli",
gender = Gender.female,
roles=[Role.admin, Role.user]
)
]
@app.get("/")
async def root():
return {"Hello": "User"}
@app.get("/api/v1/users")
async def fetch_users():
return db
@app.post("/api/v1/users")
async def register_user(user: User):
db.append(user)
return {"id":user.id}
@app.delete("/api/v1/users/{user_id}")
async def delete_user(user_id: UUID):
for user in db:
if user.id == user_id:
db.remove(user)
return
raise HTTPException(
status_code=404,
detail=f"user with id: {user_id} does not exist"
)
@app.put("/api/v1/users/{user_id}")
async def update_user(user_update: UserUpdateRequest, user_id: UUID):
for user in db:
if user.id == user_id:
if user_update.first_name is not None:
user.first_name = user_update.first_name
if user_update.last_name is not None:
user.last_name = user_update.last_name
if user_update.middle_name is not None:
user.middle_name = user_update.middle_name
if user_update.roles is not None:
user.roles = user_update.roles
return
raise HTTPException(
status_code=404,
detail=f"user with id: {user_id} does not exist"
)