From 0f2e2be246e7e6db7351c1c1d64aa2fd655861f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20M=C3=BCller?= Date: Sun, 29 Nov 2020 13:05:25 +0100 Subject: [PATCH] BETTERZON-38: Added all currently required models with required service classes and routers --- Backend/src/index.ts | 24 ++- .../models/categories/categories.interface.ts | 5 + .../models/categories/categories.router.ts | 112 ++++++++++++ .../models/categories/categories.service.ts | 135 ++++++++++++++ .../models/categories/category.interface.ts | 4 + .../manufacturers/manufacturer.interface.ts | 4 + .../manufacturers/manufacturers.interface.ts | 5 + .../manufacturers/manufacturers.router.ts | 112 ++++++++++++ .../manufacturers/manufacturers.service.ts | 135 ++++++++++++++ Backend/src/models/prices/price.interface.ts | 7 + Backend/src/models/prices/prices.interface.ts | 5 + Backend/src/models/prices/prices.router.ts | 112 ++++++++++++ Backend/src/models/prices/prices.service.ts | 140 ++++++++++++++ .../products/product.interface.ts | 0 .../products/products.interface.ts | 3 +- .../{ => models}/products/products.router.ts | 80 ++++---- .../src/models/products/products.service.ts | 147 +++++++++++++++ .../src/models/vendors/vendor.interface.ts | 10 + .../src/models/vendors/vendors.interface.ts | 5 + Backend/src/models/vendors/vendors.router.ts | 112 ++++++++++++ Backend/src/models/vendors/vendors.service.ts | 147 +++++++++++++++ Backend/src/products/products.service.ts | 172 ------------------ 22 files changed, 1255 insertions(+), 221 deletions(-) create mode 100644 Backend/src/models/categories/categories.interface.ts create mode 100644 Backend/src/models/categories/categories.router.ts create mode 100644 Backend/src/models/categories/categories.service.ts create mode 100644 Backend/src/models/categories/category.interface.ts create mode 100644 Backend/src/models/manufacturers/manufacturer.interface.ts create mode 100644 Backend/src/models/manufacturers/manufacturers.interface.ts create mode 100644 Backend/src/models/manufacturers/manufacturers.router.ts create mode 100644 Backend/src/models/manufacturers/manufacturers.service.ts create mode 100644 Backend/src/models/prices/price.interface.ts create mode 100644 Backend/src/models/prices/prices.interface.ts create mode 100644 Backend/src/models/prices/prices.router.ts create mode 100644 Backend/src/models/prices/prices.service.ts rename Backend/src/{ => models}/products/product.interface.ts (100%) rename Backend/src/{ => models}/products/products.interface.ts (55%) rename Backend/src/{ => models}/products/products.router.ts (59%) create mode 100644 Backend/src/models/products/products.service.ts create mode 100644 Backend/src/models/vendors/vendor.interface.ts create mode 100644 Backend/src/models/vendors/vendors.interface.ts create mode 100644 Backend/src/models/vendors/vendors.router.ts create mode 100644 Backend/src/models/vendors/vendors.service.ts delete mode 100644 Backend/src/products/products.service.ts diff --git a/Backend/src/index.ts b/Backend/src/index.ts index 5de3790..81a00ce 100644 --- a/Backend/src/index.ts +++ b/Backend/src/index.ts @@ -2,13 +2,17 @@ * Required External Modules */ -import * as dotenv from "dotenv"; -import express from "express"; -import cors from "cors"; -import helmet from "helmet"; -import { productsRouter } from "./products/products.router"; -import { errorHandler } from "./middleware/error.middleware"; -import {notFoundHandler} from "./middleware/notFound.middleware"; +import * as dotenv from 'dotenv'; +import express from 'express'; +import cors from 'cors'; +import helmet from 'helmet'; +import {productsRouter} from './models/products/products.router'; +import {categoriesRouter} from './models/categories/categories.router'; +import {manufacturersRouter} from './models/manufacturers/manufacturers.router'; +import {pricesRouter} from './models/prices/prices.router'; +import {vendorsRouter} from './models/vendors/vendors.router'; +import {errorHandler} from './middleware/error.middleware'; +import {notFoundHandler} from './middleware/notFound.middleware'; dotenv.config(); @@ -33,7 +37,11 @@ const app = express(); app.use(helmet()); app.use(cors()); app.use(express.json()); -app.use("/products", productsRouter); +app.use('/products', productsRouter); +app.use('/categories', categoriesRouter); +app.use('/manufacturers', manufacturersRouter); +app.use('/prices', pricesRouter); +app.use('/vendors', vendorsRouter); app.use(errorHandler); app.use(notFoundHandler); diff --git a/Backend/src/models/categories/categories.interface.ts b/Backend/src/models/categories/categories.interface.ts new file mode 100644 index 0000000..136ee49 --- /dev/null +++ b/Backend/src/models/categories/categories.interface.ts @@ -0,0 +1,5 @@ +import {Category} from './category.interface'; + +export interface Categories { + [key: number]: Category; +} diff --git a/Backend/src/models/categories/categories.router.ts b/Backend/src/models/categories/categories.router.ts new file mode 100644 index 0000000..409ca71 --- /dev/null +++ b/Backend/src/models/categories/categories.router.ts @@ -0,0 +1,112 @@ +/** + * Required External Modules and Interfaces + */ + +import express, {Request, Response} from 'express'; +import * as CategoryService from './categories.service'; +import {Category} from './category.interface'; +import {Categories} from './categories.interface'; + + +/** + * Router Definition + */ + +export const categoriesRouter = express.Router(); + + +/** + * Controller Definitions + */ + +// GET items/ + +categoriesRouter.get('/', async (req: Request, res: Response) => { + try { + const categories: Categories = await CategoryService.findAll(); + + res.status(200).send(categories); + } catch (e) { + res.status(404).send(e.message); + } +}); + +// GET items/:id + +categoriesRouter.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 category: Category = await CategoryService.find(id); + + res.status(200).send(category); + } catch (e) { + res.status(404).send(e.message); + } +}); + +// GET items/:name + +categoriesRouter.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 categories: Categories = await CategoryService.findBySearchTerm(term); + + res.status(200).send(categories); + } catch (e) { + res.status(404).send(e.message); + } +}); + + +// POST items/ + +// categoriesRouter.post('/', async (req: Request, res: Response) => { +// try { +// const category: Category = req.body.category; +// +// await CategoryService.create(category); +// +// res.sendStatus(201); +// } catch (e) { +// res.status(404).send(e.message); +// } +// }); +// +// // PUT items/ +// +// categoriesRouter.put('/', async (req: Request, res: Response) => { +// try { +// const category: Category = req.body.category; +// +// await CategoryService.update(category); +// +// res.sendStatus(200); +// } catch (e) { +// res.status(500).send(e.message); +// } +// }); +// +// // DELETE items/:id +// +// categoriesRouter.delete('/:id', async (req: Request, res: Response) => { +// try { +// const id: number = parseInt(req.params.id, 10); +// await CategoryService.remove(id); +// +// res.sendStatus(200); +// } catch (e) { +// res.status(500).send(e.message); +// } +// }); diff --git a/Backend/src/models/categories/categories.service.ts b/Backend/src/models/categories/categories.service.ts new file mode 100644 index 0000000..814d6cf --- /dev/null +++ b/Backend/src/models/categories/categories.service.ts @@ -0,0 +1,135 @@ +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 {Category} from './category.interface'; +import {Categories} from './categories.interface'; + + +/** + * Service Methods + */ + +export const findAll = async (): Promise => { + let conn; + let categRows = []; + try { + conn = await pool.getConnection(); + const rows = await conn.query('SELECT category_id, name FROM categories'); + for (let row in rows) { + if (row !== 'meta') { + let categ: Category = { + category_id: 0, + name: '' + }; + const sqlCateg = rows[row]; + + categ.category_id = sqlCateg.category_id; + categ.name = sqlCateg.name; + categRows.push(categ); + } + } + + } catch (err) { + throw err; + } finally { + if (conn) { + conn.end(); + } + } + + return categRows; +}; + +export const find = async (id: number): Promise => { + let conn; + let categ: any; + try { + conn = await pool.getConnection(); + const rows = await conn.query('SELECT category_id, name FROM categories WHERE category_id = ?', id); + for (let row in rows) { + if (row !== 'meta') { + categ = rows[row]; + } + } + + } catch (err) { + throw err; + } finally { + if (conn) { + conn.end(); + } + } + + return categ; +}; + +export const findBySearchTerm = async (term: string): Promise => { + let conn; + let categRows = []; + try { + conn = await pool.getConnection(); + term = '%' + term + '%'; + const rows = await conn.query('SELECT category_id, name FROM categories WHERE name LIKE ?', term); + for (let row in rows) { + if (row !== 'meta') { + categRows.push(rows[row]); + } + } + + } catch (err) { + throw err; + } finally { + if (conn) { + conn.end(); + } + } + + return categRows; +}; + +// export const create = async (newItem: Product): Promise => { +// 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 => { +// 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 => { +// const record: Product = models.products[id]; +// +// if (record) { +// delete models.products[id]; +// return; +// } +// +// throw new Error("No record found to delete"); +// }; diff --git a/Backend/src/models/categories/category.interface.ts b/Backend/src/models/categories/category.interface.ts new file mode 100644 index 0000000..a909167 --- /dev/null +++ b/Backend/src/models/categories/category.interface.ts @@ -0,0 +1,4 @@ +export interface Category { + category_id: number; + name: string; +} diff --git a/Backend/src/models/manufacturers/manufacturer.interface.ts b/Backend/src/models/manufacturers/manufacturer.interface.ts new file mode 100644 index 0000000..08e8ea8 --- /dev/null +++ b/Backend/src/models/manufacturers/manufacturer.interface.ts @@ -0,0 +1,4 @@ +export interface Manufacturer { + manufacturer_id: number; + name: string; +} diff --git a/Backend/src/models/manufacturers/manufacturers.interface.ts b/Backend/src/models/manufacturers/manufacturers.interface.ts new file mode 100644 index 0000000..94836e2 --- /dev/null +++ b/Backend/src/models/manufacturers/manufacturers.interface.ts @@ -0,0 +1,5 @@ +import {Manufacturer} from './manufacturer.interface'; + +export interface Manufacturers { + [key: number]: Manufacturer; +} diff --git a/Backend/src/models/manufacturers/manufacturers.router.ts b/Backend/src/models/manufacturers/manufacturers.router.ts new file mode 100644 index 0000000..e791fa4 --- /dev/null +++ b/Backend/src/models/manufacturers/manufacturers.router.ts @@ -0,0 +1,112 @@ +/** + * Required External Modules and Interfaces + */ + +import express, {Request, Response} from 'express'; +import * as ManufacturerService from './manufacturers.service'; +import {Manufacturer} from './manufacturer.interface'; +import {Manufacturers} from './manufacturers.interface'; + + +/** + * Router Definition + */ + +export const manufacturersRouter = express.Router(); + + +/** + * Controller Definitions + */ + +// GET items/ + +manufacturersRouter.get('/', async (req: Request, res: Response) => { + try { + const manufacturers: Manufacturers = await ManufacturerService.findAll(); + + res.status(200).send(manufacturers); + } catch (e) { + res.status(404).send(e.message); + } +}); + +// GET items/:id + +manufacturersRouter.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 manufacturer: Manufacturer = await ManufacturerService.find(id); + + res.status(200).send(manufacturer); + } catch (e) { + res.status(404).send(e.message); + } +}); + +// GET items/:name + +manufacturersRouter.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 manufacturer: Manufacturers = await ManufacturerService.findBySearchTerm(term); + + res.status(200).send(manufacturer); + } catch (e) { + res.status(404).send(e.message); + } +}); + + +// POST items/ + +// manufacturersRouter.post('/', async (req: Request, res: Response) => { +// try { +// const category: Category = req.body.category; +// +// await CategoryService.create(category); +// +// res.sendStatus(201); +// } catch (e) { +// res.status(404).send(e.message); +// } +// }); +// +// // PUT items/ +// +// manufacturersRouter.put('/', async (req: Request, res: Response) => { +// try { +// const category: Category = req.body.category; +// +// await CategoryService.update(category); +// +// res.sendStatus(200); +// } catch (e) { +// res.status(500).send(e.message); +// } +// }); +// +// // DELETE items/:id +// +// manufacturersRouter.delete('/:id', async (req: Request, res: Response) => { +// try { +// const id: number = parseInt(req.params.id, 10); +// await CategoryService.remove(id); +// +// res.sendStatus(200); +// } catch (e) { +// res.status(500).send(e.message); +// } +// }); diff --git a/Backend/src/models/manufacturers/manufacturers.service.ts b/Backend/src/models/manufacturers/manufacturers.service.ts new file mode 100644 index 0000000..a6f4c1d --- /dev/null +++ b/Backend/src/models/manufacturers/manufacturers.service.ts @@ -0,0 +1,135 @@ +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 {Manufacturer} from './manufacturer.interface'; +import {Manufacturers} from './manufacturers.interface'; + + +/** + * Service Methods + */ + +export const findAll = async (): Promise => { + let conn; + let manRows = []; + try { + conn = await pool.getConnection(); + const rows = await conn.query('SELECT manufacturer_id, name FROM manufacturers'); + for (let row in rows) { + if (row !== 'meta') { + let man: Manufacturer = { + manufacturer_id: 0, + name: '' + }; + const sqlMan = rows[row]; + + man.manufacturer_id = sqlMan.manufacturer_id; + man.name = sqlMan.name; + manRows.push(man); + } + } + + } catch (err) { + throw err; + } finally { + if (conn) { + conn.end(); + } + } + + return manRows; +}; + +export const find = async (id: number): Promise => { + let conn; + let man: any; + try { + conn = await pool.getConnection(); + const rows = await conn.query('SELECT manufacturer_id, name FROM manufacturers WHERE manufacturer_id = ?', id); + for (let row in rows) { + if (row !== 'meta') { + man = rows[row]; + } + } + + } catch (err) { + throw err; + } finally { + if (conn) { + conn.end(); + } + } + + return man; +}; + +export const findBySearchTerm = async (term: string): Promise => { + let conn; + let manRows = []; + try { + conn = await pool.getConnection(); + term = '%' + term + '%'; + const rows = await conn.query('SELECT manufacturer_id, name FROM manufacturers WHERE name LIKE ?', term); + for (let row in rows) { + if (row !== 'meta') { + manRows.push(rows[row]); + } + } + + } catch (err) { + throw err; + } finally { + if (conn) { + conn.end(); + } + } + + return manRows; +}; + +// export const create = async (newItem: Product): Promise => { +// 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 => { +// 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 => { +// const record: Product = models.products[id]; +// +// if (record) { +// delete models.products[id]; +// return; +// } +// +// throw new Error("No record found to delete"); +// }; diff --git a/Backend/src/models/prices/price.interface.ts b/Backend/src/models/prices/price.interface.ts new file mode 100644 index 0000000..49030e8 --- /dev/null +++ b/Backend/src/models/prices/price.interface.ts @@ -0,0 +1,7 @@ +export interface Price { + price_id: number; + product_id: number; + vendor_id: number; + price_in_cents: number; + timestamp: Date; +} diff --git a/Backend/src/models/prices/prices.interface.ts b/Backend/src/models/prices/prices.interface.ts new file mode 100644 index 0000000..9469832 --- /dev/null +++ b/Backend/src/models/prices/prices.interface.ts @@ -0,0 +1,5 @@ +import {Price} from './price.interface'; + +export interface Prices { + [key: number]: Price; +} diff --git a/Backend/src/models/prices/prices.router.ts b/Backend/src/models/prices/prices.router.ts new file mode 100644 index 0000000..976cc9f --- /dev/null +++ b/Backend/src/models/prices/prices.router.ts @@ -0,0 +1,112 @@ +/** + * 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 items/ + +pricesRouter.get('/', async (req: Request, res: Response) => { + try { + const prices: Prices = await PriceService.findAll(); + + res.status(200).send(prices); + } catch (e) { + res.status(404).send(e.message); + } +}); + +// GET items/: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) { + res.status(404).send(e.message); + } +}); + +// GET items/:name + +pricesRouter.get('/products/: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 prices: Prices = await PriceService.findByProduct(id); + + res.status(200).send(prices); + } catch (e) { + res.status(404).send(e.message); + } +}); + + +// POST items/ + +// pricesRouter.post('/', async (req: Request, res: Response) => { +// try { +// const category: Category = req.body.category; +// +// await CategoryService.create(category); +// +// res.sendStatus(201); +// } catch (e) { +// res.status(404).send(e.message); +// } +// }); +// +// // PUT items/ +// +// pricesRouter.put('/', async (req: Request, res: Response) => { +// try { +// const category: Category = req.body.category; +// +// await CategoryService.update(category); +// +// res.sendStatus(200); +// } catch (e) { +// res.status(500).send(e.message); +// } +// }); +// +// // DELETE items/:id +// +// pricesRouter.delete('/:id', async (req: Request, res: Response) => { +// try { +// const id: number = parseInt(req.params.id, 10); +// await CategoryService.remove(id); +// +// res.sendStatus(200); +// } catch (e) { +// res.status(500).send(e.message); +// } +// }); diff --git a/Backend/src/models/prices/prices.service.ts b/Backend/src/models/prices/prices.service.ts new file mode 100644 index 0000000..81915e7 --- /dev/null +++ b/Backend/src/models/prices/prices.service.ts @@ -0,0 +1,140 @@ +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 {Price} from './price.interface'; +import {Prices} from './prices.interface'; + + +/** + * Service Methods + */ + +export const findAll = async (): Promise => { + let conn; + let priceRows = []; + try { + conn = await pool.getConnection(); + const rows = await conn.query('SELECT price_id, product_id, vendor_id, price_in_cents, timestamp FROM prices'); + for (let row in rows) { + if (row !== 'meta') { + let price: Price = { + price_id: 0, + price_in_cents: 0, + product_id: 0, + timestamp: new Date(), + vendor_id: 0 + }; + const sqlPrice = rows[row]; + + price.price_id = sqlPrice.price_id; + price.product_id = sqlPrice.product_id; + price.vendor_id = sqlPrice.vendor_id; + price.price_in_cents = sqlPrice.price_in_cents; + price.timestamp = sqlPrice.timestamp; + priceRows.push(price); + } + } + + } catch (err) { + throw err; + } finally { + if (conn) { + conn.end(); + } + } + + return priceRows; +}; + +export const find = async (id: number): Promise => { + let conn; + let price: any; + try { + conn = await pool.getConnection(); + const rows = await conn.query('SELECT price_id, product_id, vendor_id, price_in_cents, timestamp FROM prices WHERE price_id = ?', id); + for (let row in rows) { + if (row !== 'meta') { + price = rows[row]; + } + } + + } catch (err) { + throw err; + } finally { + if (conn) { + conn.end(); + } + } + + return price; +}; + +export const findByProduct = async (product: number): Promise => { + let conn; + let priceRows = []; + try { + conn = await pool.getConnection(); + const rows = await conn.query('SELECT price_id, product_id, vendor_id, price_in_cents, timestamp FROM prices WHERE product_id = ?', product); + for (let row in rows) { + if (row !== 'meta') { + priceRows.push(rows[row]); + } + } + + } catch (err) { + throw err; + } finally { + if (conn) { + conn.end(); + } + } + + return priceRows; +}; + +// export const create = async (newItem: Product): Promise => { +// 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 => { +// 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 => { +// const record: Product = models.products[id]; +// +// if (record) { +// delete models.products[id]; +// return; +// } +// +// throw new Error("No record found to delete"); +// }; diff --git a/Backend/src/products/product.interface.ts b/Backend/src/models/products/product.interface.ts similarity index 100% rename from Backend/src/products/product.interface.ts rename to Backend/src/models/products/product.interface.ts diff --git a/Backend/src/products/products.interface.ts b/Backend/src/models/products/products.interface.ts similarity index 55% rename from Backend/src/products/products.interface.ts rename to Backend/src/models/products/products.interface.ts index 7cdab1e..00b5e36 100644 --- a/Backend/src/products/products.interface.ts +++ b/Backend/src/models/products/products.interface.ts @@ -1,4 +1,5 @@ -import { Product } from "./product.interface"; +import {Product} from './product.interface'; + export interface Products { [key: number]: Product; } diff --git a/Backend/src/products/products.router.ts b/Backend/src/models/products/products.router.ts similarity index 59% rename from Backend/src/products/products.router.ts rename to Backend/src/models/products/products.router.ts index 8501683..0c7cc5c 100644 --- a/Backend/src/products/products.router.ts +++ b/Backend/src/models/products/products.router.ts @@ -36,7 +36,7 @@ productsRouter.get('/', async (req: Request, res: Response) => { productsRouter.get('/:id', async (req: Request, res: Response) => { const id: number = parseInt(req.params.id, 10); - if(!id){ + if (!id) { res.status(400).send('Missing parameters.'); return; } @@ -55,7 +55,7 @@ productsRouter.get('/:id', async (req: Request, res: Response) => { productsRouter.get('/search/:term', async (req: Request, res: Response) => { const term: string = req.params.term; - if(!term){ + if (!term) { res.status(400).send('Missing parameters.'); return; } @@ -72,41 +72,41 @@ productsRouter.get('/search/:term', async (req: Request, res: Response) => { // 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); - } -}); +// 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); +// } +// }); diff --git a/Backend/src/models/products/products.service.ts b/Backend/src/models/products/products.service.ts new file mode 100644 index 0000000..237fea7 --- /dev/null +++ b/Backend/src/models/products/products.service.ts @@ -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 => { + 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 => { + 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 => { + 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 => { +// 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 => { +// 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 => { +// const record: Product = models.products[id]; +// +// if (record) { +// delete models.products[id]; +// return; +// } +// +// throw new Error("No record found to delete"); +// }; diff --git a/Backend/src/models/vendors/vendor.interface.ts b/Backend/src/models/vendors/vendor.interface.ts new file mode 100644 index 0000000..51afc49 --- /dev/null +++ b/Backend/src/models/vendors/vendor.interface.ts @@ -0,0 +1,10 @@ +export interface Vendor { + vendor_id: number; + name: string; + streetname: string; + zip_code: string; + city: string; + country_code: string; + phone: string; + website: string; +} diff --git a/Backend/src/models/vendors/vendors.interface.ts b/Backend/src/models/vendors/vendors.interface.ts new file mode 100644 index 0000000..3d460bf --- /dev/null +++ b/Backend/src/models/vendors/vendors.interface.ts @@ -0,0 +1,5 @@ +import {Vendor} from './vendor.interface'; + +export interface Vendors { + [key: number]: Vendor; +} diff --git a/Backend/src/models/vendors/vendors.router.ts b/Backend/src/models/vendors/vendors.router.ts new file mode 100644 index 0000000..3876c2b --- /dev/null +++ b/Backend/src/models/vendors/vendors.router.ts @@ -0,0 +1,112 @@ +/** + * Required External Modules and Interfaces + */ + +import express, {Request, Response} from 'express'; +import * as VendorService from './vendors.service'; +import {Vendor} from './vendor.interface'; +import {Vendors} from './vendors.interface'; + + +/** + * Router Definition + */ + +export const vendorsRouter = express.Router(); + + +/** + * Controller Definitions + */ + +// GET items/ + +vendorsRouter.get('/', async (req: Request, res: Response) => { + try { + const vendors: Vendors = await VendorService.findAll(); + + res.status(200).send(vendors); + } catch (e) { + res.status(404).send(e.message); + } +}); + +// GET items/:id + +vendorsRouter.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 vendor: Vendor = await VendorService.find(id); + + res.status(200).send(vendor); + } catch (e) { + res.status(404).send(e.message); + } +}); + +// GET items/:name + +vendorsRouter.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 vendors: Vendors = await VendorService.findBySearchTerm(term); + + res.status(200).send(vendors); + } catch (e) { + res.status(404).send(e.message); + } +}); + + +// POST items/ + +// vendorsRouter.post('/', async (req: Request, res: Response) => { +// try { +// const category: Category = req.body.category; +// +// await CategoryService.create(category); +// +// res.sendStatus(201); +// } catch (e) { +// res.status(404).send(e.message); +// } +// }); +// +// // PUT items/ +// +// vendorsRouter.put('/', async (req: Request, res: Response) => { +// try { +// const category: Category = req.body.category; +// +// await CategoryService.update(category); +// +// res.sendStatus(200); +// } catch (e) { +// res.status(500).send(e.message); +// } +// }); +// +// // DELETE items/:id +// +// vendorsRouter.delete('/:id', async (req: Request, res: Response) => { +// try { +// const id: number = parseInt(req.params.id, 10); +// await CategoryService.remove(id); +// +// res.sendStatus(200); +// } catch (e) { +// res.status(500).send(e.message); +// } +// }); diff --git a/Backend/src/models/vendors/vendors.service.ts b/Backend/src/models/vendors/vendors.service.ts new file mode 100644 index 0000000..70c0ef2 --- /dev/null +++ b/Backend/src/models/vendors/vendors.service.ts @@ -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 {Vendor} from './vendor.interface'; +import {Vendors} from './vendors.interface'; + + +/** + * Service Methods + */ + +export const findAll = async (): Promise => { + let conn; + let vendorRows = []; + try { + conn = await pool.getConnection(); + const rows = await conn.query('SELECT vendor_id, name, streetname, zip_code, city, country_code, phone, website FROM vendors'); + for (let row in rows) { + if (row !== 'meta') { + let vendor: Vendor = { + city: '', + country_code: '', + name: '', + phone: '', + streetname: '', + vendor_id: 0, + website: '', + zip_code: '' + }; + const sqlVendor = rows[row]; + + vendor.vendor_id = sqlVendor.vendor_id; + vendor.name = sqlVendor.name; + vendor.streetname = sqlVendor.streetname; + vendor.zip_code = sqlVendor.zip_code; + vendor.city = sqlVendor.city; + vendor.country_code = sqlVendor.country_code; + vendor.phone = sqlVendor.phone; + vendor.website = sqlVendor.website; + vendorRows.push(vendor); + } + } + + } catch (err) { + throw err; + } finally { + if (conn) { + conn.end(); + } + } + + return vendorRows; +}; + +export const find = async (id: number): Promise => { + let conn; + let vendor: any; + try { + conn = await pool.getConnection(); + const rows = await conn.query('SELECT vendor_id, name, streetname, zip_code, city, country_code, phone, website FROM vendors WHERE vendor_id = ?', id); + for (let row in rows) { + if (row !== 'meta') { + vendor = rows[row]; + } + } + + } catch (err) { + throw err; + } finally { + if (conn) { + conn.end(); + } + } + + return vendor; +}; + +export const findBySearchTerm = async (term: string): Promise => { + let conn; + let vendorRows = []; + try { + conn = await pool.getConnection(); + term = '%' + term + '%'; + const rows = await conn.query('SELECT vendor_id, name, streetname, zip_code, city, country_code, phone, website FROM vendors WHERE name LIKE ?', term); + for (let row in rows) { + if (row !== 'meta') { + vendorRows.push(rows[row]); + } + } + + } catch (err) { + throw err; + } finally { + if (conn) { + conn.end(); + } + } + + return vendorRows; +}; + +// export const create = async (newItem: Product): Promise => { +// 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 => { +// 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 => { +// const record: Product = models.products[id]; +// +// if (record) { +// delete models.products[id]; +// return; +// } +// +// throw new Error("No record found to delete"); +// }; diff --git a/Backend/src/products/products.service.ts b/Backend/src/products/products.service.ts deleted file mode 100644 index 0e3d7e3..0000000 --- a/Backend/src/products/products.service.ts +++ /dev/null @@ -1,172 +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 { Product } from "./product.interface"; -import { Products } from "./products.interface"; - - -/** - * In-Memory Store - */ - -const products: Products = { - 1: { - product_id: 1, - asin: "", - is_active: true, - name: "Burger", - short_description: "", - long_description: "", - image_guid: "", - date_added: new Date(), - last_modified: new Date(), - manufacturer_id: 1, - selling_rank: "", - category_id: 1 - }, - 2: { - product_id: 2, - asin: "", - is_active: true, - name: "Pizza", - short_description: "", - long_description: "", - image_guid: "", - date_added: new Date(), - last_modified: new Date(), - manufacturer_id: 2, - selling_rank: "", - category_id: 1 - }, - 3: { - product_id: 3, - asin: "", - is_active: true, - name: "Tea", - short_description: "", - long_description: "", - image_guid: "", - date_added: new Date(), - last_modified: new Date(), - manufacturer_id: 3, - selling_rank: "", - category_id: 2 - } -}; - - -/** - * Service Methods - */ - -export const findAll = async (): Promise => { - let conn; - let prodRows = []; - try { - conn = await pool.getConnection(); - const rows = await conn.query("SELECT product_id, name FROM products"); - for(let row in rows){ - if(row !== 'meta'){ - prodRows.push(rows[row]); - } - } - - } catch (err) { - throw err; - } finally { - if (conn) conn.end(); - } - - return prodRows; -}; - -export const find = async (id: number): Promise => { - 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 => { - 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 => { - let conn; - try { - conn = await pool.getConnection(); - // TODO adjust query - await conn.query("SELECT product_id, name FROM products WHERE product_id = ?"); - - } catch (err) { - throw err; - } finally { - if (conn) conn.end(); - } -}; - -export const update = async (updatedItem: Product): Promise => { - if (products[updatedItem.product_id]) { - products[updatedItem.product_id] = updatedItem; - return; - } - - throw new Error("No record found to update"); -}; - -export const remove = async (id: number): Promise => { - const record: Product = products[id]; - - if (record) { - delete products[id]; - return; - } - - throw new Error("No record found to delete"); -};