Add /family/{id}/users route
This commit is contained in:
parent
381a4797e4
commit
ad54f83c2c
@ -78,3 +78,8 @@ pub async fn single_info(f: FamilyInPath) -> HttpResult {
|
||||
Ok(HttpResponse::Ok()
|
||||
.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?))
|
||||
}
|
||||
|
@ -97,6 +97,10 @@ async fn main() -> std::io::Result<()> {
|
||||
"/family/{id}/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())?
|
||||
.run()
|
||||
|
@ -7,7 +7,7 @@ pub struct UserID(pub i32);
|
||||
|
||||
#[derive(Queryable, Debug, serde::Serialize)]
|
||||
pub struct User {
|
||||
pub id: i32,
|
||||
id: i32,
|
||||
pub name: String,
|
||||
pub email: String,
|
||||
#[serde(skip_serializing)]
|
||||
@ -58,7 +58,7 @@ pub struct FamilyID(pub i32);
|
||||
|
||||
#[derive(Queryable, Debug, serde::Serialize)]
|
||||
pub struct Family {
|
||||
pub id: i32,
|
||||
id: i32,
|
||||
pub time_create: i64,
|
||||
pub name: String,
|
||||
pub invitation_code: String,
|
||||
@ -80,8 +80,8 @@ pub struct NewFamily<'a> {
|
||||
|
||||
#[derive(Queryable, Debug, serde::Serialize)]
|
||||
pub struct Membership {
|
||||
pub user_id: i32,
|
||||
pub family_id: i32,
|
||||
user_id: i32,
|
||||
family_id: i32,
|
||||
pub time_create: i64,
|
||||
pub is_admin: bool,
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ use crate::models::{
|
||||
Family, FamilyID, FamilyMembership, Membership, NewFamily, NewMembership, UserID,
|
||||
};
|
||||
use crate::schema::{families, memberships};
|
||||
use crate::services::users_service;
|
||||
use crate::utils::string_utils::rand_str;
|
||||
use crate::utils::time_utils::time;
|
||||
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
|
||||
pub async fn get_family_membership(
|
||||
family_id: FamilyID,
|
||||
|
@ -177,7 +177,7 @@ pub async fn delete_account(user: &User) -> anyhow::Result<()> {
|
||||
login_token_service::disconnect_user_from_all_devices(user.id()).await?;
|
||||
|
||||
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(())
|
||||
})
|
||||
}
|
||||
@ -186,8 +186,8 @@ pub async fn delete_account(user: &User) -> anyhow::Result<()> {
|
||||
pub async fn validate_account(user: &mut User) -> anyhow::Result<()> {
|
||||
if user.time_activate > 0 {
|
||||
log::debug!(
|
||||
"Did not activate account {} because it is already activated!",
|
||||
user.id
|
||||
"Did not activate account {:?} because it is already activated!",
|
||||
user.id()
|
||||
);
|
||||
return Ok(());
|
||||
}
|
||||
@ -212,7 +212,7 @@ pub async fn validate_account(user: &mut User) -> anyhow::Result<()> {
|
||||
/// Update account information
|
||||
pub async fn update_account(user: &User) -> anyhow::Result<()> {
|
||||
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((
|
||||
users::dsl::name.eq(user.name.clone()),
|
||||
users::dsl::admin.eq(user.admin),
|
||||
|
Loading…
Reference in New Issue
Block a user