diff --git a/ccm-core-apiclient/src/main/typescript/clients/categorization-api.ts b/ccm-core-apiclient/src/main/typescript/clients/categorization-api.ts index 48642434d..dcd561934 100644 --- a/ccm-core-apiclient/src/main/typescript/clients/categorization-api.ts +++ b/ccm-core-apiclient/src/main/typescript/clients/categorization-api.ts @@ -395,7 +395,7 @@ export class CategorizationApiClient { const response: ApiResponse = await this.#apiClient.get( `${ this.#CATEGORIES_API_PREFIX - }/$domain/${path}/objects?limit=${limit}&offset=${offset}` + }/$domain/${path}/@objects?limit=${limit}&offset=${offset}` ); if (response.ok) { @@ -412,8 +412,8 @@ export class CategorizationApiClient { list: objects, count: result.count as number, limit: result.limit as number, - offset: result.offset as number - } + offset: result.offset as number, + }; } else { throw `Failed to get objects in category ${path} of domain ${domain}: ${response.status} ${response.statusText}`; @@ -423,4 +423,223 @@ export class CategorizationApiClient { domain ${domain}: ${err}`; } } + + async getObjectsInCategoryWithId( + categoryId: number, + limit = 20, + offset = 0 + ): Promise> { + try { + const response: ApiResponse = await this.#apiClient.get( + `${ + this.#CATEGORIES_API_PREFIX + }/ID-${categoryId}/@objects?limit=${limit}&offset=${offset}` + ); + + if (response.ok) { + const result: Record = await response.json(); + const list: Record[] = result.list as Record< + string, + unknown + >[]; + const objects: Categorization[] = list.map((record) => + buildCategorizationFromRecord(record) + ); + + return { + list: objects, + count: result.count as number, + limit: result.limit as number, + offset: result.offset as number, + }; + } else { + throw `Failed to get objects in category with + ID ${categoryId}: ${response.status} ${response.statusText}`; + } + } catch (err) { + throw `Failed to get objects in category with + Id ${categoryId}: ${err}`; + } + } + + async getObjectsInCategoryWithUuid( + uuid: string, + limit = 20, + offset = 0 + ): Promise> { + try { + const response: ApiResponse = await this.#apiClient.get( + `${ + this.#CATEGORIES_API_PREFIX + }/UUID-${uuid}/@objects?limit=${limit}&offset=${offset}` + ); + + if (response.ok) { + const result: Record = await response.json(); + const list: Record[] = result.list as Record< + string, + unknown + >[]; + const objects: Categorization[] = list.map((record) => + buildCategorizationFromRecord(record) + ); + + return { + list: objects, + count: result.count as number, + limit: result.limit as number, + offset: result.offset as number, + }; + } else { + throw `Failed to get objects in category with + UUID ${uuid}: ${response.status} ${response.statusText}`; + } + } catch (err) { + throw `Failed to get objects in category with + UUId ${uuid}: ${err}`; + } + } + + async addObjectToCategoryOfDomain( + domain: string, + path: string, + categorization: Categorization + ): Promise { + try { + const response: ApiResponse = await this.#apiClient.post( + `${this.#CATEGORIES_API_PREFIX}/${domain}/${path}`, + JSON.stringify(categorization) + ); + + if (response.ok) { + const result: string = await response.text(); + return result; + } else { + throw `Failed to add object to category ${path} of + domain ${domain}: ${response.status} ${response.statusText}`; + } + } catch (err) { + throw `Failed to add object to category ${path} of + domain ${domain}: ${err}`; + } + } + + async addObjectToCategoryWithId( + categoryId: number, + categorization: Categorization + ): Promise { + try { + const response: ApiResponse = await this.#apiClient.post( + `${this.#CATEGORIES_API_PREFIX}/ID-${categoryId}`, + JSON.stringify(categorization) + ); + + if (response.ok) { + const result: string = await response.text(); + return result; + } else { + throw `Failed to add object to category with + ID ${categoryId}: ${response.status} ${response.statusText}`; + } + } catch (err) { + throw `Failed to add object to category with + ID ${categoryId}: ${err}`; + } + } + + async addObjectToCategoryWithUuid( + uuid: string, + categorization: Categorization + ): Promise { + try { + const response: ApiResponse = await this.#apiClient.post( + `${this.#CATEGORIES_API_PREFIX}/UUID-${uuid}`, + JSON.stringify(categorization) + ); + + if (response.ok) { + const result: string = await response.text(); + return result; + } else { + throw `Failed to add object to category with + UUID ${uuid}: ${response.status} ${response.statusText}`; + } + } catch (err) { + throw `Failed to add object to category with + UUID ${uuid}: ${err}`; + } + } + + async removeObjectFromCategoryOfDomain( + domain: string, + path: string, + objectIdentifier: string + ): Promise { + try { + const response: ApiResponse = await this.#apiClient.delete( + `${ + this.#CATEGORIES_API_PREFIX + }/${domain}/${path}/@objects/${objectIdentifier}` + ); + + if (response.ok) { + return; + } else { + throw `Failed to remove object ${objectIdentifier} from + category ${path} of domain ${domain}: + ${response.status} ${response.statusText}.`; + } + } catch (err) { + throw `Failed to remove object ${objectIdentifier} from + category ${path} of domain ${domain}: ${err}`; + } + } + + async removeObjectFromCategoryWithId( + categoryId: number, + objectIdentifier: string + ): Promise { + try { + const response: ApiResponse = await this.#apiClient.delete( + `${ + this.#CATEGORIES_API_PREFIX + }/ID-${categoryId}/@objects/${objectIdentifier}` + ); + + if (response.ok) { + return; + } else { + throw `Failed to remove object ${objectIdentifier} from + category with ID ${categoryId}: + ${response.status} ${response.statusText}.`; + } + } catch (err) { + throw `Failed to remove object ${objectIdentifier} from + category with ID ${categoryId}: ${err}`; + } + } + + async removeObjectFromCategoryWithUuid( + uuid: string, + objectIdentifier: string + ): Promise { + try { + const response: ApiResponse = await this.#apiClient.delete( + `${ + this.#CATEGORIES_API_PREFIX + }/UUID-${uuid}/@objects/${objectIdentifier}` + ); + + if (response.ok) { + return; + } else { + throw `Failed to remove object ${objectIdentifier} from + category with UUID ${uuid}: + ${response.status} ${response.statusText}.`; + } + } catch (err) { + throw `Failed to remove object ${objectIdentifier} from + category with UUID ${uuid}: ${err}`; + } + } }