2022-01-27 20:56:00 +01:00
|
|
|
import type { UpEntry } from "upend";
|
|
|
|
import type { ListingResult } from "upend/types";
|
2022-02-06 13:26:35 +01:00
|
|
|
import { query as queryFn, queryOnce } from "../lib/entity";
|
2022-01-27 20:56:00 +01:00
|
|
|
|
|
|
|
export function baseSearch(query: string) {
|
2022-02-04 23:04:38 +01:00
|
|
|
return queryFn(
|
2022-02-07 22:01:10 +01:00
|
|
|
`(or (matches (contains "${query}") ? ?) (matches ? (contains "${query}") ?) (matches ? ? (contains "${query}")))`
|
2022-02-04 23:04:38 +01:00
|
|
|
);
|
2022-01-27 20:56:00 +01:00
|
|
|
}
|
|
|
|
|
2022-01-27 21:28:42 +01:00
|
|
|
export function baseSearchOnce(query: string) {
|
2022-02-04 23:04:38 +01:00
|
|
|
return queryOnce(
|
2022-02-07 22:01:10 +01:00
|
|
|
`(or (matches (contains "${query}") ? ?) (matches ? (contains "${query}") ?) (matches ? ? (contains "${query}")))`
|
2022-02-04 23:04:38 +01:00
|
|
|
);
|
2022-01-27 21:28:42 +01:00
|
|
|
}
|
|
|
|
|
2022-01-27 21:26:47 +01:00
|
|
|
export async function getObjects(
|
2022-01-30 16:29:04 +01:00
|
|
|
entries: UpEntry[],
|
|
|
|
limit = 50
|
2022-01-27 21:26:47 +01:00
|
|
|
): Promise<[string, string][]> {
|
2022-02-06 13:26:35 +01:00
|
|
|
return entries
|
2022-01-30 16:29:04 +01:00
|
|
|
.slice(0, limit)
|
2022-01-27 20:56:00 +01:00
|
|
|
.filter((e) => e.attribute == "LBL")
|
2022-01-27 21:26:47 +01:00
|
|
|
.map((e) => [e.entity, String(e.value.c)] as [string, string]);
|
2022-01-27 20:56:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function createLabelled(label: string) {
|
2022-02-06 23:38:33 +01:00
|
|
|
const response = await fetch(`api/obj`, {
|
2022-01-27 20:56:00 +01:00
|
|
|
method: "PUT",
|
|
|
|
headers: { "Content-Type": "application/json" },
|
|
|
|
body: JSON.stringify({
|
|
|
|
attribute: "LBL",
|
|
|
|
value: {
|
2022-01-28 20:51:34 +01:00
|
|
|
t: "String",
|
2022-01-27 20:56:00 +01:00
|
|
|
c: label,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error(`Failed to create object: ${await response.text()}`);
|
|
|
|
}
|
|
|
|
const result = (await response.json()) as ListingResult;
|
|
|
|
const address = Object.values(result)[0].entity;
|
|
|
|
return address;
|
|
|
|
}
|