backend/controller/
map_collaborators.rs

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use actix_web::{
    delete, get, post,
    web::{Json, Path},
    HttpResponse, Result,
};

use crate::{
    config::{
        auth::user_info::UserInfo,
        data::{SharedHttpClient, SharedKeycloakApi, SharedPool},
    },
    model::dto::{DeleteMapCollaboratorDto, NewMapCollaboratorDto},
    service::map_collaborator,
};

/// Endpoint for getting all [`MapCollaborator`](crate::model::entity::MapCollaborator)s of a map.
///
/// # Errors
/// * If the connection to the database could not be established.
/// * If the connection to the Keycloak API could not be established.
#[utoipa::path(
    context_path = "/api/maps/{map_id}/collaborators",
    params(
        ("map_id" = i32, Path, description = "The id of the map on which to collaborate"),
    ),
    responses(
        (status = 200, description = "The collaborators of this map", body = Vec<MapCollaboratorDto>),
    ),
    security(
        ("oauth2" = [])
    )
)]
#[get("")]
pub async fn find(
    map_id: Path<i32>,
    pool: SharedPool,
    keycloak_api: SharedKeycloakApi,
    http_client: SharedHttpClient,
) -> Result<HttpResponse> {
    let response =
        map_collaborator::get_all(map_id.into_inner(), &pool, &keycloak_api, &http_client).await?;

    Ok(HttpResponse::Ok().json(response))
}

/// Endpoint for creating a new [`MapCollaborator`](crate::model::entity::MapCollaborator).
///
/// # Errors
/// * If the connection to the database could not be established.
/// * If the connection to the Keycloak API could not be established.
#[utoipa::path(
    context_path = "/api/maps/{map_id}/collaborators",
    params(
        ("map_id" = i32, Path, description = "The id of the map on which to collaborate"),
    ),
    request_body = NewMapCollaboratorDto,
    responses(
        (status = 201, description = "Add a new map collaborator", body = MapCollaboratorDto),
    ),
    security(
        ("oauth2" = [])
    )
)]
#[post("")]
pub async fn create(
    json: Json<NewMapCollaboratorDto>,
    map_id: Path<i32>,
    user_info: UserInfo,
    pool: SharedPool,
    keycloak_api: SharedKeycloakApi,
    http_client: SharedHttpClient,
) -> Result<HttpResponse> {
    let response = map_collaborator::create(
        (map_id.into_inner(), json.into_inner()),
        user_info.id,
        &pool,
        &keycloak_api,
        &http_client,
    )
    .await?;

    Ok(HttpResponse::Created().json(response))
}

/// Endpoint for deleting a collaborator from a map.
///
/// # Errors
/// * If the user is not the creator of the map.
/// * If the connection to the database could not be established.
#[utoipa::path(
    context_path = "/api/maps/{map_id}/collaborators",
    params(
        ("map_id" = i32, Path, description = "The id of the map on which to collaborate"),
    ),
    request_body = DeleteMapCollaboratorDto,
    responses(
        (status = 204, description = "The collaborator was removed from the map"),
    ),
    security(
        ("oauth2" = [])
    )
)]
#[delete("")]
pub async fn delete(
    map_id: Path<i32>,
    dto: Json<DeleteMapCollaboratorDto>,
    user_info: UserInfo,
    pool: SharedPool,
) -> Result<HttpResponse> {
    map_collaborator::delete((map_id.into_inner(), dto.into_inner()), user_info.id, &pool).await?;

    Ok(HttpResponse::NoContent().finish())
}