# Config DB and query function
const mysql = require('mysql');
const mysqlConnection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'QAZwsx@123',
database: 'blog',
multipleStatements: true
});
mysqlConnection.connect((err) => {
if (!err) {
console.log('DB Connection Established Successfully');
return true;
} else {
console.log('Connection Failed!' + JSON.stringify(err, undefined, 2));
return false;
}
});
class DB {
constructor() {
this.connection = mysqlConnection;
}
// All categories
getCategories() {
return new Promise((resolve, reject) => {
this.connection.query('SELECT * FROM categories', (err, rows) => {
if (!err)
resolve(rows);
else
reject(err);
});
});
}
// Get category by id
getCategoryById(id) {
return new Promise((resolve, reject) => {
this.connection.query('SELECT * FROM categories WHERE id = ?', [id], (err, rows) => {
if (!err)
resolve(rows);
else
reject(err);
});
});
}
// Add category
addCategory(category) {
return new Promise((resolve, reject) => {
this.connection.query('INSERT INTO categories SET ?', [category], (err, rows) => {
if (!err)
resolve(this.getCategories());
else
reject(err);
});
});
}
// Update category
updateCategory(id, category) {
return new Promise((resolve, reject) => {
this.connection.query('UPDATE categories SET ? WHERE id = ?', [category, id], (err, rows) => {
if (!err)
resolve(this.getCategories());
else
reject(err);
});
});
}
// Delete category
deleteCategory(id) {
return new Promise((resolve, reject) => {
this.connection.query('DELETE FROM categories WHERE id = ?', [id], (err, rows) => {
if (!err)
resolve(this.getCategories());
else
reject(err);
});
});
}
}
module.exports = DB;
# Crud Operations
const db = require('./DB');
var dbInstance = new db();
const express = require('express');
const app = express();
const bodyparser = require('body-parser');
const port = process.env.PORT || 5000;
app.listen(port, () => console.log(`Listening on port ${port}..`));
//Configuring express server
app.use(bodyparser.json());
// Running express server
app.get('/', function (req, res) {
res.send('Hello World!')
});
// Get all categories
app.get('/categories', function (req, res) {
let categories = dbInstance.getCategories();
categories.then((result) => {
res.send(result);
}).catch((err) => {
res.send(err);
});
});
// Get category by id
app.patch('/categories/:id', function (req, res) {
let id = req.params.id;
let category = dbInstance.getCategoryById(id);
category.then((result) => {
res.send(result);
}).catch((err) => {
res.send(err);
});
});
// Add category
app.post('/categories', function (req, res) {
let category = req.body;
let addCategory = dbInstance.addCategory(category);
addCategory.then((result) => {
res.send(result);
}).catch((err) => {
res.send(err);
});
});
// Update category
app.put('/categories/:id', function (req, res) {
let id = req.params.id;
let category = req.body;
let updateCategory = dbInstance.updateCategory(id, category);
updateCategory.then((result) => {
res.send(result);
}).catch((err) => {
res.send(err);
});
});
// Delete category
app.delete('/categories/:id', function (req, res) {
let id = req.params.id;
let deleteCategory = dbInstance.deleteCategory(id);
deleteCategory.then((result) => {
res.send(result);
}).catch((err) => {
res.send(err);
});
});