Skip to content

Commit

Permalink
listSessions
Browse files Browse the repository at this point in the history
  • Loading branch information
gammam committed Jul 11, 2019
1 parent f38d420 commit 6cfee66
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 82 deletions.
48 changes: 48 additions & 0 deletions src/api/controllers/s4aAdmin.js
Original file line number Diff line number Diff line change
@@ -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);
};
142 changes: 63 additions & 79 deletions src/api/controllers/s4aController.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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"
});

}

console.log("insertResult: ", insertResult);

res.json({
statusCode: 200,
statusResponse: "OK",
messages: "updated"
});
};
6 changes: 3 additions & 3 deletions src/api/routes/s4aRoutes.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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
Expand Down

1 comment on commit 6cfee66

@vercel
Copy link

@vercel vercel bot commented on 6cfee66 Jul 11, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.