From 6cfee6635ca3930a7d45ccef8d15c49f126b44ee Mon Sep 17 00:00:00 2001 From: gammam <42178449+gammam@users.noreply.github.com> Date: Thu, 11 Jul 2019 15:19:22 +0200 Subject: [PATCH] listSessions --- src/api/controllers/s4aAdmin.js | 48 +++++++++ src/api/controllers/s4aController.js | 142 ++++++++++++--------------- src/api/routes/s4aRoutes.js | 6 +- 3 files changed, 114 insertions(+), 82 deletions(-) create mode 100644 src/api/controllers/s4aAdmin.js diff --git a/src/api/controllers/s4aAdmin.js b/src/api/controllers/s4aAdmin.js new file mode 100644 index 0000000..e70201b --- /dev/null +++ b/src/api/controllers/s4aAdmin.js @@ -0,0 +1,48 @@ +/////// MONGODB +const url = require("url"); +const MongoClient = require("mongodb").MongoClient; + +// Create cached connection variable +let cachedDb = null; + +// a function for connecting to MOngoDB +// taking a single parameter of the connection string + +async function connectToDatabase(uri) { + //if the database connection is cached , + //use it instead of creating a new connection + if (cachedDb) { + return cachedDd; + } + //if no connection is cached, create a new one + const client = await MongoClient.connect(uri, { useNewUrlParser: true }); + + //seect the database through the connection, + //using the database ath of the connection string + const db = await client.db(url.parse(uri).pathname.substr(1)); + + // cache the database connection and return the connection + cachedDb = db; + return db; +} + +exports.listSessions = async function(req, res) { + console.log("list all sessions"); + var athleteId = req.params.athleteId; + console.log("athledId = ", athleteId); + console.log(process.env.MONGODB_URI); + const db = await connectToDatabase(process.env.MONGODB_URI); + + //Select sessions collection + const sessionsCollection = await db.collection("sessions"); + //select all the sessions + const allSessions = await sessionsCollection.find({}).toArray(); + + //respond with the array created + // as a json + res.status(200).json(allSessions); +}; + +exports.addSessions = async function(req, res) { + res.status(200); +}; diff --git a/src/api/controllers/s4aController.js b/src/api/controllers/s4aController.js index 3d65d5c..403e996 100644 --- a/src/api/controllers/s4aController.js +++ b/src/api/controllers/s4aController.js @@ -1,62 +1,52 @@ - - -/////// MONGODB -const url =require('url'); -const MongoClient = require('mongodb').MongoClient; +/////// MONGODB +const url = require("url"); +const MongoClient = require("mongodb").MongoClient; // Create cached connection variable -let cachedDb = null +let cachedDb = null; -// a function for connecting to MOngoDB +// a function for connecting to MOngoDB // taking a single parameter of the connection string -async function connectToDatabase(uri){ - //if the database connection is cached , - //use it instead of creating a new connection - if (cachedDb) { - return cachedDd - } - //if no connection is cached, create a new one - const client = await MongoClient.connect(uri,{useNewUrlParser: true}) - - //seect the database through the connection, - //using the database ath of the connection string - const db = await client.db(url.parse(uri).pathname.substr(1)) - - // cache the database connection and return the connection - cachedDb = db - return db +async function connectToDatabase(uri) { + //if the database connection is cached , + //use it instead of creating a new connection + if (cachedDb) { + return cachedDd; + } + //if no connection is cached, create a new one + const client = await MongoClient.connect(uri, { useNewUrlParser: true }); + + //seect the database through the connection, + //using the database ath of the connection string + const db = await client.db(url.parse(uri).pathname.substr(1)); + + // cache the database connection and return the connection + cachedDb = db; + return db; } - - - - - - - exports.list_all_sessions = async function(req, res) { console.log("list all sessions"); var athleteId = req.params.athleteId; console.log("athledId = ", athleteId); console.log(process.env.MONGODB_URI); - const db = await connectToDatabase(process.env.MONGODB_URI) - + const db = await connectToDatabase(process.env.MONGODB_URI); + //Select sessions collection - const sessionsCollection = await db.collection('sessions') + const sessionsCollection = await db.collection("sessions"); //select all the sessions const allSessions = await sessionsCollection.find({}).toArray(); - //respond with the array created - // as a json - res.json({ - statusCode: 200, - statusResponse: "OK", - messages: "All sessions", - data: allSessions - }); - }; - + //respond with the array created + // as a json + res.json({ + statusCode: 200, + statusResponse: "OK", + messages: "All sessions", + data: allSessions + }); +}; exports.get_all_sessions_by_user = async function(req, res) { var athleteId = String(req.params.athleteId); @@ -65,51 +55,45 @@ exports.get_all_sessions_by_user = async function(req, res) { var queryFilter = {}; queryFilter["Name"] = athleteId; - console.log("queryFilter: ",queryFilter) + console.log("queryFilter: ", queryFilter); - const db = await connectToDatabase(process.env.MONGODB_URI) -//Select sessions collection - const sessionsCollection = await db.collection('sessions') + const db = await connectToDatabase(process.env.MONGODB_URI); + //Select sessions collection + const sessionsCollection = await db.collection("sessions"); //select all the sessions const userSessions = await sessionsCollection.find(queryFilter).toArray(); - - //respond with the array created - // as a json - res.json({ - statusCode: 200, - statusResponse: "OK", - messages: "get_all_sessions_by_user", - data: userSessions - }); - }; + //respond with the array created + // as a json + res.json({ + statusCode: 200, + statusResponse: "OK", + messages: "get_all_sessions_by_user", + data: userSessions + }); +}; exports.read_a_session = function(req, res) {}; exports.delete_a_session = function(req, res) {}; -exports.create_a_session = function(req, res) { - +exports.create_a_session = function(req, res) {}; -}; +exports.insert_many_sessions = async function(req, res) { + console.log("insert many sessions"); + console.log("body: ", req.body); + const db = await connectToDatabase(process.env.MONGODB_URI); -exports.insert_many_sessions =async function(req,res){ - console.log('insert many sessions'); - console.log('body: ', req.body); - - const db = await connectToDatabase(process.env.MONGODB_URI) - -//Select sessions collection - const sessionsCollection = await db.collection('sessions') + //Select sessions collection + const sessionsCollection = await db.collection("sessions"); //select all the sessions - + const insertResult = await sessionsCollection.insertMany(req.body); - - console.log('insertResult: ', insertResult); - - res.json({ - statusCode: 200, - statusResponse: "OK", - messages: "updated" - }); - -} \ No newline at end of file + + console.log("insertResult: ", insertResult); + + res.json({ + statusCode: 200, + statusResponse: "OK", + messages: "updated" + }); +}; diff --git a/src/api/routes/s4aRoutes.js b/src/api/routes/s4aRoutes.js index c29e4f7..741eb19 100644 --- a/src/api/routes/s4aRoutes.js +++ b/src/api/routes/s4aRoutes.js @@ -1,6 +1,6 @@ module.exports = function(app) { var s4a = require("../controllers/s4aController"); - + var admin = require("../controllers/s4aAdmin"); // test Routes app.route("/").get(function(req, res) { @@ -12,8 +12,8 @@ module.exports = function(app) { // Admin operations app .route("/v1/admin/sessions") - .get(s4a.listSessions) - .post(s4a.addSessions); + .get(admin.listSessions) + .post(admin.addSessions); /* TODO - Manage Athlete app