Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions Team1/db.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
}
}
34 changes: 34 additions & 0 deletions Team1/index.js
Original file line number Diff line number Diff line change
@@ -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;