Slick 3.1 pagination query

70 views
Skip to first unread message

tom.la...@incrowdnow.com

unread,
Jan 17, 2017, 4:57:28 PM1/17/17
to Slick / ScalaQuery
Hello Slickers,

I'm fairly new to Slick and trying to formulate a query to paginate objects. The following pleases the compiler:

def search(objectType: String,
           objectId
: Long,
           activityTypeIds
: Seq[Long],
           limit
: Int,
           offset
: Int): Future[(Seq[ActivityLog], Int)] = {
 
import scala.concurrent.ExecutionContext.Implicits.global
  db.run(activityLogs
    .filter(_.objectId === objectId)
   
.filter(_.objectType === objectType)
   
.filter(_.activityTypeId inSet activityTypeIds).result
     
.map { r =>
       
(r.drop(offset).take(limit), r.size)
     
})
}

and yet I don't think it is correct, and when I try to use it it appears to be returning Option[Nothing]. Here is the controller method that calls the above:

def getActivity(id:Long):Action[AnyContent] = API.secureAsync { (user, request) => 
  val offset:Int = request.queryString.get("offset").getOrElse(20)
 
val limit:Int = request.queryString.get("limit").getOrElse(0)
 
val sortBy:String = request.queryString.get("sortBy").getOrElse("activityTime")
 
val sortDir:String = request.queryString.get("sortDir").getOrElse("desc")
 
val activityTypeIds:Seq[Long] = request.queryString.get("activityTypeId").getOrElse(Seq())
 
Survey.readWithId(id).map { survey =>
   
if (user.isAnyAdmin || user.isAnySubscriber(survey.subscriptionId)) {
     
val activityLogPage:Future[(Seq[ActivityLog], Int)] = activityLog.search("survey", id, activityTypeIds, limit, offset)
      activityLogPage
.map { ap =>
       
Ok(Json.obj(
         
"activity" -> ap._1.map(_.adminJson),
         
"totalCount" -> ap._2,
         
"limit" -> limit,
         
"offset" -> offset
       
)) as JSON
     
}
   
}  //else notAllowedTo("see this surveys's activity")
  } //getOrElse notAllowedTo("see this survey's activity")
}

Can you tell me what I'm doing wrong?

TIA, Tom
Reply all
Reply to author
Forward
0 new messages