From 2572f8f06d15dfc2cb69dca6ced60164290ab010 Mon Sep 17 00:00:00 2001 From: Brooke Woolley Date: Sun, 17 Mar 2019 21:14:02 +0000 Subject: [PATCH] feat: persisting data to db --- Team1/db.js | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++ Team1/index.js | 34 +++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 Team1/db.js create mode 100644 Team1/index.js diff --git a/Team1/db.js b/Team1/db.js new file mode 100644 index 0000000..742b3fc --- /dev/null +++ b/Team1/db.js @@ -0,0 +1,52 @@ +const { MongoClient, ObjectID } = require('mongodb') + +let db, + clientConn, + msg=""; + +const url = 'mongodb://localhost:27017'; +const dbName = 'codeUntapped'; + +module.exports = () => { + return MongoClient + .connect(url, { + useNewUrlParser: true + }) + .then((client) => { + db = client.db(dbName); + clientConn = client; + }); +}; + +module.exports.Alexa = { +create(data, collection) { + return new Promise(function(resolve, reject){ + db.collection(collection).insertOne(data, { w: 1 }, function(err, docs){ + if (err) { + reject(Error("create: Unable to create record in collection: " + collection)); + clientConn.close(); + } else { + console.log(docs); + resolve(docs); + clientConn.close(); + } + }); + }); + }, + + all(query, collection) { + return new Promise(function(resolve, reject){ + db.collection(collection) + .find(query) + .sort({ name : 1}) + .toArray() + .then( function(docs) { + resolve(docs); + clientConn.close(); + }, function(error) { + reject(error); + clientConn.close(); + }); + }); + } +} diff --git a/Team1/index.js b/Team1/index.js new file mode 100644 index 0000000..077d97d --- /dev/null +++ b/Team1/index.js @@ -0,0 +1,34 @@ +const express = require('express'); +const bodyParser = require('body-parser'); + +const app = express(); + +const db = require ('./db'); + +app.set('port', process.env.PORT || 3000); + +app.use(bodyParser.json()); +app.use(bodyParser.urlencoded({ extended: true })); + +app.get("/", (req, res) => { + res.send('Hello world') +}); + +app.get('/results', (req, res, next) => { + var Timenow = new Date(); + var results = { + "type": req.query.type, + } + collection='productType'; + db().then(() => { + db.Alexa.create(results, collection).then((doc, err) => { + console.log(err) + }); + }); +}); + +app.listen(app.get('port'), () => { + console.log('Code Untapped listening on: http://localhost:%s', app.get('port')); +}); + +module.exports = app;