-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
57 lines (49 loc) · 1.93 KB
/
main.py
File metadata and controls
57 lines (49 loc) · 1.93 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
from fastapi import FastAPI, HTTPException
from model import User, Gender, Role, UpdateUser
from typing import List
from uuid import uuid4, UUID
app = FastAPI()
db: List[User] = [
User(id=UUID('0b0d6f94-1240-4bd1-889d-302b95656e75'), first_name='John', last_name='testing', middlename='', gender=Gender.Male, role=[Role.Admin, Role.User]),
User(id=UUID('00ba28d2-bfb6-4248-b3ca-c9c7f6c8d108'), first_name='Mary', last_name='testing', middlename='pydantic', gender=Gender.Female, role=[Role.User]),
User(id=UUID('feb40e09-c3d1-4bd8-b825-6a72106fe5f6'), first_name='Jamila', last_name='Ibrahim', middlename='', gender=Gender.Male, role=[Role.Student]),
]
@app.get('/')
def root():
return {'message': 'Hello World'}
@app.get('/api/v1/users')
async def fetch_users():
return db
@app.post('/api/v1/users')
async def add_user(user: User):
db.append(user)
return {'id': user.id}
@app.put('/api/v1/users/{id}')
async def update_user(update: UpdateUser, id: UUID):
for user in db:
if user.id == id:
if update.first_name is not None:
user.first_name = update.first_name
if update.last_name is not None:
user.last_name = update.last_name
if update.middlename is not None:
user.middlename = update.middlename
if update.gender is not None:
user.gender = update.gender
if update.role is not None:
user.role = update.role
return {'message': 'User updated successfully'}
raise HTTPException(
status_code=404,
detail=f'User with id: {user.id} not found',
)
@app.delete('/api/v1/users/{id}')
async def delete_user(id: UUID):
for user in db:
if user.id == id:
db.remove(user)
return {'message': 'User deleted successfully'}
raise HTTPException(
status_code=404,
detail=f'User with id: {user.id} not found',
)