forked from concourse/concourse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_since.go
76 lines (66 loc) · 1.76 KB
/
get_since.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
package usersserver
import (
"encoding/json"
"net/http"
"time"
"github.com/pf-qiu/concourse/v6/atc"
"github.com/pf-qiu/concourse/v6/atc/api/present"
"github.com/pf-qiu/concourse/v6/atc/db"
)
const since = "since"
const dateLayout = "2006-01-02"
func (s *Server) GetUsersSince(w http.ResponseWriter, r *http.Request) {
hLog := s.logger.Session("list-users")
w.Header().Set("Content-Type", "application/json")
var (
users []db.User
err error
)
err = r.ParseForm()
if err != nil {
hLog.Error("failed-to-parse-form-data", err)
w.WriteHeader(http.StatusBadRequest)
return
}
if isSinceSet(r) {
tmSince, err := time.Parse(dateLayout, r.FormValue(since))
if err != nil {
hLog.Error("failed-to-parse-time", err)
w.WriteHeader(http.StatusBadRequest)
if err = json.NewEncoder(w).Encode(map[string]string{"error": "wrong date format (yyyy-mm-dd)"}); err != nil {
hLog.Error("failed-to-encode-date-parsing-error", err)
w.WriteHeader(http.StatusInternalServerError)
}
return
}
users, err = s.userFactory.GetAllUsersByLoginDate(tmSince)
if err != nil {
hLog.Error("failed-to-get-users-since", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
} else {
users, err = s.userFactory.GetAllUsers()
if err != nil {
hLog.Error("failed-to-get-users", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
}
presentedUsers := make([]atc.User, len(users))
for idx, user := range users {
presentedUsers[idx] = present.User(user)
}
err = json.NewEncoder(w).Encode(presentedUsers)
if err != nil {
hLog.Error("failed-to-encode-users", err)
w.WriteHeader(http.StatusInternalServerError)
}
return
}
func isSinceSet(r *http.Request) bool {
if len(r.FormValue(since)) > 0 {
return true
}
return false
}