Add /family/{id}/users route

This commit is contained in:
Pierre HUBERT 2023-06-21 17:01:52 +02:00
parent 381a4797e4
commit ad54f83c2c
5 changed files with 48 additions and 8 deletions

View File

@ -78,3 +78,8 @@ pub async fn single_info(f: FamilyInPath) -> HttpResult {
Ok(HttpResponse::Ok() Ok(HttpResponse::Ok()
.json(families_service::get_family_membership(f.family_id(), f.user_id()).await?)) .json(families_service::get_family_membership(f.family_id(), f.user_id()).await?))
} }
/// Get the list of users who belongs to a family
pub async fn users(f: FamilyInPath) -> HttpResult {
Ok(HttpResponse::Ok().json(families_service::get_memberships_of_family(f.family_id()).await?))
}

View File

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

View File

@ -7,7 +7,7 @@ pub struct UserID(pub i32);
#[derive(Queryable, Debug, serde::Serialize)] #[derive(Queryable, Debug, serde::Serialize)]
pub struct User { pub struct User {
pub id: i32, id: i32,
pub name: String, pub name: String,
pub email: String, pub email: String,
#[serde(skip_serializing)] #[serde(skip_serializing)]
@ -58,7 +58,7 @@ pub struct FamilyID(pub i32);
#[derive(Queryable, Debug, serde::Serialize)] #[derive(Queryable, Debug, serde::Serialize)]
pub struct Family { pub struct Family {
pub id: i32, id: i32,
pub time_create: i64, pub time_create: i64,
pub name: String, pub name: String,
pub invitation_code: String, pub invitation_code: String,
@ -80,8 +80,8 @@ pub struct NewFamily<'a> {
#[derive(Queryable, Debug, serde::Serialize)] #[derive(Queryable, Debug, serde::Serialize)]
pub struct Membership { pub struct Membership {
pub user_id: i32, user_id: i32,
pub family_id: i32, family_id: i32,
pub time_create: i64, pub time_create: i64,
pub is_admin: bool, pub is_admin: bool,
} }

View File

@ -5,6 +5,7 @@ use crate::models::{
Family, FamilyID, FamilyMembership, Membership, NewFamily, NewMembership, UserID, Family, FamilyID, FamilyMembership, Membership, NewFamily, NewMembership, UserID,
}; };
use crate::schema::{families, memberships}; use crate::schema::{families, memberships};
use crate::services::users_service;
use crate::utils::string_utils::rand_str; use crate::utils::string_utils::rand_str;
use crate::utils::time_utils::time; use crate::utils::time_utils::time;
use diesel::prelude::*; use diesel::prelude::*;
@ -88,6 +89,36 @@ pub async fn get_membership(family_id: FamilyID, user_id: UserID) -> anyhow::Res
}) })
} }
#[derive(serde::Serialize)]
pub struct FamilyMember {
#[serde(flatten)]
membership: Membership,
user_name: String,
user_mail: String,
}
/// Get information about the users of a family
pub async fn get_memberships_of_family(family_id: FamilyID) -> anyhow::Result<Vec<FamilyMember>> {
let memberships = db_connection::execute(|conn| {
memberships::table
.filter(memberships::dsl::family_id.eq(family_id.0))
.get_results::<Membership>(conn)
})?;
let mut out = Vec::with_capacity(memberships.len());
for m in memberships {
let user = users_service::get_by_id(m.user_id()).await?;
out.push(FamilyMember {
user_name: user.name,
user_mail: user.email,
membership: m,
})
}
Ok(out)
}
/// Get information about a membership of a user, joined with family information /// Get information about a membership of a user, joined with family information
pub async fn get_family_membership( pub async fn get_family_membership(
family_id: FamilyID, family_id: FamilyID,

View File

@ -177,7 +177,7 @@ pub async fn delete_account(user: &User) -> anyhow::Result<()> {
login_token_service::disconnect_user_from_all_devices(user.id()).await?; login_token_service::disconnect_user_from_all_devices(user.id()).await?;
db_connection::execute(|conn| { db_connection::execute(|conn| {
diesel::delete(users::dsl::users.filter(users::dsl::id.eq(user.id))).execute(conn)?; diesel::delete(users::dsl::users.filter(users::dsl::id.eq(user.id().0))).execute(conn)?;
Ok(()) Ok(())
}) })
} }
@ -186,8 +186,8 @@ pub async fn delete_account(user: &User) -> anyhow::Result<()> {
pub async fn validate_account(user: &mut User) -> anyhow::Result<()> { pub async fn validate_account(user: &mut User) -> anyhow::Result<()> {
if user.time_activate > 0 { if user.time_activate > 0 {
log::debug!( log::debug!(
"Did not activate account {} because it is already activated!", "Did not activate account {:?} because it is already activated!",
user.id user.id()
); );
return Ok(()); return Ok(());
} }
@ -212,7 +212,7 @@ pub async fn validate_account(user: &mut User) -> anyhow::Result<()> {
/// Update account information /// Update account information
pub async fn update_account(user: &User) -> anyhow::Result<()> { pub async fn update_account(user: &User) -> anyhow::Result<()> {
db_connection::execute(|conn| { db_connection::execute(|conn| {
diesel::update(users::dsl::users.filter(users::dsl::id.eq(user.id))) diesel::update(users::dsl::users.filter(users::dsl::id.eq(user.id().0)))
.set(( .set((
users::dsl::name.eq(user.name.clone()), users::dsl::name.eq(user.name.clone()),
users::dsl::admin.eq(user.admin), users::dsl::admin.eq(user.admin),