From b4baa6ac9efd073f1a02e8b71d9d4d27d936608e Mon Sep 17 00:00:00 2001 From: Max Hohlfeld Date: Tue, 12 Mar 2024 12:16:42 +0100 Subject: [PATCH] feat: add location overview for admin and areamanager --- src/endpoints/location/get_overview.rs | 38 +++++++++++++++++++ src/endpoints/location/mod.rs | 33 +--------------- src/endpoints/mod.rs | 2 +- src/models/location.rs | 25 ++++++++++++ .../overview.html} | 2 +- templates/nav.html | 6 +++ 6 files changed, 72 insertions(+), 34 deletions(-) create mode 100644 src/endpoints/location/get_overview.rs rename templates/{locations.html => location/overview.html} (92%) diff --git a/src/endpoints/location/get_overview.rs b/src/endpoints/location/get_overview.rs new file mode 100644 index 00000000..30cb80ba --- /dev/null +++ b/src/endpoints/location/get_overview.rs @@ -0,0 +1,38 @@ +use actix_identity::Identity; +use actix_web::{web, HttpResponse, Responder}; +use askama::Template; +use askama_actix::TemplateToResponse; +use sqlx::PgPool; + +use crate::models::{Area, Location, Role, User}; + +#[derive(Template)] +#[template(path = "location/overview.html")] +pub struct LocationsTemplate { + user: User, + area: Option, + locations: Vec +} + +#[actix_web::get("/locations")] +pub async fn get(user: Identity, pool: web::Data) -> impl Responder { + let current_user = User::read_by_id(pool.get_ref(), user.id().unwrap().parse().unwrap()).await.unwrap(); + + if current_user.role == Role::AreaManager || current_user.role == Role::Admin { + let mut area = None; + let locations; + + if current_user.role == Role::Admin { + locations = Location::read_all_including_area(pool.get_ref()).await.unwrap(); + } else { + locations = Location::read_by_area(pool.get_ref(), current_user.area_id).await.unwrap(); + area = Some(Area::read_by_id(pool.get_ref(), current_user.area_id).await.unwrap()); + } + + let template = LocationsTemplate { user: current_user, area, locations}; + + return template.to_response() + } + + return HttpResponse::BadRequest().body("Fehler beim Zugriff auf die Veranstaltungsorte!"); +} diff --git a/src/endpoints/location/mod.rs b/src/endpoints/location/mod.rs index 4ece1bc7..c85fc4b5 100644 --- a/src/endpoints/location/mod.rs +++ b/src/endpoints/location/mod.rs @@ -1,32 +1 @@ -use actix_identity::Identity; -use actix_web::{web, HttpResponse, Responder}; -use askama::Template; -use askama_actix::TemplateToResponse; -use sqlx::PgPool; - -use crate::models::{Area, Location, Role, User}; - -#[derive(Template)] -#[template(path = "locations.html")] -pub struct LocationsTemplate { - user: User, - area: Area, - locations: Vec -} - -#[actix_web::get("/locations")] -pub async fn get(user: Identity, pool: web::Data) -> impl Responder { - let current_user = User::read_by_id(pool.get_ref(), user.id().unwrap().parse().unwrap()).await.unwrap(); - - if current_user.role == Role::AreaManager { - if let Ok(locations) = Location::read_by_area(pool.get_ref(), current_user.area_id).await { - let area = Area::read_by_id(pool.get_ref(), current_user.area_id).await.unwrap(); - - let template = LocationsTemplate { user: current_user, area, locations}; - - return template.to_response() - } - } - - return HttpResponse::BadRequest().body("Fehler beim Zugriff auf die Veranstaltungsorte!"); -} +pub mod get_overview; diff --git a/src/endpoints/mod.rs b/src/endpoints/mod.rs index 95e8c18b..69b229e9 100644 --- a/src/endpoints/mod.rs +++ b/src/endpoints/mod.rs @@ -10,7 +10,7 @@ pub struct IdPath { } pub fn init(cfg: &mut ServiceConfig) { - cfg.service(location::get); + cfg.service(location::get_overview::get); cfg.service(user::get_overview::get_overview); cfg.service(user::get_new::get_new); diff --git a/src/models/location.rs b/src/models/location.rs index 20092119..4b454b55 100644 --- a/src/models/location.rs +++ b/src/models/location.rs @@ -1,9 +1,12 @@ use sqlx::{query, PgPool}; +use super::Area; + pub struct Location { pub id: i32, pub name: String, pub area_id: i32, + pub area: Option, } impl Location { @@ -30,6 +33,28 @@ impl Location { id: lr.id, name: lr.name.to_string(), area_id: lr.areaid, + area: None, + }) + .collect(); + + Ok(locations) + } + + pub async fn read_all_including_area(pool: &PgPool) -> anyhow::Result> { + let records = query!("SELECT location.id AS locationId, location.name, location.areaId, area.id, area.name AS areaName FROM location JOIN area ON location.areaId = area.id;") + .fetch_all(pool) + .await?; + + let locations = records + .iter() + .map(|lr| Location { + id: lr.locationid, + name: lr.name.to_string(), + area_id: lr.areaid, + area: Some(Area { + id: lr.id, + name: lr.areaname.to_string(), + }), }) .collect(); diff --git a/templates/locations.html b/templates/location/overview.html similarity index 92% rename from templates/locations.html rename to templates/location/overview.html index c2a47765..2d1092c7 100644 --- a/templates/locations.html +++ b/templates/location/overview.html @@ -6,7 +6,7 @@

- Veranstaltungsorte im Bereich {{ area.name }} + Veranstaltungsorte im Bereich

diff --git a/templates/nav.html b/templates/nav.html index 3cef1254..c69c08e4 100644 --- a/templates/nav.html +++ b/templates/nav.html @@ -33,6 +33,12 @@ Nutzerverwaltung {% when Role::Admin %} + + Planung + + + Veranstaltungsorte + Nutzerverwaltung