backend/controller/
map_collaborators.rs

1use actix_web::{
2    delete, get, post,
3    web::{Json, Path},
4    HttpResponse, Result,
5};
6
7use crate::{
8    config::{
9        auth::user_info::UserInfo,
10        data::{SharedHttpClient, SharedKeycloakApi, SharedPool},
11    },
12    model::dto::{DeleteMapCollaboratorDto, NewMapCollaboratorDto},
13    service::map_collaborator,
14};
15
16/// Endpoint for getting all [`MapCollaborator`](crate::model::entity::MapCollaborator)s of a map.
17///
18/// # Errors
19/// * If the connection to the database could not be established.
20/// * If the connection to the Keycloak API could not be established.
21#[utoipa::path(
22    context_path = "/api/maps/{map_id}/collaborators",
23    params(
24        ("map_id" = i32, Path, description = "The id of the map on which to collaborate"),
25    ),
26    responses(
27        (status = 200, description = "The collaborators of this map", body = Vec<MapCollaboratorDto>),
28    ),
29    security(
30        ("oauth2" = [])
31    )
32)]
33#[get("")]
34pub async fn find(
35    map_id: Path<i32>,
36    pool: SharedPool,
37    keycloak_api: SharedKeycloakApi,
38    http_client: SharedHttpClient,
39) -> Result<HttpResponse> {
40    let response =
41        map_collaborator::get_all(map_id.into_inner(), &pool, &keycloak_api, &http_client).await?;
42
43    Ok(HttpResponse::Ok().json(response))
44}
45
46/// Endpoint for creating a new [`MapCollaborator`](crate::model::entity::MapCollaborator).
47///
48/// # Errors
49/// * If the connection to the database could not be established.
50/// * If the connection to the Keycloak API could not be established.
51#[utoipa::path(
52    context_path = "/api/maps/{map_id}/collaborators",
53    params(
54        ("map_id" = i32, Path, description = "The id of the map on which to collaborate"),
55    ),
56    request_body = NewMapCollaboratorDto,
57    responses(
58        (status = 201, description = "Add a new map collaborator", body = MapCollaboratorDto),
59    ),
60    security(
61        ("oauth2" = [])
62    )
63)]
64#[post("")]
65pub async fn create(
66    json: Json<NewMapCollaboratorDto>,
67    map_id: Path<i32>,
68    user_info: UserInfo,
69    pool: SharedPool,
70    keycloak_api: SharedKeycloakApi,
71    http_client: SharedHttpClient,
72) -> Result<HttpResponse> {
73    let response = map_collaborator::create(
74        (map_id.into_inner(), json.into_inner()),
75        user_info.id,
76        &pool,
77        &keycloak_api,
78        &http_client,
79    )
80    .await?;
81
82    Ok(HttpResponse::Created().json(response))
83}
84
85/// Endpoint for deleting a collaborator from a map.
86///
87/// # Errors
88/// * If the user is not the creator of the map.
89/// * If the connection to the database could not be established.
90#[utoipa::path(
91    context_path = "/api/maps/{map_id}/collaborators",
92    params(
93        ("map_id" = i32, Path, description = "The id of the map on which to collaborate"),
94    ),
95    request_body = DeleteMapCollaboratorDto,
96    responses(
97        (status = 204, description = "The collaborator was removed from the map"),
98    ),
99    security(
100        ("oauth2" = [])
101    )
102)]
103#[delete("")]
104pub async fn delete(
105    map_id: Path<i32>,
106    dto: Json<DeleteMapCollaboratorDto>,
107    user_info: UserInfo,
108    pool: SharedPool,
109) -> Result<HttpResponse> {
110    map_collaborator::delete((map_id.into_inner(), dto.into_inner()), user_info.id, &pool).await?;
111
112    Ok(HttpResponse::NoContent().finish())
113}