-
-
Notifications
You must be signed in to change notification settings - Fork 101
/
list.go
86 lines (70 loc) · 1.87 KB
/
list.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
74
75
76
77
78
79
80
81
82
83
84
85
86
package user
import (
"net/http"
"encoding/json"
"github.com/azukaar/cosmos-server/src/utils"
"go.mongodb.org/mongo-driver/mongo/options"
"strconv"
"math"
)
var maxLimit = 1000
func UserList(w http.ResponseWriter, req *http.Request) {
if utils.AdminOnly(w, req) != nil {
return
}
limit, _ := strconv.Atoi(req.URL.Query().Get("limit"))
// from, _ := req.URL.Query().Get("from")
if limit == 0 {
limit = maxLimit
}
if(req.Method == "GET") {
c, closeDb, errCo := utils.GetEmbeddedCollection(utils.GetRootAppId(), "users")
defer closeDb()
if errCo != nil {
utils.Error("Database Connect", errCo)
utils.HTTPError(w, "Database", http.StatusInternalServerError, "DB001")
return
}
utils.Debug("UserList: List user ")
userList := []utils.User{}
l := int64(math.Max((float64)(maxLimit), (float64)(limit)))
fOpt := options.FindOptions{
Limit: &l,
}
// TODO: Implement pagination
cursor, errDB := c.Find(
nil,
map[string]interface{}{
// "_id": map[string]interface{}{
// "$gt": from,
// },
},
&fOpt,
)
defer cursor.Close(nil)
if errDB != nil {
utils.Error("UserList: Error while getting user", errDB)
utils.HTTPError(w, "User Get Error", http.StatusInternalServerError, "UL001")
return
}
for cursor.Next(nil) {
user := utils.User{}
errDec := cursor.Decode(&user)
if errDec != nil {
utils.Error("UserList: Error while decoding user", errDec)
utils.HTTPError(w, "User Get Error", http.StatusInternalServerError, "UL001")
return
}
user.Link = "/api/user/" + user.Nickname
userList = append(userList, user)
}
json.NewEncoder(w).Encode(map[string]interface{}{
"status": "OK",
"data": userList,
})
} else {
utils.Error("UserList: Method not allowed" + req.Method, nil)
utils.HTTPError(w, "Method not allowed", http.StatusMethodNotAllowed, "HTTP001")
return
}
}