BETTERZON-101: Adding service functions for pricealarms api (#55)

- Not properly tested though as login functionality is required to test but not yet implemented
This commit is contained in:
Patrick 2021-05-19 19:46:30 +02:00 committed by GitHub
parent 1581184b57
commit 712f6c9034
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 2 deletions

View File

@ -36,7 +36,7 @@ pricealarmsRouter.get('/', async (req: Request, res: Response) => {
}); });
// POST pricealarms/create // POST pricealarms/create
pricealarmsRouter.post('/create', async (req: Request, res: Response) => { pricealarmsRouter.post('/', async (req: Request, res: Response) => {
try { try {
// Authenticate user // Authenticate user
const user_ip = req.connection.remoteAddress ?? ''; const user_ip = req.connection.remoteAddress ?? '';
@ -69,7 +69,7 @@ pricealarmsRouter.post('/create', async (req: Request, res: Response) => {
}); });
// PUT pricealarms/update // PUT pricealarms/update
pricealarmsRouter.put('/update', async (req: Request, res: Response) => { pricealarmsRouter.put('/', async (req: Request, res: Response) => {
try { try {
// Authenticate user // Authenticate user
const user_ip = req.connection.remoteAddress ?? ''; const user_ip = req.connection.remoteAddress ?? '';

View File

@ -0,0 +1,6 @@
export interface PriceAlarm {
alarm_id: number;
user_id: number;
product_id: number;
defined_price: number;
}

View File

@ -5,6 +5,7 @@ import {Product} from '../models/product';
import {Price} from '../models/price'; import {Price} from '../models/price';
import {Observable, of} from 'rxjs'; import {Observable, of} from 'rxjs';
import {Vendor} from '../models/vendor'; import {Vendor} from '../models/vendor';
import {PriceAlarm} from '../models/pricealarm';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
@ -98,4 +99,37 @@ export class ApiService {
process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`); process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`);
} }
} }
getPriceAlarms(): Observable<PriceAlarm[]> {
try {
const alarms = this.http.get<PriceAlarm[]>((this.apiUrl + '/pricealarms'));
return alarms;
} catch (exception) {
process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`);
}
}
createPriceAlarms(productId: number, definedPrice: number): Observable<any> {
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<any> {
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`);
}
}
} }