use actix_web::{web, HttpResponse, Responder}; use brass_macros::db_test; use sqlx::PgPool; #[cfg(test)] use crate::utils::test_helper::{ assert_snapshot, read_body, test_get, DbTestContext, RequestConfig, StatusCode, }; #[cfg(test)] use chrono::{NaiveDate, NaiveTime}; 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, pool: web::Data, path: web::Path, ) -> Result { 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, voluntary_fuehrungsassistent: event.voluntary_fuehrungsassistent, 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()?) } #[db_test] async fn produces_template(context: &DbTestContext) { //Location::create(&context.db_pool, &Industry().fake::(), 1) // .await // .unwrap(); // //let date: NaiveDate = Date().fake(); //let time: NaiveTime = Time().fake(); //let words: Vec = Words(3..5).fake(); //Event::create( // &context.db_pool, // &date, // &time, // &time, // &words.join(" "), // 1, // false, // false, // 2, // &Word().fake(), // None, //) //.await //.unwrap(); //// TODO: refactor Location::create(&context.db_pool, "Hauptbahnhof", 1) .await .unwrap(); let date = NaiveDate::parse_from_str("2025-01-01", "%F").unwrap(); let changeset = brass_db::models::EventChangeset { time: ( date.and_time(NaiveTime::parse_from_str("08:00", "%R").unwrap()), date.and_time(NaiveTime::parse_from_str("10:00", "%R").unwrap()), ), name: "Vorstellung".to_string(), location_id: 1, voluntary_fuehrungsassistent: false, voluntary_wachhabender: false, amount_of_posten: 2, clothing: 1, note: None, }; Event::create(&context.db_pool, changeset).await.unwrap(); let app = context.app().await; let config = RequestConfig { uri: "/events/1/edit".to_string(), role: Role::Admin, function: vec![brass_db::models::Function::Posten], user_area: 1, }; let response = test_get(&context.db_pool, app, &config).await; assert_eq!(StatusCode::OK, response.status()); let body = read_body(response).await; assert_snapshot!(body); }