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}")]