use actix_web::{
delete, get, patch, post,
web::{Json, Path, Query},
HttpResponse, Result,
};
use crate::{
config::{
auth::user_info::UserInfo,
data::{SharedBroadcaster, SharedPool},
},
model::dto::{
actions::Action,
core::ActionDtoWrapper,
shadings::{DeleteShadingDto, NewShadingDto, ShadingSearchParameters, UpdateShadingDto},
},
service::shadings,
};
#[utoipa::path(
context_path = "/api/maps/{map_id}/layers/shade/shadings",
params(
("map_id" = i32, Path, description = "The id of the map the layer is on"),
ShadingSearchParameters
),
responses(
(status = 200, description = "Find shadings", body = Vec<ShadingDto>)
),
security(
("oauth2" = [])
)
)]
#[get("")]
pub async fn find(
search_params: Query<ShadingSearchParameters>,
pool: SharedPool,
) -> Result<HttpResponse> {
let response = shadings::find(search_params.into_inner(), &pool).await?;
Ok(HttpResponse::Ok().json(response))
}
#[utoipa::path(
context_path = "/api/maps/{map_id}/layers/shade/shadings",
params(
("map_id" = i32, Path, description = "The id of the map the layer is on"),
),
request_body = NewShadingDto,
responses(
(status = 201, description = "Create a shading", body = ShadingDto)
),
security(
("oauth2" = [])
)
)]
#[post("")]
pub async fn create(
path: Path<i32>,
new_shadings: Json<ActionDtoWrapper<Vec<NewShadingDto>>>,
pool: SharedPool,
broadcaster: SharedBroadcaster,
user_info: UserInfo,
) -> Result<HttpResponse> {
let map_id = path.into_inner();
let ActionDtoWrapper { action_id, dto } = new_shadings.into_inner();
let created_shadings = shadings::create(dto, &pool).await?;
broadcaster
.broadcast(
map_id,
Action::new_create_shading_action(&created_shadings, user_info.id, action_id),
)
.await;
Ok(HttpResponse::Created().json(created_shadings))
}
#[utoipa::path(
context_path = "/api/maps/{map_id}/layers/shade/shadings",
params(
("map_id" = i32, Path, description = "The id of the map the layer is on"),
),
request_body = ActionDtoWrapperUpdateShadings,
responses(
(status = 200, description = "Update a shading", body = Vec<ShadingDto>)
),
security(
("oauth2" = [])
)
)]
#[patch("")]
pub async fn update(
path: Path<i32>,
update_shading: Json<ActionDtoWrapper<UpdateShadingDto>>,
pool: SharedPool,
broadcaster: SharedBroadcaster,
user_info: UserInfo,
) -> Result<HttpResponse> {
let map_id = path.into_inner();
let ActionDtoWrapper { action_id, dto } = update_shading.into_inner();
let shading = shadings::update(dto.clone(), &pool).await?;
let action = match &dto {
UpdateShadingDto::Update(dto) => {
Action::new_update_shading_action(dto, user_info.id, action_id)
}
UpdateShadingDto::UpdateAddDate(dto) => {
Action::new_update_shading_add_date_action(dto, user_info.id, action_id)
}
UpdateShadingDto::UpdateRemoveDate(dto) => {
Action::new_update_shading_remove_date_action(dto, user_info.id, action_id)
}
UpdateShadingDto::UpdateNotes(dto) => {
Action::new_update_shading_add_notes(dto, user_info.id, action_id)
}
};
broadcaster.broadcast(map_id, action).await;
Ok(HttpResponse::Ok().json(shading))
}
#[utoipa::path(
context_path = "/api/maps/{map_id}/layers/shade/shadings",
params(
("map_id" = i32, Path, description = "The id of the map the layer is on"),
),
request_body = DeleteShadingDto,
responses(
(status = 200, description = "Delete a shading")
),
security(
("oauth2" = [])
)
)]
#[delete("")]
pub async fn delete(
path: Path<i32>,
delete_shadings: Json<ActionDtoWrapper<Vec<DeleteShadingDto>>>,
pool: SharedPool,
broadcaster: SharedBroadcaster,
user_info: UserInfo,
) -> Result<HttpResponse> {
let map_id = path.into_inner();
let ActionDtoWrapper { action_id, dto } = delete_shadings.into_inner();
shadings::delete_by_ids(dto.clone(), &pool).await?;
broadcaster
.broadcast(
map_id,
Action::new_delete_shading_action(&dto, user_info.id, action_id),
)
.await;
Ok(HttpResponse::Ok().finish())
}