Can import basic data

This commit is contained in:
2023-08-18 11:05:32 +02:00
parent 3b5efb46cd
commit 57d919ee63
5 changed files with 182 additions and 22 deletions

View File

@ -25,10 +25,10 @@ enum CoupleControllerErr {
MalformedDateOfDivorce,
}
#[derive(serde::Deserialize)]
#[derive(serde::Deserialize, Clone)]
pub struct CoupleRequest {
wife: Option<MemberID>,
husband: Option<MemberID>,
pub wife: Option<MemberID>,
pub husband: Option<MemberID>,
state: Option<CoupleState>,
#[serde(flatten, with = "prefix_wedding")]
wedding: Option<RequestDate>,
@ -37,9 +37,14 @@ pub struct CoupleRequest {
}
impl CoupleRequest {
pub async fn to_couple(self, couple: &mut Couple) -> anyhow::Result<()> {
pub async fn to_couple(
self,
couple: &mut Couple,
check_members_existence: bool,
) -> anyhow::Result<()> {
if let Some(wife) = self.wife {
if !members_service::exists(couple.family_id(), wife).await? {
if check_members_existence && !members_service::exists(couple.family_id(), wife).await?
{
return Err(CoupleControllerErr::WifeNotExisting.into());
}
@ -49,7 +54,9 @@ impl CoupleRequest {
}
if let Some(husband) = self.husband {
if !members_service::exists(couple.family_id(), husband).await? {
if check_members_existence
&& !members_service::exists(couple.family_id(), husband).await?
{
return Err(CoupleControllerErr::HusbandNotExisting.into());
}
}
@ -102,7 +109,7 @@ impl CoupleAPI {
pub async fn create(m: FamilyInPath, req: web::Json<CoupleRequest>) -> HttpResult {
let mut couple = couples_service::create(m.family_id()).await?;
if let Err(e) = req.0.to_couple(&mut couple).await {
if let Err(e) = req.0.to_couple(&mut couple, true).await {
log::error!("Failed to apply couple information! {e}");
couples_service::delete(&mut couple).await?;
return Ok(HttpResponse::BadRequest().body(e.to_string()));
@ -137,7 +144,7 @@ pub async fn get_single(m: FamilyAndCoupleInPath) -> HttpResult {
pub async fn update(m: FamilyAndCoupleInPath, req: web::Json<CoupleRequest>) -> HttpResult {
let mut couple = m.to_couple();
if let Err(e) = req.0.to_couple(&mut couple).await {
if let Err(e) = req.0.to_couple(&mut couple, true).await {
log::error!("Failed to parse couple information {e}!");
return Ok(HttpResponse::BadRequest().body(e.to_string()));
}