updateCategory, deleteCategory

Jens Pelzetter 2020-07-24 17:36:46 +02:00
parent 3e33913689
commit af67ac1a97
1 changed files with 126 additions and 15 deletions

View File

@ -9,24 +9,23 @@ import * as Constants from "./constants";
export class CategorizationApiClient { export class CategorizationApiClient {
#apiClient: LibreCcmApiClient; #apiClient: LibreCcmApiClient;
readonly #CATEGORIES_API_PREFIX = `${Constants.ADMIN_API_PREFIX}/categories` readonly #CATEGORIES_API_PREFIX = `${Constants.ADMIN_API_PREFIX}/categories`;
constructor(apiClient: LibreCcmApiClient) { constructor(apiClient: LibreCcmApiClient) {
this.#apiClient = apiClient; this.#apiClient = apiClient;
} }
async getCategoryByDomainAndPath(
domain: string,
async getCategoryByDomainAndPath(domain: string, path: string): Promise<Category> { path: string
): Promise<Category> {
try { try {
const response: ApiResponse = await this.#apiClient.get( const response: ApiResponse = await this.#apiClient.get(
`${this.#CATEGORIES_API_PREFIX}/${domain}/${path}` `${this.#CATEGORIES_API_PREFIX}/${domain}/${path}`
); );
if (response.ok) { if (response.ok) {
return buildCategoryFromRecord( return buildCategoryFromRecord(await response.json());
await response.json()
);
} else { } else {
throw `Failed to get category ${path} of domain ${domain}: throw `Failed to get category ${path} of domain ${domain}:
${response.status} ${response.statusText}`; ${response.status} ${response.statusText}`;
@ -43,14 +42,12 @@ export class CategorizationApiClient {
); );
if (response.ok) { if (response.ok) {
return buildCategoryFromRecord( return buildCategoryFromRecord(await response.json());
await response.json()
);
} else { } else {
throw `Failed to get category with ID ${categoryId}: throw `Failed to get category with ID ${categoryId}:
${response.status} ${response.statusText}`; ${response.status} ${response.statusText}`;
} }
} catch(err) { } catch (err) {
throw `Failed to get category with Id ${categoryId}: ${err}`; throw `Failed to get category with Id ${categoryId}: ${err}`;
} }
} }
@ -62,19 +59,133 @@ export class CategorizationApiClient {
); );
if (response.ok) { if (response.ok) {
return buildCategoryFromRecord( return buildCategoryFromRecord(await response.json());
await response.json()
);
} else { } else {
throw `Failed to get category with UUID ${uuid}: throw `Failed to get category with UUID ${uuid}:
${response.status} ${response.statusText}`; ${response.status} ${response.statusText}`;
} }
} catch(err) { } catch (err) {
throw `Failed to get category with UUID ${uuid}: ${err}`; throw `Failed to get category with UUID ${uuid}: ${err}`;
} }
} }
async updateCategoryByDomainAndPath(
domain: string,
path: string,
category: Category
): Promise<void> {
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<void> {
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<void> {
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<void> {
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<void> {
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<void> {
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}`;
}
}
} }