lila/app/controllers/RequestGetter.scala

43 lines
1.5 KiB
Scala
Raw Permalink Normal View History

package controllers
2013-12-27 15:12:20 -07:00
import lila.user.UserContext
import lila.common.Form.trueish
import lila.common.IsMobile
2012-04-07 06:22:33 -06:00
import play.api.mvc.RequestHeader
trait RequestGetter {
2013-12-27 15:12:20 -07:00
protected def get(name: String)(implicit ctx: UserContext): Option[String] = get(name, ctx.req)
2012-05-20 17:18:25 -06:00
protected def get(name: String, req: RequestHeader): Option[String] =
2014-03-26 15:11:14 -06:00
req.queryString get name flatMap (_.headOption) filter (_.nonEmpty)
2013-12-27 15:12:20 -07:00
protected def getInt(name: String)(implicit ctx: UserContext) =
2019-11-28 18:34:46 -07:00
get(name) flatMap (_.toIntOption)
2013-05-31 06:27:18 -06:00
protected def getInt(name: String, req: RequestHeader): Option[Int] =
2019-11-28 18:34:46 -07:00
req.queryString get name flatMap (_.headOption) flatMap (_.toIntOption)
2014-02-11 14:07:28 -07:00
2018-03-07 17:20:00 -07:00
protected def getLong(name: String)(implicit ctx: UserContext) =
2019-11-28 18:34:46 -07:00
get(name) flatMap (_.toLongOption)
2018-03-07 17:20:00 -07:00
2018-03-31 19:48:52 -06:00
protected def getLong(name: String, req: RequestHeader) =
2019-11-28 18:34:46 -07:00
get(name, req) flatMap (_.toLongOption)
2018-03-31 19:48:52 -06:00
2014-02-11 14:07:28 -07:00
protected def getBool(name: String)(implicit ctx: UserContext) =
2018-04-04 08:06:08 -06:00
(getInt(name) exists trueish) || (get(name) exists trueish)
protected def getBool(name: String, req: RequestHeader) =
2018-04-04 08:06:08 -06:00
(getInt(name, req) exists trueish) || (get(name, req) exists trueish)
protected def getBoolOpt(name: String)(implicit ctx: UserContext) =
2020-05-05 22:11:15 -06:00
(getInt(name) map trueish) orElse (get(name) map trueish)
protected def getBoolOpt(name: String, req: RequestHeader) =
2020-05-05 22:11:15 -06:00
(getInt(name, req) map trueish) orElse (get(name, req) map trueish)
protected def getMobile(implicit ctx: UserContext) =
IsMobile(getBool("mobile"))
}