Skip to content

Commit

Permalink
Implement serializers (#19)
Browse files Browse the repository at this point in the history
* Use serializers to generate JSON responses

* Add new files to cleanup script
  • Loading branch information
talyssonoc authored Oct 7, 2017
1 parent a3979dc commit 6116c5f
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 9 deletions.
4 changes: 3 additions & 1 deletion scripts/cleanup.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ const tasks = new Listr([
from: [
/\s*const \{(\n.*)+.*app\/user'\);/,
/\s*const.*UsersRepository'\);/,
/\s*const.*UserSerializer'\);/,
/\, User: UserModel/,
/\s*usersRepository.*\}\]/,
/\,\s*UserModel/,
/\s+createUser(.|\n)+.*DeleteUser\n/,
/\s+userSerializer: UserSerializer\n/
],
to: ''
});
Expand All @@ -44,10 +46,10 @@ const tasks = new Listr([
path.join(srcAndTestPath, 'app', 'user', '**'),
path.join(srcAndTestPath, 'domain', 'user', '**'),
path.join(srcAndTestPath, 'infra', 'user', '**'),
path.join(srcAndTestPath, 'interfaces', 'http', 'user', '**'),
path.join(srcPath, 'infra', 'database', 'migrate', '*.js'),
path.join(srcPath, 'infra', 'database', 'seeds', '*.js'),
path.join(srcPath, 'infra', 'database', 'models', 'User.js'),
path.join(srcPath, 'interfaces', 'http', 'user', '**'),
path.join(testPath, 'features', 'api', 'users', '**'),
path.join(testPath, 'support', 'factories', '*.js')
]);
Expand Down
7 changes: 7 additions & 0 deletions src/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const {
DeleteUser
} = require('./app/user');

const UserSerializer = require('./interfaces/http/user/UserSerializer');

const Server = require('./interfaces/http/Server');
const router = require('./interfaces/http/router');
const loggerMiddleware = require('./interfaces/http/logging/loggerMiddleware');
Expand Down Expand Up @@ -65,4 +67,9 @@ container.registerClass({
deleteUser: DeleteUser
});

// Serializers
container.registerValue({
userSerializer: UserSerializer
});

module.exports = container;
10 changes: 10 additions & 0 deletions src/interfaces/http/user/UserSerializer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const UserSerializer = {
serialize({ id, name }) {
return {
id,
name
};
}
};

module.exports = UserSerializer;
26 changes: 18 additions & 8 deletions src/interfaces/http/user/UsersController.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const UsersController = {
get router() {
const router = Router();

router.use(inject('userSerializer'));

router.get('/', inject('getAllUsers'), this.index);
router.get('/:id', inject('getUser'), this.show);
router.post('/', inject('createUser'), this.create);
Expand All @@ -16,26 +18,30 @@ const UsersController = {
},

index(req, res, next) {
const { getAllUsers } = req;
const { getAllUsers, userSerializer } = req;
const { SUCCESS, ERROR } = getAllUsers.outputs;

getAllUsers
.on(SUCCESS, (users) => {
res.status(Status.OK).json(users);
res
.status(Status.OK)
.json(users.map(userSerializer.serialize));
})
.on(ERROR, next);

getAllUsers.execute();
},

show(req, res, next) {
const { getUser } = req;
const { getUser, userSerializer } = req;

const { SUCCESS, ERROR, NOT_FOUND } = getUser.outputs;

getUser
.on(SUCCESS, (user) => {
res.status(Status.OK).json(user);
res
.status(Status.OK)
.json(userSerializer.serialize(user));
})
.on(NOT_FOUND, (error) => {
res.status(Status.NOT_FOUND).json({
Expand All @@ -49,12 +55,14 @@ const UsersController = {
},

create(req, res, next) {
const { createUser } = req;
const { createUser, userSerializer } = req;
const { SUCCESS, ERROR, VALIDATION_ERROR } = createUser.outputs;

createUser
.on(SUCCESS, (user) => {
res.status(Status.CREATED).json(user);
res
.status(Status.CREATED)
.json(userSerializer.serialize(user));
})
.on(VALIDATION_ERROR, (error) => {
res.status(Status.BAD_REQUEST).json({
Expand All @@ -68,12 +76,14 @@ const UsersController = {
},

update(req, res, next) {
const { updateUser } = req;
const { updateUser, userSerializer } = req;
const { SUCCESS, ERROR, VALIDATION_ERROR, NOT_FOUND } = updateUser.outputs;

updateUser
.on(SUCCESS, (user) => {
res.status(Status.ACCEPTED).json(user);
res
.status(Status.ACCEPTED)
.json(userSerializer.serialize(user));
})
.on(VALIDATION_ERROR, (error) => {
res.status(Status.BAD_REQUEST).json({
Expand Down
40 changes: 40 additions & 0 deletions test/unit/interfaces/http/user/UserSerializer.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const { expect } = require('chai');
const UserSerializer = require('src/interfaces/http/user/UserSerializer');
const User = require('src/domain/user/User');

describe('Interfaces :: HTTP :: User :: UserSerializer', () => {
it('returns id and name', () => {
const serializedUser = UserSerializer.serialize({
id: 123,
name: 'The User'
});

expect(serializedUser).to.eql({
id: 123,
name: 'The User'
});
});

it('ignores extra attributes', () => {
const serializedUser = UserSerializer.serialize({
id: 321,
name: 'The User',
unknown: 'Hello!'
});

expect(serializedUser).to.eql({
id: 321,
name: 'The User'
});
});

it('is able to serialize user entity instances', () => {
const user = new User({ id: 1, name: 'User :)' });
const serializedUser = UserSerializer.serialize(user);

expect(serializedUser).to.eql({
id: 1,
name: 'User :)'
});
});
});

0 comments on commit 6116c5f

Please sign in to comment.