Skip to content

Commit

Permalink
Add user model and check input email by dublicates
Browse files Browse the repository at this point in the history
  • Loading branch information
KaaPex committed Jun 14, 2016
1 parent 634783f commit b738f46
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions db.js
Expand Up @@ -14,6 +14,7 @@ if (env === "production") {
var db = {};

db.todo = sequelize.import(path.join(__dirname, '/models/todo.js'));
db.user = sequelize.import(path.join(__dirname, '/models/user.js'));
db.sequelize = sequelize;
db.Sequelize = Sequelize;

Expand Down
2 changes: 1 addition & 1 deletion models/todo.js
@@ -1,5 +1,5 @@
"use strict";
module.exports = function(sequelize, DataType) {
module.exports = (sequelize, DataType) => {
var todo = sequelize.define('todo', {
description: {
type: DataType.STRING,
Expand Down
29 changes: 29 additions & 0 deletions models/user.js
@@ -0,0 +1,29 @@
"use strict";
module.exports = (sequelize, DataType) => {
var User = sequelize.define('user', {
email: {
type: DataType.STRING,
allowNull: false,
unique: true,
validate: {
isEmail: true
}
},
password: {
type: DataType.STRING,
allowNull: false,
validate: {
len: [7, 100]
}
}
}, {
hooks: {
beforeValidate: (user, options) => {
if (typeof user.email === 'string') {
user.email = user.email.toLowerCase();
}
}
}
});
return User;
};
10 changes: 10 additions & 0 deletions server.js
Expand Up @@ -136,6 +136,16 @@ app.put('/todos/:id', (req, res) => {
}
});

app.post('/users',upload.array() , (req, res, next) => {
var body = _.pick(req.body, 'email', 'password');

db.user.create(body).then((user) => {
res.json(user.toJSON());
}, (e) => {
res.status(400).json(e);
});
});

db.sequelize.sync().then(function() {
app.listen(PORT, function() {
console.log('Express listening on port ' + PORT + '!');
Expand Down

0 comments on commit b738f46

Please sign in to comment.