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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//! Layer endpoints.

use actix_web::{
    delete, get, post, put,
    web::{Json, Path, Query},
    HttpResponse, Result,
};
use uuid::Uuid;

use crate::{
    config::{
        auth::user_info::UserInfo,
        data::{SharedBroadcaster, SharedPool},
    },
    model::dto::{
        actions::{Action, ActionType},
        core::{
            ActionDtoWrapper, ActionDtoWrapperDeleteLayer, ActionDtoWrapperNewLayer,
            ActionDtoWrapperUpdateLayer,
        },
        layers::LayerSearchParameters,
    },
    service::layer,
};

/// Endpoint for searching layers. Layers are returned in order of their
/// `order_index`.
///
/// # Errors
/// * If the connection to the database could not be established.
#[utoipa::path(
    context_path = "/api/maps/{map_id}/layers",
    params(
        ("map_id" = i32, Path, description = "The id of the map the layer is on"),
        LayerSearchParameters,
    ),
    responses(
        (status = 200, description = "Search layers", body = VecLayerDto)
    ),
    security(
        ("oauth2" = [])
    )
)]
#[get("")]
pub async fn find(
    search_query: Query<LayerSearchParameters>,
    map_id: Path<i32>,
    pool: SharedPool,
) -> Result<HttpResponse> {
    let mut search_params = search_query.into_inner();
    search_params.map_id = Some(map_id.into_inner());

    let response = layer::find(search_params, &pool).await?;
    Ok(HttpResponse::Ok().json(response))
}

/// Endpoint for fetching a layer by its id.
///
/// # Errors
/// * If the connection to the database could not be established.
#[utoipa::path(
    context_path = "/api/maps/{map_id}/layers",
    params(
        ("map_id" = i32, Path, description = "The id of the map the layer is on"),
    ),
    responses(
        (status = 200, description = "Fetch layer by id", body = LayerDto)
    ),
    security(
        ("oauth2" = [])
    )
)]
#[get("/{id}")]
pub async fn find_by_id(path: Path<(i32, Uuid)>, pool: SharedPool) -> Result<HttpResponse> {
    let (_, id) = path.into_inner();
    let response = layer::find_by_id(id, &pool).await?;
    Ok(HttpResponse::Ok().json(response))
}

/// Endpoint for creating a new layer.
///
/// # Errors
/// * If the connection to the database could not be established.
#[utoipa::path(
    context_path = "/api/maps/{map_id}/layers",
    params(
        ("map_id" = i32, Path, description = "The id of the map the layer is on"),
    ),
    request_body = ActionDtoWrapperUpdateLayer,
    responses(
        (status = 201, description = "Create a plant layer", body = LayerDto)
    ),
    security(
        ("oauth2" = [])
    )
)]
#[post("")]
pub async fn create(
    path: Path<i32>,
    new_layer: Json<ActionDtoWrapperNewLayer>,
    pool: SharedPool,
    broadcaster: SharedBroadcaster,
    user_info: UserInfo,
) -> Result<HttpResponse> {
    let ActionDtoWrapper { action_id, dto } = new_layer.into_inner();
    let map_id = path.into_inner();
    let user_id = user_info.id;

    let dto = layer::create(map_id, dto, &pool).await?;

    broadcaster
        .broadcast(
            map_id,
            Action {
                action_id,
                user_id,
                action: ActionType::CreateLayer(dto.clone()),
            },
        )
        .await;

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

/// Endpoint for updating layers.
///
/// # Errors
/// * If the connection to the database could not be established.
#[utoipa::path(
    context_path = "/api/maps/{map_id}/layers",
    params(
        ("map_id" = i32, Path, description = "The id of the map"),
    ),
    request_body = ActionDtoWrapperUpdateLayer,
    responses(
        (status = 200, description = "Layers have been reordered")
    ),
    security(
        ("oauth2" = [])
    )
)]
#[put("")]
pub async fn update(
    path: Path<i32>,
    update: Json<ActionDtoWrapperUpdateLayer>,
    pool: SharedPool,
    broadcaster: SharedBroadcaster,
    user_info: UserInfo,
) -> Result<HttpResponse> {
    let ActionDtoWrapper { action_id, dto } = update.into_inner();
    let map_id = path.into_inner();
    let user_id = user_info.id;

    let action = layer::update(map_id, dto.clone(), &pool).await?;

    broadcaster
        .broadcast(
            map_id,
            Action {
                action_id,
                user_id,
                action,
            },
        )
        .await;

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

/// Endpoint for deleting a layer.
/// Layers are soft-deleted and marked as deleted.
///
/// # Errors
/// * If the connection to the database could not be established.
#[utoipa::path(
    context_path = "/api/maps/{map_id}/layers",
    params(
        ("map_id" = i32, Path, description = "The id of the map the layer is on"),
    ),
    request_body = ActionDtoWrapperDeleteLayer,
    responses(
        (status = 200, description = "Delete a layer")
    ),
    security(
        ("oauth2" = [])
    )
)]
#[delete("")]
pub async fn delete(
    path: Path<i32>,
    delete_layer: Json<ActionDtoWrapperDeleteLayer>,
    pool: SharedPool,
    broadcaster: SharedBroadcaster,
    user_info: UserInfo,
) -> Result<HttpResponse> {
    let ActionDtoWrapper { action_id, dto } = delete_layer.into_inner();
    let map_id = path.into_inner();
    let user_id = user_info.id;
    let layer_id = dto.id;

    layer::delete_by_id(map_id, layer_id, &pool).await?;

    broadcaster
        .broadcast(
            map_id,
            Action {
                action_id,
                user_id,
                action: ActionType::DeleteLayer(layer_id),
            },
        )
        .await;

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