-
Notifications
You must be signed in to change notification settings - Fork 0
/
item.go
96 lines (87 loc) · 2.07 KB
/
item.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
package item
import (
"fmt"
"html/template"
"net/http"
"strconv"
"github.com/bountyeq/web/config"
"github.com/bountyeq/web/model"
"github.com/bountyeq/web/sanitize"
"github.com/bountyeq/web/site"
"github.com/gin-gonic/gin"
)
// List returns a list of creatures
func List(c *gin.Context) {
var err error
search, ok := c.Get("search")
fmt.Println("search", search)
if ok {
fmt.Println("search", search)
}
id := c.Param("id")
if len(id) > 0 {
err = getItem(c, id)
if err != nil {
c.Error(err)
return
}
return
}
page := 1
pageStr, ok := c.GetQuery("page")
if ok {
page, err = strconv.Atoi(pageStr)
if err != nil {
page = 1
}
}
if page < 1 {
page = 1
}
limit := 10
items := []model.Items{}
err = config.ContentDB.Limit(limit).Offset(limit*page - 1).Find(&items).Error
if err != nil {
c.Error(fmt.Errorf("find Items: %w", err))
return
}
site := site.Fetch()
site.Title = "Item List"
previousDisplay := fmt.Sprintf("%d", page-1)
previous := fmt.Sprintf("/item?page=%s", previousDisplay)
if page == 1 {
previous = ""
previousDisplay = ""
}
site.Page.Generate("/item", "Start", previous, previousDisplay, fmt.Sprintf("%d", page), fmt.Sprintf("/item?page=%d", page+1), fmt.Sprintf("%d", page+1))
c.HTML(http.StatusOK, "item_list.tmpl", gin.H{
"Title": "BountyEQ",
"Items": items,
"Site": site,
})
}
func getItem(c *gin.Context, strID string) error {
description := ""
id, err := strconv.Atoi(strID)
if err != nil {
return fmt.Errorf("parse id %s: %w", strID, err)
}
if id == 0 {
return fmt.Errorf("invalid id %s", strID)
}
item := &model.Items{}
config.ContentDB.Where("id = ?", id).First(item)
if err != nil {
return fmt.Errorf("could not find ITEM ID %d: %w", id, err)
}
description += fmt.Sprintf("<p>%s is an item that I'll add more intel later about.</p>", item.Name)
site := site.Fetch()
site.Title = item.Name
c.HTML(http.StatusOK, "item_view.tmpl", gin.H{
"Title": sanitize.CleanName(item.Name),
"Item": item,
"Description": template.HTML(description),
"Site": site,
})
return nil
}