diff --git a/src/tera/admin/mod.rs b/src/tera/admin/mod.rs index 7f3dae4..f32a2db 100644 --- a/src/tera/admin/mod.rs +++ b/src/tera/admin/mod.rs @@ -10,6 +10,7 @@ use crate::{ pub mod boat; pub mod mail; +pub mod notification; pub mod planned_event; pub mod schnupper; pub mod user; @@ -77,6 +78,7 @@ pub fn routes() -> Vec { ret.append(&mut user::routes()); ret.append(&mut schnupper::routes()); ret.append(&mut boat::routes()); + ret.append(&mut notification::routes()); ret.append(&mut mail::routes()); ret.append(&mut planned_event::routes()); ret.append(&mut routes![rss, show_rss, show_list, list]); diff --git a/src/tera/admin/notification.rs b/src/tera/admin/notification.rs new file mode 100644 index 0000000..e8d6cb3 --- /dev/null +++ b/src/tera/admin/notification.rs @@ -0,0 +1,72 @@ +use crate::model::{ + log::Log, + notification::Notification, + role::Role, + user::{AdminUser, User, UserWithRoles}, +}; +use rocket::{ + form::Form, + get, post, + request::FlashMessage, + response::{Flash, Redirect}, + routes, FromForm, Route, State, +}; +use rocket_dyn_templates::{tera::Context, Template}; +use sqlx::SqlitePool; + +#[get("/notification")] +async fn index( + db: &State, + user: AdminUser, + flash: Option>, +) -> Template { + let mut context = Context::new(); + if let Some(msg) = flash { + context.insert("flash", &msg.into_inner()); + } + context.insert( + "loggedin_user", + &UserWithRoles::from_user(user.user.into(), db).await, + ); + context.insert("roles", &Role::all(db).await); + + Template::render("admin/notification", context.into_json()) +} + +#[derive(FromForm, Debug)] +pub struct NotificationToSend { + pub(crate) role_id: i32, + pub(crate) category: String, + pub(crate) message: String, +} + +#[post("/notification", data = "")] +async fn send( + db: &State, + data: Form, + admin: AdminUser, +) -> Flash { + let d = data.into_inner(); + Log::create( + db, + format!("{admin:?} trying to send this notification: {d:?}"), + ) + .await; + + let Some(role) = Role::find_by_id(db, d.role_id).await else { + return Flash::error(Redirect::to("/admin/notification"), "Rolle gibt's ned"); + }; + + for user in User::all_with_role(&db, &role).await { + Notification::create(db, &user, &d.message, &d.category, None).await; + } + Log::create(db, "Notification successfully sent".into()).await; + Flash::success( + Redirect::to("/admin/notification"), + "Nachricht ausgeschickt", + ) +} + +pub fn routes() -> Vec { + routes![index, send] +} diff --git a/templates/admin/notification.html.tera b/templates/admin/notification.html.tera new file mode 100644 index 0000000..88b209a --- /dev/null +++ b/templates/admin/notification.html.tera @@ -0,0 +1,20 @@ +{% import "includes/macros" as macros %} +{% import "includes/forms/boat" as boat %} +{% extends "base" %} +{% block content %} +
+

Nachricht

+
+ +
+
+{% endblock content %} diff --git a/templates/index.html.tera b/templates/index.html.tera index 040388e..06d985e 100644 --- a/templates/index.html.tera +++ b/templates/index.html.tera @@ -153,6 +153,9 @@
  • Fingerabdruck-Liste überprüfen
  • +
  • + Nachricht ausschreiben +
  • {% endif %}