mirror of
https://github.com/Mueller-Patrick/Betterzon.git
synced 2025-04-19 15:29:18 +00:00
102 lines
3.4 KiB
TypeScript
102 lines
3.4 KiB
TypeScript
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 {Observable, of} from 'rxjs';
|
|
import {Vendor} from '../models/vendor';
|
|
|
|
@Injectable({
|
|
providedIn: 'root'
|
|
})
|
|
export class ApiService {
|
|
apiUrl = 'https://backend.betterzon.xyz';
|
|
|
|
constructor(
|
|
private http: HttpClient
|
|
) {
|
|
}
|
|
|
|
getProduct(id): Observable<Product> {
|
|
try {
|
|
const prod = this.http.get<Product>((this.apiUrl + '/products/' + id));
|
|
return prod;
|
|
} catch (exception) {
|
|
process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`);
|
|
}
|
|
}
|
|
|
|
getProductsByQuery(query): Observable<Product[]> {
|
|
try {
|
|
const prods = this.http.get<Product[]>((this.apiUrl + '/products/search/' + query));
|
|
return prods;
|
|
} catch (exception) {
|
|
process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`);
|
|
}
|
|
}
|
|
|
|
getProducts(): Observable<Product[]> {
|
|
try {
|
|
const prods = this.http.get<Product[]>((this.apiUrl + '/products'));
|
|
return prods;
|
|
} catch (exception) {
|
|
process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`);
|
|
}
|
|
}
|
|
|
|
getPrices(): Observable<Price[]> {
|
|
try {
|
|
const prices = this.http.get<Price[]>((this.apiUrl + '/prices'));
|
|
return prices;
|
|
} catch (exception) {
|
|
process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`);
|
|
}
|
|
}
|
|
|
|
getLowestPrices(productId): Observable<Price[]> {
|
|
try {
|
|
let params = new HttpParams();
|
|
params = params.append('product', productId);
|
|
params = params.append('type', 'lowest');
|
|
const prices = this.http.get<Price[]>((this.apiUrl + '/prices'), {params});
|
|
return prices;
|
|
} catch (exception) {
|
|
process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`);
|
|
}
|
|
}
|
|
|
|
getAmazonPrice(productId): Observable<Price> {
|
|
try {
|
|
let params = new HttpParams();
|
|
params = params.append('product', productId);
|
|
params = params.append('vendor', '1');
|
|
params = params.append('type', 'newest');
|
|
const price = this.http.get<Price>((this.apiUrl + '/prices'), {params});
|
|
return price;
|
|
} catch (exception) {
|
|
process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`);
|
|
}
|
|
}
|
|
|
|
getCurrentPricePerVendor(productId): Observable<Price[]> {
|
|
try {
|
|
let params = new HttpParams();
|
|
params = params.append('product', productId);
|
|
params = params.append('type', 'newest');
|
|
const prices = this.http.get<Price[]>((this.apiUrl + '/prices'), {params});
|
|
return prices;
|
|
} catch (exception) {
|
|
process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`);
|
|
}
|
|
}
|
|
|
|
getVendors(): Observable<Vendor[]> {
|
|
try {
|
|
const vendors = this.http.get<Vendor[]>((this.apiUrl + '/vendors'));
|
|
return vendors;
|
|
} catch (exception) {
|
|
process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`);
|
|
}
|
|
}
|
|
}
|