This repository has been archived by the owner on Jul 12, 2023. It is now read-only.
/
index.go
148 lines (128 loc) · 3.3 KB
/
index.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package user
import (
"context"
"net/http"
"strconv"
"github.com/google/exposure-notifications-verification-server/pkg/controller"
"github.com/google/exposure-notifications-verification-server/pkg/database"
)
// pageSize is const for now, could provide options in UX
const pageSize = 25
type PageLabel struct {
Offset int
Label string
}
type Pages struct {
Previous int
Current int
Next int
Offsets []PageLabel
}
func (c *Controller) HandleIndex() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
offset := 0
var err error
if qvar := r.FormValue("offset"); qvar != "" {
offset, err = strconv.Atoi(qvar)
if err != nil {
controller.InternalError(w, r, c.h, err)
return
}
}
realm := controller.RealmFromContext(ctx)
if realm == nil {
controller.MissingRealm(w, r, c.h)
return
}
count, err := realm.CountUsers(c.db)
if err != nil {
controller.InternalError(w, r, c.h, err)
return
}
users, err := realm.ListUsers(c.db, offset, pageSize)
if err != nil {
controller.InternalError(w, r, c.h, err)
return
}
var pages *Pages
if count > pageSize {
pages = populatePageStrip(offset, count)
}
c.renderIndex(ctx, w, users, pages)
})
}
func populatePageStrip(offset, count int) *Pages {
pages := &Pages{
Current: offset,
Offsets: []PageLabel{},
Previous: -1,
Next: -1,
}
// Calc start and end for paging as +/- 5 pages from current
iStart := offset - pageSize*5
if iStart <= 0 {
iStart = 0
}
iEnd := iStart + pageSize*11
if iEnd > count-pageSize {
iEnd = count - pageSize
}
// Page number series
for i := iStart; i <= iEnd; i += pageSize {
pl := PageLabel{
Offset: i,
Label: strconv.Itoa((i / pageSize) + 1),
}
pages.Offsets = append(pages.Offsets, pl)
}
// Fast forward halfway to the start
if iStart > pageSize*5 {
pages.Offsets[0] = PageLabel{
Offset: iStart/2 - (iStart / 2 % pageSize),
Label: "<<",
}
}
// Fast forward halfway to the end
if iEnd < count-pageSize*5 {
halfway := (count - iEnd) / 2
pages.Offsets[len(pages.Offsets)-1] = PageLabel{
Offset: halfway + iEnd - halfway%pageSize,
Label: ">>",
}
}
// Previous button data
if offset != 0 {
pages.Previous = offset - pageSize
if pages.Previous < 0 {
pages.Previous = 0
}
}
// Next button data
if offset != count-pageSize {
pages.Next = offset + pageSize
if pages.Next > count-pageSize {
pages.Next = count - pageSize
}
}
return pages
}
func (c *Controller) renderIndex(ctx context.Context, w http.ResponseWriter, users []*database.User, pages *Pages) {
m := controller.TemplateMapFromContext(ctx)
m["users"] = users
m["pages"] = pages
c.h.RenderHTML(w, "users/index", m)
}