-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuser.go
73 lines (60 loc) · 1.53 KB
/
user.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package scouter
import (
"github.com/globalsign/mgo/bson"
"github.com/google/go-github/github"
)
type User struct {
ID int64 `bson:"_id" json:"id"`
*github.User
Contribution int `bson:"contribution" json:"contribution"`
}
func CountUsers() (int, error) {
return CountCollectionRecords(UserCollection)
}
func FindUser(query bson.M) (User, error) {
var user User
if err := mongoSession.DB("").C(UserCollection).Find(query).One(&user); err != nil {
return user, err
}
return user, nil
}
func FindUsers(selector bson.M, sort string, page, pageSize int) ([]User, error) {
var users []User
skip := (page - 1) * pageSize
if skip < 0 {
skip = 0
}
if err := mongoSession.DB("").C(UserCollection).Find(selector).Sort(sort).Skip(skip).Limit(pageSize).All(&users); err != nil {
return users, nil
}
return users, nil
}
func InsertUsers(users []User) error {
for _, user := range users {
if err := InsertRecord(UserCollection, user); err != nil {
return err
}
}
return nil
}
func UpdateUserById(id interface{}, update interface{}) error {
return UpdateById(UserCollection, id, update)
}
func UpsertUser(user User) error {
_, err := UpsertRecord(UserCollection, bson.M{"_id": user.ID}, user)
if err != nil {
return err
}
return nil
}
func UpsertUsers(users []User) error {
for _, user := range users {
if err := UpsertUser(user); err != nil {
return err
}
}
return nil
}
func PatchUserContribution(user User) error {
return UpdateById(UserCollection, bson.M{"_id": user.ID}, bson.M{"contribution": user.Contribution})
}