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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 124 additions and 2 deletions

View File

@ -14,6 +14,7 @@ import {vendorsRouter} from './models/vendors/vendors.router';
import {errorHandler} from './middleware/error.middleware'; import {errorHandler} from './middleware/error.middleware';
import {notFoundHandler} from './middleware/notFound.middleware'; import {notFoundHandler} from './middleware/notFound.middleware';
import {usersRouter} from './models/users/users.router'; import {usersRouter} from './models/users/users.router';
import {pricealarmsRouter} from './models/pricealarms/pricealarms.router';
dotenv.config(); dotenv.config();
@ -44,6 +45,7 @@ app.use('/manufacturers', manufacturersRouter);
app.use('/prices', pricesRouter); app.use('/prices', pricesRouter);
app.use('/users', usersRouter); app.use('/users', usersRouter);
app.use('/vendors', vendorsRouter); app.use('/vendors', vendorsRouter);
app.use('/pricealarms', pricealarmsRouter);
app.use(errorHandler); app.use(errorHandler);
app.use(notFoundHandler); app.use(notFoundHandler);

View File

@ -0,0 +1,6 @@
export interface PriceAlarm {
alarm_id: number;
user_id: number;
product_id: number;
defined_price: number;
}

View File

@ -0,0 +1,5 @@
import {PriceAlarm} from './pricealarm.interface';
export interface PriceAlarms {
[key: number]: PriceAlarm;
}

View File

@ -0,0 +1,62 @@
/**
* 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
*/
// 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(200).send(JSON.stringify({success: true}));
return;
} else {
res.status(400).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."}));
}
});

View File

@ -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;
};

View File

@ -4,7 +4,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 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> <modelVersion>4.0.0</modelVersion>
<groupId>de.taskhub</groupId> <groupId>xyz.betterzon</groupId>
<artifactId>CucumberTests</artifactId> <artifactId>CucumberTests</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
<properties> <properties>
@ -30,4 +30,4 @@
<version>3.8.1</version> <version>3.8.1</version>
</dependency> </dependency>
</dependencies> </dependencies>
</project> </project>