From e7543e6430065075ed83e332376e2aa5f16ecf51 Mon Sep 17 00:00:00 2001 From: Patrick <50352812+Mueller-Patrick@users.noreply.github.com> Date: Thu, 20 May 2021 10:23:41 +0200 Subject: [PATCH] BETTERZON-107: Refactoring code with Proxy as design pattern (#49) --- 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 acc6793..3175b66 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'; @@ -255,7 +255,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)]) { @@ -287,7 +287,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); } } });