Files
rustical/crates/frontend/src/routes/addressbook.rs

60 lines
1.7 KiB
Rust

use std::sync::Arc;
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::{Addressbook, AddressbookStore, auth::Principal};
use crate::pages::DefaultLayoutData;
#[derive(Template, WebTemplate)]
#[template(path = "pages/addressbook.html")]
struct AddressbookPage {
addressbook: Addressbook,
user: Principal,
}
impl DefaultLayoutData for AddressbookPage {
fn get_user(&self) -> Option<&Principal> {
Some(&self.user)
}
}
pub async fn route_addressbook<AS: AddressbookStore>(
Path((owner, addrbook_id)): Path<(String, String)>,
Extension(store): Extension<Arc<AS>>,
user: Principal,
) -> Result<Response, rustical_store::Error> {
if !user.is_principal(&owner) {
return Ok(StatusCode::UNAUTHORIZED.into_response());
}
Ok(AddressbookPage {
addressbook: store.get_addressbook(&owner, &addrbook_id, true).await?,
user,
}
.into_response())
}
pub async fn route_addressbook_restore<AS: AddressbookStore>(
Path((owner, addressbook_id)): Path<(String, String)>,
Extension(store): Extension<Arc<AS>>,
user: Principal,
referer: Option<TypedHeader<Referer>>,
) -> Result<Response, rustical_store::Error> {
if !user.is_principal(&owner) {
return Ok(StatusCode::UNAUTHORIZED.into_response());
}
store.restore_addressbook(&owner, &addressbook_id).await?;
Ok(referer.map_or_else(
|| (StatusCode::CREATED, "Restored").into_response(),
|referer| Redirect::to(&referer.to_string()).into_response(),
))
}