Scala/Play/Squeryl检索多个参数 [英] Scala/Play/Squeryl Retrieve multiple params

查看:99
本文介绍了Scala/Play/Squeryl检索多个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下网址:http://localhost/api/books/?bookId=21&bookId=62?authorId=2

我想用Scala检索所有bookId值,然后使用Squeryl在数据库中进行提取.

I want to retrieve all the bookId values with Scala and then use Squeryl to do a fetch in a the database.

我将PlayFrameWork用作Web服务器,所以这是我的代码:

I'm using the PlayFrameWork as the WebServer, so here's my code :

val params = request.queryString.map { case (k, v) => k -> v(0) } // Retrieve only one the first occurence of a param

因此 params.get("bookId")将仅获取bookId参数中的最后一个值. e-g: 62 .

So params.get("bookId") will only get the last value in the bookId params. e-g : 62.

要检索我所有的bookId参数,我尝试了以下操作: val params = request.queryString.map { case (k, v) => k -> v }这样我可以获得Seq [String],但是不是Seq [String]的authorId呢?

To retrieve all my bookId params i tried this : val params = request.queryString.map { case (k, v) => k -> v } so i can get a Seq[String], but what about the authorId which is not a Seq[String]? .

最后,我想使用Squeryl在数据库中获取bookIds和authorId:

At the end i want to fetch the bookIds and authorId in my DB using Squeryl :

(a.author_id === params.get("authorId").?) and
(params.get("bookId").map(bookIds: Seq[String] => b.bookId in bookIds))

在我的控制器中,我得到了参数并打开了数据库连接:

In my controller i get the params and open the DB connection :

val params = request.queryString.map { case (k, v) => k -> v(0) }

DB.withTransaction() { where(Library.whereHelper(params)}

在我的模型中,我使用以下查询:

In my model i use the queries :

def whereHelper(params : Map[String,String]) = {

 (a.author_id === params.get("authorId").?) and
 (params.get("bookId").map{bookIds: Seq[String] => b.bookId in bookIds})
}

由于bookIds是一个列表,因此我需要使用Seq [String].有一种方法可以对字符串(authorId)和字符串列表(bookIds)使用request.queryString.map {case(k,v)=> k-> v}?

Since bookIds is a list, i need to use the Seq[String]. There's a way to use request.queryString.map { case (k, v) => k -> v } for both a string (authorId) and a list of strings (bookIds) ?

谢谢

推荐答案

如果我真的了解您要执行的操作,则想知道如何从queryString获取参数.这非常简单,您可以在控制器上执行以下操作:

If I really understand what you are trying to do, you want to know how to get the parameters from queryString. This is pretty simple and you can do the following at your controller:

def myAction = Action { request =>
    // get all the values from parameter named bookId and 
    // transforming it to Long. Maybe you don't want the map
    // and then you can just remove it.
    val bookIds: Seq[Long] = request.queryString("bookId").map(_.toLong)

    // Notice that now I'm using getQueryString which is a helper
    // method to access a queryString parameter. It returns an
    // Option[String] which we are mapping to a Option[Long].
    // Again, if you don't need the mapping, just remove it.
    val authorId: Option[Long] = request.getQueryString("authorId").map(_.toLong)

    DB.withTransaction() { where(Library.whereHelper(authorId, bookIds) }

    // Do something with the result
}

在您的模型上,您将拥有:

At your model you will have:

def whereHelper(authorId: Option[Long], booksId: List[Long]) = authorId match {
  case Some(author_id) =>
      (a.author_id === author_id) and
      (b.bookId in bookIds)
  case None =>
      (b.bookId in bookIds)
}

我留下了明确的类型来帮助您了解发生了什么.现在,由于您同时拥有这两个值,因此您只需在查询中使用这些值即可.

I've left explicit types to help you understand what is happen. Now, since you have both values, you can just use the values at your query.

但是,由于您希望在模型上收到params: Map[String, Seq[String]],并且在获取authorId方面存在问题,因此您可以执行以下操作:

But, since you want to receive a params: Map[String, Seq[String]] at your models and is just having problems about how to get the authorId, here is what you can do:

def whereHelper(params: Map[String, Seq[String]]) = {
  // Here I'm being defensive to the fact that maybe there is no
  // "booksIds" key at the map. So, if there is not, an Seq.empty
  // will be returned. map method will run only if there is something
  // at the Seq.
  val booksIds = params.getOrElse("booksIds", Seq.empty).map(_.toLong)

  // The same defensive approach is being used here, and also getting
  // the head as an Option, so if the Seq is empty, a None will be 
  // returned. Again, the map will be executed only if the Option
  // is a Some, returning another Some with the value as a Long.
  val authorId = params.getOrElse("authorId", Seq.empty).headOption
  authorId.map(_.toLong) match {
    case Some(author_id) =>
      (a.author_id === author_id) and
      (b.bookId in booksIds)
    case None =>
      (b.bookId in booksIds)
  }
}

当然,您拥有的参数越多,此方法将越复杂.

Of course, more parameters you have, more complicated this method will be.

这篇关于Scala/Play/Squeryl检索多个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆