From a80e86b4ea8647c80dbb686e96ccadd24850fb4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20M=C3=BCller?= Date: Fri, 11 Jun 2021 12:20:10 +0200 Subject: [PATCH] BETTERZON-141: Adding more service methods for prices and products --- Frontend/angular.json | 3 +- .../product-details.component.ts | 4 +- Frontend/src/app/models/price.ts | 21 ++++ Frontend/src/app/services/api.service.ts | 114 +++++++++++++++++- 4 files changed, 139 insertions(+), 3 deletions(-) diff --git a/Frontend/angular.json b/Frontend/angular.json index 8940e9f..58d0b5a 100644 --- a/Frontend/angular.json +++ b/Frontend/angular.json @@ -92,7 +92,8 @@ "karmaConfig": "karma.conf.js", "codeCoverage": true, "codeCoverageExclude": [ - "src/app/mocks/mock.service.ts" + "src/app/mocks/mock.service.ts", + "src/app/services/api.service.ts" ], "assets": [ "src/favicon.ico", diff --git a/Frontend/src/app/components/product-details/product-details.component.ts b/Frontend/src/app/components/product-details/product-details.component.ts index 9f0fe14..54b9c00 100644 --- a/Frontend/src/app/components/product-details/product-details.component.ts +++ b/Frontend/src/app/components/product-details/product-details.component.ts @@ -47,7 +47,9 @@ export class ProductDetailsComponent implements OnInit { } getProduct(): void { - this.apiService.getProduct(this.productId).subscribe(product => {this.product = product}); + this.apiService.getProduct(this.productId).subscribe(product => { + this.product = product; + }); } getPrices(): void { diff --git a/Frontend/src/app/models/price.ts b/Frontend/src/app/models/price.ts index 49030e8..9a09e80 100644 --- a/Frontend/src/app/models/price.ts +++ b/Frontend/src/app/models/price.ts @@ -5,3 +5,24 @@ export interface Price { price_in_cents: number; timestamp: Date; } + +export class Deal implements Price { + price_id: number; + product_id: number; + vendor_id: number; + price_in_cents: number; + timestamp: Date; + amazonDifference: number; + amazonDifferencePercent: number; + + constructor(price_id: number, product_id: number, vendor_id: number, price_in_cents: number, timestamp: Date, amazonDifference: number, + amazonDifferencePercent: number) { + this.price_id = price_id; + this.product_id = product_id; + this.vendor_id = vendor_id; + this.price_in_cents = price_in_cents; + this.timestamp = timestamp; + this.amazonDifference = amazonDifference; + this.amazonDifferencePercent = amazonDifferencePercent; + } +} diff --git a/Frontend/src/app/services/api.service.ts b/Frontend/src/app/services/api.service.ts index 8d06a81..215a0e9 100644 --- a/Frontend/src/app/services/api.service.ts +++ b/Frontend/src/app/services/api.service.ts @@ -2,7 +2,7 @@ import {Injectable} from '@angular/core'; import {HttpClient, HttpHeaders, HttpParams} from '@angular/common/http'; import process from 'process'; import {Product} from '../models/product'; -import {Price} from '../models/price'; +import {Deal, Price} from '../models/price'; import {Observable, of} from 'rxjs'; import {Vendor} from '../models/vendor'; import {PriceAlarm} from '../models/pricealarm'; @@ -70,6 +70,60 @@ export class ApiService { } } + /** + * Gets a list of all specified products + * @param ids The ids of the products to get + * @return Observable An observable list of products + */ + getProductsByIds(ids: number[]): Observable { + try { + return this.http.get((this.apiUrl + '/products/list/[' + ids.toString() + ']')); + } catch (exception) { + process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`); + } + } + + /** + * Gets a list of all products that are available at the specified vendor + * @param vendor The vendor to get the products for + * @return Observable An observable list of products + */ + getProductsByVendor(vendor: number): Observable { + try { + return this.http.get((this.apiUrl + '/products/vendor/' + vendor)); + } catch (exception) { + process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`); + } + } + + /** + * Creates a new product entry + * @param asinOrLink The amazon link or asin of the product + * @return Observable The observable response of the api + */ + addNewProduct(asinOrLink: string): Observable { + let asin = ''; + + // Check if the parameter is a link or an asin + const linkRegex: RegExp = /^http[s]{0,1}:\/\/.*\/dp\/(.[^\/]*)\/{0,1}.*$/; + const matches = linkRegex.exec(asinOrLink); + if (matches) { + // param is a link, extract asin + asin = matches[1] ?? ''; + } else { + // param is not a link, suspect it is an asin + asin = asinOrLink; + } + + try { + return this.http.post((this.apiUrl + '/products'), JSON.stringify({ + asin + })); + } catch (exception) { + process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`); + } + } + /* ____ _ / __ \_____(_)_______ _____ @@ -78,6 +132,19 @@ export class ApiService { /_/ /_/ /_/\___/\___/____/ */ + /** + * Gets the specified price from the API + * @param id The id of the price to get + * @return Observable An observable containing a single price + */ + getPrice(id: number): Observable { + try { + return this.http.get((this.apiUrl + '/prices/' + id)); + } catch (exception) { + process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`); + } + } + /** * Gets a list of all prices * @return Observable An observable list of prices @@ -139,6 +206,51 @@ export class ApiService { } } + /** + * Gets the currently best deals + * @param amount The amount of deals to get + * @return Observable An observable list of deals + */ + getBestDeals(amount: number): Observable { + try { + return this.http.get((this.apiUrl + '/prices/bestDeals/' + amount)); + } catch (exception) { + process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`); + } + } + + /** + * Gets a list of all prices for the specified product + * @param product The product to get prices for + * @return Observable An observable list of prices + */ + getPricesByProduct(products: number[]): Observable { + try { + console.log('IDs: ' + products.toString()); + return this.http.get((this.apiUrl + '/prices/byProduct/list/[' + products.toString() + ']')); + } catch (exception) { + process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`); + } + } + + /** + * Creates a new price entry + * @param vendorId The vendor to add the price for + * @param productId The product to add the price to + * @param price The price in cents to add + * @return Observable The observable response of the api + */ + addNewPrice(vendorId: number, productId: number, price: number): Observable { + try { + return this.http.post((this.apiUrl + '/prices'), JSON.stringify({ + vendor_id: vendorId, + product_id: productId, + price_in_cents: price + })); + } catch (exception) { + process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`); + } + } /* _ __ __ | | / /__ ____ ____/ /___ __________