BETTERZON-38: Added all currently required models with required service classes and routers

This commit is contained in:
2020-11-29 13:05:25 +01:00
parent 3de28d8835
commit 0f2e2be246
22 changed files with 1255 additions and 221 deletions
@@ -0,0 +1,14 @@
export interface Product {
product_id: number;
asin: string;
is_active: boolean;
name: string;
short_description: string;
long_description: string;
image_guid: string;
date_added: Date;
last_modified: Date;
manufacturer_id: number;
selling_rank: string;
category_id: number;
}
@@ -0,0 +1,5 @@
import {Product} from './product.interface';
export interface Products {
[key: number]: Product;
}
@@ -0,0 +1,112 @@
/**
* Required External Modules and Interfaces
*/
import express, {Request, Response} from 'express';
import * as ProductService from './products.service';
import {Product} from './product.interface';
import {Products} from './products.interface';
/**
* Router Definition
*/
export const productsRouter = express.Router();
/**
* Controller Definitions
*/
// GET items/
productsRouter.get('/', async (req: Request, res: Response) => {
try {
const products: Products = await ProductService.findAll();
res.status(200).send(products);
} catch (e) {
res.status(404).send(e.message);
}
});
// GET items/:id
productsRouter.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 product: Product = await ProductService.find(id);
res.status(200).send(product);
} catch (e) {
res.status(404).send(e.message);
}
});
// GET items/:name
productsRouter.get('/search/:term', async (req: Request, res: Response) => {
const term: string = req.params.term;
if (!term) {
res.status(400).send('Missing parameters.');
return;
}
try {
const products: Products = await ProductService.findBySearchTerm(term);
res.status(200).send(products);
} catch (e) {
res.status(404).send(e.message);
}
});
// POST items/
// productsRouter.post('/', async (req: Request, res: Response) => {
// try {
// const product: Product = req.body.product;
//
// await ProductService.create(product);
//
// res.sendStatus(201);
// } catch (e) {
// res.status(404).send(e.message);
// }
// });
//
// // PUT items/
//
// productsRouter.put('/', async (req: Request, res: Response) => {
// try {
// const product: Product = req.body.product;
//
// await ProductService.update(product);
//
// res.sendStatus(200);
// } catch (e) {
// res.status(500).send(e.message);
// }
// });
//
// // DELETE items/:id
//
// productsRouter.delete('/:id', async (req: Request, res: Response) => {
// try {
// const id: number = parseInt(req.params.id, 10);
// await ProductService.remove(id);
//
// res.sendStatus(200);
// } catch (e) {
// res.status(500).send(e.message);
// }
// });
@@ -0,0 +1,147 @@
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 {Product} from './product.interface';
import {Products} from './products.interface';
/**
* Service Methods
*/
export const findAll = async (): Promise<Products> => {
let conn;
let prodRows = [];
try {
conn = await pool.getConnection();
const rows = await conn.query('SELECT product_id, name, asin FROM products');
for (let row in rows) {
if (row !== 'meta') {
let prod: Product = {
asin: '',
category_id: 0,
date_added: new Date(),
image_guid: '',
is_active: false,
last_modified: new Date(),
long_description: '',
manufacturer_id: 0,
name: '',
product_id: 0,
selling_rank: '',
short_description: ''
};
const sqlProd = rows[row];
prod.product_id = sqlProd.product_id;
prod.name = sqlProd.name;
prod.asin = sqlProd.asin;
prodRows.push(prod);
}
}
} catch (err) {
throw err;
} finally {
if (conn) {
conn.end();
}
}
return prodRows;
};
export const find = async (id: number): Promise<Product> => {
let conn;
let prod: any;
try {
conn = await pool.getConnection();
const rows = await conn.query('SELECT product_id, name FROM products WHERE product_id = ?', id);
for (let row in rows) {
if (row !== 'meta') {
prod = rows[row];
}
}
} catch (err) {
throw err;
} finally {
if (conn) {
conn.end();
}
}
return prod;
};
export const findBySearchTerm = async (term: string): Promise<Products> => {
let conn;
let prodRows = [];
try {
conn = await pool.getConnection();
term = '%' + term + '%';
const rows = await conn.query('SELECT product_id, name FROM products WHERE name LIKE ?', term);
for (let row in rows) {
if (row !== 'meta') {
prodRows.push(rows[row]);
}
}
} catch (err) {
console.log(err);
throw err;
} finally {
if (conn) {
conn.end();
}
}
return prodRows;
};
// export const create = async (newItem: Product): Promise<void> => {
// let conn;
// try {
// conn = await pool.getConnection();
// await conn.query("");
//
// } catch (err) {
// throw err;
// } finally {
// if (conn) conn.end();
// }
// };
//
// export const update = async (updatedItem: Product): Promise<void> => {
// if (models.products[updatedItem.product_id]) {
// models.products[updatedItem.product_id] = updatedItem;
// return;
// }
//
// throw new Error("No record found to update");
// };
//
// export const remove = async (id: number): Promise<void> => {
// const record: Product = models.products[id];
//
// if (record) {
// delete models.products[id];
// return;
// }
//
// throw new Error("No record found to delete");
// };