Add /family/{id}/info route

This commit is contained in:
2023-06-21 16:36:46 +02:00
parent 053f08f927
commit 381a4797e4
7 changed files with 111 additions and 6 deletions

View File

@ -0,0 +1,56 @@
use crate::models::{FamilyID, Membership, UserID};
use crate::services::families_service;
use crate::services::login_token_service::LoginToken;
use actix_web::dev::Payload;
use actix_web::{FromRequest, HttpRequest};
use serde::Deserialize;
#[derive(Debug)]
pub struct FamilyInPath(Membership);
impl FamilyInPath {
async fn load_family_from_path(t: &LoginToken, id: FamilyID) -> anyhow::Result<Self> {
Ok(Self(families_service::get_membership(id, t.user_id).await?))
}
pub fn user_id(&self) -> UserID {
self.0.user_id()
}
pub fn family_id(&self) -> FamilyID {
self.0.family_id()
}
pub fn is_admin(&self) -> bool {
self.0.is_admin
}
}
#[derive(Deserialize)]
struct FamilyIdInPath {
id: FamilyID,
}
impl FromRequest for FamilyInPath {
type Error = actix_web::Error;
type Future = futures_util::future::LocalBoxFuture<'static, Result<Self, Self::Error>>;
fn from_request(req: &HttpRequest, _payload: &mut Payload) -> Self::Future {
let req = req.clone();
Box::pin(async move {
let token = LoginToken::extract(&req).await?;
let family_id =
actix_web::web::Path::<FamilyIdInPath>::from_request(&req, &mut Payload::None)
.await?
.id;
FamilyInPath::load_family_from_path(&token, family_id)
.await
.map_err(|e| {
log::error!("Failed to extract family ID from URL! {}", e);
actix_web::error::ErrorNotFound("Could not fetch family information!")
})
})
}
}

View File

@ -0,0 +1 @@
pub mod family_extractor;