Can create couple
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
use crate::app_config::AppConfig;
|
||||
use crate::schema::{families, members, memberships, photos, users};
|
||||
use crate::schema::{couples, families, members, memberships, photos, users};
|
||||
use crate::utils::crypt_utils::sha256;
|
||||
use diesel::prelude::*;
|
||||
|
||||
@ -297,3 +297,105 @@ pub struct NewMember {
|
||||
pub time_create: i64,
|
||||
pub time_update: i64,
|
||||
}
|
||||
|
||||
/// Member ID holder
|
||||
#[derive(Debug, Clone, Copy, serde::Serialize, serde::Deserialize, Eq, PartialEq, Hash)]
|
||||
pub struct CoupleID(pub i32);
|
||||
|
||||
#[derive(serde::Serialize, serde::Deserialize)]
|
||||
pub enum CoupleState {
|
||||
#[serde(rename = "N")]
|
||||
None,
|
||||
#[serde(rename = "E")]
|
||||
Engaged,
|
||||
#[serde(rename = "M")]
|
||||
Married,
|
||||
#[serde(rename = "D")]
|
||||
Divorced,
|
||||
}
|
||||
|
||||
impl CoupleState {
|
||||
pub fn as_str(&self) -> &str {
|
||||
match self {
|
||||
CoupleState::None => "N",
|
||||
CoupleState::Engaged => "E",
|
||||
CoupleState::Married => "M",
|
||||
CoupleState::Divorced => "D",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_str(s: &str) -> Option<Self> {
|
||||
serde_json::from_str(s).ok()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Queryable, Debug, serde::Serialize)]
|
||||
pub struct Couple {
|
||||
id: i32,
|
||||
family_id: i32,
|
||||
wife: Option<i32>,
|
||||
husband: Option<i32>,
|
||||
state: Option<String>,
|
||||
photo_id: Option<i32>,
|
||||
time_create: i64,
|
||||
pub time_update: i64,
|
||||
pub wedding_year: Option<i16>,
|
||||
pub wedding_month: Option<i16>,
|
||||
pub wedding_day: Option<i16>,
|
||||
pub divorce_year: Option<i16>,
|
||||
pub divorce_month: Option<i16>,
|
||||
pub divorce_day: Option<i16>,
|
||||
}
|
||||
|
||||
impl Couple {
|
||||
pub fn id(&self) -> CoupleID {
|
||||
CoupleID(self.id)
|
||||
}
|
||||
|
||||
pub fn family_id(&self) -> FamilyID {
|
||||
FamilyID(self.family_id)
|
||||
}
|
||||
|
||||
pub fn state(&self) -> Option<CoupleState> {
|
||||
self.state
|
||||
.as_deref()
|
||||
.map(CoupleState::parse_str)
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
pub fn set_state(&mut self, s: Option<CoupleState>) {
|
||||
self.state = s.map(|s| s.as_str().to_string())
|
||||
}
|
||||
|
||||
pub fn set_wife(&mut self, s: Option<MemberID>) {
|
||||
self.wife = s.map(|s| s.0)
|
||||
}
|
||||
|
||||
pub fn wife(&self) -> Option<MemberID> {
|
||||
self.wife.map(MemberID)
|
||||
}
|
||||
|
||||
pub fn set_husband(&mut self, s: Option<MemberID>) {
|
||||
self.husband = s.map(|s| s.0)
|
||||
}
|
||||
|
||||
pub fn husband(&self) -> Option<MemberID> {
|
||||
self.husband.map(MemberID)
|
||||
}
|
||||
|
||||
pub fn set_photo_id(&mut self, p: Option<PhotoID>) {
|
||||
self.photo_id = p.map(|p| p.0);
|
||||
}
|
||||
|
||||
pub fn photo_id(&self) -> Option<PhotoID> {
|
||||
self.photo_id.map(PhotoID)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Insertable)]
|
||||
#[diesel(table_name = couples)]
|
||||
pub struct NewCouple {
|
||||
pub family_id: i32,
|
||||
pub time_create: i64,
|
||||
pub time_update: i64,
|
||||
}
|
||||
|
Reference in New Issue
Block a user