Skip to content

Commit

Permalink
user prefs routes: create new, get one, update one (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
dyoungsmith authored and johannaperez committed Oct 5, 2016
1 parent 0495303 commit 84a2768
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 41 deletions.
3 changes: 2 additions & 1 deletion server/app/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var router = require('express').Router(); // eslint-disable-line new-cap

router.use('/members', require('./members'));
router.use('/signup', require('./signup'));
router.use('/api/preferences', require('./user-pref'));
router.use('/users', require('./user'));

// Make sure this is after all of
// the registered routes!
Expand All @@ -14,4 +14,5 @@ router.use(function (req, res, next) {
next(err);
});


module.exports = router;
39 changes: 0 additions & 39 deletions server/app/routes/user-pref/index.js

This file was deleted.

59 changes: 59 additions & 0 deletions server/app/routes/user/index.js
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;
2 changes: 1 addition & 1 deletion server/db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ const Ingredient = require('./models/ingredient-model.js');
Ingredient.belongsToMany(Recipe, { through: 'recipes_ingredients', as: 'recipes' });
Recipe.belongsToMany(Ingredient, { through: 'recipes_ingredients', as: 'ingredients' });

UserPref.hasOne(User);
UserPref.belongsTo(User); // UserPref has userId column

module.exports = db;
72 changes: 72 additions & 0 deletions tests/server/routes/user-test.js
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');
// })
// })
});

});
});

0 comments on commit 84a2768

Please sign in to comment.