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 909c08fc4..936647b9e 100644 --- a/ccm-core-apiclient/src/main/typescript/ccm-core-apiclient.ts +++ b/ccm-core-apiclient/src/main/typescript/ccm-core-apiclient.ts @@ -9,24 +9,23 @@ import * as Constants from "./constants"; export class CategorizationApiClient { #apiClient: LibreCcmApiClient; - readonly #CATEGORIES_API_PREFIX = `${Constants.ADMIN_API_PREFIX}/categories` + readonly #CATEGORIES_API_PREFIX = `${Constants.ADMIN_API_PREFIX}/categories`; constructor(apiClient: LibreCcmApiClient) { this.#apiClient = apiClient; } - - - async getCategoryByDomainAndPath(domain: string, path: string): Promise { + async getCategoryByDomainAndPath( + domain: string, + path: string + ): Promise { try { const response: ApiResponse = await this.#apiClient.get( `${this.#CATEGORIES_API_PREFIX}/${domain}/${path}` ); if (response.ok) { - return buildCategoryFromRecord( - await response.json() - ); + return buildCategoryFromRecord(await response.json()); } else { throw `Failed to get category ${path} of domain ${domain}: ${response.status} ${response.statusText}`; @@ -43,14 +42,12 @@ export class CategorizationApiClient { ); if (response.ok) { - return buildCategoryFromRecord( - await response.json() - ); + return buildCategoryFromRecord(await response.json()); } else { throw `Failed to get category with ID ${categoryId}: ${response.status} ${response.statusText}`; } - } catch(err) { + } catch (err) { throw `Failed to get category with Id ${categoryId}: ${err}`; } } @@ -62,19 +59,133 @@ export class CategorizationApiClient { ); if (response.ok) { - return buildCategoryFromRecord( - await response.json() - ); + return buildCategoryFromRecord(await response.json()); } else { throw `Failed to get category with UUID ${uuid}: ${response.status} ${response.statusText}`; } - } catch(err) { + } catch (err) { throw `Failed to get category with UUID ${uuid}: ${err}`; } } + async updateCategoryByDomainAndPath( + domain: string, + path: string, + category: Category + ): Promise { + try { + const response: ApiResponse = await this.#apiClient.put( + `${this.#CATEGORIES_API_PREFIX}/${domain}/${path}`, + JSON.stringify(category) + ); + if (response.ok) { + return; + } else { + throw `Failed to update category ${path} of domain ${domain}: + ${response.status} ${response.statusText}`; + } + } catch (err) { + throw `Failed to update category ${path} of domain ${domain}: ${err}`; + } + } + async updateCategoryById( + categoryId: number, + category: Category + ): Promise { + try { + const response: ApiResponse = await this.#apiClient.put( + `${this.#CATEGORIES_API_PREFIX}/ID-${categoryId}`, + JSON.stringify(category) + ); + if (response.ok) { + return; + } else { + throw `Failed to update category with ID ${categoryId}: + ${response.status} ${response.statusText}`; + } + } catch (err) { + throw `Failed to update category ${categoryId}: ${err}`; + } + } + + async updateCategoryByUuid( + uuid: string, + category: Category + ): Promise { + try { + const response: ApiResponse = await this.#apiClient.put( + `${this.#CATEGORIES_API_PREFIX}/UUID-${uuid}`, + JSON.stringify(category) + ); + + if (response.ok) { + return; + } else { + throw `Failed to update category with UUID ${uuid}: + ${response.status} ${response.statusText}`; + } + } catch (err) { + throw `Failed to update category with UUID ${uuid}: ${err}`; + } + } + + async deleteCategoryByDomainAndPath( + domain: string, + path: string + ): Promise { + try { + const response: ApiResponse = await this.#apiClient.delete( + `${this.#CATEGORIES_API_PREFIX}/${domain}/${path}` + ); + + if (response.ok) { + return; + } else { + throw `Failed to update category ${path} of domain ${domain}: + ${response.status} ${response.statusText}`; + } + } catch { + throw `Failed to delete category ${path} of domain ${domain}: ${err}`; + } + } + + async deleteCategoryById(categoryId: number): Promise { + try { + const response: ApiResponse = await this.#apiClient.delete( + `${this.#CATEGORIES_API_PREFIX}/ID-${categoryId}` + ); + + if (response.ok) { + return; + } else { + throw `Failed to delete category with ID ${categoryId}: + ${response.status} ${response.statusText}`; + } + } catch (err) { + throw `Failed to delete category ${categoryId}: ${err}`; + } + } + + async deleteCategoryByUuid( + uuid: string, + ): Promise { + try { + const response: ApiResponse = await this.#apiClient.delete( + `${this.#CATEGORIES_API_PREFIX}/UUID-${uuid}`, + ); + + if (response.ok) { + return; + } else { + throw `Failed to delete category with UUID ${uuid}: + ${response.status} ${response.statusText}`; + } + } catch (err) { + throw `Failed to delete category with UUID ${uuid}: ${err}`; + } + } }