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
const { attributes } = require('structure'); | |
const User = attributes({ | |
id: Number, | |
name: { | |
type: String, | |
required: true | |
}, | |
age: Number | |
})(class User { |
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
const EventEmitter = require('events'); | |
class CreateUser extends EventEmitter { | |
constructor({ usersRepository }) { | |
super(); | |
this.usersRepository = usersRepository; | |
} | |
execute(userData) { | |
const user = new User(userData); |
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
const UsersController = { | |
create(req, res) { | |
const createUser = new CreateUser({ usersRepository }); | |
createUser | |
.on('SUCCESS', (user) => { | |
res.status(201).json(user); | |
}) | |
.on('VALIDATION_ERROR', (error) => { | |
res.status(400).json({ |
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
class SequelizeUsersRepository { | |
add(user) { | |
const { valid, errors } = user.validate(); | |
if(!valid) { | |
const error = new Error('ValidationError'); | |
error.details = errors; | |
return Promise.reject(error); | |
} |
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
import api from './infra/api'; // has no dependencies | |
import { validateUser } from './domain/user'; // has no dependencies | |
import makeUserRepository from './infra/user/userRepository'; | |
import makeArticleRepository from './infra/article/articleRepository'; | |
import makeCreateUser from './app/user/createUser'; | |
import makeGetArticle from './app/article/getArticle'; | |
const userRepository = makeUserRepository({ | |
api | |
}); |
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
export default ({ validateUser, userRepository }) => async (userData, { onSuccess, onError, onValidationError }) => { | |
if(!validateUser(userData)) { | |
return onValidationError(new Error('Invalid user')); | |
} | |
try { | |
const user = await userRepository.add(userData); | |
onSuccess(user); | |
} catch(error) { |
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
// User.js | |
const LEGAL_AGE = 21; | |
export const isMajor = (user) => { | |
return user.age >= LEGAL_AGE; | |
}; | |
// usage | |
import * as User from './User.js'; |
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
// User.js | |
export default class User { | |
static LEGAL_AGE = 21; | |
constructor({ id, age }) { | |
this.id = id; | |
this.age = age; | |
} | |
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
class User { | |
constructor({ name, age }) { | |
this.name = name; | |
this.age = age; | |
} | |
validate() { | |
const hasName = Boolean(this.name); | |
const hasMinAge = this.age >= User.MIN_AGE; |
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
var path = require('path'); | |
module.exports = { | |
'config': path.resolve('config', 'database.json'), | |
'migrations-path': path.resolve('src/infra/sequelize', 'migrations'), | |
'models-path': path.resolve('src/infra/sequelize', 'models'), | |
'seeders-path': path.resolve('src/infra/sequelize', 'seeders') | |
}; |
OlderNewer