# 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);
});
});