From 682eca3e5a5fbbc4acd3290896d20631df490294 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Ml=C3=A1dek?= Date: Sun, 20 Jun 2021 14:07:15 +0200 Subject: [PATCH] allow insertion of invariants via api --- src/database.rs | 25 +++++++++++++++++++++++++ src/routes.rs | 18 ++++++++++++++---- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/database.rs b/src/database.rs index 1213639..decdb00 100644 --- a/src/database.rs +++ b/src/database.rs @@ -71,6 +71,13 @@ mod macros { } } +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct InEntry { + pub entity: Option
, + pub attribute: String, + pub value: EntryValue, +} + #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Entry { pub entity: Address, @@ -129,6 +136,24 @@ impl TryFrom<&InvariantEntry> for Entry { } } +impl TryFrom for Entry { + type Error = anyhow::Error; + + fn try_from(in_entry: InEntry) -> Result { + match in_entry.entity { + Some(entity) => Ok(Entry { + entity, + attribute: in_entry.attribute, + value: in_entry.value, + }), + None => Ok(Entry::try_from(&InvariantEntry { + attribute: in_entry.attribute, + value: in_entry.value, + })?), + } + } +} + impl InvariantEntry { pub fn entity(&self) -> Result
{ let mut entity = Cursor::new(vec![0u8; 0]); diff --git a/src/routes.rs b/src/routes.rs index fbf9519..49a2245 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -1,7 +1,7 @@ use crate::addressing::Address; use crate::database::{ get_latest_files, insert_entry, query, remove_object, retrieve_file, retrieve_object, - Addressable, DbPool, Entry, Query, + Addressable, DbPool, Entry, InEntry, Query, }; use crate::filesystem::{list_directory, UPath}; use crate::hash::{decode, encode}; @@ -132,11 +132,21 @@ pub async fn put_object( body.extend_from_slice(&chunk); } - let entry = serde_json::from_slice::(&body).map_err(ErrorBadRequest)?; + let in_entry = serde_json::from_slice::(&body).map_err(ErrorBadRequest)?; + let entry = Entry::try_from(in_entry).map_err(ErrorInternalServerError)?; - let result_address = insert_entry(&connection, entry).map_err(ErrorInternalServerError)?; + let result_address = + insert_entry(&connection, entry.clone()).map_err(ErrorInternalServerError)?; - Ok(HttpResponse::Ok().json(result_address)) + Ok(HttpResponse::Ok().json( + [( + encode(result_address.encode().map_err(ErrorInternalServerError)?), + entry, + )] + .iter() + .cloned() + .collect::>(), + )) } #[delete("/api/obj/{address_str}")]