Last active
June 15, 2017 23:40
-
-
Save jonathan-fulton/33ba2d5df6c3f8cca6f67be3362e5f45 to your computer and use it in GitHub Desktop.
Simple SFV example
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 Router {\ | |
registerRoutes() { | |
this.post('/user', UserController.create); | |
} | |
} | |
class UserController { | |
create(req, res) { | |
this._userService.createFromRequest(req) | |
.then(user => res.send(user.toJSON())); | |
} | |
} | |
class UserService { | |
/** | |
* Parses info off the request object in order to create a new user | |
* | |
* @param {object} req | |
* @param {string} req.email | |
* @param {striong} req.name | |
* | |
* @returns {UserVO} | |
*/ | |
createFromRequest(req) { | |
const createVO = this._userService_CreateVOFactory.createFromRequest(req); | |
return this.create(createVO); | |
} | |
/** | |
* Core function that handles creating a new user | |
* | |
* @param {UserService_CreateVO} createVO | |
* @returns {UserVO} | |
*/ | |
async create(createVO) { | |
const newUserId = await this._userService_Dao.create(createVO); | |
return this.get(newUserId); | |
} | |
/** | |
* @param {int} id | |
* @returns {UserVO} | |
*/ | |
async get(id) { | |
return this._userService_Dao.get(id); | |
} | |
} | |
class UserService_Dao { | |
/** | |
* @param {UserService_CreateVO} createVO | |
* @returns {int} | |
*/ | |
async create(createVO) { | |
return await this.db.insert(createVO); | |
} | |
} | |
class UserService_CreateVOFactory { | |
createFromRequest(req) { | |
return new UserService_CreateVO({ | |
email: req.email, | |
name: req.name | |
}); | |
} | |
} | |
class UserService_CreateVO { | |
constructor(obj) { | |
// assigns params from obj --> properties | |
} | |
get email() | |
get name() | |
} | |
class UserVO { | |
constructor(obj) { | |
// assigns params from obj --> properties | |
} | |
get id() | |
get email() | |
get name() | |
get dateCreated() | |
get dateUpdated() | |
} | |
class UserVOFactory { | |
createFromDbResults(dbResults) { | |
return new UserVO({ | |
id: dbResults.id, | |
email: dbResults.email, | |
name: dbResults.name, | |
dateCreated: dbResults.date_created, | |
dateUpdated: dbResults.date_updated | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment