Skip to content

user

This module defines router for the api that is dedicated to user related responsibilities.

create_user

create_user(user_create: models.UserCreate, session: Annotated[Session, Depends(database.get_session)])

Endpoint for creating a new user.

Parameters:

Name Type Description Default
user_create UserCreate

The data required to create a new user.

required
session Session

The session to interact with the database.

required

Returns:

Type Description
UserRead

The created user data to be read.

Raises:

Type Description
HTTPException

If a user with the same email already exists, raises status code 409 (CONFLICT).

Source code in api/routers/user.py
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
@router.post("/", response_model=models.UserRead, status_code=status.HTTP_201_CREATED)
def create_user(
    user_create: models.UserCreate,
    session: Annotated[Session, Depends(database.get_session)],
):
    """
    Endpoint for creating a new user.

    Parameters
    ----------
    user_create : models.UserCreate
        The data required to create a new user.
    session : Session
        The session to interact with the database.

    Returns
    -------
    models.UserRead
        The created user data to be read.

    Raises
    ------
    HTTPException
        If a user with the same email already exists, raises status code 409 (CONFLICT).
    """
    user_exists = database.get_user(user_create.email, session)

    if user_exists:
        raise HTTPException(
            status_code=status.HTTP_409_CONFLICT,
            detail=f"User with email: {user_create.email} already exists",
        )
    user_create.password = oauth2.hash_password(user_create.password)
    return database.add_user(user_create, session)

get_users

get_users(session: Annotated[Session, Depends(database.get_session)], current_user: Annotated[models.User, Depends(oauth2.get_current_user)])

Endpoint for retrieving all users.

Parameters:

Name Type Description Default
session Session

The session to interact with the database.

required
current_user User

The current authenticated user.

required

Returns:

Type Description
list[UserRead]

A list of user data to be read.

Source code in api/routers/user.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
@router.get("/", response_model=list[models.UserRead])
def get_users(
    session: Annotated[Session, Depends(database.get_session)],
    current_user: Annotated[models.User, Depends(oauth2.get_current_user)],  # noqa: ARG001
):
    """
    Endpoint for retrieving all users.

    Parameters
    ----------
    session : Session
        The session to interact with the database.
    current_user : models.User
        The current authenticated user.

    Returns
    -------
    list[models.UserRead]
        A list of user data to be read.
    """
    return database.get_all_users(session)