-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a7351bd
commit e772c02
Showing
3 changed files
with
127 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
'use strict'; | ||
|
||
class User { | ||
constructor(r, name) { | ||
this.r = r.table(name); | ||
this._name = name; | ||
this._indexes = ['createdAt'] | ||
} | ||
|
||
find(id, done) { | ||
this.r.get(id) | ||
.run() | ||
.then(function(user) { | ||
return done(null, user) | ||
}) | ||
.error(function(err) { | ||
return done(err) | ||
}) | ||
} | ||
|
||
create(user, done) { | ||
var fullUser = Object.assign(user, {createdAt: new Date()}); | ||
this.r.insert(fullUser, {returnChanges: true}) | ||
.run() | ||
.then(function(result) { | ||
return done(null, result.changes[0].new_val) | ||
}) | ||
.error(function(err) { | ||
return done(err) | ||
}) | ||
} | ||
|
||
remove(id, done) { | ||
this.r.get(id) | ||
.delete({returnChanges: true}) | ||
.run() | ||
.then(function(result) { | ||
return done(null, result) | ||
}) | ||
.error(function(err) { | ||
return done(err) | ||
}) | ||
} | ||
} | ||
|
||
module.exports = User; |
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,78 @@ | ||
var path = require('path'); | ||
var db = require(path.join(path.dirname(require.main.filename), 'models')); | ||
|
||
var Boom = require('boom'); | ||
var _ = require('lodash'); | ||
|
||
exports.register = function(server, options, next) { | ||
var User = db.model('User'); | ||
|
||
server.route([ | ||
{ | ||
method: 'GET', | ||
path: '/users/{id?}', | ||
config: { | ||
handler: function(req, reply) { | ||
if (req.params.id) { | ||
User.find(req.params.id, function(err, user) { | ||
if (err) { | ||
return reply(Boom.badImplementation(err)); | ||
} | ||
if (_.isEmpty(user)) { | ||
return reply(Boom.notFound()); | ||
} | ||
return reply(user); | ||
}); | ||
} else { | ||
// Alternative: use native r methods | ||
User.r.run() | ||
.then(function(users) { | ||
return reply(users); | ||
}) | ||
.error(function(err) { | ||
return reply(Boom.badImplementation(err)); | ||
}) | ||
} | ||
} | ||
} | ||
}, | ||
{ | ||
method: 'POST', | ||
path: '/users', | ||
config: { | ||
handler: function(req, reply) { | ||
User.create(req.payload, function(err, newUser) { | ||
if (err) { | ||
return reply(Boom.badImplementation(err)); | ||
} | ||
return reply(newUser); | ||
}); | ||
} | ||
} | ||
}, | ||
{ | ||
method: 'DELETE', | ||
path: '/users/{id}', | ||
config: { | ||
handler: function(req, reply) { | ||
if (req.params.id) { | ||
User.remove(req.params.id, function(err, result) { | ||
if (err) { | ||
return reply(Boom.badImplementation(err)); | ||
} | ||
return reply(result); | ||
}); | ||
} else { | ||
return reply(Boom.badImplementation()); | ||
} | ||
} | ||
} | ||
} | ||
]); | ||
|
||
next(); | ||
}; | ||
|
||
exports.register.attributes = { | ||
name: 'users' | ||
}; |