It looks like your post is mostly code; please add some more details.It looks like your post is mostly code; please add some more details.It looks like your post is mostly code; please add some more details.It looks like your post is mostly code; please add some more details.It looks like your post is mostly code; please add some more details.It looks like your post is mostly code; please add some more details.It looks like your post is mostly code; please add some more details.
BACKEND/package.json
{
"name": "backend",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo "Error: no test specified" && exit 1"
},
"author": "lkh-krishen",
"license": "ISC",
"dependencies": {
"@koa/router": "^10.1.1",
"koa": "^2.13.4",
"koa-bodyparser": "^4.3.0",
"mongodb": "^4.7.0"
}
}
BACKEND/index.js
const Koa = require('koa');
const bodyParser = require('koa-bodyparser');
const productRoutes = require('./routes/products.routes');
const promotionRoutes = require('./routes/promotions.routes');
const app = new Koa();
app.use(bodyParser());
app.use(productRoutes.routes()).use(productRoutes.allowedMethods());
app.use(promotionRoutes.routes()).use(promotionRoutes.allowedMethods());
app.listen(3000, () => {
console.log("Application is running on port 3000");
});
BACKEND/dal/index.js
const {MongoClient} = require("mongodb");
const client = new MongoClient('mongodb://localhost:27017', {
useNewUrlParser: true,
useUnifiedTopology: true
});
client.connect(err => {
if(err){
console.error(err);
process.exit(-1);
}
console.log("Successfully connected to MongoDB");
});
module.exports = client;
BACKEND/dal/products.dao.js
const products = require('./index').db('store').collection('products');
const ObjectId = require('mongodb').ObjectId;
const save = async ({name, description, qty, price}) => {
const result = await products.insertOne({name, description, qty, price});
//console.log(result.ops);
return await getById(result.insertedId)
//return result;
}
const getAll = async () => {
const cursor = await products.find();
return cursor.toArray();
}
const getById = async (id) => {
return await products.findOne({_id:ObjectId(id)});
}
const update = async (id, {name, description, qty, price}) => {
const result = await products.replaceOne({_id:ObjectId(id)}, {name, description, qty, price});
return result.ops[0];
}
const removeById = async id => {
await products.deleteOne({_id:ObjectId(id)});
}
module.exports = {save, getAll, getById, update, removeById};
BACKEND/api/products.api.js
const {getAll, save, update, getById, removeById} = require('../dal/products.dao');
const createProduct = async ({name, description, qty, price}) => {
const product = {
name,
description,
qty,
price
}
return await save(product);
}
const getProducts = async () => {
return await getAll();
}
const getProduct = async id => {
return await getById(id);
}
const deleteProduct = async id => {
return await removeById(id);
}
const updateProduct = async (id, {name, description, qty, price}) => {
return await update(id, {name, description, qty, price});
}
module.exports = {createProduct, getProducts, getProduct, deleteProduct, updateProduct};
BACKEND/routes/products.routes.js
const Router = require('@koa/router');
const {createProduct, deleteProduct, updateProduct, getProduct, getProducts} = require('../api/products.api');
const router = new Router ({
prefix: '/products'
});
router.get('/', async ctx => {
ctx.body = await getProducts();
});
router.post('/', async ctx => {
let product = ctx.request.body;
product = await createProduct(product);
ctx.response.status = 200;
ctx.body = product;
});
router.get('/:id', async ctx => {
const id = ctx.params.id;
ctx.body = await getProduct(id);
});
router.delete('/:id', async ctx => {
const id = router.params.id;
ctx.body = await deleteProduct(id);
});
router.put('/:id', async ctx => {
const id = ctx.params.id;
let product = ctx.request.body;
product = await updateProduct(product);
ctx.response.status = 200;
ctx.body = product;
});
module.exports = router;
2
Answers
backend/api/customer.api.js
backend/models/Customer.js
backend/router/customer.router.js