BETTERZON-110: Refactoring, reformatting and commenting api service (#56)

This commit is contained in:
Patrick 2021-05-20 10:20:50 +02:00 committed by GitHub
parent 712f6c9034
commit ead1f10b25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -18,116 +18,191 @@ export class ApiService {
) { ) {
} }
/* ____ __ __
/ __ \_________ ____/ /_ _______/ /______
/ /_/ / ___/ __ \/ __ / / / / ___/ __/ ___/
/ ____/ / / /_/ / /_/ / /_/ / /__/ /_(__ )
/_/ /_/ \____/\__,_/\__,_/\___/\__/____/
*/
/**
* Gets the specified product from the API
* @param id The id of the product to get
* @return Observable<Product> An observable containing a single product
*/
getProduct(id): Observable<Product> { getProduct(id): Observable<Product> {
try { try {
const prod = this.http.get<Product>((this.apiUrl + '/products/' + id)); return this.http.get<Product>((this.apiUrl + '/products/' + id));
return prod;
} catch (exception) { } catch (exception) {
process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`); process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`);
} }
} }
/**
* Gets a list of products that match the given search term
* @param query The search term to match
* @return Observable<Product[]> An observable list of products
*/
getProductsByQuery(query): Observable<Product[]> { getProductsByQuery(query): Observable<Product[]> {
try { try {
const prods = this.http.get<Product[]>((this.apiUrl + '/products/search/' + query)); return this.http.get<Product[]>((this.apiUrl + '/products/search/' + query));
return prods;
} catch (exception) { } catch (exception) {
process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`); process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`);
} }
} }
/**
* Gets a list of all products
* @return Observable<Product[]> An observable list of products
*/
getProducts(): Observable<Product[]> { getProducts(): Observable<Product[]> {
try { try {
const prods = this.http.get<Product[]>((this.apiUrl + '/products')); return this.http.get<Product[]>((this.apiUrl + '/products'));
return prods;
} catch (exception) { } catch (exception) {
process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`); process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`);
} }
} }
/* ____ _
/ __ \_____(_)_______ _____
/ /_/ / ___/ / ___/ _ \/ ___/
/ ____/ / / / /__/ __(__ )
/_/ /_/ /_/\___/\___/____/
*/
/**
* Gets a list of all prices
* @return Observable<Price[]> An observable list of prices
*/
getPrices(): Observable<Price[]> { getPrices(): Observable<Price[]> {
try { try {
const prices = this.http.get<Price[]>((this.apiUrl + '/prices')); return this.http.get<Price[]>((this.apiUrl + '/prices'));
return prices;
} catch (exception) { } catch (exception) {
process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`); process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`);
} }
} }
/**
* Gets the lowest prices of every vendor for the given product
* @param productId The product id of the product to fetch the prices for
* @return Observable<Price[]> An observable list of prices
*/
getLowestPrices(productId): Observable<Price[]> { getLowestPrices(productId): Observable<Price[]> {
try { try {
let params = new HttpParams(); let params = new HttpParams();
params = params.append('product', productId); params = params.append('product', productId);
params = params.append('type', 'lowest'); params = params.append('type', 'lowest');
const prices = this.http.get<Price[]>((this.apiUrl + '/prices'), {params}); return this.http.get<Price[]>((this.apiUrl + '/prices'), {params});
return prices;
} catch (exception) { } catch (exception) {
process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`); process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`);
} }
} }
/**
* Gets the latest amazon price for the given product
* @param productId The product id of the product to get the price for
* @return Observable<Price> An observable containing a single price
*/
getAmazonPrice(productId): Observable<Price> { getAmazonPrice(productId): Observable<Price> {
try { try {
let params = new HttpParams(); let params = new HttpParams();
params = params.append('product', productId); params = params.append('product', productId);
params = params.append('vendor', '1'); params = params.append('vendor', '1');
params = params.append('type', 'newest'); params = params.append('type', 'newest');
const price = this.http.get<Price>((this.apiUrl + '/prices'), {params}); return this.http.get<Price>((this.apiUrl + '/prices'), {params});
return price;
} catch (exception) { } catch (exception) {
process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`); process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`);
} }
} }
/**
* Gets the newest prices of every vendor for the given product
* @param productId The product id of the product to fetch the prices for
* @return Observable<Price[]> An observable list of prices
*/
getCurrentPricePerVendor(productId): Observable<Price[]> { getCurrentPricePerVendor(productId): Observable<Price[]> {
try { try {
let params = new HttpParams(); let params = new HttpParams();
params = params.append('product', productId); params = params.append('product', productId);
params = params.append('type', 'newest'); params = params.append('type', 'newest');
const prices = this.http.get<Price[]>((this.apiUrl + '/prices'), {params}); return this.http.get<Price[]>((this.apiUrl + '/prices'), {params});
return prices;
} catch (exception) { } catch (exception) {
process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`); process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`);
} }
} }
/* _ __ __
| | / /__ ____ ____/ /___ __________
| | / / _ \/ __ \/ __ / __ \/ ___/ ___/
| |/ / __/ / / / /_/ / /_/ / / (__ )
|___/\___/_/ /_/\__,_/\____/_/ /____/
*/
/**
* Gets a list of all vendors
* @return Observable<Vendor[]> An observable list of vendors
*/
getVendors(): Observable<Vendor[]> { getVendors(): Observable<Vendor[]> {
try { try {
const vendors = this.http.get<Vendor[]>((this.apiUrl + '/vendors')); return this.http.get<Vendor[]>((this.apiUrl + '/vendors'));
return vendors;
} catch (exception) { } catch (exception) {
process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`); process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`);
} }
} }
/* ____ _ ___ __
/ __ \_____(_)_______ / | / /___ __________ ___ _____
/ /_/ / ___/ / ___/ _ \ / /| | / / __ `/ ___/ __ `__ \/ ___/
/ ____/ / / / /__/ __/ / ___ |/ / /_/ / / / / / / / (__ )
/_/ /_/ /_/\___/\___/ /_/ |_/_/\__,_/_/ /_/ /_/ /_/____/
*/
/**
* Gets a list of all price alarms
* @return Observable<PriceAlarm[]> An observable list of price alarms
*/
getPriceAlarms(): Observable<PriceAlarm[]> { getPriceAlarms(): Observable<PriceAlarm[]> {
try { try {
const alarms = this.http.get<PriceAlarm[]>((this.apiUrl + '/pricealarms')); return this.http.get<PriceAlarm[]>((this.apiUrl + '/pricealarms'));
return alarms;
} catch (exception) { } catch (exception) {
process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`); process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`);
} }
} }
/**
* Creates a new price alarm
* @param productId The product id of the product to create the alarm for
* @param definedPrice The defined target price
* @return Observable<any> The observable response of the api
*/
createPriceAlarms(productId: number, definedPrice: number): Observable<any> { createPriceAlarms(productId: number, definedPrice: number): Observable<any> {
try { try {
const res = this.http.post((this.apiUrl + '/pricealarms'), JSON.stringify({ return this.http.post((this.apiUrl + '/pricealarms'), JSON.stringify({
product_id: productId, product_id: productId,
defined_price: definedPrice defined_price: definedPrice
})); }));
return res;
} catch (exception) { } catch (exception) {
process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`); process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`);
} }
} }
/**
* Updates the given price alarm
* @param alarmId The alarm id of the alarm to update
* @param definedPrice The defined target price
* @return Observable<any> The observable response of the api
*/
updatePriceAlarms(alarmId: number, definedPrice: number): Observable<any> { updatePriceAlarms(alarmId: number, definedPrice: number): Observable<any> {
try { try {
const res = this.http.put((this.apiUrl + '/pricealarms'), JSON.stringify({ return this.http.put((this.apiUrl + '/pricealarms'), JSON.stringify({
alarm_id: alarmId, alarm_id: alarmId,
defined_price: definedPrice defined_price: definedPrice
})); }));
return res;
} catch (exception) { } catch (exception) {
process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`); process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`);
} }