forked from FurqanSoftware/too
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rater.go
51 lines (40 loc) · 923 Bytes
/
rater.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
// Copyright 2014 The Too Authors. All rights reserved.
package too
import (
"fmt"
"github.com/garyburd/redigo/redis"
)
type Rater struct {
e *Engine
kind string
}
// Add adds a rating by user for item
func (r Rater) Add(user User, item Item) error {
yes, err := redis.Bool(r.e.c.Do("SISMEMBER", fmt.Sprintf("%s:%s:%s", r.e.class, item, r.kind), user))
if err != nil {
return err
}
if !yes {
_, err = r.e.c.Do("ZINCRBY", fmt.Sprintf("%s:mosts:%s", r.e.class, r.kind), 1, item)
if err != nil {
return err
}
}
_, err = r.e.c.Do("SADD", fmt.Sprintf("%s:%s:%s", r.e.class, user, r.kind), item)
if err != nil {
return err
}
_, err = r.e.c.Do("SADD", fmt.Sprintf("%s:%s:%s", r.e.class, item, r.kind), user)
if err != nil {
return err
}
err = r.e.Similars.update(user)
if err != nil {
return err
}
err = r.e.Suggestions.update(user)
if err != nil {
return err
}
return nil
}