mirror of
https://github.com/Mueller-Patrick/Betterzon.git
synced 2026-05-26 12:38:03 +00:00
BETTERZON-38: Added all currently required models with required service classes and routers
This commit is contained in:
@@ -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;
|
||||
}
|
||||
@@ -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);
|
||||
// }
|
||||
// });
|
||||
@@ -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");
|
||||
// };
|
||||
Reference in New Issue
Block a user