This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const productMssql = require('./product.mssql'); | |
class product { | |
async getAllProducts(req, res) { | |
try { | |
const output = await productMssql.getAllProducts(); | |
res.sendResponse(output); | |
} | |
catch (error) { | |
console.log(error); | |
res.sendError(global.HELPER.getException({ error, message: 'Error in getting products list.' })); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bindResponse(req, res, next) { | |
this.request = req; | |
res.sendResponse = (response) => { | |
res.json({ | |
status: 'success', | |
data: response | |
}); | |
}; | |
res.sendError = (error) => { | |
if(error.httpCode) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Helper { | |
static getException({error, message}) { | |
if(error instanceof EXCEPTION) { | |
return error; | |
} else { | |
console.error(error); | |
console.error(error.stack); | |
return new global.EXCEPTION('UnKnownError', message); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const util = require('util'); | |
const EXCEPTION_MESSAGES = require('./exceptions.json'); | |
class Exception { | |
constructor(errorName, params) { | |
Error.captureStackTrace(this, Exception); | |
this.errorName = errorName; | |
this.params = params; | |
this.code = EXCEPTION_MESSAGES[errorName]['code']; | |
this.httpCode = EXCEPTION_MESSAGES[errorName]['httpCode']; | |
this.errorMessage = EXCEPTION_MESSAGES[errorName]['message']; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"InvalidAuthRequest": { | |
"code": "1001", | |
"httpCode": "404", | |
"message": "", | |
"sendNotification": false, | |
"log": "log" | |
}, | |
"InvalidPassword": { | |
"code": "1003", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Response { | |
constructor(app) { | |
app.use((req,res,next) => this.bindResponse(req,res,next)); | |
} | |
bindResponse(req,res,next) { | |
this.request = req; | |
res.sendResponse = (response) => { | |
res.json({ | |
status: 'success', | |
data: response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ProductMSSql { | |
async getAllProducts() { | |
const res = await global.MSSQLConnection.request() | |
.execute("getAllProducts"); | |
return res.recordset; | |
} | |
async addProduct(prod) { | |
const res = await global.MSSQLConnection.request() | |
.input("product_name", prod.product_name) | |
.input("product_price", prod.product_price) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
global.CONFIG = require('./config/environment'); | |
const bodyparser = require('body-parser'); | |
const app = express(); | |
async function init() { | |
const mssqlconnection = require('./utilities/mssqlconnection'); | |
global.MSSQLConnection = mssqlconnection.getConnection(global.CONFIG.mssql); | |
app.use(bodyparser.json({ limit: '50mb' })); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const sql = require('mssql'); | |
class MSSqlconnection { | |
async getConnection(dbConfig) { | |
try { | |
return await sql.connect({ | |
user: dbConfig.user, | |
password: dbConfig.password, | |
server: dbConfig.server, | |
database: dbConfig.database, | |
port: dbConfig.port, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"server": { | |
"host": "0.0.0.0", | |
"port": "4001" | |
}, | |
"mssql": { | |
"user": "sa", | |
"password": "sa", | |
"server": "localhost", | |
"database": "Demo", |
NewerOlder