Learning Scala and trying to refactor following two functions to remove duplicate logic. Should I be creating a higher order function or something else to avoid duplicate code? Have a few more similar methods with such duplicate code with only difference in calling different Domain methods.
Little confused over refactoring. Using Scala version 2.10
def authenticate = Action(parse.json) { request =>
val json = request.body
val input = Json.fromJson[User](json)
input.asOpt match {
case Some(m: User) => Ok(Domain.authenticate(m)).as("application/json")
case None => Ok("bad input")
}
}
def addUser = Action(parse.json) { request =>
val json = request.body
val input = Json.fromJson[User](json)
input.asOpt match {
case Some(m: User) => Ok(Domain.addUser(m)).as("application/json")
case None => Ok("bad input")
}
}
match
portion of the two methods) will do fine. – Randall Schulz