From 712f6c9034dc2fb245ea9c3645ecea4fced16b31 Mon Sep 17 00:00:00 2001 From: Patrick <50352812+Mueller-Patrick@users.noreply.github.com> Date: Wed, 19 May 2021 19:46:30 +0200 Subject: [PATCH] BETTERZON-101: Adding service functions for pricealarms api (#55) - Not properly tested though as login functionality is required to test but not yet implemented --- .../models/pricealarms/pricealarms.router.ts | 4 +-- Frontend/src/app/models/pricealarm.ts | 6 ++++ Frontend/src/app/services/api.service.ts | 34 +++++++++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 Frontend/src/app/models/pricealarm.ts diff --git a/Backend/src/models/pricealarms/pricealarms.router.ts b/Backend/src/models/pricealarms/pricealarms.router.ts index faa3c40..4ba1f9f 100644 --- a/Backend/src/models/pricealarms/pricealarms.router.ts +++ b/Backend/src/models/pricealarms/pricealarms.router.ts @@ -36,7 +36,7 @@ pricealarmsRouter.get('/', async (req: Request, res: Response) => { }); // POST pricealarms/create -pricealarmsRouter.post('/create', async (req: Request, res: Response) => { +pricealarmsRouter.post('/', async (req: Request, res: Response) => { try { // Authenticate user const user_ip = req.connection.remoteAddress ?? ''; @@ -69,7 +69,7 @@ pricealarmsRouter.post('/create', async (req: Request, res: Response) => { }); // PUT pricealarms/update -pricealarmsRouter.put('/update', async (req: Request, res: Response) => { +pricealarmsRouter.put('/', async (req: Request, res: Response) => { try { // Authenticate user const user_ip = req.connection.remoteAddress ?? ''; diff --git a/Frontend/src/app/models/pricealarm.ts b/Frontend/src/app/models/pricealarm.ts new file mode 100644 index 0000000..c8a1717 --- /dev/null +++ b/Frontend/src/app/models/pricealarm.ts @@ -0,0 +1,6 @@ +export interface PriceAlarm { + alarm_id: number; + user_id: number; + product_id: number; + defined_price: number; +} diff --git a/Frontend/src/app/services/api.service.ts b/Frontend/src/app/services/api.service.ts index ec91051..e80e1a8 100644 --- a/Frontend/src/app/services/api.service.ts +++ b/Frontend/src/app/services/api.service.ts @@ -5,6 +5,7 @@ import {Product} from '../models/product'; import {Price} from '../models/price'; import {Observable, of} from 'rxjs'; import {Vendor} from '../models/vendor'; +import {PriceAlarm} from '../models/pricealarm'; @Injectable({ providedIn: 'root' @@ -98,4 +99,37 @@ export class ApiService { process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`); } } + + getPriceAlarms(): Observable { + try { + const alarms = this.http.get((this.apiUrl + '/pricealarms')); + return alarms; + } catch (exception) { + process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`); + } + } + + createPriceAlarms(productId: number, definedPrice: number): Observable { + try { + const res = this.http.post((this.apiUrl + '/pricealarms'), JSON.stringify({ + product_id: productId, + defined_price: definedPrice + })); + return res; + } catch (exception) { + process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`); + } + } + + updatePriceAlarms(alarmId: number, definedPrice: number): Observable { + try { + const res = this.http.put((this.apiUrl + '/pricealarms'), JSON.stringify({ + alarm_id: alarmId, + defined_price: definedPrice + })); + return res; + } catch (exception) { + process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`); + } + } }