Add /family/{id}/info route
This commit is contained in:
56
geneit_backend/src/extractors/family_extractor.rs
Normal file
56
geneit_backend/src/extractors/family_extractor.rs
Normal 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!")
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
1
geneit_backend/src/extractors/mod.rs
Normal file
1
geneit_backend/src/extractors/mod.rs
Normal file
@ -0,0 +1 @@
|
||||
pub mod family_extractor;
|
Reference in New Issue
Block a user