get user repo to compile against new db abstractions
This commit is contained in:
parent
28affdd596
commit
8e232dc75f
|
@ -59,6 +59,8 @@ trait PackageObject
|
|||
trait WithDb { self: PackageObject ⇒
|
||||
|
||||
type LilaDB = reactivemongo.api.DB
|
||||
|
||||
type ReactiveColl = reactivemongo.api.collections.default.BSONCollection
|
||||
}
|
||||
|
||||
trait WithPlay { self: PackageObject ⇒
|
||||
|
|
|
@ -3,7 +3,6 @@ package lila.db
|
|||
import reactivemongo.api._
|
||||
import reactivemongo.bson._
|
||||
import reactivemongo.core.commands._
|
||||
import reactivemongo.api.collections.default.BSONCollection
|
||||
|
||||
import play.modules.reactivemongo.Implicits._
|
||||
|
||||
|
@ -12,7 +11,16 @@ import play.api.libs.concurrent.Execution.Implicits._
|
|||
|
||||
import scala.concurrent.Future
|
||||
|
||||
abstract class Coll[Doc <: WithStringId](coll: BSONCollection, json: JsonTube[Doc]) {
|
||||
abstract class Coll[Doc <: WithStringId](coll: ReactiveColl, json: JsonTube[Doc]) {
|
||||
|
||||
object query {
|
||||
|
||||
def apply(q: JsObject) = builder query q
|
||||
|
||||
def all = builder
|
||||
|
||||
def byIds(ids: Seq[ID]) = apply(select byIds ids)
|
||||
}
|
||||
|
||||
object count {
|
||||
|
||||
|
@ -35,8 +43,10 @@ abstract class Coll[Doc <: WithStringId](coll: BSONCollection, json: JsonTube[Do
|
|||
}
|
||||
|
||||
def apply(q: JsObject): Fu[List[Doc]] = cursor(q).toList map (_.flatten)
|
||||
|
||||
def apply(q: JsObject, nb: Int): Fu[List[Doc]] = cursor(q, nb) toList nb map (_.flatten)
|
||||
|
||||
def apply(b: QueryBuilder): Fu[List[Doc]] = cursor(b).toList map (_.flatten)
|
||||
def apply(b: QueryBuilder, nb: Int): Fu[List[Doc]] = cursor(b, nb) toList nb map (_.flatten)
|
||||
}
|
||||
|
||||
object insert {
|
||||
|
@ -65,14 +75,14 @@ abstract class Coll[Doc <: WithStringId](coll: BSONCollection, json: JsonTube[Do
|
|||
}
|
||||
}
|
||||
|
||||
object projection {
|
||||
object primitive {
|
||||
|
||||
def primitive[A](q: JsObject, field: String)(extract: JsValue ⇒ Option[A]): Fu[List[A]] =
|
||||
def apply[A](q: JsObject, field: String)(extract: JsValue ⇒ Option[A]): Fu[List[A]] =
|
||||
coll.find(q, Json.obj(field -> 1)).cursor.toList map (list ⇒ list map { obj ⇒
|
||||
extract(JsObjectReader.read(obj) \ field)
|
||||
} flatten)
|
||||
|
||||
def primitiveOne[A](q: JsObject, field: String)(extract: JsValue ⇒ Option[A]): Fu[Option[A]] =
|
||||
def one[A](q: JsObject, field: String)(extract: JsValue ⇒ Option[A]): Fu[Option[A]] =
|
||||
coll.find(q, Json.obj(field -> 1)).one map (opt ⇒ opt map { obj ⇒
|
||||
extract(JsObjectReader.read(obj) \ field)
|
||||
} flatten)
|
||||
|
@ -80,15 +90,31 @@ abstract class Coll[Doc <: WithStringId](coll: BSONCollection, json: JsonTube[Do
|
|||
|
||||
type ID = String
|
||||
|
||||
// hack, this should be in reactivemongo
|
||||
protected implicit def queryBuilderSortable(b: QueryBuilder) = new {
|
||||
def sort(sorters: (String, SortOrder)*): QueryBuilder =
|
||||
if (sorters.size == 0) b
|
||||
else b sort {
|
||||
BSONDocument(
|
||||
(for (sorter ← sorters) yield sorter._1 -> BSONInteger(
|
||||
sorter._2 match {
|
||||
case SortOrder.Ascending ⇒ 1
|
||||
case SortOrder.Descending ⇒ -1
|
||||
})).toStream)
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////
|
||||
// PRIVATE SHIT //
|
||||
//////////////////
|
||||
|
||||
private def cursor(q: JsObject): Cursor[Option[Doc]] = query(q).cursor[Option[Doc]]
|
||||
private def cursor(q: JsObject, nb: Int): Cursor[Option[Doc]] = query(q).options(opts batchSize nb).cursor[Option[Doc]]
|
||||
private def cursor(q: JsObject): Cursor[Option[Doc]] = cursor(query(q))
|
||||
private def cursor(q: JsObject, nb: Int): Cursor[Option[Doc]] = cursor(query(q), nb)
|
||||
|
||||
private def cursor(b: QueryBuilder): Cursor[Option[Doc]] = b.cursor[Option[Doc]]
|
||||
private def cursor(b: QueryBuilder, nb: Int): Cursor[Option[Doc]] = cursor(b.options(opts batchSize nb))
|
||||
|
||||
private def builder = coll.genericQueryBuilder
|
||||
private def query(q: JsObject) = builder query q
|
||||
private val opts = QueryOpts()
|
||||
|
||||
private def db = coll.db
|
||||
|
|
|
@ -44,8 +44,8 @@ trait select { self: operator ⇒
|
|||
object sort extends sort
|
||||
trait sort {
|
||||
|
||||
def naturalDesc: BSONDocument = JsObjectWriter write Json.obj("$natural" -> desc)
|
||||
def naturalDesc = "$natural" -> desc
|
||||
|
||||
def asc = 1
|
||||
def desc = -1
|
||||
def asc = SortOrder.Ascending
|
||||
def desc = SortOrder.Descending
|
||||
}
|
||||
|
|
|
@ -93,7 +93,8 @@ object ApplicationBuild extends Build with Resolvers with Dependencies {
|
|||
lazy val user = project("user", Seq(common, memo, db, scalachess)).settings(
|
||||
libraryDependencies := Seq(
|
||||
scalaz, scalalib, hasher, jodaTime, jodaConvert, playProvided, salat,
|
||||
paginator, paginatorSalat, sprayCaching, playTestProvided),
|
||||
paginator, paginatorSalat, sprayCaching, playTestProvided,
|
||||
reactivemongo, playReactivemongo),
|
||||
scalaSource in Compile <<= (sourceDirectory in Compile)(identity),
|
||||
scalaSource in Test <<= (sourceDirectory in Test)(identity)
|
||||
)
|
||||
|
|
|
@ -5,13 +5,13 @@ import akka.actor.ActorSystem
|
|||
|
||||
import chess.EloCalculator
|
||||
|
||||
final class UserEnv(settings: Settings, db: LilaDB) {
|
||||
final class UserEnv(settings: Settings, db: String ⇒ ReactiveColl) {
|
||||
|
||||
import settings._
|
||||
|
||||
// lazy val historyRepo = new HistoryRepo(mongodb(CollectionHistory))
|
||||
|
||||
lazy val userRepo = new UserRepo(db, CollectionUser)
|
||||
lazy val userRepo = new UserRepo(db(CollectionUser))
|
||||
|
||||
// lazy val paginator = new PaginatorBuilder(
|
||||
// userRepo = userRepo,
|
||||
|
|
|
@ -3,29 +3,28 @@ package lila.user
|
|||
import lila.db.{ Coll, DbApi }
|
||||
|
||||
import play.api.libs.json.Json
|
||||
import reactivemongo.bson.handlers._
|
||||
import reactivemongo.bson.handlers.DefaultBSONHandlers._
|
||||
import play.modules.reactivemongo._
|
||||
import play.modules.reactivemongo.PlayBsonImplicits._
|
||||
import play.api.libs.concurrent.Execution.Implicits._
|
||||
|
||||
import reactivemongo.api._
|
||||
import reactivemongo.bson._
|
||||
import reactivemongo.core.commands._
|
||||
|
||||
import play.modules.reactivemongo.Implicits._
|
||||
|
||||
import com.roundeights.hasher.Implicits._
|
||||
import org.joda.time.DateTime
|
||||
import ornicar.scalalib.Random
|
||||
|
||||
final class UserRepo(
|
||||
db: LilaDB,
|
||||
name: String
|
||||
) extends Coll[User](db, name, Users.json) with DbApi {
|
||||
final class UserRepo(coll: ReactiveColl) extends Coll[User](coll, Users.json) with DbApi {
|
||||
|
||||
def normalize(id: String) = id.toLowerCase
|
||||
|
||||
def byIdsSortElo(ids: Seq[ID], limit: Option[Int] = None) =
|
||||
find(query byIds ids sort sortEloDesc, limit.fold(query.o)(query.o.batchSize))
|
||||
// def byIdsSortElo(ids: Seq[ID], limit: Option[Int] = None) =
|
||||
// find(query byIds ids sort sortEloDesc, limit.fold(query.o)(query.o.batchSize))
|
||||
|
||||
def allSortToints(nb: Int) = find(query.q sort ("toints" -> sort.desc), nb)
|
||||
def allSortToints(nb: Int) = find(query.all sort ("toints" -> sort.desc), nb)
|
||||
|
||||
def usernameById(id: ID) = projection.primitiveOne(query byId id, "username")(_.asOpt[String])
|
||||
def usernameById(id: ID) = primitive.one(select byId id, "username")(_.asOpt[String])
|
||||
|
||||
def rank(user: User) = count(enabledQuery ++ Json.obj("elo" -> $gt(user.elo))) map (1+)
|
||||
|
||||
|
|
Loading…
Reference in a new issue