From 6bb1c8f66b45e1774375ea3a56c1defae4aeba63 Mon Sep 17 00:00:00 2001 From: Patrick <50352812+Mueller-Patrick@users.noreply.github.com> Date: Sat, 29 May 2021 13:43:50 +0200 Subject: [PATCH] BETTERZON-123: Adding service functions for categories API (#69) --- Frontend/src/app/models/category.ts | 4 ++ Frontend/src/app/services/api.service.ts | 49 ++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 Frontend/src/app/models/category.ts diff --git a/Frontend/src/app/models/category.ts b/Frontend/src/app/models/category.ts new file mode 100644 index 0000000..a909167 --- /dev/null +++ b/Frontend/src/app/models/category.ts @@ -0,0 +1,4 @@ +export interface Category { + category_id: number; + name: string; +} diff --git a/Frontend/src/app/services/api.service.ts b/Frontend/src/app/services/api.service.ts index 77e3424..cdc0b94 100644 --- a/Frontend/src/app/services/api.service.ts +++ b/Frontend/src/app/services/api.service.ts @@ -8,6 +8,7 @@ import {Vendor} from '../models/vendor'; import {PriceAlarm} from '../models/pricealarm'; import {FavoriteShop} from '../models/favoriteshop'; import {ContactPerson} from '../models/contactperson'; +import {Category} from '../models/category'; @Injectable({ providedIn: 'root' @@ -479,4 +480,52 @@ export class ApiService { process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`); } } + + + /* ______ __ _ + / ____/___ _/ /____ ____ _____ _____(_)__ _____ + / / / __ `/ __/ _ \/ __ `/ __ \/ ___/ / _ \/ ___/ + / /___/ /_/ / /_/ __/ /_/ / /_/ / / / / __(__ ) + \____/\__,_/\__/\___/\__, /\____/_/ /_/\___/____/ + /____/ + */ + + /** + * Gets the specified category from the API + * @param id The id of the category to get + * @return Observable An observable containing a single product + */ + getCategoryById(id: number): Observable { + try { + return this.http.get((this.apiUrl + '/categories/' + id)); + } catch (exception) { + process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`); + } + } + + + /** + * Gets a list of categories that match the given search term + * @param query The search term to match + * @return Observable An observable list of categories + */ + getCategoryByQuery(query: string): Observable { + try { + return this.http.get((this.apiUrl + '/categories/search/' + query)); + } catch (exception) { + process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`); + } + } + + /** + * Gets a list of all categories + * @return Observable An observable list of categories + */ + getCategories(): Observable { + try { + return this.http.get((this.apiUrl + '/categories')); + } catch (exception) { + process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`); + } + } }