BETTERZON-91: Adding API endpoint to GET all price alarms for the currently logged in user (#43)

This commit is contained in:
Patrick 2021-05-13 00:29:01 +02:00 committed by GitHub
parent f333bbfc05
commit cd0c11dbc7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 65 additions and 9 deletions

View File

@ -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.'}));
}
});

View File

@ -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<Boolean> => {
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);
const res = await conn.query('INSERT INTO price_alarms (user_id, product_id, defined_price) VALUES (?, ?, ?)', [user_id, product_id, defined_price]);
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<PriceAlarms> => {
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();
}
}
};