tetratto/crates/app/src/routes/api/v1/notifications.rs

111 lines
3.1 KiB
Rust
Raw Normal View History

use super::UpdateNotificationRead;
use crate::{State, get_user_from_token};
use axum::{Extension, Json, extract::Path, response::IntoResponse};
use axum_extra::extract::CookieJar;
use tetratto_core::model::{ApiReturn, Error};
pub async fn delete_request(
jar: CookieJar,
Extension(data): Extension<State>,
Path(id): Path<usize>,
) -> impl IntoResponse {
let data = &(data.read().await).0;
let user = match get_user_from_token!(jar, data) {
Some(ua) => ua,
None => return Json(Error::NotAllowed.into()),
};
match data.delete_notification(id, &user).await {
Ok(_) => Json(ApiReturn {
ok: true,
message: "Notification deleted".to_string(),
payload: (),
}),
Err(e) => Json(e.into()),
}
}
pub async fn delete_all_request(
jar: CookieJar,
Extension(data): Extension<State>,
) -> impl IntoResponse {
let data = &(data.read().await).0;
let user = match get_user_from_token!(jar, data) {
Some(ua) => ua,
None => return Json(Error::NotAllowed.into()),
};
match data.delete_all_notifications(&user).await {
Ok(_) => Json(ApiReturn {
ok: true,
message: "Notifications cleared".to_string(),
payload: (),
}),
Err(e) => Json(e.into()),
}
}
pub async fn delete_all_by_tag_request(
jar: CookieJar,
Extension(data): Extension<State>,
Path(tag): Path<String>,
) -> impl IntoResponse {
let data = &(data.read().await).0;
let user = match get_user_from_token!(jar, data) {
Some(ua) => ua,
None => return Json(Error::NotAllowed.into()),
};
match data.delete_all_notifications_by_tag(&user, &tag).await {
Ok(_) => Json(ApiReturn {
ok: true,
message: "Notifications cleared".to_string(),
payload: (),
}),
Err(e) => Json(e.into()),
}
}
pub async fn update_read_status_request(
jar: CookieJar,
Extension(data): Extension<State>,
Path(id): Path<usize>,
Json(req): Json<UpdateNotificationRead>,
) -> impl IntoResponse {
let data = &(data.read().await).0;
let user = match get_user_from_token!(jar, data) {
Some(ua) => ua,
None => return Json(Error::NotAllowed.into()),
};
match data.update_notification_read(id, req.read, &user).await {
Ok(_) => Json(ApiReturn {
ok: true,
message: "Notification updated".to_string(),
payload: (),
}),
Err(e) => Json(e.into()),
}
}
pub async fn update_all_read_status_request(
jar: CookieJar,
Extension(data): Extension<State>,
Json(req): Json<UpdateNotificationRead>,
) -> impl IntoResponse {
let data = &(data.read().await).0;
let user = match get_user_from_token!(jar, data) {
Some(ua) => ua,
None => return Json(Error::NotAllowed.into()),
};
match data.update_all_notifications_read(&user, req.read).await {
Ok(_) => Json(ApiReturn {
ok: true,
message: "Notifications updated".to_string(),
payload: (),
}),
Err(e) => Json(e.into()),
}
}