patternMinor
Scala Play Controller - Refactoring out Duplication for Restful Web Services
Viewed 0 times
refactoringduplicationcontrollerscalaplayservicesforrestfulwebout
Problem
I'm new to scala and I'm noticing I have some repeating code in my controllers.
I'm wondering if anyone can give some hints for traits and parameterization for eliminating duplicate code for basic crud operations on the web services - I'm still a little confused about how to use scala 'generics'/parameters.
Don't mind the json - I've fixed this horrible scrappy code now using the objects to render themselves as json.
Code piece
```
package controllers
import play.api.mvc._
import securesocial.core._
import play.Logger
import models.{ResponseMessage, GraphedUser}
import com.tinkerpop.blueprints.Vertex
import service.{BaseFramedEntity, SecureSocialUserService, FramedGraphService, GraphDbService}
import play.api.libs.json._
import scala.util.parsing.json
import json.JSONArray
import play.api.libs.concurrent.{Akka, Promise}
import play.api.Play.current
import securesocial.core.UserId
import Predef._
import play.mvc.With
/**
* Controller for user api
*/
object UsersController extends BaseController with securesocial.core.SecureSocial {
val collectionName = "users"
def index = Action {
implicit request =>
//get all users
val pageOption = request.queryString.get("page")
pageOption match {
case Some(page) => {
val resultsPerPageOption = request.queryString.get("numResults")
resultsPerPageOption match {
case Some(size) => Ok(renderUserJson(getUserList(page.head.toInt, size.head.toInt))) //Both page and size exists
case None => Ok(renderUserJson(getUserList(page.head.toInt, defaultPageSize))) //only page exists
}
}
case None => Ok(renderUserJson(getUserList(defaultPage, defaultPageSize))) //page doesn't exist. Ignore size.
}
}
def view(id: String) = Action {
implicit request =>
val userOption = FramedGraphService.getFramedVertexByProperty("userId", id, classOf[GraphedUser])
userOption match {
case Some(user) => {
Ok(u
I'm wondering if anyone can give some hints for traits and parameterization for eliminating duplicate code for basic crud operations on the web services - I'm still a little confused about how to use scala 'generics'/parameters.
Don't mind the json - I've fixed this horrible scrappy code now using the objects to render themselves as json.
Code piece
```
package controllers
import play.api.mvc._
import securesocial.core._
import play.Logger
import models.{ResponseMessage, GraphedUser}
import com.tinkerpop.blueprints.Vertex
import service.{BaseFramedEntity, SecureSocialUserService, FramedGraphService, GraphDbService}
import play.api.libs.json._
import scala.util.parsing.json
import json.JSONArray
import play.api.libs.concurrent.{Akka, Promise}
import play.api.Play.current
import securesocial.core.UserId
import Predef._
import play.mvc.With
/**
* Controller for user api
*/
object UsersController extends BaseController with securesocial.core.SecureSocial {
val collectionName = "users"
def index = Action {
implicit request =>
//get all users
val pageOption = request.queryString.get("page")
pageOption match {
case Some(page) => {
val resultsPerPageOption = request.queryString.get("numResults")
resultsPerPageOption match {
case Some(size) => Ok(renderUserJson(getUserList(page.head.toInt, size.head.toInt))) //Both page and size exists
case None => Ok(renderUserJson(getUserList(page.head.toInt, defaultPageSize))) //only page exists
}
}
case None => Ok(renderUserJson(getUserList(defaultPage, defaultPageSize))) //page doesn't exist. Ignore size.
}
}
def view(id: String) = Action {
implicit request =>
val userOption = FramedGraphService.getFramedVertexByProperty("userId", id, classOf[GraphedUser])
userOption match {
case Some(user) => {
Ok(u
Solution
for all the option matchers you could use getOrElse
And remember: Option is also a list with all given features
here an example
And remember: Option is also a list with all given features
here an example
val resultsPerPageOption = request.queryString.get("numResults")
val size= resultsPerPageOption.map(_.head.toInt).getOrElse(defaultPageSize)
Ok(renderUserJson(getUserList(page.head.toInt, size)))Code Snippets
val resultsPerPageOption = request.queryString.get("numResults")
val size= resultsPerPageOption.map(_.head.toInt).getOrElse(defaultPageSize)
Ok(renderUserJson(getUserList(page.head.toInt, size)))Context
StackExchange Code Review Q#21637, answer score: 2
Revisions (0)
No revisions yet.