-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.go
77 lines (67 loc) · 1.66 KB
/
util.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
package main
import (
"strconv"
)
type ArticleSlice []*Article
func (as ArticleSlice) Len() int {
return len(as)
}
func (as ArticleSlice) Less(i, j int) bool {
return as[i].Time.Before(as[j].Time)
}
func (as ArticleSlice) Swap(i, j int) {
as[i], as[j] = as[j], as[i]
}
func getPageNum(params map[string]string) (page int) {
//the default page is the first one
page = 1
if p, ok := params["page"]; ok {
pint64, err := strconv.ParseInt(p, 0, 0)
if err != nil {
return 1
}
page = int(pint64)
}
return
}
func paginate(articles ArticleSlice, numPerPage, page int) (rArticles ArticleSlice, prev, next int) {
//pages are one indexed whereas slices
//are zero indexed.
start := (page - 1) * numPerPage
end := start + numPerPage
prev, next = -1, page+1
//If we got handed an empty list of articles
//just return that list and signal there is
//no next page
if len(articles) == 0 {
rArticles = articles
next = -1
return
}
if start > 0 && start < len(articles) {
prev = page - 1
}
//bounds checks to make sure we
//are within the slice and don't
//indicate pages that aren't there
if start < 0 || start >= len(articles) {
//This is bad, somehow we got to
//a page that doesn't exist. This
//was probably someone messing with
//the url parameter. Oh Well.
rArticles = make(ArticleSlice, 0)
next = -1
} else if end >= len(articles) {
//Handles the end of the list
//we have no more pages after this one!
rArticles = articles[start:]
next = -1
} else {
//The normal case, everything within
//the proper limits
rArticles = articles[start:end]
//if this isn't the first page we'll return
//a valid previous page number
}
return
}