Skip to content

Instantly share code, notes, and snippets.

@erangaeb
Created January 31, 2016 08:56
Show Gist options
  • Save erangaeb/d8f0918f0dca82b85b96 to your computer and use it in GitHub Desktop.
Save erangaeb/d8f0918f0dca82b85b96 to your computer and use it in GitHub Desktop.
Handler dependencies via self typed annotaion
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