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

@ -12,7 +12,7 @@ use actix_web::{web, HttpResponse};
serde_with::with_prefix!(prefix_birth "birth_");
serde_with::with_prefix!(prefix_death "death_");
#[derive(serde::Deserialize)]
#[derive(serde::Deserialize, Clone)]
pub struct RequestDate {
pub year: Option<i16>,
pub month: Option<i16>,
@ -29,7 +29,7 @@ impl RequestDate {
}
}
#[derive(serde::Deserialize)]
#[derive(serde::Deserialize, Clone)]
pub struct MemberRequest {
first_name: Option<String>,
last_name: Option<String>,
@ -41,8 +41,8 @@ pub struct MemberRequest {
postal_code: Option<String>,
country: Option<String>,
sex: Option<Sex>,
mother: Option<MemberID>,
father: Option<MemberID>,
pub mother: Option<MemberID>,
pub father: Option<MemberID>,
#[serde(flatten, with = "prefix_birth")]
birth: Option<RequestDate>,
#[serde(default)]
@ -104,7 +104,11 @@ fn check_opt_str_val(
}
impl MemberRequest {
pub async fn to_member(self, member: &mut Member) -> anyhow::Result<()> {
pub async fn to_member(
self,
member: &mut Member,
check_members_existence: bool, /* TODO: remove this field */
) -> anyhow::Result<()> {
let c = StaticConstraints::default();
check_opt_str_val(
&self.first_name,
@ -191,7 +195,9 @@ impl MemberRequest {
)?;
if let Some(mother) = self.mother {
if !members_service::exists(member.family_id(), mother).await? {
if check_members_existence
&& !members_service::exists(member.family_id(), mother).await?
{
return Err(MemberControllerErr::MotherNotExisting.into());
}
@ -201,7 +207,9 @@ impl MemberRequest {
}
if let Some(father) = self.father {
if !members_service::exists(member.family_id(), father).await? {
if check_members_existence
&& !members_service::exists(member.family_id(), father).await?
{
return Err(MemberControllerErr::FatherNotExisting.into());
}
}
@ -254,7 +262,7 @@ impl MemberAPI {
pub async fn create(f: FamilyInPath, req: web::Json<MemberRequest>) -> HttpResult {
let mut member = members_service::create(f.family_id()).await?;
if let Err(e) = req.0.to_member(&mut member).await {
if let Err(e) = req.0.to_member(&mut member, true).await {
log::error!("Failed to apply member information! {e}");
members_service::delete(&mut member).await?;
return Ok(HttpResponse::BadRequest().body(e.to_string()));
@ -284,7 +292,7 @@ pub async fn get_single(m: FamilyAndMemberInPath) -> HttpResult {
pub async fn update(m: FamilyAndMemberInPath, req: web::Json<MemberRequest>) -> HttpResult {
let mut member = m.to_member();
if let Err(e) = req.0.to_member(&mut member).await {
if let Err(e) = req.0.to_member(&mut member, true).await {
log::error!("Failed to parse member information {e}!");
return Ok(HttpResponse::BadRequest().body(e.to_string()));
}