-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
55 lines (49 loc) · 1.23 KB
/
list.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
package app
import (
"net/http"
"time"
"github.com/crispgm/read-track/internal/model"
"github.com/gin-gonic/gin"
"github.com/osteele/liquid"
)
// ListParams .
type ListParams struct {
Year int `form:"year"`
Month int `form:"month"`
}
// List .
func (app Application) List(c *gin.Context) {
var params ListParams
err := c.BindQuery(¶ms)
if err != nil {
c.JSON(http.StatusOK, NewError(ErrCodeParams, err).Response())
return
}
if params.Year < 1970 || params.Year > 2100 {
params.Year = time.Now().Year()
}
if params.Month == 0 || params.Month > 12 {
params.Month = int(time.Now().Month())
}
articles, err := model.ListArticles(app.DB(), app.loc, params.Year, params.Month)
if err != nil {
c.JSON(http.StatusOK, NewError(ErrCodeDBFailed, err).Response())
return
}
for i, a := range articles {
articles[i].ReadAtText = a.UpdatedAt.In(app.loc).Format("2006-01-02 15:04")
desc := []rune(a.Description)
if len(desc) > 140 {
desc = desc[0:140]
articles[i].DescriptionText = string(desc) + " ..."
} else {
articles[i].DescriptionText = a.Description
}
}
app.RenderHTML(c, "list.liquid", liquid.Bindings{
"layout": "page",
"path": "/page/list",
"title": "List",
"articles": articles,
})
}