From cb22756a4732cc91e33479d3efad008892a7ee5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Ml=C3=A1dek?= Date: Wed, 2 Feb 2022 00:45:05 +0100 Subject: [PATCH] separate endpoint for attr overwrites, implement AttributeUpdate --- src/main.rs | 1 + src/routes.rs | 32 ++++++++++++++++++++++++++++- webui/src/components/Inspect.svelte | 10 ++------- webui/src/types/base.ts | 3 +-- 4 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5896bd4..03bfd9f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -156,6 +156,7 @@ fn main() -> Result<()> { .service(routes::get_query) .service(routes::get_object) .service(routes::put_object) + .service(routes::put_object_attribute) .service(routes::delete_object) .service(routes::get_all_attributes) .service(routes::api_refresh) diff --git a/src/routes.rs b/src/routes.rs index 8909f78..dd4ae1a 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -1,5 +1,5 @@ use crate::addressing::{Address, Addressable}; -use crate::database::entry::{Entry, InEntry}; +use crate::database::entry::{Entry, EntryValue, InEntry}; use crate::database::hierarchies::{list_roots, resolve_path, UHierPath}; use crate::database::lang::Query; use crate::database::UpEndDatabase; @@ -273,6 +273,36 @@ pub async fn put_object( )) } +#[put("/api/obj/{address}/{attribute}")] +pub async fn put_object_attribute( + state: web::Data, + web::Path((address, attribute)): web::Path<(Address, String)>, + value: web::Json, +) -> Result { + let connection = state.upend.connection().map_err(ErrorInternalServerError)?; + + let new_address = connection + .transaction::<_, anyhow::Error, _>(|| { + let existing_attr_entries = + connection.query(format!("(matches \"{address}\" \"{attribute}\" ?)").parse()?)?; + + for eae in existing_attr_entries { + let _ = connection.remove_object(eae.address()?)?; + } + + let new_attr_entry = Entry { + entity: address, + attribute, + value: value.into_inner(), + }; + + connection.insert_entry(new_attr_entry) + }) + .map_err(ErrorInternalServerError)?; + + Ok(HttpResponse::Ok().json(new_address)) +} + #[delete("/api/obj/{address_str}")] pub async fn delete_object( state: web::Data, diff --git a/webui/src/components/Inspect.svelte b/webui/src/components/Inspect.svelte index a924877..00bd2bd 100644 --- a/webui/src/components/Inspect.svelte +++ b/webui/src/components/Inspect.svelte @@ -123,16 +123,10 @@ await fetch(`/api/obj/${change.address}`, { method: "DELETE" }); break; case "update": - // TODO - await fetch(`/api/obj/${change.address}`, { method: "DELETE" }); - await fetch(`/api/obj`, { + await fetch(`/api/obj/${address}/${change.attribute}`, { method: "PUT", headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ - entity: address, - attribute: change.attribute, - value: change.value, - }), + body: JSON.stringify(change.value), }); break; default: diff --git a/webui/src/types/base.ts b/webui/src/types/base.ts index 8f33e79..4881e56 100644 --- a/webui/src/types/base.ts +++ b/webui/src/types/base.ts @@ -13,8 +13,7 @@ export interface AttributeCreate { export interface AttributeUpdate { type: "update"; - address: string; - attribute: string; // TODO: remove + attribute: string; value: IValue; }