BETTERZON-90: Adding API endpoint for creating price alarms (#42)

This commit is contained in:
Patrick
2021-05-12 23:57:24 +02:00
committed by GitHub
parent 3874957d5a
commit f333bbfc05
6 changed files with 124 additions and 2 deletions
@@ -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<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);
return true;
} catch (err) {
throw err;
} finally {
if (conn) {
conn.end();
}
}
return false;
};