mirror of
				https://github.com/Mueller-Patrick/Betterzon.git
				synced 2025-10-31 00:35:48 +00:00 
			
		
		
		
	BETTERZON-38: Added all currently required models with required service classes and routers
This commit is contained in:
		
							parent
							
								
									3de28d8835
								
							
						
					
					
						commit
						0f2e2be246
					
				|  | @ -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); | ||||
|  |  | |||
							
								
								
									
										5
									
								
								Backend/src/models/categories/categories.interface.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								Backend/src/models/categories/categories.interface.ts
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,5 @@ | |||
| import {Category} from './category.interface'; | ||||
| 
 | ||||
| export interface Categories { | ||||
|     [key: number]: Category; | ||||
| } | ||||
							
								
								
									
										112
									
								
								Backend/src/models/categories/categories.router.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										112
									
								
								Backend/src/models/categories/categories.router.ts
									
									
									
									
									
										Normal file
									
								
							|  | @ -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);
 | ||||
| //     }
 | ||||
| // });
 | ||||
							
								
								
									
										135
									
								
								Backend/src/models/categories/categories.service.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										135
									
								
								Backend/src/models/categories/categories.service.ts
									
									
									
									
									
										Normal file
									
								
							|  | @ -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<Categories> => { | ||||
|     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<Category> => { | ||||
|     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<Categories> => { | ||||
|     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<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");
 | ||||
| // };
 | ||||
							
								
								
									
										4
									
								
								Backend/src/models/categories/category.interface.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								Backend/src/models/categories/category.interface.ts
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,4 @@ | |||
| export interface Category { | ||||
|     category_id: number; | ||||
|     name: string; | ||||
| } | ||||
|  | @ -0,0 +1,4 @@ | |||
| export interface Manufacturer { | ||||
|     manufacturer_id: number; | ||||
|     name: string; | ||||
| } | ||||
|  | @ -0,0 +1,5 @@ | |||
| import {Manufacturer} from './manufacturer.interface'; | ||||
| 
 | ||||
| export interface Manufacturers { | ||||
|     [key: number]: Manufacturer; | ||||
| } | ||||
							
								
								
									
										112
									
								
								Backend/src/models/manufacturers/manufacturers.router.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										112
									
								
								Backend/src/models/manufacturers/manufacturers.router.ts
									
									
									
									
									
										Normal file
									
								
							|  | @ -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);
 | ||||
| //     }
 | ||||
| // });
 | ||||
							
								
								
									
										135
									
								
								Backend/src/models/manufacturers/manufacturers.service.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										135
									
								
								Backend/src/models/manufacturers/manufacturers.service.ts
									
									
									
									
									
										Normal file
									
								
							|  | @ -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<Manufacturers> => { | ||||
|     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<Manufacturer> => { | ||||
|     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<Manufacturers> => { | ||||
|     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<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");
 | ||||
| // };
 | ||||
							
								
								
									
										7
									
								
								Backend/src/models/prices/price.interface.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								Backend/src/models/prices/price.interface.ts
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,7 @@ | |||
| export interface Price { | ||||
|     price_id: number; | ||||
|     product_id: number; | ||||
|     vendor_id: number; | ||||
|     price_in_cents: number; | ||||
|     timestamp: Date; | ||||
| } | ||||
							
								
								
									
										5
									
								
								Backend/src/models/prices/prices.interface.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								Backend/src/models/prices/prices.interface.ts
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,5 @@ | |||
| import {Price} from './price.interface'; | ||||
| 
 | ||||
| export interface Prices { | ||||
|     [key: number]: Price; | ||||
| } | ||||
							
								
								
									
										112
									
								
								Backend/src/models/prices/prices.router.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										112
									
								
								Backend/src/models/prices/prices.router.ts
									
									
									
									
									
										Normal file
									
								
							|  | @ -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);
 | ||||
| //     }
 | ||||
| // });
 | ||||
							
								
								
									
										140
									
								
								Backend/src/models/prices/prices.service.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										140
									
								
								Backend/src/models/prices/prices.service.ts
									
									
									
									
									
										Normal file
									
								
							|  | @ -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<Prices> => { | ||||
|     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<Price> => { | ||||
|     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<Prices> => { | ||||
|     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<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");
 | ||||
| // };
 | ||||
|  | @ -1,4 +1,5 @@ | |||
| import { Product } from "./product.interface"; | ||||
| import {Product} from './product.interface'; | ||||
| 
 | ||||
| export interface Products { | ||||
|     [key: number]: Product; | ||||
| } | ||||
|  | @ -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);
 | ||||
| //     }
 | ||||
| // });
 | ||||
							
								
								
									
										147
									
								
								Backend/src/models/products/products.service.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										147
									
								
								Backend/src/models/products/products.service.ts
									
									
									
									
									
										Normal file
									
								
							|  | @ -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");
 | ||||
| // };
 | ||||
							
								
								
									
										10
									
								
								Backend/src/models/vendors/vendor.interface.ts
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								Backend/src/models/vendors/vendor.interface.ts
									
									
									
									
										vendored
									
									
										Normal file
									
								
							|  | @ -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; | ||||
| } | ||||
							
								
								
									
										5
									
								
								Backend/src/models/vendors/vendors.interface.ts
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								Backend/src/models/vendors/vendors.interface.ts
									
									
									
									
										vendored
									
									
										Normal file
									
								
							|  | @ -0,0 +1,5 @@ | |||
| import {Vendor} from './vendor.interface'; | ||||
| 
 | ||||
| export interface Vendors { | ||||
|     [key: number]: Vendor; | ||||
| } | ||||
							
								
								
									
										112
									
								
								Backend/src/models/vendors/vendors.router.ts
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										112
									
								
								Backend/src/models/vendors/vendors.router.ts
									
									
									
									
										vendored
									
									
										Normal file
									
								
							|  | @ -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);
 | ||||
| //     }
 | ||||
| // });
 | ||||
							
								
								
									
										147
									
								
								Backend/src/models/vendors/vendors.service.ts
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										147
									
								
								Backend/src/models/vendors/vendors.service.ts
									
									
									
									
										vendored
									
									
										Normal file
									
								
							|  | @ -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<Vendors> => { | ||||
|     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<Vendor> => { | ||||
|     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<Vendors> => { | ||||
|     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<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");
 | ||||
| // };
 | ||||
|  | @ -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<Products> => { | ||||
|     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<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(); | ||||
|         // 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<void> => { | ||||
|     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<void> => { | ||||
|     const record: Product = products[id]; | ||||
| 
 | ||||
|     if (record) { | ||||
|         delete products[id]; | ||||
|         return; | ||||
|     } | ||||
| 
 | ||||
|     throw new Error("No record found to delete"); | ||||
| }; | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user