Created
January 31, 2016 08:56
-
-
Save erangaeb/d8f0918f0dca82b85b96 to your computer and use it in GitHub Desktop.
Handler dependencies via self typed annotaion
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 EmployeeHandler { | |
| this: EmployeeDbComp with EmployeeServiceComp => | |
| def logger = LoggerFactory.getLogger(this.getClass) | |
| def createEmployee(inputEmp: String): Employee = { | |
| // inputEmp comes as 'emp_id name department' | |
| val tokens = inputEmp.split(" ") | |
| // validate input content | |
| if (tokens.length != 3) { | |
| logger.error(s"Invalid input: ${inputEmp}, employee should contains [id name department]") | |
| throw InvalidEmployeeInput("Invalid input, employee should contains [id name department]") | |
| } | |
| // validate emp_id | |
| if (!Try(tokens(0).toInt).isSuccess) { | |
| logger.error(s"Invalid employee ID: ${tokens(0)}") | |
| throw InvalidEmployeeId("Invalid employee ID " + tokens(0)) | |
| } | |
| val employee = Employee(tokens(0).toInt, tokens(1), tokens(2)) | |
| // create employee via db | |
| employeeDb.createEmployee(employee) | |
| // POST employee to cloud | |
| employeeService.POST(employee) | |
| employee | |
| } | |
| def findEmployee(empId: Int): Employee = { | |
| // validate empId | |
| if (empId == 0) | |
| throw InvalidEmployeeId("Invalid employee ID " + empId) | |
| // find employee via db | |
| employeeDb.getEmployee(empId) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment