feat: add location overview for admin and areamanager

This commit is contained in:
Max Hohlfeld 2024-03-12 12:16:42 +01:00
parent 0172e9bdad
commit b4baa6ac9e
6 changed files with 72 additions and 34 deletions

View File

@ -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<Area>,
locations: Vec<Location>
}
#[actix_web::get("/locations")]
pub async fn get(user: Identity, pool: web::Data<PgPool>) -> 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!");
}

View File

@ -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<Location>
}
#[actix_web::get("/locations")]
pub async fn get(user: Identity, pool: web::Data<PgPool>) -> 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;

View File

@ -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);

View File

@ -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<Area>,
}
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<Vec<Location>> {
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();

View File

@ -6,7 +6,7 @@
<div class="level">
<div class="level-left">
<h3 class="title is-3">
Veranstaltungsorte im Bereich {{ area.name }}
Veranstaltungsorte im Bereich
</h3>
</div>
<div class="level-right">

View File

@ -33,6 +33,12 @@
Nutzerverwaltung
</a>
{% when Role::Admin %}
<a class="navbar-item">
Planung
</a>
<a href="/locations" class="navbar-item">
Veranstaltungsorte
</a>
<a href="/users" class="navbar-item">
Nutzerverwaltung
</a>