Add /family/{id}/info route

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

View File

@ -1,5 +1,6 @@
use crate::constants::StaticConstraints; use crate::constants::StaticConstraints;
use crate::controllers::HttpResult; use crate::controllers::HttpResult;
use crate::extractors::family_extractor::FamilyInPath;
use crate::services::login_token_service::LoginToken; use crate::services::login_token_service::LoginToken;
use crate::services::rate_limiter_service::RatedAction; use crate::services::rate_limiter_service::RatedAction;
use crate::services::{families_service, rate_limiter_service}; use crate::services::{families_service, rate_limiter_service};
@ -66,5 +67,14 @@ pub async fn join(
/// Get the list of families of the user /// Get the list of families of the user
pub async fn list(token: LoginToken) -> HttpResult { pub async fn list(token: LoginToken) -> HttpResult {
Ok(HttpResponse::Ok().json(families_service::get_user_memberships(token.user_id).await?)) Ok(
HttpResponse::Ok()
.json(families_service::get_user_family_memberships(token.user_id).await?),
)
}
/// Get the information of a single family
pub async fn single_info(f: FamilyInPath) -> HttpResult {
Ok(HttpResponse::Ok()
.json(families_service::get_family_membership(f.family_id(), f.user_id()).await?))
} }

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;

View File

@ -2,6 +2,7 @@ pub mod app_config;
pub mod connections; pub mod connections;
pub mod constants; pub mod constants;
pub mod controllers; pub mod controllers;
pub mod extractors;
pub mod services; pub mod services;
pub mod utils; pub mod utils;

View File

@ -93,6 +93,10 @@ async fn main() -> std::io::Result<()> {
) )
.route("/family/join", web::post().to(families_controller::join)) .route("/family/join", web::post().to(families_controller::join))
.route("/family/list", web::get().to(families_controller::list)) .route("/family/list", web::get().to(families_controller::list))
.route(
"/family/{id}/info",
web::get().to(families_controller::single_info),
)
}) })
.bind(AppConfig::get().listen_address.as_str())? .bind(AppConfig::get().listen_address.as_str())?
.run() .run()

View File

@ -80,10 +80,20 @@ pub struct NewFamily<'a> {
#[derive(Queryable, Debug, serde::Serialize)] #[derive(Queryable, Debug, serde::Serialize)]
pub struct Membership { pub struct Membership {
user_id: i32, pub user_id: i32,
family_id: i32, pub family_id: i32,
time_create: i64, pub time_create: i64,
is_admin: bool, pub is_admin: bool,
}
impl Membership {
pub fn user_id(&self) -> UserID {
UserID(self.user_id)
}
pub fn family_id(&self) -> FamilyID {
FamilyID(self.family_id)
}
} }
#[derive(Insertable)] #[derive(Insertable)]

View File

@ -70,7 +70,7 @@ pub async fn is_member(family_id: FamilyID, user_id: UserID) -> anyhow::Result<b
} }
/// Get the memberships of a user /// Get the memberships of a user
pub async fn get_user_memberships(user_id: UserID) -> anyhow::Result<Vec<FamilyMembership>> { pub async fn get_user_family_memberships(user_id: UserID) -> anyhow::Result<Vec<FamilyMembership>> {
db_connection::execute(|conn| { db_connection::execute(|conn| {
families_memberships::table families_memberships::table
.filter(families_memberships::dsl::user_id.eq(user_id.0)) .filter(families_memberships::dsl::user_id.eq(user_id.0))
@ -78,6 +78,29 @@ pub async fn get_user_memberships(user_id: UserID) -> anyhow::Result<Vec<FamilyM
}) })
} }
/// Get information about a membership of a user
pub async fn get_membership(family_id: FamilyID, user_id: UserID) -> anyhow::Result<Membership> {
db_connection::execute(|conn| {
memberships::table
.filter(memberships::dsl::user_id.eq(user_id.0))
.filter(memberships::dsl::family_id.eq(family_id.0))
.get_result(conn)
})
}
/// Get information about a membership of a user, joined with family information
pub async fn get_family_membership(
family_id: FamilyID,
user_id: UserID,
) -> anyhow::Result<FamilyMembership> {
db_connection::execute(|conn| {
families_memberships::table
.filter(families_memberships::dsl::user_id.eq(user_id.0))
.filter(families_memberships::dsl::family_id.eq(family_id.0))
.get_result(conn)
})
}
/// Remove a membership to a family /// Remove a membership to a family
pub async fn remove_membership(_family_id: FamilyID, _user_id: UserID) { pub async fn remove_membership(_family_id: FamilyID, _user_id: UserID) {
todo!() todo!()