From 0942be0f77236935c1a05e05f10f0518c2be0747 Mon Sep 17 00:00:00 2001 From: Matt Huntington Date: Thu, 25 Aug 2016 16:41:24 -0400 Subject: [PATCH] API adds/gets runs for/from users --- controllers/runs.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/controllers/runs.js b/controllers/runs.js index 7ea11ce..d957b43 100644 --- a/controllers/runs.js +++ b/controllers/runs.js @@ -2,13 +2,15 @@ var express = require('express'); var bodyParser = require('body-parser'); var controller = express.Router(); var Run = require('../models/runs.js'); +var User = require('../models/users.js'); controller.use(bodyParser.json()); controller.get('/', function(req, res){ - Run.findAll({}).then(function(foundRuns){ - //createdRun is the object representation of the row created in the DB - res.json(foundRuns); + User.findById(req.session.currentUser.id).then(function(foundUser){ + foundUser.getRuns().then(function(currentUserRuns){ + res.json(currentUserRuns); + }) }); }); @@ -23,9 +25,13 @@ controller.get('/:id', function(req, res){ }); controller.post('/', function(req, res){ - Run.create(req.body).then(function(createdRun){ - //createdRun is the object representation of the row created in the DB - res.json(createdRun); + User.findById(req.session.currentUser.id).then(function(foundUser){ + Run.create(req.body).then(function(createdRun){ + //createdRun is the object representation of the row created in the DB + foundUser.addRun(createdRun).then(function(createdRun){ + res.json(createdRun); + }) + }); }); });