rowt/src/tera/notification.rs
Philipp Hofer 1ad4c31979
Some checks failed
CI/CD Pipeline / deploy-staging (push) Has been cancelled
CI/CD Pipeline / test (push) Has been cancelled
CI/CD Pipeline / deploy-main (push) Has been cancelled
allow lazy people to mark all notifcations as read
2024-12-19 21:15:27 +01:00

39 lines
1.1 KiB
Rust

use rocket::{
get,
response::{Flash, Redirect},
routes, Route, State,
};
use sqlx::SqlitePool;
use crate::model::{notification::Notification, user::User};
#[get("/<notification_id>/read")]
async fn mark_read(db: &State<SqlitePool>, user: User, notification_id: i64) -> Flash<Redirect> {
let Some(notification) = Notification::find_by_id(db, notification_id).await else {
return Flash::error(
Redirect::to("/"),
format!("Nachricht mit ID {notification_id} nicht gefunden."),
);
};
if notification.user_id == user.id {
notification.mark_read(db).await;
Flash::success(Redirect::to("/"), "Nachricht als gelesen markiert")
} else {
Flash::success(
Redirect::to("/"),
"Du kannst fremde Nachrichten nicht als gelesen markieren.",
)
}
}
#[get("/read/all")]
async fn mark_all_read(db: &State<SqlitePool>, user: User) -> Flash<Redirect> {
Notification::mark_all_read(db, &user).await;
Flash::success(Redirect::to("/"), "Alle Nachrichten als gelesen markiert")
}
pub fn routes() -> Vec<Route> {
routes![mark_read, mark_all_read]
}