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
//! `Users` endpoints.

use actix_web::{
    get, post,
    web::{Json, Query},
    HttpResponse, Result,
};

use crate::{
    config::{
        auth::user_info::UserInfo,
        data::{SharedHttpClient, SharedKeycloakApi, SharedPool},
    },
    model::dto::{PageParameters, UserSearchParameters, UsersDto},
    service,
};

/// Endpoint for searching users.
///
/// # Errors
/// * If the connection to the keycloak API could not be established.
#[utoipa::path(
    context_path = "/api/users",
    params(
        UserSearchParameters,
    ),
    responses(
        (status = 200, description = "Users matching the username search", body = Vec<UserDto>)
    ),
    security(
        ("oauth2" = [])
    )
)]
#[get("")]
pub async fn find(
    search_query: Query<UserSearchParameters>,
    pagination_query: Query<PageParameters>,
    user_info: UserInfo,
    keycloak_api: SharedKeycloakApi,
    http_client: SharedHttpClient,
) -> Result<HttpResponse> {
    let response = service::users::search_by_username(
        &search_query,
        &pagination_query,
        user_info.id,
        &keycloak_api,
        &http_client,
    )
    .await?;
    Ok(HttpResponse::Ok().json(response))
}

/// Endpoint for creating an [`Users`](crate::model::entity::Users) entry.
///
/// # Errors
/// * If the connection to the database could not be established.
#[utoipa::path(
    context_path = "/api/users",
    responses(
        (status = 201, description = "Create user data entry for new user", body = UsersDto)
    ),
    security(
        ("oauth2" = [])
    )
)]
#[post("")]
pub async fn create(
    user_info: UserInfo,
    user_data_json: Json<UsersDto>,
    pool: SharedPool,
) -> Result<HttpResponse> {
    let response = service::users::create(user_info.id, user_data_json.0, &pool).await?;
    Ok(HttpResponse::Created().json(response))
}