Tutorial de como construir o framework do projeto
Adapter.js
--
export default class Adapter {
constructor() {
self.mongoose = require('mongoose')
self.MenorModel = mongoose.model('Menor')
}
save(data) {
const menor = new self.MenorModel(data)
return menor.save().then((data) => {
return data
})
}
}
Entity.js
Classe que contém os dados e as regras de negócio.
import Joi from 'joi'
export default class Entity {
constructor {
self.Adapter = require('./Adapter').default
}
validateMenorData(data) {
return new Promise((resolve, reject) => {
const schema = Joi.object().keys({
username: Joi.string().optional(),
email: Joi.string().email()
})
const result = Joi.validate(data, schema);
if (result) {
resolve(data)
} else {
reject('')
}
})
}
createMenor(data) {
const adapter = new self.Adapter
return adapter.save(data)
}
}
Interactor.js
Contém as regras de negócio específicas. Manipula os dados relevantes do Request Model e interage com as Entities.
export default class Interactor {
constructor() {
self.Entity = require('./Entity').default
}
createUser(data) {
const entity = new self.Entity
return entity.validateMenorData(data)
.then(data => {
entity.createMenor(data)
})
.catch(error => {
console.log(error)
})
}
}
Translator.js
--
export default class Translator {
constructor() {
self.Interactor = require('./Interactor').default
}
post(request, response) {
const interactor = new self.Interactor()
let data = request.body || {}
interactor.createUser(data).then(data => console.log(data))
}
get()
}