use actix_web::web::Query;
use actix_web::{
delete, get, patch, post, put,
web::{Json, Path},
HttpResponse, Result,
};
use uuid::Uuid;
use crate::{
config::{
auth::user_info::UserInfo,
data::{SharedBroadcaster, SharedPool},
},
model::dto::{
actions::{Action, ActionType, UpdatePlantingAdditionalNamePayload},
ArchiveSeedDto, NewSeedDto, PageParameters, SeedSearchParameters,
},
service,
};
#[utoipa::path(
context_path = "/api/seeds",
params(
SeedSearchParameters,
PageParameters
),
responses(
(status = 200, description = "Fetch all seeds", body = PageSeedDto)
),
security(
("oauth2" = [])
)
)]
#[get("")]
pub async fn find(
search_query: Query<SeedSearchParameters>,
page_query: Query<PageParameters>,
user_info: UserInfo,
pool: SharedPool,
) -> Result<HttpResponse> {
let response = service::seed::find(
search_query.into_inner(),
page_query.into_inner(),
user_info.id,
&pool,
)
.await?;
Ok(HttpResponse::Ok().json(response))
}
#[utoipa::path(
context_path = "/api/seeds",
responses(
(status = 200, description = "Fetch seed by id", body = SeedDto)
),
security(
("oauth2" = [])
)
)]
#[get("/{id}")]
pub async fn find_by_id(
id: Path<i32>,
user_info: UserInfo,
pool: SharedPool,
) -> Result<HttpResponse> {
let response = service::seed::find_by_id(*id, user_info.id, &pool).await?;
Ok(HttpResponse::Ok().json(response))
}
#[utoipa::path(
context_path = "/api/seeds",
request_body = NewSeedDto,
responses(
(status = 201, description = "Create a seed", body = SeedDto)
),
security(
("oauth2" = [])
)
)]
#[post("")]
pub async fn create(
new_seed_json: Json<NewSeedDto>,
user_info: UserInfo,
pool: SharedPool,
) -> Result<HttpResponse> {
let response = service::seed::create(new_seed_json.0, user_info.id, &pool).await?;
Ok(HttpResponse::Created().json(response))
}
#[utoipa::path(
context_path = "/api/seeds",
responses(
(status = 200, description = "Delete a seed", body = String)
),
security(
("oauth2" = [])
)
)]
#[delete("/{id}")]
pub async fn delete_by_id(
path: Path<i32>,
user_info: UserInfo,
pool: SharedPool,
) -> Result<HttpResponse> {
service::seed::delete_by_id(*path, user_info.id, &pool).await?;
Ok(HttpResponse::Ok().json(""))
}
#[put("/{id}")]
pub async fn edit_by_id(
id: Path<i32>,
edit_seed_json: Json<NewSeedDto>,
user_info: UserInfo,
pool: SharedPool,
broadcaster: SharedBroadcaster,
) -> Result<HttpResponse> {
let response = service::seed::edit(*id, user_info.id, edit_seed_json.0, &pool).await?;
let affected_plantings = service::plantings::find_by_seed_id(*id, &pool);
for planting in affected_plantings.await? {
broadcaster
.broadcast_all_maps(Action {
action_id: Uuid::new_v4(),
user_id: user_info.id,
action: ActionType::UpdatePlantingAdditionalName(
UpdatePlantingAdditionalNamePayload::new(
&planting,
Some(response.name.clone()),
),
),
})
.await;
}
Ok(HttpResponse::Accepted().json(response))
}
#[patch("/{id}/archive")]
pub async fn archive(
id: Path<i32>,
archive_seed_json: Json<ArchiveSeedDto>,
user_info: UserInfo,
pool: SharedPool,
) -> Result<HttpResponse> {
let response = service::seed::archive(*id, user_info.id, archive_seed_json.0, &pool).await?;
Ok(HttpResponse::Accepted().json(response))
}