forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DonationApi.scala
56 lines (47 loc) · 1.71 KB
/
DonationApi.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package lila.donation
import lila.db.BSON.BSONJodaDateTimeHandler
import lila.db.Types.Coll
import org.joda.time.DateTime
import reactivemongo.bson._
final class DonationApi(coll: Coll, monthlyGoal: Int) {
private implicit val donationBSONHandler = Macros.handler[Donation]
private val decentAmount = BSONDocument("gross" -> BSONDocument("$gte" -> BSONInteger(200)))
def list(nb: Int) = coll.find(decentAmount)
.sort(BSONDocument("date" -> -1))
.cursor[Donation]()
.collect[List](nb)
def top(nb: Int) = coll.find(BSONDocument(
"userId" -> BSONDocument("$exists" -> true)
)).sort(BSONDocument(
"gross" -> -1,
"date" -> -1
)).cursor[Donation]()
.collect[List](nb)
def create(donation: Donation) = coll insert donation recover {
case e: reactivemongo.core.commands.LastError if e.getMessage.contains("duplicate key error") =>
println(e.getMessage)
} void
// in $ cents
def donatedByUser(userId: String): Fu[Int] =
coll.find(
decentAmount ++ BSONDocument("userId" -> userId),
BSONDocument("net" -> true, "_id" -> false)
).cursor[BSONDocument]().collect[List]() map2 { (obj: BSONDocument) =>
~obj.getAs[Int]("net")
} map (_.sum)
def progress: Fu[Progress] = {
val from = DateTime.now withDayOfMonth 1 withHourOfDay 0 withMinuteOfHour 0 withSecondOfMinute 0
val to = from plusMonths 1
coll.find(
BSONDocument("date" -> BSONDocument(
"$gte" -> from,
"$lt" -> to
)),
BSONDocument("net" -> true, "_id" -> false)
).cursor[BSONDocument]().collect[List]() map2 { (obj: BSONDocument) =>
~obj.getAs[Int]("net")
} map (_.sum) map { amount =>
Progress(from, monthlyGoal, amount)
}
}
}