-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
79 lines (63 loc) · 1.96 KB
/
main.py
File metadata and controls
79 lines (63 loc) · 1.96 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
from uuid import uuid4, UUID
from typing import List
import random
from fastapi import FastAPI, HTTPException
from models import User, Gender, Role, ChangeUser
app = FastAPI()
db: List[User] = [
User(id=uuid4(),
first_name="Mila",
last_name="Py",
gender=Gender.female,
roles=[Role.user]
),
User(id=uuid4(),
first_name="James",
last_name="Jones",
gender=Gender.male,
roles=[Role.admin]
)
]
@app.get('/')
async def root():
return {'example': 'this is an example', 'data': 100}
@app.get('/api/v1/users')
async def fetch_users():
return db
@app.post('/api/v1/users')
async def register_users(user: User):
db.append(user)
return {"id": user.id}
@app.put('/api/v1/users/{user_id}')
async def update_users(user_update: ChangeUser,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.roles is not None:
user.roles = user_update.roles
return f"{user_id} has been updated."
raise HTTPException(
status_code=404,
detail=f"user with id: {user_id} does not exist"
)
@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 f"{user_id} has been removed"
raise HTTPException(
status_code=404,
detail=f"user with id: {user_id} does not exist"
)
@app.get('/api/v1/random')
async def get_random():
random_n:int = random.randint(0,100)
return {'number':random_n, 'limit': 100}
@app.get('/api/v1/random/{limit}')
async def get_random_limit(limit:int):
random_n:int = random.randint(0,limit)
return {'number':random_n, 'limit': limit}