diff --git a/src/routes.rs b/src/routes.rs index 2e5d120..a6c653b 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -324,12 +324,18 @@ pub async fn get_object( }))) } +#[derive(Debug, Clone, Deserialize)] +pub struct InEntry { + pub entity: Option
, + pub attribute: String, + pub value: EntryValue, +} + #[derive(Debug, Clone, Deserialize)] #[serde(untagged)] -pub enum InEntry { - Entry(Entry), +pub enum PutInput { + Entry(InEntry), EntryList(Vec), - Invariant(InvariantEntry), Address { entity: InAddress }, } @@ -367,7 +373,7 @@ impl TryInto
for InAddress { pub async fn put_object( req: HttpRequest, state: web::Data, - payload: Either, Multipart>, + payload: Either, Multipart>, ) -> Result { check_auth(&req, &state)?; @@ -379,15 +385,40 @@ pub async fn put_object( debug!("PUTting {in_entry:?}"); match in_entry { - InEntry::Entry(entry) => Ok(web::block::<_, _, anyhow::Error>(move || { - Ok(( - Some(connection.insert_entry(entry.clone())?), - Some(entry.entity), - )) - }) - .await - .map_err(ErrorInternalServerError)?), - InEntry::EntryList(entries) => { + PutInput::Entry(in_entry) => { + if let Some(entity) = in_entry.entity { + let entry = Entry { + entity, + attribute: in_entry.attribute, + value: in_entry.value, + }; + + Ok(web::block::<_, _, anyhow::Error>(move || { + Ok(( + Some(connection.insert_entry(entry.clone())?), + Some(entry.entity), + )) + }) + .await + .map_err(ErrorInternalServerError)?) + } else { + let invariant = Entry::try_from(&InvariantEntry { + attribute: in_entry.attribute, + value: in_entry.value, + }) + .map_err(ErrorInternalServerError)?; + + Ok(web::block::<_, _, anyhow::Error>(move || { + Ok(( + Some(connection.insert_entry(invariant.clone())?), + Some(invariant.entity), + )) + }) + .await + .map_err(ErrorInternalServerError)?) + } + } + PutInput::EntryList(entries) => { web::block(move || { connection.transaction::<_, anyhow::Error, _>(|| { for entry in entries { @@ -400,23 +431,7 @@ pub async fn put_object( .map_err(ErrorInternalServerError)?; Ok((None, None)) } - InEntry::Invariant(in_entry) => { - let invariant = Entry::try_from(&InvariantEntry { - attribute: in_entry.attribute, - value: in_entry.value, - }) - .map_err(ErrorInternalServerError)?; - - Ok(web::block::<_, _, anyhow::Error>(move || { - Ok(( - Some(connection.insert_entry(invariant.clone())?), - Some(invariant.entity), - )) - }) - .await - .map_err(ErrorInternalServerError)?) - } - InEntry::Address { entity: in_address } => { + PutInput::Address { entity: in_address } => { let address = in_address.try_into().map_err(ErrorBadRequest)?; let label_entry = match &address {