mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-26 20:49:24 +00:00
58 lines
1.6 KiB
Rust
58 lines
1.6 KiB
Rust
use crate::pages::DefaultLayoutData;
|
|
use askama::Template;
|
|
use askama_web::WebTemplate;
|
|
use axum::{
|
|
Extension,
|
|
extract::Path,
|
|
response::{IntoResponse, Redirect, Response},
|
|
};
|
|
use axum_extra::TypedHeader;
|
|
use headers::Referer;
|
|
use http::StatusCode;
|
|
use rustical_store::{Calendar, CalendarStore, auth::Principal};
|
|
use std::sync::Arc;
|
|
|
|
#[derive(Template, WebTemplate)]
|
|
#[template(path = "pages/calendar.html")]
|
|
struct CalendarPage {
|
|
calendar: Calendar,
|
|
user: Principal,
|
|
}
|
|
|
|
impl DefaultLayoutData for CalendarPage {
|
|
fn get_user(&self) -> Option<&Principal> {
|
|
Some(&self.user)
|
|
}
|
|
}
|
|
|
|
pub async fn route_calendar<C: CalendarStore>(
|
|
Path((owner, cal_id)): Path<(String, String)>,
|
|
Extension(store): Extension<Arc<C>>,
|
|
user: Principal,
|
|
) -> Result<Response, rustical_store::Error> {
|
|
if !user.is_principal(&owner) {
|
|
return Ok(StatusCode::UNAUTHORIZED.into_response());
|
|
}
|
|
Ok(CalendarPage {
|
|
calendar: store.get_calendar(&owner, &cal_id, true).await?,
|
|
user,
|
|
}
|
|
.into_response())
|
|
}
|
|
|
|
pub async fn route_calendar_restore<CS: CalendarStore>(
|
|
Path((owner, cal_id)): Path<(String, String)>,
|
|
Extension(store): Extension<Arc<CS>>,
|
|
user: Principal,
|
|
referer: Option<TypedHeader<Referer>>,
|
|
) -> Result<Response, rustical_store::Error> {
|
|
if !user.is_principal(&owner) {
|
|
return Ok(StatusCode::UNAUTHORIZED.into_response());
|
|
}
|
|
store.restore_calendar(&owner, &cal_id).await?;
|
|
Ok(referer.map_or_else(
|
|
|| (StatusCode::CREATED, "Restored").into_response(),
|
|
|referer| Redirect::to(&referer.to_string()).into_response(),
|
|
))
|
|
}
|