Add route /family/list

This commit is contained in:
Pierre HUBERT 2023-06-20 18:55:14 +02:00
parent 0ef722d29f
commit 3004f57185
5 changed files with 33 additions and 3 deletions

View File

@ -63,3 +63,8 @@ pub async fn join(
Ok(HttpResponse::Accepted().finish())
}
/// Get the list of families of the user
pub async fn list(token: LoginToken) -> HttpResult {
Ok(HttpResponse::Ok().json(families_service::get_user_memberships(token.user_id).await?))
}

View File

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

View File

@ -6,7 +6,7 @@ diesel::table! {
time_create -> Int8,
is_admin -> Bool,
invitation_code -> Varchar,
count_members -> Int4,
count_admins -> Int4,
count_members -> Int8,
count_admins -> Int8,
}
}

View File

@ -94,3 +94,15 @@ pub struct NewMembership {
pub time_create: i64,
pub is_admin: bool,
}
#[derive(Queryable, Debug, serde::Serialize)]
pub struct FamilyMembership {
user_id: i32,
family_id: i32,
name: String,
time_create: i64,
is_admin: bool,
invitation_code: String,
count_members: i64,
count_admins: i64,
}

View File

@ -1,6 +1,9 @@
use crate::connections::db_connection;
use crate::constants::FAMILY_INVITATION_CODE_LEN;
use crate::models::{Family, FamilyID, Membership, NewFamily, NewMembership, UserID};
use crate::manual_schema::families_memberships;
use crate::models::{
Family, FamilyID, FamilyMembership, Membership, NewFamily, NewMembership, UserID,
};
use crate::schema::{families, memberships};
use crate::utils::string_utils::rand_str;
use crate::utils::time_utils::time;
@ -66,6 +69,15 @@ pub async fn is_member(family_id: FamilyID, user_id: UserID) -> anyhow::Result<b
.map(|c: i64| c > 0)
}
/// Get the memberships of a user
pub async fn get_user_memberships(user_id: UserID) -> anyhow::Result<Vec<FamilyMembership>> {
db_connection::execute(|conn| {
families_memberships::table
.filter(families_memberships::dsl::user_id.eq(user_id.0))
.get_results(conn)
})
}
/// Remove a membership to a family
pub async fn remove_membership(_family_id: FamilyID, _user_id: UserID) {
todo!()