From 9415b46e9573b5c6c9861e393db0a0e0362575ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20M=C3=BCller?= Date: Sun, 16 May 2021 16:22:25 +0200 Subject: [PATCH] BETTERZON-107: Refactoring code with Proxy as design pattern --- Backend/src/models/prices/price.interface.ts | 23 +++++++++++++++++--- Backend/src/models/prices/prices.service.ts | 6 ++--- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/Backend/src/models/prices/price.interface.ts b/Backend/src/models/prices/price.interface.ts index 956c9d5..702015a 100644 --- a/Backend/src/models/prices/price.interface.ts +++ b/Backend/src/models/prices/price.interface.ts @@ -4,7 +4,24 @@ export interface Price { vendor_id: number; price_in_cents: number; timestamp: Date; - // Only for deals - amazonDifference?: number; - amazonDifferencePercent?: number; +} + +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/Backend/src/models/prices/prices.service.ts b/Backend/src/models/prices/prices.service.ts index 405cfd4..0c0277b 100644 --- a/Backend/src/models/prices/prices.service.ts +++ b/Backend/src/models/prices/prices.service.ts @@ -15,7 +15,7 @@ const pool = mariadb.createPool({ * Data Model Interfaces */ -import {Price} from './price.interface'; +import {Deal, Price} from './price.interface'; import {Prices} from './prices.interface'; @@ -254,7 +254,7 @@ export const getBestDeals = async (amount: number): Promise => { } // Iterate over all prices to find the products with the biggest difference between amazon and other vendor - let deals: Price[] = []; + let deals: Deal[] = []; Object.keys(allPrices).forEach(productId => { if (allPrices[parseInt(productId)]) { @@ -286,7 +286,7 @@ export const getBestDeals = async (amount: number): Promise => { // Push only deals were the amazon price is actually higher if (deal.amazonDifferencePercent > 0) { - deals.push(deal as Price); + deals.push(deal as Deal); } } });