Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- app.use(cors());
- app.use(bodyParser.json({limit:'4096mb'}));
- mongoose.connect('mongodb://localhost:27017/Taxonomy', { useNewUrlParser: true, useUnifiedTopology: true } );
- const connection = mongoose.connection;
- connection.once('open', () => {
- console.log('MongoDB connection established.');
- });
- router.route('/login').post((req, res) => {
- let userLogin = false;
- let email = req.body.email;
- let password = req.body.password;
- let userResponseObj;
- User.find((err, users) => {
- users.forEach(user => {
- if (user.email == email && user.password == password) {
- userResponseObj = {
- email: user.email,
- company: user.company
- };
- userLogin = true;
- }
- });
- (!!userLogin) ? res.status(200).json(userResponseObj) : res.status(403).json('wrong password / username');
- });
- });
- router.route('/users').get((req, res) => {
- User.find((err, users) => {
- (err) ? console.log(err) : res.json(users);
- });
- });
- router.route('/users/:email').get((req, res) => {
- let paramenterEmail = req.params.email;
- User.find((err, users) => {
- if (err) {
- res.status(400).send('Fetch failed.');
- } else {
- users.forEach(user => {
- if (user.email == paramenterEmail) {
- res.json(user);
- return;
- }
- })
- }
- });
- });
- router.route('/users/update').post((req, res) => {
- User.deleteMany({}, (err) => {
- if (err) {
- res.status(400).send('Fetch failed.');
- } else {
- let users = req.body;
- users.forEach(user => {
- let newUser = new User(user);
- newUser.save();
- });
- res.status(200).send('Update successful.');
- }
- });
- });
- router.route('/mandants').get((req, res) => {
- Mandant.find((err, mandants) => {
- (err) ? console.log(err) : res.json(mandants);
- });
- });
- router.route('/mandants/:id').get((req, res) => {
- Mandant.findOne({'id': req.params.id}, (err, mandant) => {
- if (err) {
- res.status(400).send('Fetch failed.');
- } else {
- (!mandant) ? res.status(200).json({}) : res.status(200).json(mandant);
- }
- })
- });
- router.route('/addmandant').post((req, res) => {
- Mandant.findOne({'id': req.body.id}, (err, mandant) => {
- if (err) {
- res.status(400).send('Fetch failed.');
- } else {
- if (!!mandant) {
- res.status(403).json('mandant already exists');
- } else {
- let mandant = new Mandant({
- name: req.body.name,
- tools: req.body.tools,
- vendors: req.body.vendors,
- id: req.body.id,
- thresHoldLicenceUrgent: req.body.thresHoldLicenceUrgent,
- thresHoldLicenceMedium: req.body.thresHoldLicenceMedium
- });
- mandant.save(err => {
- (err) ? console.log(err) : res.status(200).json('Mandant saved successfully.');
- });
- }
- }
- })
- });
- router.route('/mandants/update/:id').post((req, res) => {
- Mandant.findById({_id: req.params.id}, (err, mandant) => {
- if (!mandant) {
- res.status(400).send('Update failed.');
- } else {
- mandant.name = req.body.name;
- mandant.tools = req.body.tools;
- mandant.vendors = req.body.vendors;
- mandant.thresHoldLicenceUrgent = req.body.thresHoldLicenceUrgent;
- mandant.thresHoldLicenceMedium = req.body.thresHoldLicenceMedium;
- mandant.save()
- .then(() => {
- res.json('Update done.');
- })
- .catch((err) => {
- res.status(400).send('Update failed.');
- });
- }
- })
- });
- router.route('/mandants/updateprocess/:id').post((req, res) => {
- Mandant.findById({_id: req.params.id}, (err, mandant) => {
- if (!mandant) {
- res.status(400).send('Update failed.');
- } else {
- mandant.process = req.body.process;
- mandant.save()
- .then(() => {
- res.json('Update done.');
- })
- .catch(() => {
- res.status(400).send('Update failed.');
- });
- }
- })
- });
- router.route('/mandants/delete/:id').get((req, res) => {
- Mandant.findByIdAndRemove({_id: req.params.id}, (err, mandant) => {
- (err) ? res.json(err) : res.json('Remove successfully.');
- });
- });
- router.route('/tools').get((req, res) => {
- Product.find((err, tools) => {
- (err) ? console.log(err) : res.json(tools);
- });
- });
- router.route('/vendors').get((req, res) => {
- Vendor.find((err, vendors) => {
- (err) ? console.log(err) : res.json(vendors);
- });
- });
- router.route('/category').get((req, res) => {
- Category.find((err, category) => {
- (err) ? console.log(err) : res.json(category);
- });
- });
- router.route('/taxonomy').get((req, res) => {
- Taxonomy.find((err, taxonomy) => {
- (err) ? console.log(err) : res.json(taxonomy);
- });
- });
- const fetchDCSOData = () => {
- fetchDCSOVendors();
- fetchDCSOProducts();
- fetchDCSOTaxonomy();
- fetchDCSOCategories();
- }
- const fetchDCSOVendors = () => {
- requestify.request(dcsoApiCfg.url + '/vendors', {
- method: 'GET',
- headers: { 'x-api-key': dcsoApiCfg.key },
- dataType: 'json'
- })
- .then(res => {
- Vendor.deleteMany({}, (err) => {
- if (err) {
- console.log(err);
- } else {
- let vendors = JSON.parse(res.body);
- vendors.forEach(vendor => {
- let newVendor = new Vendor(vendor);
- newVendor.save();
- });
- }
- });
- })
- .fail(res => {
- console.log(res);
- });
- }
- const fetchDCSOProducts = () => {
- requestify.request(dcsoApiCfg.url + '/products', {
- method: 'GET',
- headers: { 'x-api-key': dcsoApiCfg.key },
- dataType: 'json'
- })
- .then(res => {
- Product.deleteMany({}, (err) => {
- if (err) {
- console.log(err);
- } else {
- let products = JSON.parse(res.body);
- products.forEach(product => {
- fetchDCSOProductsById(product.id);
- });
- }
- });
- })
- .fail(res => {
- console.log(res);
- });
- }
- const fetchDCSOProductsById = (id) => {
- requestify.request(dcsoApiCfg.url + '/products/' + id, {
- method: 'GET',
- headers: { 'x-api-key': dcsoApiCfg.key },
- dataType: 'json'
- })
- .then(res => {
- let product = JSON.parse(res.body);
- let newProduct = new Product(product);
- newProduct.save();
- })
- .fail(res => {
- console.log(res);
- });
- }
- const fetchDCSOTaxonomy = () => {
- requestify.request(dcsoApiCfg.url + '/taxonomy', {
- method: 'GET',
- headers: { 'x-api-key': dcsoApiCfg.key },
- dataType: 'json'
- })
- .then(res => {
- Taxonomy.deleteMany({}, (err) => {
- if (err) {
- console.log(err);
- } else {
- let taxonomy = JSON.parse(res.body);
- taxonomy.forEach(taxonomyEntry => {
- let newTaxonomy = new Taxonomy(taxonomyEntry);
- newTaxonomy.save();
- });
- }
- });
- })
- .fail(res => {
- console.log(res);
- });
- }
- const fetchDCSOCategories = () => {
- requestify.request(dcsoApiCfg.url + '/categories', {
- method: 'GET',
- headers: { 'x-api-key': dcsoApiCfg.key },
- dataType: 'json'
- })
- .then(res => {
- Category.deleteMany({}, (err) => {
- if (err) {
- console.log(err);
- } else {
- let categories = JSON.parse(res.body);
- categories.forEach(category => {
- let newCategory = new Category(category);
- newCategory.save();
- })
- }
- });
- })
- .fail(res => {
- console.log(res);
- });
- }
- fetchDCSOData();
- setInterval(() => {
- fetchDCSOData();
- }, 86400000);
- app.use('/', router);
- app.listen(4000, () => console.log('Server started.'));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement