import * as dotenv from 'dotenv'; import {Manufacturer} from './manufacturer.interface'; import {Manufacturers} from './manufacturers.interface'; import {BetterzonDB} from '../Betterzon.db'; dotenv.config(); /** * Data Model Interfaces */ /** * Service Methods */ /** * Fetches and returns all known manufacturers */ export const findAll = async (): Promise => { let conn = await BetterzonDB.getConnection(); let manRows = []; try { 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 { // Return connection await conn.end(); } return manRows; }; /** * Fetches and returns the manufacturer with the specified id * @param id The id of the manufacturer to fetch */ export const find = async (id: number): Promise => { let conn = await BetterzonDB.getConnection(); let man: any; try { 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 { // Return connection await conn.end(); } return man; }; /** * Fetches and returns all manufacturers that match the search term * @param term the term to match */ export const findBySearchTerm = async (term: string): Promise => { let conn = await BetterzonDB.getConnection(); let manRows = []; try { 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 { // Return connection await conn.end(); } return manRows; };