diff --git a/src/database/mod.rs b/src/database/mod.rs index 2d7447b..c378934 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -308,12 +308,12 @@ impl UpEndConnection { } } - pub fn retrieve_object(&self, object_address: Address) -> Result> { + pub fn retrieve_object(&self, object_address: &Address) -> Result> { use crate::database::inner::schema::data::dsl::*; let primary = data .filter(entity.eq(object_address.encode()?)) - .or_filter(value_str.eq(EntryValue::Address(object_address).to_string()?)) + .or_filter(value_str.eq(EntryValue::Address(object_address.clone()).to_string()?)) .load::(&self.conn)?; let entries = primary diff --git a/src/routes.rs b/src/routes.rs index 6fc4373..f280fb0 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -207,18 +207,30 @@ impl EntriesAsHash for Vec { #[get("/api/obj/{address_str}")] pub async fn get_object( state: web::Data, - address_str: web::Path, + address: web::Path
, ) -> Result { let connection = state.upend.connection().map_err(ErrorInternalServerError)?; + let address = address.into_inner(); let result: Vec = connection - .retrieve_object( - Address::decode(&b58_decode(address_str.into_inner()).map_err(ErrorBadRequest)?) - .map_err(ErrorBadRequest)?, - ) + .retrieve_object(&address) .map_err(ErrorInternalServerError)?; debug!("{:?}", result); - Ok(HttpResponse::Ok().json(result.as_hash().map_err(ErrorInternalServerError)?)) + + let (entity_type, entity_content) = match address { + Address::Hash(_) => ("Hash", None), + Address::Uuid(_) => ("Uuid", None), + Address::Attribute(attribute) => ("Attribute", Some(attribute)), + Address::Url(url) => ("Url", Some(url)), + }; + + Ok(HttpResponse::Ok().json(json!({ + "entity": { + "t": entity_type, + "c": entity_content + }, + "entries": result.as_hash().map_err(ErrorInternalServerError)? + }))) } #[put("/api/obj")] @@ -417,7 +429,7 @@ pub async fn list_hier_roots(state: web::Data) -> Result>>>() .map_err(ErrorInternalServerError)? .concat(); diff --git a/webui/src/lib/entity.ts b/webui/src/lib/entity.ts index 295adda..9401f5e 100644 --- a/webui/src/lib/entity.ts +++ b/webui/src/lib/entity.ts @@ -5,23 +5,47 @@ import { UpListing, UpObject } from "upend"; import type { ListingResult } from "upend/types"; import { useSWR } from "../util/fetch"; +export type EntityInfo = + | { + t: "Hash" | "Uuid"; + } + | { + t: "Attribute" | "Url"; + c: string; + }; + +export interface EntityListing { + entity: EntityInfo; + entries: ListingResult; +} + const queryOnceLRU = new LRU(128); const inFlightRequests: { [key: string]: Promise } = {}; export function useEntity(address: string) { - const { data, error, revalidate } = useSWR( + const { data, error, revalidate } = useSWR( `api/obj/${address}` ); const entity: Readable = derived(data, ($listing) => { if ($listing) { - const listing = new UpListing($listing); + const listing = new UpListing($listing.entries); return listing.getObject(address); } }); + const entityInfo: Readable = derived( + data, + ($listing) => { + if ($listing) { + return $listing.entity; + } + } + ); + return { entity, + entityInfo, error, revalidate, };