-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker_process_review.go
55 lines (45 loc) · 2.14 KB
/
worker_process_review.go
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
package main
import (
"github.com/gobuffalo/nulls"
"github.com/jinzhu/gorm"
"github.com/kiambogo/coffeeworks/models"
"github.com/kiambogo/coffeeworks/support"
)
// ProcessReview is used to update the score for a cafe based on a new review
func ProcessReview(review *models.Review) error {
tx := models.DB.Begin()
// Pull latest score
score := &models.Score{}
err := score.LoadLatest(review.PlaceID, tx)
if err != nil {
if !gorm.IsRecordNotFoundError(err) {
tx.Rollback()
return support.LogError(err, "ProcessReview (%v) - retrieving score", review.ID.String())
}
}
newScore := &models.Score{}
newScore.PlaceID = review.PlaceID
// Update each metric
newScore.WifiSpeed, newScore.WifiSpeedWeight = updateOptionalRating(score.WifiSpeed, review.WifiSpeed, score.WifiSpeedWeight)
newScore.WifiRestrictions, newScore.WifiRestrictionsWeight = updateOptionalRating(score.WifiRestrictions, review.WifiRestrictions, score.WifiRestrictionsWeight)
newScore.BeverageSelection, newScore.BeverageSelectionWeight = updateOptionalRating(score.BeverageSelection, review.BeverageSelection, score.BeverageSelectionWeight)
newScore.BeverageQuality, newScore.BeverageQualityWeight = updateOptionalRating(score.BeverageQuality, review.BeverageQuality, score.BeverageQualityWeight)
newScore.NoiseLevel, newScore.NoiseLevelWeight = updateOptionalRating(score.NoiseLevel, review.NoiseLevel, score.NoiseLevelWeight)
newScore.FoodOptions, newScore.FoodOptionsWeight = updateOptionalRating(score.FoodOptions, review.FoodOptions, score.FoodOptionsWeight)
if err = tx.Create(newScore).Error; err != nil {
tx.Rollback()
return support.LogError(err, "ProcessReview (%v) - saving new score", review.ID.String())
}
if err = tx.Delete(score).Error; err != nil {
tx.Rollback()
return support.LogError(err, "ProcessReview (%v) - soft deleting old score", review.ID.String())
}
return tx.Commit().Error
}
func updateOptionalRating(rating float32, newRating nulls.Int, oldWeight int) (float32, int) {
if !newRating.Valid {
return rating, oldWeight
}
newWeight := oldWeight + 1
return ((rating * float32(oldWeight)) + float32(newRating.Int)) / float32(newWeight), newWeight
}