Skip to content

Commit

Permalink
added mongodb functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
underdorff18 committed Jun 29, 2022
1 parent 0bc7774 commit 4fa588e
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 35 deletions.
Binary file added JS/1000.xlsx
Binary file not shown.
File renamed without changes.
35 changes: 35 additions & 0 deletions JS/dbtools.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const { MongoClient, ServerApiVersion } = require('mongodb');

const uri = 'mongodb+srv://nickoverdorff:[email protected]/?retryWrites=true&w=majority';
const client = new MongoClient(uri);

client.connect();

const dbtools = {
collection: client.db('inventorydb').collection('systems'),
insertSystem: async function (collection, system) {
const result = await collection.insertOne(system);
console.log(`Created a system entry with id: ${result.insertedId}`);
},
deleteSystem: async function (collection, serial) {
const result = await collection.deleteOne({ serialnum: serial});
console.log(`${result.deletedCount} document(s) was/were deleted.`);
},
getAllSystems: async function (collection) {
cursor = await collection.find();
let allSystems = []
await cursor.forEach( function (result) {allSystems.push(result);});
return allSystems;
}
}

module.exports = dbtools;









36 changes: 23 additions & 13 deletions JS/excelreader.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,43 @@
const fs = require('fs');
const path = require('path');
const Path = require('path');
const { MongoClient, ServerApiVersion } = require('mongodb');
dbtools = require('./dbtools');
const xlsx = require('xlsx');
const chokidar = require('chokidar');
const System = require('./system');
const { all } = require('express/lib/application');

const myfilepath = path.join(__dirname, 'Inventory', 'samplepc1234.xlsx');
//const myfilepath = Path.join(__dirname, 'Inventory', 'samplepc1234.xlsx');

function readInventoryFile(filepath) {
let workbook = xlsx.readFileSync(filepath);
let sheetNames = workbook.SheetNames;
let sheetData = workbook.Sheets[sheetNames[0]];

//Here is where we use known cell locations of the specs needed to build the systems file
let serialNum = sheetData.D1.v;
let serialnum = sheetData.D1.v;
let model = sheetData.D2.v;
let OS = sheetData.D3.v;
let price = sheetData.D4.v;
let specs = [sheetData.D5.v, sheetData.D6.v, sheetData.D7.v, sheetData.D8.v];

return new System(serialNum, model, OS, price, specs);
return new System(serialnum, model, OS, price, specs);
}


console.log(readInventoryFile(myfilepath));

var inventoryWatcher = chokidar.watch(path.join(__dirname, "Inventory"), {ignored: /^\./, persistent: true});

// inventoryWatcher
// .on('add', function(path) {
// fs.appendFile('systems.json', ', ')
// })
//
var inventoryWatcher = chokidar.watch(Path.join(__dirname, "Inventory"), {ignored: /^\./, persistent: true});
console.log(`Watching directory for changes...` )

inventoryWatcher
.on('add', function(filepath) {
dbtools.insertSystem(dbtools.collection, readInventoryFile(filepath));
})
.on('change', function(filepath) {
console.log('File', path, 'has been changed');
})
.on('unlink', function(filepath) {
dbtools.deleteSystem(dbtools.collection, parseInt(Path.basename(filepath, '.xlsx')));
})
.on('error', function(error) {
console.error('Error happened', error);
});
13 changes: 7 additions & 6 deletions JS/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
const express = require('express', '4.17.1');
const fs = require('fs');
dbtools = require('./dbtools');

const app = express();
//const myparse = require('./parse');
const port = 3000;


app.get('/', (req, res) => {
res.send('Homepage');
});

app.get('/systems.json', (req, res) => {
fs.readFile('systems.json', 'utf8', (err, data) => {
if (err) throw err;
res.json(JSON.parse(data));
});
app.get('/systems', async function(req, res) {
result = await dbtools.getAllSystems(dbtools.collection);
console.log(result);
res.send(result);

});


Expand Down
16 changes: 0 additions & 16 deletions JS/systems.json

This file was deleted.

0 comments on commit 4fa588e

Please sign in to comment.