mirror of
https://github.com/Mueller-Patrick/Betterzon.git
synced 2025-05-12 09:49:18 +00:00
103 lines
2.8 KiB
TypeScript
103 lines
2.8 KiB
TypeScript
/**
|
|
* Required External Modules and Interfaces
|
|
*/
|
|
|
|
import express, {Request, Response} from 'express';
|
|
import * as PriceService from './prices.service';
|
|
import {Price} from './price.interface';
|
|
import {Prices} from './prices.interface';
|
|
|
|
|
|
/**
|
|
* Router Definition
|
|
*/
|
|
|
|
export const pricesRouter = express.Router();
|
|
|
|
|
|
/**
|
|
* Controller Definitions
|
|
*/
|
|
|
|
// GET prices/
|
|
pricesRouter.get('/', async (req: Request, res: Response) => {
|
|
try {
|
|
let prices: Prices = [];
|
|
const product = req.query.product;
|
|
const vendor = req.query.vendor;
|
|
const type = req.query.type;
|
|
|
|
if (product) {
|
|
if (vendor) {
|
|
prices = await PriceService.findByVendor(<string> product, <string> vendor, <string> type);
|
|
} else {
|
|
prices = await PriceService.findByType(<string> product, <string> type);
|
|
}
|
|
} else {
|
|
prices = await PriceService.findAll();
|
|
}
|
|
|
|
res.status(200).send(prices);
|
|
} catch (e) {
|
|
console.log('Error handling a request: ' + e.message);
|
|
res.status(500).send(JSON.stringify({"message": "Internal Server Error. Try again later."}));
|
|
}
|
|
});
|
|
|
|
// GET prices/:id
|
|
pricesRouter.get('/:id', async (req: Request, res: Response) => {
|
|
const id: number = parseInt(req.params.id, 10);
|
|
|
|
if (!id) {
|
|
res.status(400).send('Missing parameters.');
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const price: Price = await PriceService.find(id);
|
|
|
|
res.status(200).send(price);
|
|
} catch (e) {
|
|
console.log('Error handling a request: ' + e.message);
|
|
res.status(500).send(JSON.stringify({"message": "Internal Server Error. Try again later."}));
|
|
}
|
|
});
|
|
|
|
// GET prices/bestDeals
|
|
pricesRouter.get('/bestDeals/:amount', async (req: Request, res: Response) => {
|
|
const amount: number = parseInt(req.params.amount, 10);
|
|
|
|
if (!amount) {
|
|
res.status(400).send('Missing parameters.');
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const prices: Prices = await PriceService.getBestDeals(amount);
|
|
|
|
res.status(200).send(prices);
|
|
} catch (e) {
|
|
console.log('Error handling a request: ' + e.message);
|
|
res.status(500).send(JSON.stringify({"message": "Internal Server Error. Try again later."}));
|
|
}
|
|
});
|
|
|
|
// GET prices/byProduct/list/[]
|
|
pricesRouter.get('/byProduct/list/:ids', async (req: Request, res: Response) => {
|
|
const productIds: [number] = JSON.parse(req.params.ids);
|
|
|
|
if (!productIds) {
|
|
res.status(400).send('Missing parameters.');
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const prices: Prices = await PriceService.findListByProducts(productIds);
|
|
|
|
res.status(200).send(prices);
|
|
} catch (e) {
|
|
console.log('Error handling a request: ' + e.message);
|
|
res.status(500).send(JSON.stringify({"message": "Internal Server Error. Try again later."}));
|
|
}
|
|
});
|