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