Advertisement
Guest User

Untitled

a guest
Sep 14th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class UserModel {
  2.  
  3.   constructor() {
  4.     this.username = ''
  5.     this.password = ''
  6.  
  7.     this.firstName = ''
  8.     this.lastName = ''
  9.     this.age = null
  10.   }
  11. }
  12.  
  13. class UserRepository {
  14.  
  15.   constructor(dbSession) {
  16.     this.dbSession = dbSession
  17.   }
  18.  
  19.   getAll() {
  20.     const results = this.dbSession.query("SELECT * FROM users;")
  21.    
  22.     return results.map(result => {
  23.       const model = new UserModel()
  24.       model.firstName = result.get('firstName')
  25.       model.lastName = result.get('lastName')
  26.       model.age = result.get('age')
  27.       model.password = result.get('password')
  28.       model.username = result.get('username')
  29.  
  30.       return model
  31.     })
  32.   }
  33.  
  34.   create() {
  35.     // implement
  36.   }
  37.  
  38.   update() {
  39.     // implement
  40.   }
  41.  
  42.   delete() {
  43.     // implement
  44.   }
  45. }
  46.  
  47. class UserDTO {
  48.  
  49.   constructor() {
  50.     this.firstName = ''
  51.     this.lastName = ''
  52.     this.age = null
  53.   }
  54.  
  55.   static fromUserModel(userModel) {
  56.     const userDTO = new UserDTO()
  57.     userDTO.firstName = userModel.firstName
  58.     userDTO.lastName = userModel.lastName
  59.     userDTO.age = userModel.age
  60.  
  61.     return userDTO
  62.   }
  63. }
  64.  
  65. class UserService {
  66.  
  67.   constructor(userRepository) {
  68.     this.userRepository = userRepository
  69.   }
  70.  
  71.   getAllUsers() {
  72.     const users = this.userRepository.getAll()
  73.     return users.map(userModel => UserDTO.fromModel(userModel))
  74.   }
  75. }
  76.  
  77. class UserController {
  78.  
  79.   constructor(userService) {
  80.     this.userService = userService
  81.   }
  82.  
  83.   getAllUsers() {
  84.     this.userService.getAllUsers()
  85.   }
  86. }
  87.  
  88. // main.js
  89. const dbSession = driver.getSession()
  90. const userRepostory = new UserRepository(dbSession)
  91. const userService = new UserService(userRepostory)
  92. const userController = new UserController(userService)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement