use actix_web::{
delete, get, patch, post,
web::{Data, Json, Path},
HttpResponse, Result,
};
use uuid::Uuid;
use crate::{
config::{auth::user_info::UserInfo, data::AppDataInner},
model::dto::{
actions::{
Action, ActionType, CreateBaseLayerImageActionPayload,
DeleteBaseLayerImageActionPayload, UpdateBaseLayerImageActionPayload,
},
core::{
ActionDtoWrapper, ActionDtoWrapperDeleteBaseLayerImage,
ActionDtoWrapperNewBaseLayerImage, ActionDtoWrapperUpdateBaseLayerImage,
},
},
service::base_layer_images,
};
#[utoipa::path(
context_path = "/api/maps/{map_id}/layers/base/{layer_id}/images",
params(
("map_id" = i32, Path, description = "The id of the map the layer is on"),
("layer_id" = i32, Path, description = "The id of the layer"),
),
responses(
(status = 200, description = "Find base layer images", body = Vec<BaseLayerImageDto>)
),
security(
("oauth2" = [])
)
)]
#[get("")]
pub async fn find(path: Path<(i32, Uuid)>, app_data: Data<AppDataInner>) -> Result<HttpResponse> {
let (_map_id, layer_id) = path.into_inner();
let response = base_layer_images::find(&app_data, layer_id).await?;
Ok(HttpResponse::Ok().json(response))
}
#[utoipa::path(
context_path = "/api/maps/{map_id}/layers/base/images",
params(
("map_id" = i32, Path, description = "The id of the map the layer is on"),
),
request_body = ActionDtoWrapperNewBaseLayerImage,
responses(
(status = 201, description = "Create a planting", body = BaseLayerImageDto)
),
security(
("oauth2" = [])
)
)]
#[post("")]
pub async fn create(
path: Path<i32>,
json: Json<ActionDtoWrapperNewBaseLayerImage>,
app_data: Data<AppDataInner>,
user_info: UserInfo,
) -> Result<HttpResponse> {
let ActionDtoWrapper { action_id, dto } = json.into_inner();
let dto = base_layer_images::create(dto.clone(), &app_data).await?;
app_data
.broadcaster
.broadcast(
path.into_inner(),
Action {
action_id,
user_id: user_info.id,
action: ActionType::CreateBaseLayerImage(CreateBaseLayerImageActionPayload::new(
dto.clone(),
)),
},
)
.await;
Ok(HttpResponse::Created().json(dto))
}
#[utoipa::path(
context_path = "/api/maps/{map_id}/layers/base/images",
params(
("map_id" = i32, Path, description = "The id of the map the layer is on"),
("base_layer_image_id" = Uuid, Path, description = "The id of the BaseLayerImage to update"),
),
request_body = ActionDtoWrapperUpdateBaseLayerImage,
responses(
(status = 200, description = "Update a planting", body = BaseLayerImageDto)
),
security(
("oauth2" = [])
)
)]
#[patch("/{base_layer_image_id}")]
pub async fn update(
path: Path<(i32, Uuid)>,
json: Json<ActionDtoWrapperUpdateBaseLayerImage>,
app_data: Data<AppDataInner>,
user_info: UserInfo,
) -> Result<HttpResponse> {
let (map_id, base_layer_image_id) = path.into_inner();
let ActionDtoWrapper { action_id, dto } = json.into_inner();
let dto = base_layer_images::update(base_layer_image_id, dto.clone(), &app_data).await?;
app_data
.broadcaster
.broadcast(
map_id,
Action {
action_id,
user_id: user_info.id,
action: ActionType::UpdateBaseLayerImage(UpdateBaseLayerImageActionPayload::new(
dto.clone(),
)),
},
)
.await;
Ok(HttpResponse::Ok().json(dto))
}
#[utoipa::path(
context_path = "/api/maps/{map_id}/layers/base/images",
params(
("map_id" = i32, Path, description = "The id of the map the layer is on"),
),
responses(
(status = 200, description = "Delete a planting")
),
security(
("oauth2" = [])
)
)]
#[delete("")]
pub async fn delete(
path: Path<i32>,
json: Json<ActionDtoWrapperDeleteBaseLayerImage>,
app_data: Data<AppDataInner>,
user_info: UserInfo,
) -> Result<HttpResponse> {
let map_id = path.into_inner();
let ActionDtoWrapper { action_id, dto } = json.into_inner();
let id = dto.id;
base_layer_images::delete_by_id(id, &app_data).await?;
app_data
.broadcaster
.broadcast(
map_id,
Action {
action_id,
user_id: user_info.id,
action: ActionType::DeleteBaseLayerImage(DeleteBaseLayerImageActionPayload::new(
id,
)),
},
)
.await;
Ok(HttpResponse::Ok().finish())
}