|
#Tutorial de como construir o framework do projeto
|
|
#Tutorial de como construir o framework do projeto
|
|
|
|
|
|
##Adapter.js
|
|
##Adapter.js
|
|
````
|
|
|
|
export default class Adapter {
|
|
export default class Adapter {
|
|
constructor() {
|
|
constructor() {
|
|
self.mongoose = require('mongoose')
|
|
self.mongoose = require('mongoose')
|
... | @@ -16,3 +16,73 @@ export default class Adapter { |
... | @@ -16,3 +16,73 @@ export default class Adapter { |
|
})
|
|
})
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
##Entity.js
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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()
|
|
|
|
} |