use actix_web::{web, HttpResponse, Responder}; use brass_macros::db_test; use rinja::Template; 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::NewEventTemplate, IdPath}, models::{Assignment, Event, Function, Location, Role, User}, utils::ApplicationError, }; #[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 template = NewEventTemplate { user: user.into_inner(), date: event.date, locations, event: Some(event), 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), }; Ok(HttpResponse::Ok().body(template.render()?)) } #[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(); Location::create(&context.db_pool, "Hauptbahnhof", 1) .await .unwrap(); Event::create( &context.db_pool, &NaiveDate::parse_from_str("2025-01-01", "%F").unwrap(), &NaiveTime::parse_from_str("08:00", "%R").unwrap(), &NaiveTime::parse_from_str("10:00", "%R").unwrap(), &"Vorstellung".to_string(), 1, false, false, 2, &"Tuchuniform".to_string(), None, ) .await .unwrap(); let app = context.app().await; let config = RequestConfig { uri: "/events/1/edit".to_string(), role: Role::Admin, function: crate::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); }