mirror of
https://github.com/Mueller-Patrick/Betterzon.git
synced 2024-11-22 14:23:57 +00:00
BETTERZON-91: Adding API endpoint to GET all price alarms for the currently logged in user (#43)
This commit is contained in:
parent
f333bbfc05
commit
cd0c11dbc7
|
@ -19,7 +19,32 @@ export const pricealarmsRouter = express.Router();
|
||||||
* Controller Definitions
|
* 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) => {
|
pricealarmsRouter.post('/create', async (req: Request, res: Response) => {
|
||||||
try {
|
try {
|
||||||
// Authenticate user
|
// Authenticate user
|
||||||
|
@ -48,15 +73,15 @@ pricealarmsRouter.post('/create', async (req: Request, res: Response) => {
|
||||||
// Create price alarm
|
// Create price alarm
|
||||||
const success = await PriceAlarmsService.createPriceAlarm(user.user_id, product_id, defined_price);
|
const success = await PriceAlarmsService.createPriceAlarm(user.user_id, product_id, defined_price);
|
||||||
|
|
||||||
if(success) {
|
if (success) {
|
||||||
res.status(200).send(JSON.stringify({success: true}));
|
res.status(201).send(JSON.stringify({success: true}));
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
res.status(400).send(JSON.stringify({success: false}));
|
res.status(500).send(JSON.stringify({success: false}));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log('Error handling a request: ' + e.message);
|
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.'}));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -25,16 +25,21 @@ import {PriceAlarms} from './pricealarms.interface';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a price alarm for the given user for the product with the defined price
|
* 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> => {
|
export const createPriceAlarm = async (user_id: number, product_id: number, defined_price: number): Promise<Boolean> => {
|
||||||
let conn;
|
let conn;
|
||||||
try {
|
try {
|
||||||
conn = await pool.getConnection();
|
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);
|
|
||||||
|
|
||||||
|
if (res.affectedRows === 1) {
|
||||||
return true;
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw err;
|
throw err;
|
||||||
} finally {
|
} finally {
|
||||||
|
@ -45,3 +50,29 @@ export const createPriceAlarm = async (user_id: number, product_id: number, defi
|
||||||
|
|
||||||
return false;
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
Loading…
Reference in New Issue
Block a user