backend/controller/
users.rs

1//! `Users` endpoints.
2
3use actix_web::{
4    get, post,
5    web::{Json, Query},
6    HttpResponse, Result,
7};
8
9use crate::{
10    config::{
11        auth::user_info::UserInfo,
12        data::{SharedHttpClient, SharedKeycloakApi, SharedPool},
13    },
14    model::dto::{PageParameters, UserSearchParameters, UsersDto},
15    service,
16};
17
18/// Endpoint for searching users.
19///
20/// # Errors
21/// * If the connection to the keycloak API could not be established.
22#[utoipa::path(
23    context_path = "/api/users",
24    params(
25        UserSearchParameters,
26    ),
27    responses(
28        (status = 200, description = "Users matching the username search", body = Vec<UserDto>)
29    ),
30    security(
31        ("oauth2" = [])
32    )
33)]
34#[get("")]
35pub async fn find(
36    search_query: Query<UserSearchParameters>,
37    pagination_query: Query<PageParameters>,
38    user_info: UserInfo,
39    keycloak_api: SharedKeycloakApi,
40    http_client: SharedHttpClient,
41) -> Result<HttpResponse> {
42    let response = service::users::search_by_username(
43        &search_query,
44        &pagination_query,
45        user_info.id,
46        &keycloak_api,
47        &http_client,
48    )
49    .await?;
50    Ok(HttpResponse::Ok().json(response))
51}
52
53/// Endpoint for creating an [`Users`](crate::model::entity::Users) entry.
54///
55/// # Errors
56/// * If the connection to the database could not be established.
57#[utoipa::path(
58    context_path = "/api/users",
59    responses(
60        (status = 201, description = "Create user data entry for new user", body = UsersDto)
61    ),
62    security(
63        ("oauth2" = [])
64    )
65)]
66#[post("")]
67pub async fn create(
68    user_info: UserInfo,
69    user_data_json: Json<UsersDto>,
70    pool: SharedPool,
71) -> Result<HttpResponse> {
72    let response = service::users::create(user_info.id, user_data_json.0, &pool).await?;
73    Ok(HttpResponse::Created().json(response))
74}