return attribute labels in /all/attributes endpoint

feat/vaults
Tomáš Mládek 2022-03-02 22:39:05 +01:00
parent b6e5368a6c
commit 376d929232
No known key found for this signature in database
GPG Key ID: 65E225C8B3E2ED8A
4 changed files with 36 additions and 2 deletions

View File

@ -565,9 +565,36 @@ pub async fn delete_object(
#[get("/api/all/attributes")]
pub async fn get_all_attributes(state: web::Data<State>) -> Result<HttpResponse, Error> {
let connection = state.upend.connection().map_err(ErrorInternalServerError)?;
let result = web::block(move || connection.get_all_attributes())
let attributes = web::block(move || connection.get_all_attributes())
.await
.map_err(ErrorInternalServerError)?;
let connection = state.upend.connection().map_err(ErrorInternalServerError)?;
let result: serde_json::Value = attributes
.into_iter()
.map(|attribute| {
json!({
"name": attribute,
"labels": connection
.retrieve_object(&Address::Attribute(attribute))
.unwrap_or_else(|_| vec![])
.into_iter()
.filter_map(|e| {
if e.attribute == LABEL_ATTR {
if let EntryValue::String(label) = e.value {
Some(label)
} else {
None
}
} else {
None
}
})
.collect::<Vec<String>>(),
})
})
.collect();
Ok(HttpResponse::Ok().json(result))
}

View File

@ -53,6 +53,11 @@ export type PutResult = [string | undefined, string];
// export type OrderedListing = [Address, IEntry][];
export type AttributeListingResult = Array<{
name: string;
labels: string[];
}>;
export interface IFile {
hash: string;
path: string;

View File

@ -56,6 +56,7 @@
case "attribute": {
const allAttributes = await fetchAllAttributes();
options = allAttributes
.map((attr) => attr.name)
.filter((attr) => attr.toLowerCase().includes(query.toLowerCase()))
.map((attribute) => {
return {

View File

@ -9,6 +9,7 @@ import type {
PutResult,
VaultInfo,
StoreInfo,
AttributeListingResult,
} from "upend/types";
import type { EntityListing } from "./entity";
@ -117,7 +118,7 @@ export async function fetchJobs(): Promise<IJob[]> {
return await response.json();
}
export async function fetchAllAttributes(): Promise<string[]> {
export async function fetchAllAttributes(): Promise<AttributeListingResult> {
const response = await fetch("api/all/attributes");
return await response.json();
}