-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
62 lines (55 loc) · 1.1 KB
/
common.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
package ironclad
import (
"net/http"
"strconv"
)
type Common struct {
Subject *Subject
Message string
Query string
Category Category
Order SortOrder
}
func NewCommon(s *Subject, r *http.Request) Common {
return Common{
Message: "",
Query: r.FormValue("q"),
Category: ParseCategory(r.FormValue("category")),
Order: ParseSortOrder(r.FormValue("order")),
Subject: s,
}
}
func (c Common) SuffixWith(query string, category Category, order SortOrder, offset int) string {
if query == "" {
query = c.Query
}
if category == Category(0) {
category = c.Category
}
if order == SortOrder("") {
order = c.Order
}
r := ""
if query != "" {
r += "&q=" + query
}
if category != General {
r += "&category=" + category.ForURL()
}
if order != AgeNewToOld {
r += "&order=" + string(order)
}
if offset != 0 {
r += "&offset=" + strconv.FormatInt(int64(offset), 10)
}
if r != "" {
return "?" + r[1:]
}
return ""
}
func (c Common) URLSuffix() string {
return c.SuffixWith("", Category(0), SortOrder(""), 0)
}
func (c Common) AllCategories() []Category {
return AllCategories
}