mirror of
https://github.com/Mueller-Patrick/Betterzon.git
synced 2024-12-23 12:15:11 +00:00
Compare commits
No commits in common. "0be394fc1df297ebb809438bfd33ab07358ef9bd" and "3874957d5aa595a220572faeec17ff5e3dd6090c" have entirely different histories.
0be394fc1d
...
3874957d5a
|
@ -14,7 +14,6 @@ import {vendorsRouter} from './models/vendors/vendors.router';
|
|||
import {errorHandler} from './middleware/error.middleware';
|
||||
import {notFoundHandler} from './middleware/notFound.middleware';
|
||||
import {usersRouter} from './models/users/users.router';
|
||||
import {pricealarmsRouter} from './models/pricealarms/pricealarms.router';
|
||||
|
||||
dotenv.config();
|
||||
|
||||
|
@ -45,7 +44,6 @@ app.use('/manufacturers', manufacturersRouter);
|
|||
app.use('/prices', pricesRouter);
|
||||
app.use('/users', usersRouter);
|
||||
app.use('/vendors', vendorsRouter);
|
||||
app.use('/pricealarms', pricealarmsRouter);
|
||||
|
||||
app.use(errorHandler);
|
||||
app.use(notFoundHandler);
|
||||
|
|
|
@ -1,6 +0,0 @@
|
|||
export interface PriceAlarm {
|
||||
alarm_id: number;
|
||||
user_id: number;
|
||||
product_id: number;
|
||||
defined_price: number;
|
||||
}
|
|
@ -1,5 +0,0 @@
|
|||
import {PriceAlarm} from './pricealarm.interface';
|
||||
|
||||
export interface PriceAlarms {
|
||||
[key: number]: PriceAlarm;
|
||||
}
|
|
@ -1,129 +0,0 @@
|
|||
/**
|
||||
* Required External Modules and Interfaces
|
||||
*/
|
||||
|
||||
import express, {Request, Response} from 'express';
|
||||
import * as PriceAlarmsService from './pricealarms.service';
|
||||
import {PriceAlarm} from './pricealarm.interface';
|
||||
import {PriceAlarms} from './pricealarms.interface';
|
||||
import * as UserService from '../users/users.service';
|
||||
|
||||
|
||||
/**
|
||||
* Router Definition
|
||||
*/
|
||||
export const pricealarmsRouter = express.Router();
|
||||
|
||||
|
||||
/**
|
||||
* Controller Definitions
|
||||
*/
|
||||
|
||||
//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
|
||||
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);
|
||||
|
||||
// Get info for price alarm creation
|
||||
const product_id = req.body.product_id;
|
||||
const defined_price = req.body.defined_price;
|
||||
|
||||
if (!product_id || !defined_price) {
|
||||
// Missing
|
||||
res.status(400).send(JSON.stringify({message: 'Missing parameters'}));
|
||||
return;
|
||||
}
|
||||
|
||||
// Create price alarm
|
||||
const success = await PriceAlarmsService.createPriceAlarm(user.user_id, product_id, defined_price);
|
||||
|
||||
if (success) {
|
||||
res.status(201).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.'}));
|
||||
}
|
||||
});
|
||||
|
||||
// PUT pricealarms/update
|
||||
pricealarmsRouter.put('/update', 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);
|
||||
|
||||
// Get info for price alarm creation
|
||||
const alarm_id = req.body.alarm_id;
|
||||
const defined_price = req.body.defined_price;
|
||||
|
||||
if (!alarm_id || !defined_price) {
|
||||
// Missing
|
||||
res.status(400).send(JSON.stringify({message: 'Missing parameters'}));
|
||||
return;
|
||||
}
|
||||
|
||||
// Create price alarm
|
||||
const success = await PriceAlarmsService.updatePriceAlarm(alarm_id, user.user_id, defined_price);
|
||||
|
||||
if (success) {
|
||||
res.status(201).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.'}));
|
||||
}
|
||||
});
|
|
@ -1,106 +0,0 @@
|
|||
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
|
||||
* @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 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 {
|
||||
if (conn) {
|
||||
conn.end();
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Updates the given price alarm with the given fields
|
||||
* @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
|
||||
* @param defined_price The defined price for the price alarm
|
||||
*/
|
||||
export const updatePriceAlarm = async (alarm_id: number, user_id: number, defined_price: number): Promise<Boolean> => {
|
||||
let conn;
|
||||
try {
|
||||
conn = await pool.getConnection();
|
||||
const res = await conn.query('UPDATE price_alarms SET defined_price = ? WHERE alarm_id = ? AND user_id = ?', [defined_price, alarm_id, user_id]);
|
||||
|
||||
if (res.affectedRows === 1) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} catch (err) {
|
||||
throw err;
|
||||
} finally {
|
||||
if (conn) {
|
||||
conn.end();
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
|
@ -4,7 +4,7 @@
|
|||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>xyz.betterzon</groupId>
|
||||
<groupId>de.taskhub</groupId>
|
||||
<artifactId>CucumberTests</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<properties>
|
||||
|
|
Loading…
Reference in New Issue
Block a user