-
Notifications
You must be signed in to change notification settings - Fork 398
/
SolutionAggregation.kt
70 lines (57 loc) · 2.3 KB
/
SolutionAggregation.kt
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.google.example.firestore.kotlin
import com.google.android.gms.tasks.Task
import com.google.firebase.firestore.DocumentReference
import com.google.firebase.firestore.FirebaseFirestore
import com.google.firebase.firestore.SetOptions
/**
* https://firebase.google.com/docs/firestore/solutions/aggregation
*/
abstract class SolutionAggregation(private val db: FirebaseFirestore) {
// [START restaurant_class]
data class Restaurant(
// default values required for use with "toObject"
internal var name: String = "",
internal var avgRating: Double = 0.0,
internal var numRatings: Int = 0
)
// [END restaurant_class]
fun exampleRestaurant() {
// [START example_restaurant]
val arinell = Restaurant("Arinell Pizza", 4.65, 683)
// [END example_restaurant]
}
fun getAllRatings() {
// [START get_all_ratings]
db.collection("restaurants")
.document("arinell-pizza")
.collection("ratings")
.get()
// [END get_all_ratings]
}
// [START add_rating]
private fun addRating(restaurantRef: DocumentReference, rating: Float): Task<Void> {
// Create reference for new rating, for use inside the transaction
val ratingRef = restaurantRef.collection("ratings").document()
// In a transaction, add the new rating and update the aggregate totals
return db.runTransaction { transaction ->
val restaurant = transaction.get(restaurantRef).toObject(Restaurant::class.java)!!
// Compute new number of ratings
val newNumRatings = restaurant.numRatings + 1
// Compute new average rating
val oldRatingTotal = restaurant.avgRating * restaurant.numRatings
val newAvgRating = (oldRatingTotal + rating) / newNumRatings
// Set new restaurant info
restaurant.numRatings = newNumRatings
restaurant.avgRating = newAvgRating
// Update restaurant
transaction.set(restaurantRef, restaurant)
// Update rating
val data = hashMapOf<String, Any>(
"rating" to rating
)
transaction.set(ratingRef, data, SetOptions.merge())
null
}
}
// [END add_rating]
}