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

122 lines
3.4 KiB
Rust

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<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 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::<String>(), 1)
// .await
// .unwrap();
//
//let date: NaiveDate = Date().fake();
//let time: NaiveTime = Time().fake();
//let words: Vec<String> = 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();
let changeset = crate::models::EventChangeset {
date: NaiveDate::parse_from_str("2025-01-01", "%F").unwrap(),
time: (
NaiveTime::parse_from_str("08:00", "%R").unwrap(),
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: "Tuchuniform".to_string(),
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: 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);
}