pagination for the team users API
This commit is contained in:
parent
b1ee6bb0d0
commit
2a43513873
|
@ -35,12 +35,12 @@ object Api extends LilaController {
|
|||
}
|
||||
|
||||
def users = ApiRequest { implicit ctx =>
|
||||
get("team") ?? { teamId =>
|
||||
userApi.list(
|
||||
teamId = teamId,
|
||||
engine = getBoolOpt("engine"),
|
||||
nb = getInt("nb")
|
||||
).map(_.some)
|
||||
(get("team") ?? Env.team.api.team).flatMap {
|
||||
_ ?? { team =>
|
||||
val page = (getInt("page") | 1) max 1 min 50
|
||||
val nb = (getInt("nb") | 10) max 1 min 50
|
||||
Env.team.pager(team, page, nb) map userApi.pager map some
|
||||
}
|
||||
} map toApiResult
|
||||
}
|
||||
|
||||
|
|
|
@ -10,10 +10,7 @@ import views._
|
|||
|
||||
object Game extends LilaController {
|
||||
|
||||
private def paginator = Env.game.paginator
|
||||
private def cached = Env.game.cached
|
||||
private def searchEnv = Env.gameSearch
|
||||
def searchForm = searchEnv.forms.search
|
||||
|
||||
def delete(gameId: String) = Auth { implicit ctx =>
|
||||
me =>
|
||||
|
|
|
@ -2,6 +2,7 @@ package lila.api
|
|||
|
||||
import play.api.libs.json._
|
||||
|
||||
import lila.common.paginator.{ Paginator, PaginatorJson }
|
||||
import lila.common.PimpedJson._
|
||||
import lila.db.dsl._
|
||||
import lila.game.GameRepo
|
||||
|
@ -17,20 +18,10 @@ private[api] final class UserApi(
|
|||
prefApi: lila.pref.PrefApi,
|
||||
makeUrl: String => String) {
|
||||
|
||||
def list(
|
||||
teamId: String,
|
||||
nb: Option[Int],
|
||||
engine: Option[Boolean]): Fu[JsObject] =
|
||||
lila.team.MemberRepo userIdsByTeam teamId map (_ take makeNb(nb)) flatMap UserRepo.enabledByIds map { users =>
|
||||
Json.obj(
|
||||
"list" -> JsArray(
|
||||
users map { u =>
|
||||
jsonView(u) ++
|
||||
Json.obj("url" -> makeUrl(s"@/${u.username}"))
|
||||
}
|
||||
)
|
||||
)
|
||||
}
|
||||
def pager(pag: Paginator[User]): JsObject =
|
||||
Json.obj("paginator" -> PaginatorJson(pag.mapResults { u =>
|
||||
jsonView(u) ++ Json.obj("url" -> makeUrl(s"@/${u.username}"))
|
||||
}))
|
||||
|
||||
def one(username: String)(implicit ctx: Context): Fu[Option[JsObject]] = UserRepo named username flatMap {
|
||||
case None => fuccess(none)
|
||||
|
@ -70,6 +61,4 @@ private[api] final class UserApi(
|
|||
}.noNull
|
||||
} map (_.some)
|
||||
}
|
||||
|
||||
private def makeNb(nb: Option[Int]) = math.min(100, nb | 10)
|
||||
}
|
||||
|
|
|
@ -23,6 +23,8 @@ final class Env(config: Config, hub: lila.hub.Env, notifyApi: NotifyApi, db: lil
|
|||
|
||||
lazy val forms = new DataForm(colls.team, hub.actor.captcher)
|
||||
|
||||
lazy val pager = new MemberPager(colls.member)
|
||||
|
||||
lazy val api = new TeamApi(
|
||||
coll = colls,
|
||||
cached = cached,
|
||||
|
|
29
modules/team/src/main/MemberPager.scala
Normal file
29
modules/team/src/main/MemberPager.scala
Normal file
|
@ -0,0 +1,29 @@
|
|||
package lila.team
|
||||
|
||||
import lila.common.paginator._
|
||||
import lila.db.dsl._
|
||||
import lila.db.paginator._
|
||||
import lila.user.{ User, UserRepo }
|
||||
|
||||
final class MemberPager(coll: Coll) {
|
||||
|
||||
def apply(team: Team, page: Int, maxPerPage: Int): Fu[Paginator[User]] =
|
||||
Paginator(
|
||||
new MemberAdapter(team),
|
||||
currentPage = page,
|
||||
maxPerPage = maxPerPage)
|
||||
|
||||
final class MemberAdapter(team: Team) extends AdapterLike[User] {
|
||||
|
||||
def nbResults: Fu[Int] = fuccess(team.nbMembers)
|
||||
|
||||
def slice(offset: Int, length: Int): Fu[Seq[User]] =
|
||||
coll.find($doc("team" -> team.id), $doc("user" -> true))
|
||||
.sort($sort desc "date")
|
||||
.skip(offset)
|
||||
.cursor[Bdoc]()
|
||||
.gather[List](length) map {
|
||||
_ flatMap { _.getAs[String]("user") }
|
||||
} flatMap UserRepo.usersFromSecondary
|
||||
}
|
||||
}
|
|
@ -74,6 +74,9 @@ object UserRepo {
|
|||
_.flatMap { _.getAs[String]("_id") }
|
||||
}
|
||||
|
||||
def usersFromSecondary(userIds: Seq[ID]): Fu[List[User]] =
|
||||
coll.byOrderedIds[User](userIds, readPreference = ReadPreference.secondaryPreferred)(_.id)
|
||||
|
||||
private[user] def allSortToints(nb: Int) =
|
||||
coll.find($empty).sort($sort desc F.toints).cursor[User]().gather[List](nb)
|
||||
|
||||
|
|
Loading…
Reference in a new issue