From cd0c11dbc709141ef2b94285a6e7a6c3e84ae664 Mon Sep 17 00:00:00 2001 From: Patrick <50352812+Mueller-Patrick@users.noreply.github.com> Date: Thu, 13 May 2021 00:29:01 +0200 Subject: [PATCH] BETTERZON-91: Adding API endpoint to GET all price alarms for the currently logged in user (#43) --- .../models/pricealarms/pricealarms.router.ts | 35 ++++++++++++++--- .../models/pricealarms/pricealarms.service.ts | 39 +++++++++++++++++-- 2 files changed, 65 insertions(+), 9 deletions(-) diff --git a/Backend/src/models/pricealarms/pricealarms.router.ts b/Backend/src/models/pricealarms/pricealarms.router.ts index 3378583..4e0e014 100644 --- a/Backend/src/models/pricealarms/pricealarms.router.ts +++ b/Backend/src/models/pricealarms/pricealarms.router.ts @@ -19,7 +19,32 @@ export const pricealarmsRouter = express.Router(); * Controller Definitions */ -// POST priceAlarms/create +//GET pricealarms/ +pricealarmsRouter.get('/', 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); + + const priceAlarms = await PriceAlarmsService.getPriceAlarms(user.user_id); + + res.status(200).send(priceAlarms); + } catch (e) { + console.log('Error handling a request: ' + e.message); + res.status(500).send(JSON.stringify({'message': 'Internal Server Error. Try again later.'})); + } +}); + +// POST pricealarms/create pricealarmsRouter.post('/create', async (req: Request, res: Response) => { try { // Authenticate user @@ -48,15 +73,15 @@ pricealarmsRouter.post('/create', async (req: Request, res: Response) => { // 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})); + if (success) { + res.status(201).send(JSON.stringify({success: true})); return; } else { - res.status(400).send(JSON.stringify({success: false})); + res.status(500).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."})); + 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 index 0bccd3b..ef44db6 100644 --- a/Backend/src/models/pricealarms/pricealarms.service.ts +++ b/Backend/src/models/pricealarms/pricealarms.service.ts @@ -25,16 +25,21 @@ import {PriceAlarms} from './pricealarms.interface'; /** * Creates a price alarm for the given user for the product with the defined price + * @param user_id The id of the user to create the price alarm for + * @param product_id The id of the product to create the price alarm for + * @param defined_price The defined price for the price alarm */ 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]); + const res = 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; + if (res.affectedRows === 1) { + return true; + } else { + return false; + } } catch (err) { throw err; } finally { @@ -45,3 +50,29 @@ export const createPriceAlarm = async (user_id: number, product_id: number, defi return false; }; + +/** + * Fetches and returns all price alarms for the given user + * @param user_id + */ +export const getPriceAlarms = async (user_id: number): Promise => { + let conn; + let priceAlarms = []; + try { + conn = await pool.getConnection(); + const rows = await conn.query('SELECT alarm_id, user_id, product_id, defined_price FROM price_alarms WHERE user_id = ?', user_id); + for (let row in rows) { + if (row !== 'meta') { + priceAlarms.push(rows[row]); + } + } + + return priceAlarms; + } catch (err) { + throw err; + } finally { + if (conn) { + conn.end(); + } + } +};