From 3add883c259690acb9e19fa2b6c4b88a8497feac Mon Sep 17 00:00:00 2001 From: Jens Pelzetter Date: Thu, 23 Jul 2020 20:45:55 +0200 Subject: [PATCH] getCategories implemented for API client Former-commit-id: b58517802dec36b73e665c53ae4a1a26031a3f80 --- .../src/main/typescript/ccm-core-apiclient.ts | 102 ++++++++++++------ .../src/main/typescript/constants.ts | 5 + .../typescript/entities/categorization.ts | 41 ++++++- 3 files changed, 112 insertions(+), 36 deletions(-) create mode 100644 ccm-core-apiclient/src/main/typescript/constants.ts diff --git a/ccm-core-apiclient/src/main/typescript/ccm-core-apiclient.ts b/ccm-core-apiclient/src/main/typescript/ccm-core-apiclient.ts index ae2d4759c..909c08fc4 100644 --- a/ccm-core-apiclient/src/main/typescript/ccm-core-apiclient.ts +++ b/ccm-core-apiclient/src/main/typescript/ccm-core-apiclient.ts @@ -1,46 +1,80 @@ -import { RequestInitProvider} from "@libreccm/ccm-apiclient-commons"; +import { + ApiResponse, + LibreCcmApiClient, +} from "@libreccm/ccm-apiclient-commons"; -import { Category } from "./entities/categorization"; - -// interface RequestInitProvider { -// buildRequestInit(): RequestInit; -// } - -// export class CcmCoreAdminApiClient { -// libreCcmHost: string; -// jwt: string; - -// constructor(libreCcmHost: string, jwt: string) { -// this.libreCcmHost = libreCcmHost; -// this.jwt = jwt; -// } - -// jwtRequired(): boolean { -// const documentUrl = new URL(document.documentURI); -// return documentUrl.host != this.libreCcmHost; -// } -// } +import { Category, buildCategoryFromRecord } from "./entities/categorization"; +import * as Constants from "./constants"; export class CategorizationApiClient { - #requestInitProvider: RequestInitProvider; + #apiClient: LibreCcmApiClient; - constructor(requestInitProvider: RequestInitProvider) { - this.#requestInitProvider = requestInitProvider; + readonly #CATEGORIES_API_PREFIX = `${Constants.ADMIN_API_PREFIX}/categories` + + constructor(apiClient: LibreCcmApiClient) { + this.#apiClient = apiClient; } - async getCategory(domain: string, path: string): Promise { - const fetchInit = this.#requestInitProvider.buildRequestInit(); - fetchInit.method = "GET"; + + + async getCategoryByDomainAndPath(domain: string, path: string): Promise { try { - const result = await fetch("localhost:8080", fetchInit); - if (result.ok) { - const category: Category = await result.json(); - return category; + const response: ApiResponse = await this.#apiClient.get( + `${this.#CATEGORIES_API_PREFIX}/${domain}/${path}` + ); + + if (response.ok) { + return buildCategoryFromRecord( + await response.json() + ); } else { - throw new Error(`Failed to get category ${path} of domain ${domain}: ${result.status} ${result.statusText}`); + throw `Failed to get category ${path} of domain ${domain}: + ${response.status} ${response.statusText}`; } - } catch(err) { - throw new Error(`Failed to get category ${path} of domain ${domain}: ${err}`); + } catch (err) { + throw `Failed to get category ${path} of domain ${domain}: ${err}`; } } + + async getCategoryById(categoryId: number): Promise { + try { + const response: ApiResponse = await this.#apiClient.get( + `${this.#CATEGORIES_API_PREFIX}/categories/ID-${categoryId}` + ); + + if (response.ok) { + return buildCategoryFromRecord( + await response.json() + ); + } else { + throw `Failed to get category with ID ${categoryId}: + ${response.status} ${response.statusText}`; + } + } catch(err) { + throw `Failed to get category with Id ${categoryId}: ${err}`; + } + } + + async getCategoryByUuid(uuid: string): Promise { + try { + const response: ApiResponse = await this.#apiClient.get( + `${this.#CATEGORIES_API_PREFIX}/categories/UUID-${uuid}` + ); + + if (response.ok) { + return buildCategoryFromRecord( + await response.json() + ); + } else { + throw `Failed to get category with UUID ${uuid}: + ${response.status} ${response.statusText}`; + } + } catch(err) { + throw `Failed to get category with UUID ${uuid}: ${err}`; + } + } + + + + } diff --git a/ccm-core-apiclient/src/main/typescript/constants.ts b/ccm-core-apiclient/src/main/typescript/constants.ts new file mode 100644 index 000000000..4fc192357 --- /dev/null +++ b/ccm-core-apiclient/src/main/typescript/constants.ts @@ -0,0 +1,5 @@ +const ADMIN_API_PREFIX = "/api/admin"; + +export { + ADMIN_API_PREFIX +} \ No newline at end of file diff --git a/ccm-core-apiclient/src/main/typescript/entities/categorization.ts b/ccm-core-apiclient/src/main/typescript/entities/categorization.ts index 516224ace..8660dcbaa 100644 --- a/ccm-core-apiclient/src/main/typescript/entities/categorization.ts +++ b/ccm-core-apiclient/src/main/typescript/entities/categorization.ts @@ -1,4 +1,7 @@ -import { LocalizedString } from "@libreccm/ccm-apiclient-commons"; +import { + LocalizedString, + assertProperties, +} from "@libreccm/ccm-apiclient-commons"; export interface AssociatedCategory { name: string; @@ -15,6 +18,40 @@ export interface Category { description: LocalizedString; enabled: boolean; abstractCategory: boolean; - parentCategory: AssociatedCategory; + parentCategory: AssociatedCategory | null; categoryOrder: number; } + +export function buildCategoryFromRecord( + record: Record +): Category { + assertProperties(record, [ + "categoryId", + "uuid", + "uniqueId", + "name", + "title", + "description", + "enabled", + "abstractCategory", + "parentCategory", + "categoryOrder", + ]); + + const parentCategory: AssociatedCategory | null = record.parentCategory + ? (record.parentCategory as AssociatedCategory) + : null; + + return { + categoryId: record.categoryId as number, + uuid: record.uuid as string, + uniqueId: record.uniqueId as string, + name: record.name as string, + title: record.title as LocalizedString, + description: record.description as LocalizedString, + enabled: record.enable as boolean, + abstractCategory: record.abstractCategory as boolean, + parentCategory, + categoryOrder: record.categoryOrder as number, + }; +}