-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
user prefs routes: create new, get one, update one (#36)
- Loading branch information
1 parent
0495303
commit 84a2768
Showing
5 changed files
with
134 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
'use strict'; | ||
|
||
const db = require('../../../db'); | ||
const User = db.model('user'); | ||
const UserPref = db.model('userPrefs'); | ||
const router = require('express').Router(); | ||
|
||
// Mounted on /api/users | ||
|
||
// GET all users (ADMIN) | ||
router.get('/', (req, res, next) => { | ||
User.findAll() | ||
.then(allUsers => { | ||
res.status(200).json(allUsers); | ||
}) | ||
.catch(next); | ||
}); | ||
|
||
// Create new preferences | ||
router.post('/:userId/preferences', (req, res, next) => { | ||
req.body.userId = req.params.userId; | ||
UserPref.create(req.body) | ||
.then(createdPref => { | ||
res.status(201).json(createdPref); | ||
}) | ||
.catch(next); | ||
}); | ||
|
||
// Get a particular user loaded with their preferences | ||
router.get('/:userId/preferences', (req, res, next) => { | ||
UserPref.findOne({ | ||
where: { | ||
userId: req.params.userId | ||
} | ||
}) | ||
.then(foundPref => { | ||
res.status(200).json(foundPref); | ||
}) | ||
.catch(next); | ||
}); | ||
|
||
// Edit user preferences | ||
router.put('/:userId/preferences', (req, res, next) => { | ||
UserPref.findOne({ | ||
where: { | ||
userId: req.params.userId | ||
} | ||
}) | ||
.then(foundPref => { | ||
return foundPref.update(req.body) // will be on ctrl scope | ||
}) | ||
.then(updatedPref => { | ||
res.status(200).json(updatedPref); | ||
}) | ||
.catch(next); | ||
}); | ||
|
||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Instantiate all models | ||
var expect = require('chai').expect; | ||
var Sequelize = require('sequelize'); | ||
var db = require('../../../server/db'); | ||
var supertest = require('supertest'); | ||
|
||
|
||
describe('User Route', function () { | ||
var app, User; | ||
|
||
var fakeUser = { | ||
email: 'tubular@tube.noob', | ||
password: 'n00b', | ||
firstName: 'Bob', | ||
lastName: 'HeartThrob', | ||
}; | ||
|
||
// var fakeUserPref = { | ||
// vegetarian: true, | ||
// dislikes: ['eggs', 'soup'], | ||
// userId: 1 | ||
// }; | ||
|
||
beforeEach('Sync DB', function () { | ||
return db.sync({ force: true }) | ||
}); | ||
|
||
beforeEach('Create app, seed User', function () { | ||
app = require('../../../server/app')(db); | ||
User = db.model('user'); | ||
}); | ||
|
||
describe('User preferences routes', function () { | ||
|
||
var guestAgent; | ||
|
||
beforeEach('Create fake user', function() { | ||
return User.create(fakeUser); | ||
}) | ||
|
||
beforeEach('Create guest agent', function () { | ||
guestAgent = supertest.agent(app); | ||
}); | ||
|
||
it('should get a 201 response with the NEW preference as the body', function (done) { | ||
guestAgent.post('/api/users/1/preferences') | ||
.send({ | ||
vegetarian: true, | ||
dislikes: ['eggs', 'soup'], | ||
}) | ||
.expect(201) | ||
.end(function (err, response) { | ||
if (err) return done(err); | ||
expect(response.body.vegetarian).to.be.equal(true); | ||
expect(response.body.userId).to.be.equal(1); | ||
done(); | ||
}); | ||
|
||
// it('should get user pref information', function(done) { | ||
// guestAgent.get('/api/users/1/preferences') | ||
// .expect(201) | ||
// .end(function(err, response) { | ||
// if (err) return done(err); | ||
// expect(response.body.vegetarian).to.be.equal(true); | ||
// expect(response.body.dislikes).to.be.an('array'); | ||
// }) | ||
// }) | ||
}); | ||
|
||
}); | ||
}); | ||
|