Automatically delete members when families are deleted
This commit is contained in:
		@@ -46,8 +46,8 @@ CREATE TABLE members (
 | 
			
		||||
    sex VARCHAR(1) NULL,
 | 
			
		||||
    time_create BIGINT NOT NULL,
 | 
			
		||||
    time_update BIGINT NOT NULL,
 | 
			
		||||
    mother integer NULL REFERENCES members,
 | 
			
		||||
    father integer NULL REFERENCES members,
 | 
			
		||||
    mother integer NULL REFERENCES members ON DELETE SET NULL,
 | 
			
		||||
    father integer NULL REFERENCES members ON DELETE SET NULL,
 | 
			
		||||
    birth_year smallint NULL,
 | 
			
		||||
    birth_month smallint NULL,
 | 
			
		||||
    birth_day smallint NULL,
 | 
			
		||||
 
 | 
			
		||||
@@ -130,7 +130,7 @@ pub enum Sex {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Sex {
 | 
			
		||||
    pub fn from_str(s: &str) -> Option<Self> {
 | 
			
		||||
    pub fn parse_str(s: &str) -> Option<Self> {
 | 
			
		||||
        match s {
 | 
			
		||||
            "M" => Some(Sex::Male),
 | 
			
		||||
            "F" => Some(Sex::Female),
 | 
			
		||||
@@ -184,10 +184,7 @@ impl Member {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn sex(&self) -> Option<Sex> {
 | 
			
		||||
        self.sex
 | 
			
		||||
            .as_deref()
 | 
			
		||||
            .map(|s| Sex::from_str(s))
 | 
			
		||||
            .unwrap_or_default()
 | 
			
		||||
        self.sex.as_deref().map(Sex::parse_str).unwrap_or_default()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn set_sex(&mut self, s: Option<Sex>) {
 | 
			
		||||
 
 | 
			
		||||
@@ -5,7 +5,7 @@ use crate::models::{
 | 
			
		||||
    Family, FamilyID, FamilyMembership, Membership, NewFamily, NewMembership, UserID,
 | 
			
		||||
};
 | 
			
		||||
use crate::schema::{families, memberships};
 | 
			
		||||
use crate::services::users_service;
 | 
			
		||||
use crate::services::{members_service, users_service};
 | 
			
		||||
use crate::utils::string_utils::rand_str;
 | 
			
		||||
use crate::utils::time_utils::time;
 | 
			
		||||
use diesel::prelude::*;
 | 
			
		||||
@@ -183,7 +183,10 @@ pub async fn update_family(family: &Family) -> anyhow::Result<()> {
 | 
			
		||||
 | 
			
		||||
/// Delete a family
 | 
			
		||||
pub async fn delete_family(family_id: FamilyID) -> anyhow::Result<()> {
 | 
			
		||||
    // TODO : delete members and couples
 | 
			
		||||
    // TODO : delete couples
 | 
			
		||||
 | 
			
		||||
    // Remove all family members
 | 
			
		||||
    members_service::delete_all_family(family_id).await?;
 | 
			
		||||
 | 
			
		||||
    // Remove all memberships
 | 
			
		||||
    db_connection::execute(|conn| {
 | 
			
		||||
 
 | 
			
		||||
@@ -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(())
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user