refactor: move modules and use whole user in templates
This commit is contained in:
parent
b736b04ced
commit
a06f86dcaa
@ -3,7 +3,7 @@ use actix_web::{http::header::LOCATION, web, HttpMessage, HttpRequest, HttpRespo
|
|||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use sqlx::PgPool;
|
use sqlx::PgPool;
|
||||||
|
|
||||||
use crate::{auth::utils::hash_plain_password_with_salt, models::user::User};
|
use crate::{auth::utils::hash_plain_password_with_salt, models::User};
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct LoginForm {
|
struct LoginForm {
|
||||||
|
@ -2,7 +2,7 @@ use actix_web::{http::header::LOCATION, web, HttpResponse, Responder};
|
|||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use sqlx::PgPool;
|
use sqlx::PgPool;
|
||||||
|
|
||||||
use crate::{auth::utils::generate_salt_and_hash_plain_password, models::user::User};
|
use crate::{auth::utils::generate_salt_and_hash_plain_password, models::User};
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct RegisterForm {
|
struct RegisterForm {
|
||||||
|
@ -3,7 +3,7 @@ use actix_web::{web, HttpResponse, Responder};
|
|||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use sqlx::PgPool;
|
use sqlx::PgPool;
|
||||||
|
|
||||||
use crate::models::availabillity::Availabillity;
|
use crate::models::Availabillity;
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
pub struct AvailabillityPath {
|
pub struct AvailabillityPath {
|
||||||
|
@ -6,12 +6,12 @@ use chrono::NaiveDate;
|
|||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use sqlx::PgPool;
|
use sqlx::PgPool;
|
||||||
|
|
||||||
use crate::models::{role::Role, user::User};
|
use crate::models::User;
|
||||||
|
|
||||||
#[derive(Template)]
|
#[derive(Template)]
|
||||||
#[template(path = "availabillity_new.html")]
|
#[template(path = "availabillity_new.html")]
|
||||||
struct AvailabillityNewTemplate {
|
struct AvailabillityNewTemplate {
|
||||||
user_role: Role,
|
user: User,
|
||||||
date: NaiveDate
|
date: NaiveDate
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -30,7 +30,7 @@ pub async fn get_availabillity_new(
|
|||||||
if let Some(user) = user {
|
if let Some(user) = user {
|
||||||
let current_user = User::read_by_id(pool.as_ref(), user.id().unwrap().parse().unwrap()).await.unwrap();
|
let current_user = User::read_by_id(pool.as_ref(), user.id().unwrap().parse().unwrap()).await.unwrap();
|
||||||
|
|
||||||
let template = AvailabillityNewTemplate { user_role: current_user.role, date: query.date };
|
let template = AvailabillityNewTemplate { user: current_user, date: query.date };
|
||||||
|
|
||||||
template.to_response()
|
template.to_response()
|
||||||
} else {
|
} else {
|
||||||
|
@ -4,7 +4,7 @@ use chrono::{NaiveDate, NaiveTime};
|
|||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use sqlx::PgPool;
|
use sqlx::PgPool;
|
||||||
|
|
||||||
use crate::models::{availabillity::Availabillity, user::User};
|
use crate::models::{Availabillity, User};
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
pub struct AvailabillityForm {
|
pub struct AvailabillityForm {
|
||||||
|
@ -5,11 +5,14 @@ use chrono::{NaiveDate, Utc};
|
|||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use sqlx::PgPool;
|
use sqlx::PgPool;
|
||||||
|
|
||||||
use crate::models::{
|
use crate::models::{Area, Availabillity, Event, Function, User};
|
||||||
area::Area, availabillity::Availabillity, event::Event, function, role::Role, user::User
|
|
||||||
};
|
|
||||||
|
|
||||||
use super::{delete_availabillity::delete_availabillity, get_availabillity_new::get_availabillity_new, post_availabillity::post_availabillity, update_availabillity::{get_update_availabillity, post_update_availabillity}};
|
use super::{
|
||||||
|
delete_availabillity::delete_availabillity,
|
||||||
|
get_availabillity_new::get_availabillity_new,
|
||||||
|
post_availabillity::post_availabillity,
|
||||||
|
update_availabillity::{get_update_availabillity, post_update_availabillity},
|
||||||
|
};
|
||||||
|
|
||||||
pub fn init(cfg: &mut web::ServiceConfig) {
|
pub fn init(cfg: &mut web::ServiceConfig) {
|
||||||
cfg.service(get_index);
|
cfg.service(get_index);
|
||||||
@ -28,16 +31,13 @@ pub struct CalendarQuery {
|
|||||||
#[derive(Template)]
|
#[derive(Template)]
|
||||||
#[template(path = "index.html")]
|
#[template(path = "index.html")]
|
||||||
struct CalendarTemplate {
|
struct CalendarTemplate {
|
||||||
user_role: Role,
|
user: User,
|
||||||
current_user_id: i32,
|
|
||||||
date: NaiveDate,
|
date: NaiveDate,
|
||||||
area: Area,
|
area: Area,
|
||||||
events: Vec<Event>,
|
events: Vec<Event>,
|
||||||
availabillities: Vec<Availabillity>,
|
availabillities: Vec<Availabillity>,
|
||||||
}
|
}
|
||||||
|
|
||||||
type Function = function::Function;
|
|
||||||
|
|
||||||
#[actix_web::get("/")]
|
#[actix_web::get("/")]
|
||||||
async fn get_index(
|
async fn get_index(
|
||||||
user: Identity,
|
user: Identity,
|
||||||
@ -60,8 +60,7 @@ async fn get_index(
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let template = CalendarTemplate {
|
let template = CalendarTemplate {
|
||||||
user_role: current_user.role,
|
user: current_user,
|
||||||
current_user_id: current_user.id,
|
|
||||||
date,
|
date,
|
||||||
area,
|
area,
|
||||||
events,
|
events,
|
||||||
|
@ -5,14 +5,14 @@ use askama_actix::TemplateToResponse;
|
|||||||
use chrono::{NaiveDate, NaiveTime};
|
use chrono::{NaiveDate, NaiveTime};
|
||||||
use sqlx::PgPool;
|
use sqlx::PgPool;
|
||||||
|
|
||||||
use crate::{calendar::post_availabillity::AvailabillityForm, models::{availabillity::Availabillity, role::Role, user::User}};
|
use crate::{calendar::post_availabillity::AvailabillityForm, models::{Availabillity, User}};
|
||||||
|
|
||||||
use super::delete_availabillity::AvailabillityPath;
|
use super::delete_availabillity::AvailabillityPath;
|
||||||
|
|
||||||
#[derive(Template)]
|
#[derive(Template)]
|
||||||
#[template(path = "availabillity_edit.html")]
|
#[template(path = "availabillity_edit.html")]
|
||||||
pub struct AvailabillityEditTemplate {
|
pub struct AvailabillityEditTemplate {
|
||||||
user_role: Role,
|
user: User,
|
||||||
date: NaiveDate,
|
date: NaiveDate,
|
||||||
id: i32,
|
id: i32,
|
||||||
start_time: String,
|
start_time: String,
|
||||||
@ -50,7 +50,7 @@ pub async fn get_update_availabillity(
|
|||||||
let comment = availabillity.comment.unwrap_or(String::new());
|
let comment = availabillity.comment.unwrap_or(String::new());
|
||||||
|
|
||||||
let template = AvailabillityEditTemplate {
|
let template = AvailabillityEditTemplate {
|
||||||
user_role: current_user.role,
|
user: current_user,
|
||||||
date: availabillity.date,
|
date: availabillity.date,
|
||||||
id: path.id,
|
id: path.id,
|
||||||
start_time,
|
start_time,
|
||||||
@ -73,10 +73,6 @@ pub async fn post_update_availabillity(
|
|||||||
path: web::Path<AvailabillityPath>,
|
path: web::Path<AvailabillityPath>,
|
||||||
form: web::Form<AvailabillityForm>
|
form: web::Form<AvailabillityForm>
|
||||||
) -> impl Responder {
|
) -> impl Responder {
|
||||||
let current_user = User::read_by_id(pool.as_ref(), user.id().unwrap().parse().unwrap())
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
if let Ok(mut availabillity) = Availabillity::read_by_id(pool.get_ref(), path.id).await {
|
if let Ok(mut availabillity) = Availabillity::read_by_id(pool.get_ref(), path.id).await {
|
||||||
if availabillity.user_id == user.id().unwrap().parse::<i32>().unwrap() {
|
if availabillity.user_id == user.id().unwrap().parse::<i32>().unwrap() {
|
||||||
let mut has_changed = false;
|
let mut has_changed = false;
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use chrono::{NaiveDate, NaiveTime};
|
use chrono::{NaiveDate, NaiveTime};
|
||||||
use sqlx::{query, query_as, PgPool};
|
use sqlx::{query, PgPool};
|
||||||
|
|
||||||
pub struct Event {
|
pub struct Event {
|
||||||
pub id: i32,
|
pub id: i32,
|
||||||
|
@ -1,9 +1,17 @@
|
|||||||
pub mod role;
|
mod area;
|
||||||
pub mod function;
|
mod assignement;
|
||||||
pub mod user;
|
mod availabillity;
|
||||||
pub mod event;
|
mod event;
|
||||||
pub mod area;
|
mod function;
|
||||||
pub mod vehicle;
|
mod location;
|
||||||
pub mod availabillity;
|
mod role;
|
||||||
pub mod assignement;
|
mod user;
|
||||||
pub mod location;
|
mod vehicle;
|
||||||
|
|
||||||
|
pub use area::Area;
|
||||||
|
pub use availabillity::Availabillity;
|
||||||
|
pub use event::Event;
|
||||||
|
pub use function::Function;
|
||||||
|
pub use location::Location;
|
||||||
|
pub use role::Role;
|
||||||
|
pub use user::User;
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#[derive(sqlx::Type, Debug, Clone)]
|
#[derive(sqlx::Type, Debug, Clone, PartialEq)]
|
||||||
#[sqlx(type_name = "role", rename_all = "lowercase")]
|
#[sqlx(type_name = "role", rename_all = "lowercase")]
|
||||||
pub enum Role {
|
pub enum Role {
|
||||||
Staff = 1,
|
Staff = 1,
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
use sqlx::PgPool;
|
use sqlx::PgPool;
|
||||||
|
|
||||||
use super::{function::Function, role::Role};
|
use super::{Function, Role};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct User {
|
pub struct User {
|
||||||
|
@ -98,7 +98,7 @@
|
|||||||
{{ availabillity.comment.as_deref().unwrap_or("") }}
|
{{ availabillity.comment.as_deref().unwrap_or("") }}
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
{% if availabillity.user_id == current_user_id %}
|
{% if availabillity.user_id == user.id %}
|
||||||
<div class="buttons is-right">
|
<div class="buttons is-right">
|
||||||
<a class="button is-link" href="/availabillity/edit/{{ availabillity.id }}">Bearbeiten</a>
|
<a class="button is-link" href="/availabillity/edit/{{ availabillity.id }}">Bearbeiten</a>
|
||||||
<button class="button is-danger" name="delete-availabillity">Löschen</button>
|
<button class="button is-danger" name="delete-availabillity">Löschen</button>
|
||||||
|
@ -20,7 +20,7 @@
|
|||||||
Kalender
|
Kalender
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
{% match user_role %}
|
{% match user.role %}
|
||||||
{% when AreaManager %}
|
{% when AreaManager %}
|
||||||
<a class="navbar-item">
|
<a class="navbar-item">
|
||||||
Planung
|
Planung
|
||||||
|
Loading…
x
Reference in New Issue
Block a user