Automatically delete members when families are deleted

This commit is contained in:
2023-08-04 19:17:51 +02:00
parent f344765dd8
commit 8f5131429f
6 changed files with 34 additions and 9 deletions

View File

@ -25,6 +25,15 @@ pub async fn get_by_id(id: MemberID) -> anyhow::Result<Member> {
db_connection::execute(|conn| members::table.filter(members::dsl::id.eq(id.0)).first(conn))
}
/// Get all the members of a family
pub async fn get_all_of_family(id: FamilyID) -> anyhow::Result<Vec<Member>> {
db_connection::execute(|conn| {
members::table
.filter(members::dsl::family_id.eq(id.0))
.get_results(conn)
})
}
/// Check whether a member with a given id exists or not
pub async fn exists(family_id: FamilyID, member_id: MemberID) -> anyhow::Result<bool> {
db_connection::execute(|conn| {
@ -89,3 +98,11 @@ pub async fn delete(member: &Member) -> anyhow::Result<()> {
Ok(())
}
/// Delete all the members of a family
pub async fn delete_all_family(family_id: FamilyID) -> anyhow::Result<()> {
for m in get_all_of_family(family_id).await? {
delete(&m).await?;
}
Ok(())
}