From 53b50a153c4bf7ce1f6c5e9294c447d67e6e9de4 Mon Sep 17 00:00:00 2001 From: Max Hohlfeld Date: Sun, 12 May 2024 22:46:47 +0200 Subject: [PATCH] feat: creation of assignments works --- src/endpoints/assignment/post_new.rs | 25 +++++++++++++++---------- src/endpoints/mod.rs | 1 + src/models/assignement.rs | 4 ++-- templates/index.html | 8 +++++--- templates/nav.html | 3 ++- 5 files changed, 25 insertions(+), 16 deletions(-) diff --git a/src/endpoints/assignment/post_new.rs b/src/endpoints/assignment/post_new.rs index 8aebe1f9..edd3ad1a 100644 --- a/src/endpoints/assignment/post_new.rs +++ b/src/endpoints/assignment/post_new.rs @@ -12,17 +12,20 @@ pub struct NewAssignmentsForm { } #[actix_web::post("/assignments/new")] -pub async fn post(pool: web::Data, form: web::Form) -> impl Responder { +pub async fn post(pool: web::Data, form: web::Form>) -> impl Responder { + let event_id = form.iter().find(|x| x.0 == "event").unwrap().1.parse().unwrap(); + let wachhabender = form.iter().find(|x| x.0 == "wachhabender"); + let posten: Vec = form.iter().filter(|x| x.0 == "posten").map(|x| x.1.parse().unwrap()).collect(); - let event = Event::read_by_id_including_location(&pool, form.event).await.unwrap(); // TODO: Check if location is needed + let event = Event::read_by_id_including_location(&pool, event_id).await.unwrap(); // TODO: Check if location is needed - if event.voluntary_wachhabender && form.wachhabender.is_some() && form.posten.contains(&form.wachhabender.unwrap()) { + if event.voluntary_wachhabender && wachhabender.is_some() && posten.contains(&wachhabender.unwrap().1.parse().unwrap()) { return HttpResponse::BadRequest().body("Wachhabender kann nicht zugleich Posten sein!"); } - let mut joined_ids = form.posten.clone(); - if let Some(id) = form.wachhabender { - joined_ids.push(id); + let mut joined_ids = posten.clone(); + if let Some((_,id)) = wachhabender { + joined_ids.push(id.parse().unwrap()); } for availabillity_id in joined_ids { @@ -30,8 +33,10 @@ pub async fn post(pool: web::Data, form: web::Form) let mut can_be_used = true; + for assignment in assignments { if event.start_time >= assignment.start_time && event.start_time <= assignment.end_time { + } else { can_be_used = false; break; } @@ -42,12 +47,12 @@ pub async fn post(pool: web::Data, form: web::Form) } } - if let Some(id) = form.wachhabender { - Assignment::create(pool.get_ref(), id, event.id, Function::Wachhabender, event.start_time, event.end_time).await.unwrap(); + if let Some((_,id)) = wachhabender { + Assignment::create(pool.get_ref(), event.id, id.parse().unwrap(), Function::Wachhabender, event.start_time, event.end_time).await.unwrap(); } - for id in &form.posten { - Assignment::create(pool.get_ref(), *id, event.id, Function::Posten, event.start_time, event.end_time).await.unwrap(); + for id in posten { + Assignment::create(pool.get_ref(), event.id, id, Function::Posten, event.start_time, event.end_time).await.unwrap(); } HttpResponse::Ok().finish() diff --git a/src/endpoints/mod.rs b/src/endpoints/mod.rs index 293b247c..99c82d3a 100644 --- a/src/endpoints/mod.rs +++ b/src/endpoints/mod.rs @@ -37,4 +37,5 @@ pub fn init(cfg: &mut ServiceConfig) { cfg.service(events::post_new::post); cfg.service(assignment::get_new::get); + cfg.service(assignment::post_new::post); } diff --git a/src/models/assignement.rs b/src/models/assignement.rs index a4ce9961..038c9c7c 100644 --- a/src/models/assignement.rs +++ b/src/models/assignement.rs @@ -14,8 +14,8 @@ pub struct Assignment { impl Assignment { pub async fn create( pool: &PgPool, - availabillity_id: i32, event_id: i32, + availabillity_id: i32, function: Function, start_time: NaiveTime, end_time: NaiveTime, @@ -25,8 +25,8 @@ impl Assignment { INSERT INTO assignment (eventId, availabillityId, function, startTime, endTime) VALUES ($1, $2, $3, $4, $5); "##, - availabillity_id, event_id, + availabillity_id, function as Function, start_time, end_time diff --git a/templates/index.html b/templates/index.html index 3d9ae446..350e74da 100644 --- a/templates/index.html +++ b/templates/index.html @@ -41,9 +41,11 @@
{{ event.name }}
+ {% if user.role == Role::AreaManager || user.role == Role::Admin %} Planen bearbeiten als abgesagt markieren + {% endif %}

Ort: {{ event.location.as_ref().unwrap().name }}

@@ -88,11 +90,11 @@ {% for availabillity in availabillities %} - {% let user = availabillity.user.as_ref().unwrap() %} + {% let u = availabillity.user.as_ref().unwrap() %} - {{ user.name }} + {{ u.name }} - {% match user.function %} + {% match u.function %} {% when Function::Posten %} Posten {% when Function::Wachhabender %} diff --git a/templates/nav.html b/templates/nav.html index 7631370f..fed18cd8 100644 --- a/templates/nav.html +++ b/templates/nav.html @@ -48,7 +48,8 @@