49 lines
1.4 KiB
TypeScript
49 lines
1.4 KiB
TypeScript
import { API_ENDPOINTS } from "@/api/config";
|
|
import { requestJson } from "@/api/http";
|
|
|
|
export type Entity = {
|
|
id: string;
|
|
name: string;
|
|
slug?: string | null;
|
|
description?: string | null;
|
|
type_id?: string | null;
|
|
status?: number | null;
|
|
geometry_count?: number;
|
|
created_at?: string;
|
|
updated_at?: string;
|
|
};
|
|
|
|
export type CreateEntityPayload = {
|
|
name: string;
|
|
slug?: string | null;
|
|
description?: string | null;
|
|
type_id?: string | null;
|
|
status?: number | null;
|
|
};
|
|
|
|
export async function fetchEntities(query?: { q?: string }): Promise<Entity[]> {
|
|
const params = new URLSearchParams();
|
|
if (query?.q) {
|
|
params.set("q", query.q);
|
|
}
|
|
const suffix = params.toString();
|
|
const url = suffix ? `${API_ENDPOINTS.entities}?${suffix}` : API_ENDPOINTS.entities;
|
|
return requestJson<Entity[]>(url);
|
|
}
|
|
|
|
export async function createEntity(payload: CreateEntityPayload): Promise<Entity> {
|
|
return requestJson<Entity>(API_ENDPOINTS.entities, {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(payload),
|
|
});
|
|
}
|
|
|
|
export async function updateEntity(id: string, payload: CreateEntityPayload): Promise<Entity> {
|
|
return requestJson<Entity>(`${API_ENDPOINTS.entities}/${id}`, {
|
|
method: "PUT",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(payload),
|
|
});
|
|
}
|