API adds/gets runs for/from users

master
Matt Huntington 9 years ago
parent 3adcd6ff71
commit 0942be0f77

@ -2,13 +2,15 @@ var express = require('express');
var bodyParser = require('body-parser'); var bodyParser = require('body-parser');
var controller = express.Router(); var controller = express.Router();
var Run = require('../models/runs.js'); var Run = require('../models/runs.js');
var User = require('../models/users.js');
controller.use(bodyParser.json()); controller.use(bodyParser.json());
controller.get('/', function(req, res){ controller.get('/', function(req, res){
Run.findAll({}).then(function(foundRuns){ User.findById(req.session.currentUser.id).then(function(foundUser){
//createdRun is the object representation of the row created in the DB foundUser.getRuns().then(function(currentUserRuns){
res.json(foundRuns); res.json(currentUserRuns);
})
}); });
}); });
@ -23,9 +25,13 @@ controller.get('/:id', function(req, res){
}); });
controller.post('/', function(req, res){ controller.post('/', function(req, res){
User.findById(req.session.currentUser.id).then(function(foundUser){
Run.create(req.body).then(function(createdRun){ Run.create(req.body).then(function(createdRun){
//createdRun is the object representation of the row created in the DB //createdRun is the object representation of the row created in the DB
foundUser.addRun(createdRun).then(function(createdRun){
res.json(createdRun); res.json(createdRun);
})
});
}); });
}); });

Loading…
Cancel
Save