Spaces:
Paused
Paused
Update backend/mongodb/endpoints/users.py
Browse files
backend/mongodb/endpoints/users.py
CHANGED
@@ -1,37 +1,48 @@
|
|
1 |
from fastapi import APIRouter, Body, Request, status
|
2 |
from typing import List
|
3 |
import sys
|
4 |
-
# sys.path.append('/Users/benolojo/DCU/CA4/ca400_FinalYearProject/2024-ca400-olojob2-majdap2/src/backend/src/')
|
5 |
from ..models.users import User, UpdateUser
|
6 |
from ..operations import users as users
|
7 |
|
8 |
router = APIRouter(prefix="/user",
|
9 |
tags=["User"])
|
10 |
|
|
|
|
|
|
|
|
|
11 |
@router.post("/", response_description="Create a new user", status_code=status.HTTP_201_CREATED, response_model=User)
|
12 |
-
async def create_user(request: Request, user: User = Body(...)):
|
13 |
-
|
|
|
14 |
|
15 |
@router.get("/", response_description="List users", response_model=List[User])
|
16 |
async def list_users(request: Request):
|
17 |
-
|
|
|
18 |
|
19 |
@router.put("/{user_id}", response_description="Update a User", response_model=UpdateUser)
|
20 |
async def update_user(request: Request, user_id: str, user: UpdateUser = Body(...)):
|
21 |
-
|
|
|
22 |
|
23 |
@router.get("/{user_id}", response_description="Get a single user by id", response_model=User)
|
24 |
-
async def find_user(request: Request, user_id: str):
|
25 |
-
|
|
|
26 |
|
27 |
@router.get("/name/{user_name}", response_description="Get a single user by name", response_model=User)
|
28 |
-
async def find_user_name(request: Request, name: str):
|
29 |
-
|
|
|
30 |
|
31 |
@router.get("/email/{email_addr}", response_description="Get a single user by email", response_model=User)
|
32 |
-
async def find_user_email(request: Request, email: str):
|
33 |
-
|
|
|
34 |
|
35 |
@router.delete("/{user_id}", response_description="Delete a user")
|
36 |
async def delete_user(request: Request, user_id:str):
|
37 |
-
|
|
|
|
|
|
1 |
from fastapi import APIRouter, Body, Request, status
|
2 |
from typing import List
|
3 |
import sys
|
|
|
4 |
from ..models.users import User, UpdateUser
|
5 |
from ..operations import users as users
|
6 |
|
7 |
router = APIRouter(prefix="/user",
|
8 |
tags=["User"])
|
9 |
|
10 |
+
def get_collection_users(request: Request):
|
11 |
+
test = request.app.database["user_records"]
|
12 |
+
return test
|
13 |
+
|
14 |
@router.post("/", response_description="Create a new user", status_code=status.HTTP_201_CREATED, response_model=User)
|
15 |
+
async def create_user(request: Request, user: User = Body(...)):
|
16 |
+
collection = get_collection_users(request)
|
17 |
+
return users.create_user(collection, user)
|
18 |
|
19 |
@router.get("/", response_description="List users", response_model=List[User])
|
20 |
async def list_users(request: Request):
|
21 |
+
collection = get_collection_users(request)
|
22 |
+
return users.list_users(collection, 100)
|
23 |
|
24 |
@router.put("/{user_id}", response_description="Update a User", response_model=UpdateUser)
|
25 |
async def update_user(request: Request, user_id: str, user: UpdateUser = Body(...)):
|
26 |
+
collection = get_collection_users(request)
|
27 |
+
return users.update_user(collection, user_id, user)
|
28 |
|
29 |
@router.get("/{user_id}", response_description="Get a single user by id", response_model=User)
|
30 |
+
async def find_user(request: Request, user_id: str):
|
31 |
+
collection = get_collection_users(request)
|
32 |
+
return users.find_user(collection, user_id)
|
33 |
|
34 |
@router.get("/name/{user_name}", response_description="Get a single user by name", response_model=User)
|
35 |
+
async def find_user_name(request: Request, name: str):
|
36 |
+
collection = get_collection_users(request)
|
37 |
+
return users.find_user_name(collection, name)
|
38 |
|
39 |
@router.get("/email/{email_addr}", response_description="Get a single user by email", response_model=User)
|
40 |
+
async def find_user_email(request: Request, email: str):
|
41 |
+
collection = get_collection_users(request)
|
42 |
+
return users.find_user_email(collection, email)
|
43 |
|
44 |
@router.delete("/{user_id}", response_description="Delete a user")
|
45 |
async def delete_user(request: Request, user_id:str):
|
46 |
+
collection = get_collection_users(request)
|
47 |
+
return users.delete_user(collection, user_id)
|
48 |
+
|