Can create a family member
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
use crate::schema::{families, memberships, users};
|
||||
use crate::schema::{families, members, memberships, users};
|
||||
use diesel::prelude::*;
|
||||
|
||||
/// User ID holder
|
||||
@ -116,3 +116,105 @@ pub struct FamilyMembership {
|
||||
pub count_members: i64,
|
||||
pub count_admins: i64,
|
||||
}
|
||||
|
||||
/// Member ID holder
|
||||
#[derive(Debug, Clone, Copy, serde::Serialize, serde::Deserialize)]
|
||||
pub struct MemberID(pub i32);
|
||||
|
||||
#[derive(serde::Serialize, serde::Deserialize)]
|
||||
pub enum Sex {
|
||||
#[serde(rename = "M")]
|
||||
Male,
|
||||
#[serde(rename = "F")]
|
||||
Female,
|
||||
}
|
||||
|
||||
impl Sex {
|
||||
pub fn from_str(s: &str) -> Option<Self> {
|
||||
match s {
|
||||
"M" => Some(Sex::Male),
|
||||
"F" => Some(Sex::Female),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_str(&self) -> &'static str {
|
||||
match self {
|
||||
Sex::Male => "M",
|
||||
Sex::Female => "F",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Queryable, Debug, serde::Serialize)]
|
||||
pub struct Member {
|
||||
id: i32,
|
||||
family_id: i32,
|
||||
pub first_name: Option<String>,
|
||||
pub last_name: Option<String>,
|
||||
pub birth_last_name: Option<String>,
|
||||
pub photo_id: Option<String>,
|
||||
pub email: Option<String>,
|
||||
pub phone: Option<String>,
|
||||
pub address: Option<String>,
|
||||
pub city: Option<String>,
|
||||
pub postal_code: Option<String>,
|
||||
pub country: Option<String>,
|
||||
sex: Option<String>,
|
||||
time_create: i64,
|
||||
pub time_update: i64,
|
||||
mother: Option<i32>,
|
||||
father: Option<i32>,
|
||||
pub birth_year: Option<i16>,
|
||||
pub birth_month: Option<i16>,
|
||||
pub birth_day: Option<i16>,
|
||||
pub death_year: Option<i16>,
|
||||
pub death_month: Option<i16>,
|
||||
pub death_day: Option<i16>,
|
||||
pub note: Option<String>,
|
||||
}
|
||||
|
||||
impl Member {
|
||||
pub fn id(&self) -> MemberID {
|
||||
MemberID(self.id)
|
||||
}
|
||||
|
||||
pub fn family_id(&self) -> FamilyID {
|
||||
FamilyID(self.family_id)
|
||||
}
|
||||
|
||||
pub fn sex(&self) -> Option<Sex> {
|
||||
self.sex
|
||||
.as_deref()
|
||||
.map(|s| Sex::from_str(s))
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
pub fn set_sex(&mut self, s: Option<Sex>) {
|
||||
self.sex = s.map(|s| s.to_str().to_string())
|
||||
}
|
||||
|
||||
pub fn mother(&self) -> Option<MemberID> {
|
||||
self.mother.map(MemberID)
|
||||
}
|
||||
|
||||
pub fn set_mother(&mut self, p: Option<MemberID>) {
|
||||
self.mother = p.map(|p| p.0);
|
||||
}
|
||||
|
||||
pub fn father(&self) -> Option<MemberID> {
|
||||
self.father.map(MemberID)
|
||||
}
|
||||
|
||||
pub fn set_father(&mut self, p: Option<MemberID>) {
|
||||
self.father = p.map(|p| p.0);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Insertable)]
|
||||
#[diesel(table_name = members)]
|
||||
pub struct NewMember {
|
||||
pub family_id: i32,
|
||||
pub time_create: i64,
|
||||
pub time_update: i64,
|
||||
}
|
||||
|
Reference in New Issue
Block a user