backend/controller/
base_layer_image.rs

1//! `BaseLayerImage` endpoints.
2
3use actix_web::{
4    delete, get, patch, post,
5    web::{Json, Path},
6    HttpResponse, Result,
7};
8use uuid::Uuid;
9
10use crate::config::data::{SharedBroadcaster, SharedPool};
11use crate::{
12    config::auth::user_info::UserInfo,
13    model::dto::{
14        actions::{
15            Action, ActionType, CreateBaseLayerImageActionPayload,
16            DeleteBaseLayerImageActionPayload, UpdateBaseLayerImageActionPayload,
17        },
18        core::{
19            ActionDtoWrapper, ActionDtoWrapperDeleteBaseLayerImage,
20            ActionDtoWrapperNewBaseLayerImage, ActionDtoWrapperUpdateBaseLayerImage,
21        },
22    },
23    service::base_layer_images,
24};
25
26/// Endpoint for listing and filtering `BaseLayerImage`.
27///
28/// # Errors
29/// * If the connection to the database could not be established.
30#[utoipa::path(
31    context_path = "/api/maps/{map_id}/layers/base/{layer_id}/images",
32    params(
33        ("map_id" = i32, Path, description = "The id of the map the layer is on"),
34        ("layer_id" = i32, Path, description = "The id of the layer"),
35    ),
36    responses(
37        (status = 200, description = "Find base layer images", body = Vec<BaseLayerImageDto>)
38    ),
39    security(
40        ("oauth2" = [])
41    )
42)]
43#[get("")]
44pub async fn find(path: Path<(i32, Uuid)>, pool: SharedPool) -> Result<HttpResponse> {
45    let (_map_id, layer_id) = path.into_inner();
46    let response = base_layer_images::find(&pool, layer_id).await?;
47    Ok(HttpResponse::Ok().json(response))
48}
49
50/// Endpoint for creating a new `BaseLayerImage`.
51///
52/// # Errors
53/// * If the connection to the database could not be established.
54#[utoipa::path(
55    context_path = "/api/maps/{map_id}/layers/base/images",
56    params(
57        ("map_id" = i32, Path, description = "The id of the map the layer is on"),
58    ),
59    request_body = ActionDtoWrapperNewBaseLayerImage,
60    responses(
61        (status = 201, description = "Create a planting", body = BaseLayerImageDto)
62    ),
63    security(
64        ("oauth2" = [])
65    )
66)]
67#[post("")]
68pub async fn create(
69    path: Path<i32>,
70    json: Json<ActionDtoWrapperNewBaseLayerImage>,
71    user_info: UserInfo,
72    pool: SharedPool,
73    broadcaster: SharedBroadcaster,
74) -> Result<HttpResponse> {
75    let ActionDtoWrapper { action_id, dto } = json.into_inner();
76
77    let dto = base_layer_images::create(dto.clone(), &pool).await?;
78
79    broadcaster
80        .broadcast(
81            path.into_inner(),
82            Action {
83                action_id,
84                user_id: user_info.id,
85                action: ActionType::CreateBaseLayerImage(CreateBaseLayerImageActionPayload::new(
86                    dto.clone(),
87                )),
88            },
89        )
90        .await;
91
92    Ok(HttpResponse::Created().json(dto))
93}
94
95/// Endpoint for updating a `BaseLayerImage`.
96///
97/// # Errors
98/// * If the connection to the database could not be established.
99#[utoipa::path(
100    context_path = "/api/maps/{map_id}/layers/base/images",
101    params(
102        ("map_id" = i32, Path, description = "The id of the map the layer is on"),
103        ("base_layer_image_id" = Uuid, Path, description = "The id of the BaseLayerImage to update"),
104    ),
105    request_body = ActionDtoWrapperUpdateBaseLayerImage,
106    responses(
107        (status = 200, description = "Update a planting", body = BaseLayerImageDto)
108    ),
109    security(
110        ("oauth2" = [])
111    )
112)]
113#[patch("/{base_layer_image_id}")]
114pub async fn update(
115    path: Path<(i32, Uuid)>,
116    json: Json<ActionDtoWrapperUpdateBaseLayerImage>,
117    user_info: UserInfo,
118    pool: SharedPool,
119    broadcaster: SharedBroadcaster,
120) -> Result<HttpResponse> {
121    let (map_id, base_layer_image_id) = path.into_inner();
122    let ActionDtoWrapper { action_id, dto } = json.into_inner();
123
124    let dto = base_layer_images::update(base_layer_image_id, dto.clone(), &pool).await?;
125
126    broadcaster
127        .broadcast(
128            map_id,
129            Action {
130                action_id,
131                user_id: user_info.id,
132                action: ActionType::UpdateBaseLayerImage(UpdateBaseLayerImageActionPayload::new(
133                    dto.clone(),
134                )),
135            },
136        )
137        .await;
138
139    Ok(HttpResponse::Ok().json(dto))
140}
141
142/// Endpoint for deleting a `BaseLayerImage`.
143///
144/// # Errors
145/// * If the connection to the database could not be established.
146#[utoipa::path(
147    context_path = "/api/maps/{map_id}/layers/base/images",
148    params(
149        ("map_id" = i32, Path, description = "The id of the map the layer is on"),
150    ),
151    responses(
152        (status = 200, description = "Delete a planting")
153    ),
154    security(
155        ("oauth2" = [])
156    )
157)]
158#[delete("")]
159pub async fn delete(
160    path: Path<i32>,
161    json: Json<ActionDtoWrapperDeleteBaseLayerImage>,
162    user_info: UserInfo,
163    pool: SharedPool,
164    broadcaster: SharedBroadcaster,
165) -> Result<HttpResponse> {
166    let map_id = path.into_inner();
167    let ActionDtoWrapper { action_id, dto } = json.into_inner();
168
169    let id = dto.id;
170    base_layer_images::delete_by_id(id, &pool).await?;
171
172    broadcaster
173        .broadcast(
174            map_id,
175            Action {
176                action_id,
177                user_id: user_info.id,
178                action: ActionType::DeleteBaseLayerImage(DeleteBaseLayerImageActionPayload::new(
179                    id,
180                )),
181            },
182        )
183        .await;
184
185    Ok(HttpResponse::Ok().finish())
186}