Commit c0fbeb27 authored by Navodya Pasqual's avatar Navodya Pasqual

Backend updated

parent 161b8850
......@@ -3,16 +3,11 @@ const mongoose = require('mongoose');
const dotenv = require('dotenv');
const cors = require('cors');
const bodyParser = require('body-parser');
const SiteRoute = require('./src/routes/route.site');
const SupplierRoute = require('./src/routes/route.supplier');
dotenv.config();
const app = express();
app.use(cors());
//
// parse requests of content-type - application/json
app.use(bodyParser.json());
// parse requests of content-type - application/x-www-form-urlencoded
......@@ -44,17 +39,12 @@ mongoose.connection.once('open', () => {
});
app.route('/').get((req, res) => {
res.send('CSSE Group Project');
res.send('Research Project');
});
require('./src/routes/auth.routes')(app);
require('./src/routes/user.routes')(app);
app.use('/site', SiteRoute());
app.use('/supplier', SupplierRoute());
app.listen(PORT, () => {
console.log('######################################################');
console.log(`Server is ON and running on PORT : ${PORT}`);
......
const Site = require('../modules/module.site');
const {next} = require("lodash");
const createSite = (req, res) => {
const {siteID,name,location} = req.body
// validate fields
if(!siteID || !name || !location) {
return res.status(400).json({
error: "All fields are required"
});
}
Site.create({siteID,name,location},(err,name) => {
if(err) {
return res.status(400).json({
error: 'Error Found'
});
}
res.json(name);
});
}
const getAllSites = (req, res) => {
let order = req.query.order ? req.query.order : 'asc'
let sortBy = req.query.sortBy ? req.query.sortBy : '_id'
Site.find()
.sort([[sortBy, order]])
.exec((err, site) => {
if(err) {
return res.status(400).json ({
error: 'No data Found'
});
}
res.json(site);
});
}
const siteById = async (req, res) => {
Site.findById(req.params.id, (error, data) =>{
if (error) {
return next(error)
} else {
res.json(data)
}
})
};
const updateById = async(req, res) => {
const { slug } = req.params
const {siteID,name,location} = req.body
Site.findOneAndUpdate({slug}, {siteID,name,location}, {new: true})
.exec((err,site) => {
if(err) console.log(err)
res.json(site);
})
};
const deleteById = async (req, res) => {
const id = req.params.id
await Site.findByIdAndRemove(id).exec()
res.send("Deleted");
};
const countSites = (req, res) => {
Site.count({ }, function(err, result) {
if (err) {
res.send(err);
} else {
res.json(result);
}
});
};
module.exports = {
createSite,
siteById,
getAllSites,
updateById,
deleteById,
countSites
}
\ No newline at end of file
const Supplier = require('../modules/module.supplier');
const {next} = require("lodash");
const createSupplier = (req, res) => {
const {supplierID,name,address,contactNo} = req.body
// validate fields
if(!supplierID || !name || !address || !contactNo) {
return res.status(400).json({
error: "All fields are required"
});
}
Supplier.create({supplierID,name,address,contactNo},(err,name) => {
if(err) {
return res.status(400).json({
error: 'Error Found'
});
}
res.json(name);
});
}
const getAllSuppliers = (req, res) => {
let order = req.query.order ? req.query.order : 'asc'
let sortBy = req.query.sortBy ? req.query.sortBy : '_id'
Supplier.find()
.sort([[sortBy, order]])
.exec((err, supplier) => {
if(err) {
return res.status(400).json ({
error: 'No data Found'
});
}
res.json(supplier);
});
}
const siteById = async (req, res) => {
Supplier.findById(req.params.id, (error, data) =>{
if (error) {
return next(error)
} else {
res.json(data)
}
})
};
const updateById = async(req, res) => {
const { slug } = req.params
const {supplierID,name,address,contactNo} = req.body
Supplier.findOneAndUpdate({slug}, {supplierID,name,address,contactNo}, {new: true})
.exec((err,supplier) => {
if(err) console.log(err)
res.json(supplier);
})
};
const deleteById = async (req, res) => {
const id = req.params.id
await Supplier.findByIdAndRemove(id).exec()
res.send("Deleted");
};
const countSuppliers = (req, res) => {
Supplier.count({ }, function(err, result) {
if (err) {
res.send(err);
} else {
res.json(result);
}
});
};
module.exports = {
createSupplier,
siteById,
getAllSuppliers,
updateById,
deleteById,
countSuppliers
}
\ No newline at end of file
const mongoose = require('mongoose');
const { ObjectId } = mongoose.Schema;
const SiteSchema = new mongoose.Schema(
{
siteID: {
type: String,
required: true,
trim: true,
min: 1,
max: 50
},
name: {
type: String,
required: true,
trim: true,
min: 1,
max: 50
},
location : {
type: String,
required: true,
trim: true,
min: 1,
max: 50
}
},
{ timestamps: true}
);
module.exports = mongoose.model('sites', SiteSchema);
\ No newline at end of file
const mongoose = require('mongoose');
const { ObjectId } = mongoose.Schema;
const SupplierSchema = new mongoose.Schema(
{
supplierID: {
type: String,
required: true,
trim: true,
min: 1,
max: 50
},
name: {
type: String,
required: true,
trim: true,
min: 1,
max: 50
},
address : {
type: String,
required: true,
trim: true,
min: 1,
max: 50
},
contactNo : {
type: String,
required: true,
trim: true,
min: 1,
max: 50
}
},
{ timestamps: true}
);
module.exports = mongoose.model('suppliers', SupplierSchema);
\ No newline at end of file
const express = require('express');
const router = express.Router();
const controller = require('../controllers/controller.site');
module.exports = function () {
router.post('/create', controller.createSite);
router.get('/', controller.getAllSites);
router.get('/:id', controller.siteById);
router.delete('/delete/:id', controller.deleteById);
router.put('/update/:id', controller.updateById);
router.get('/sites/count', controller.countSites);
return router;
}
\ No newline at end of file
const express = require('express');
const router = express.Router();
const controller = require('../controllers/controller.supplier');
module.exports = function () {
router.post('/create', controller.createSupplier);
router.get('/', controller.getAllSuppliers);
router.get('/:id', controller.siteById);
router.delete('/delete/:id', controller.deleteById);
router.put('/update/:id', controller.updateById);
router.get('/suppliers/count', controller.countSuppliers);
return router;
}
\ No newline at end of file
{
"name": "Backend",
"lockfileVersion": 2,
"requires": true,
"packages": {}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment