mirror of
https://github.com/Mueller-Patrick/Betterzon.git
synced 2026-05-06 19:57:58 +00:00
BETTERZON-151: Adding option to delete price alarm (#94)
This commit is contained in:
@@ -106,3 +106,29 @@ pricealarmsRouter.put('/', async (req: Request, res: Response) => {
|
||||
res.status(500).send(JSON.stringify({'message': 'Internal Server Error. Try again later.'}));
|
||||
}
|
||||
});
|
||||
|
||||
// DELETE pricealarms/:id
|
||||
pricealarmsRouter.delete('/:id', async (req, res) => {
|
||||
try {
|
||||
// Authenticate user
|
||||
const user_ip = req.connection.remoteAddress ?? '';
|
||||
const session_id = (req.query.session_id ?? '').toString();
|
||||
const session_key = (req.query.session_key ?? '').toString();
|
||||
const user = await UserService.checkSession(session_id, session_key, user_ip);
|
||||
|
||||
const id: number = parseInt(req.params.id, 10);
|
||||
|
||||
const success = await PriceAlarmsService.deletePriceAlarm(id, user.user_id);
|
||||
|
||||
if (success) {
|
||||
res.status(200).send(JSON.stringify({success: true}));
|
||||
return;
|
||||
} else {
|
||||
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.'}));
|
||||
}
|
||||
});
|
||||
|
||||
@@ -92,3 +92,24 @@ export const updatePriceAlarm = async (alarm_id: number, user_id: number, define
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Deletes the given price alarm
|
||||
* @param alarm_id The id of the price alarm to update
|
||||
* @param user_id The id of the user that wants to update the price alarm
|
||||
*/
|
||||
export const deletePriceAlarm = async (alarm_id: number, user_id: number): Promise<boolean> => {
|
||||
let conn;
|
||||
try {
|
||||
conn = await pool.getConnection();
|
||||
const res = await conn.query('DELETE FROM price_alarms WHERE alarm_id = ? AND user_id = ?', [alarm_id, user_id]);
|
||||
|
||||
return res.affectedRows === 1;
|
||||
} catch (err) {
|
||||
throw err;
|
||||
} finally {
|
||||
if (conn) {
|
||||
conn.end();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user