Last active
March 30, 2016 20:16
-
-
Save keithmichelson/1d0305886d34b9e1bbc009d45205f235 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
//Following Model | |
var _ = require('underscore'); | |
module.exports = function(sequelize, DataTypes) { | |
return sequelize.define('following', { | |
following: { | |
type: DataTypes.INTEGER, | |
allowNull: false, | |
validate: { | |
isBoolean: function (value) { | |
if (typeof value !== 'number') { | |
throw new Error('following must be a number'); | |
} | |
} | |
} | |
} | |
}, | |
{ | |
//instance can be used anywhere in an instance | |
instanceMethods: { | |
//return only JSON the public should see | |
toPublicJSON: function () { | |
var json = this.toJSON(); | |
json.name = json.user.profile.firstName; | |
json.job = json.user.profile.job; | |
json.image = json.user.profile.image; | |
//console.log(json); | |
return _.pick(json, 'id', 'createdAt', 'updatedAt', 'job', 'name', 'image'); | |
} | |
} | |
}); | |
}; |
This file contains hidden or 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
//route | |
app.get('/followings-people', middleware.requireAuthentication, function(req, res) { | |
var query = req.query; | |
var where = { | |
userId: req.user.get('id') | |
}; | |
var include = [ | |
{model: db.user, include: [ | |
{model: db.profile} | |
]} | |
]; | |
db.following.findAll({where: where, include: include}).then( | |
function(following) { | |
res.json(following.map(function(following) { | |
return following.toPublicJSON(); | |
})); | |
}, | |
function(e) { | |
res.status(500).send(); | |
} | |
); | |
}); | |
This file contains hidden or 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
module.exports = function(sequelize, DataTypes) { | |
return sequelize.define('profiles', { | |
firstName: { | |
type: DataTypes.STRING, | |
allowNull: false, | |
validate: { | |
isString: function (value) { | |
if (typeof value !== 'string') { | |
throw new Error('Completed must be a string'); | |
} | |
}, | |
//length 1 or greater or less than 250 | |
len: [1, 250] | |
} | |
}, | |
lastName: { | |
type: DataTypes.STRING, | |
allowNull: false, | |
validate: { | |
isString: function (value) { | |
if (typeof value !== 'string') { | |
throw new Error('Completed must be a string'); | |
} | |
}, | |
//length 1 or greater or less than 250 | |
len: [1, 250] | |
} | |
}, | |
job: { | |
type: DataTypes.STRING, | |
allowNull: false, | |
validate: { | |
isString: function (value) { | |
if (typeof value !== 'string') { | |
throw new Error('Completed must be a string'); | |
} | |
}, | |
//length 1 or greater or less than 250 | |
len: [1, 250] | |
} | |
}, | |
image: { | |
type: DataTypes.STRING, | |
allowNull: false, | |
validate: { | |
isString: function (value) { | |
if (typeof value !== 'string') { | |
throw new Error('Completed must be a string'); | |
} | |
}, | |
//length 1 or greater or less than 250 | |
len: [1, 250] | |
} | |
}, | |
website: { | |
type: DataTypes.STRING, | |
allowNull: false, | |
validate: { | |
isString: function (value) { | |
if (typeof value !== 'string') { | |
throw new Error('Completed must be a string'); | |
} | |
}, | |
//length 1 or greater or less than 250 | |
len: [1, 250] | |
} | |
}, | |
completed: { | |
type: DataTypes.BOOLEAN, | |
allowNull: false, | |
defaultValue: false, | |
validate: { | |
isBoolean: function (value) { | |
if (typeof value !== 'boolean') { | |
throw new Error('Completed must be a boolean'); | |
} | |
} | |
} | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment