From f333bbfc0595ba92a3194b7ea12f1cdde8def6c9 Mon Sep 17 00:00:00 2001 From: Patrick <50352812+Mueller-Patrick@users.noreply.github.com> Date: Wed, 12 May 2021 23:57:24 +0200 Subject: [PATCH] BETTERZON-90: Adding API endpoint for creating price alarms (#42) --- Backend/src/index.ts | 2 + .../pricealarms/pricealarm.interface.ts | 6 ++ .../pricealarms/pricealarms.interface.ts | 5 ++ .../models/pricealarms/pricealarms.router.ts | 62 +++++++++++++++++++ .../models/pricealarms/pricealarms.service.ts | 47 ++++++++++++++ CucumberTests/pom.xml | 4 +- 6 files changed, 124 insertions(+), 2 deletions(-) create mode 100644 Backend/src/models/pricealarms/pricealarm.interface.ts create mode 100644 Backend/src/models/pricealarms/pricealarms.interface.ts create mode 100644 Backend/src/models/pricealarms/pricealarms.router.ts create mode 100644 Backend/src/models/pricealarms/pricealarms.service.ts diff --git a/Backend/src/index.ts b/Backend/src/index.ts index 3f7bed5..6d46ba9 100644 --- a/Backend/src/index.ts +++ b/Backend/src/index.ts @@ -14,6 +14,7 @@ import {vendorsRouter} from './models/vendors/vendors.router'; import {errorHandler} from './middleware/error.middleware'; import {notFoundHandler} from './middleware/notFound.middleware'; import {usersRouter} from './models/users/users.router'; +import {pricealarmsRouter} from './models/pricealarms/pricealarms.router'; dotenv.config(); @@ -44,6 +45,7 @@ app.use('/manufacturers', manufacturersRouter); app.use('/prices', pricesRouter); app.use('/users', usersRouter); app.use('/vendors', vendorsRouter); +app.use('/pricealarms', pricealarmsRouter); app.use(errorHandler); app.use(notFoundHandler); diff --git a/Backend/src/models/pricealarms/pricealarm.interface.ts b/Backend/src/models/pricealarms/pricealarm.interface.ts new file mode 100644 index 0000000..c8a1717 --- /dev/null +++ b/Backend/src/models/pricealarms/pricealarm.interface.ts @@ -0,0 +1,6 @@ +export interface PriceAlarm { + alarm_id: number; + user_id: number; + product_id: number; + defined_price: number; +} diff --git a/Backend/src/models/pricealarms/pricealarms.interface.ts b/Backend/src/models/pricealarms/pricealarms.interface.ts new file mode 100644 index 0000000..c1dcbbd --- /dev/null +++ b/Backend/src/models/pricealarms/pricealarms.interface.ts @@ -0,0 +1,5 @@ +import {PriceAlarm} from './pricealarm.interface'; + +export interface PriceAlarms { + [key: number]: PriceAlarm; +} diff --git a/Backend/src/models/pricealarms/pricealarms.router.ts b/Backend/src/models/pricealarms/pricealarms.router.ts new file mode 100644 index 0000000..3378583 --- /dev/null +++ b/Backend/src/models/pricealarms/pricealarms.router.ts @@ -0,0 +1,62 @@ +/** + * Required External Modules and Interfaces + */ + +import express, {Request, Response} from 'express'; +import * as PriceAlarmsService from './pricealarms.service'; +import {PriceAlarm} from './pricealarm.interface'; +import {PriceAlarms} from './pricealarms.interface'; +import * as UserService from '../users/users.service'; + + +/** + * Router Definition + */ +export const pricealarmsRouter = express.Router(); + + +/** + * Controller Definitions + */ + +// POST priceAlarms/create +pricealarmsRouter.post('/create', async (req: Request, res: Response) => { + try { + // Authenticate user + const session_id = req.body.session_id; + const session_key = req.body.session_key; + const user_ip = req.connection.remoteAddress ?? ''; + + if (!session_id || !session_key) { + // Missing + res.status(400).send(JSON.stringify({message: 'Missing parameters'})); + return; + } + + const user = await UserService.checkSession(session_id, session_key, user_ip); + + // Get info for price alarm creation + const product_id = req.body.product_id; + const defined_price = req.body.defined_price; + + if (!product_id || !defined_price) { + // Missing + res.status(400).send(JSON.stringify({message: 'Missing parameters'})); + return; + } + + // Create price alarm + const success = await PriceAlarmsService.createPriceAlarm(user.user_id, product_id, defined_price); + + if(success) { + res.status(200).send(JSON.stringify({success: true})); + return; + } else { + res.status(400).send(JSON.stringify({success: false})); + return; + } + } catch (e) { + console.log('Error handling a request: ' + e.message); + res.status(500).send(JSON.stringify({"message": "Internal Server Error. Try again later."})); + } +}); diff --git a/Backend/src/models/pricealarms/pricealarms.service.ts b/Backend/src/models/pricealarms/pricealarms.service.ts new file mode 100644 index 0000000..0bccd3b --- /dev/null +++ b/Backend/src/models/pricealarms/pricealarms.service.ts @@ -0,0 +1,47 @@ +import * as dotenv from 'dotenv'; + +dotenv.config(); + +const mariadb = require('mariadb'); +const pool = mariadb.createPool({ + host: process.env.DB_HOST, + user: process.env.DB_USER, + password: process.env.DB_PASSWORD, + database: process.env.DB_DATABASE, + connectionLimit: 5 +}); + +/** + * Data Model Interfaces + */ + +import {PriceAlarm} from './pricealarm.interface'; +import {PriceAlarms} from './pricealarms.interface'; + + +/** + * Service Methods + */ + +/** + * Creates a price alarm for the given user for the product with the defined price + */ +export const createPriceAlarm = async (user_id: number, product_id: number, defined_price: number): Promise => { + let conn; + try { + conn = await pool.getConnection(); + const affected_rows = await conn.query('INSERT INTO price_alarms (user_id, product_id, defined_price) VALUES (?, ?, ?)', [user_id, product_id, defined_price]); + + console.log(affected_rows); + + return true; + } catch (err) { + throw err; + } finally { + if (conn) { + conn.end(); + } + } + + return false; +}; diff --git a/CucumberTests/pom.xml b/CucumberTests/pom.xml index 8aaba7a..3f233e8 100644 --- a/CucumberTests/pom.xml +++ b/CucumberTests/pom.xml @@ -4,7 +4,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - de.taskhub + xyz.betterzon CucumberTests 1.0-SNAPSHOT @@ -30,4 +30,4 @@ 3.8.1 - \ No newline at end of file +