brass/web/src/endpoints/events/get_edit.rs

106 lines
3.4 KiB
Rust

use actix_web::{web, HttpResponse, Responder};
use sqlx::PgPool;
use crate::{
endpoints::{events::NewOrEditEventTemplate, IdPath},
utils::{ApplicationError, TemplateResponse},
};
use brass_db::models::{Assignment, Clothing, Event, Function, Location, Role, User};
#[actix_web::get("/events/{id}/edit")]
pub async fn get(
user: web::ReqData<User>,
pool: web::Data<PgPool>,
path: web::Path<IdPath>,
) -> Result<impl Responder, ApplicationError> {
if user.role != Role::Admin && user.role != Role::AreaManager {
return Err(ApplicationError::Unauthorized);
}
let Some(event) = Event::read_by_id_including_location(pool.get_ref(), path.id).await? else {
return Ok(HttpResponse::NotFound().finish());
};
let locations = Location::read_by_area_including_area(
pool.get_ref(),
event.location.as_ref().unwrap().area_id,
)
.await?;
let assignments = Assignment::read_all_by_event(pool.get_ref(), event.id).await?;
let clothing_options = Clothing::read_all(pool.get_ref()).await?;
let template = NewOrEditEventTemplate {
user: user.into_inner(),
date: event.start.date(),
locations,
amount_of_planned_posten: assignments
.iter()
.filter(|x| x.function == Function::Posten)
.count(),
is_fuehrungsassistent_planned: assignments
.iter()
.any(|x| x.function == Function::Fuehrungsassistent),
is_wachhabender_planned: assignments
.iter()
.any(|x| x.function == Function::Wachhabender),
id: Some(event.id),
start: event.start.time(),
end: event.end,
name: Some(event.name),
location: Some(event.location_id),
voluntary_wachhabender: event.voluntary_wachhabender,
fuehrungsassistent_required: event.fuehrungsassistent_required,
amount_of_posten: Some(event.amount_of_posten),
clothing: Some(event.clothing.id),
clothing_options,
canceled: event.canceled,
note: event.note,
};
Ok(template.to_response()?)
}
#[cfg(test)]
mod tests {
use brass_db::models::{Event, Location, Role};
use brass_macros::db_test;
use chrono::NaiveDateTime;
use crate::utils::test_helper::{
assert_snapshot, test_get, DbTestContext, NaiveDateTimeExt, RequestConfig,
ServiceResponseExt, StatusCode,
};
#[db_test]
async fn produces_template(context: &DbTestContext) {
Location::create(&context.db_pool, "Hauptbahnhof", 1)
.await
.unwrap();
let changeset = brass_db::models::EventChangeset {
time: (
NaiveDateTime::from_ymd_and_hms(2025, 01, 01, 8, 0, 0).unwrap(),
NaiveDateTime::from_ymd_and_hms(2025, 01, 01, 10, 0, 0).unwrap(),
),
name: "Vorstellung".to_string(),
location_id: 1,
fuehrungsassistent_required: false,
voluntary_wachhabender: false,
amount_of_posten: 2,
clothing: 1,
note: None,
};
Event::create(&context.db_pool, changeset).await.unwrap();
let config = RequestConfig::new("/events/1/edit").with_role(Role::Admin);
let response = test_get(&context.db_pool, context.app().await, &config).await;
let (status, body) = response.into_status_and_body().await;
assert_eq!(StatusCode::OK, status);
assert_snapshot!(body);
}
}