allow insertion of invariants via api

feat/vaults
Tomáš Mládek 2021-06-20 14:07:15 +02:00
parent 9c0f38364b
commit 682eca3e5a
2 changed files with 39 additions and 4 deletions

View File

@ -71,6 +71,13 @@ mod macros {
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InEntry {
pub entity: Option<Address>,
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<InEntry> for Entry {
type Error = anyhow::Error;
fn try_from(in_entry: InEntry) -> Result<Self, Self::Error> {
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<Address> {
let mut entity = Cursor::new(vec![0u8; 0]);

View File

@ -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::<Entry>(&body).map_err(ErrorBadRequest)?;
let in_entry = serde_json::from_slice::<InEntry>(&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::<HashMap<String, Entry>>(),
))
}
#[delete("/api/obj/{address_str}")]