forked from studygolang/studygolang
/
reading.go
96 lines (79 loc) · 1.91 KB
/
reading.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
// Copyright 2014 The StudyGolang Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// http://studygolang.com
// Author:polaris studygolang@gmail.com
package controller
import (
"net/http"
"strconv"
"filter"
"github.com/studygolang/mux"
"model"
"service"
"util"
)
// 晨读列表页
// uri: /readings
func ReadingsHandler(rw http.ResponseWriter, req *http.Request) {
limit := 20
lastId := req.FormValue("lastid")
if lastId == "" {
lastId = "0"
}
rtype, err := strconv.Atoi(req.FormValue("rtype"))
if err != nil {
rtype = model.RtypeGo
}
readings := service.FindReadings(lastId, "25", rtype)
if readings == nil {
// TODO:服务暂时不可用?
}
num := len(readings)
if num == 0 {
if lastId == "0" {
util.Redirect(rw, req, "/")
} else {
util.Redirect(rw, req, "/readings")
}
return
}
var (
hasPrev, hasNext bool
prevId, nextId int
)
if lastId != "0" {
prevId, _ = strconv.Atoi(lastId)
// 避免因为项目下线,导致判断错误(所以 > 5)
if prevId-readings[0].Id > 5 {
hasPrev = false
} else {
prevId += limit
hasPrev = true
}
}
if num > limit {
hasNext = true
readings = readings[:limit]
nextId = readings[limit-1].Id
} else {
nextId = readings[num-1].Id
}
pageInfo := map[string]interface{}{
"has_prev": hasPrev,
"prev_id": prevId,
"has_next": hasNext,
"next_id": nextId,
}
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/readings/list.html")
// 设置模板数据
filter.SetData(req, map[string]interface{}{"activeReadings": "active", "readings": readings, "page": pageInfo, "rtype": rtype})
}
// 点击 【我要晨读】,记录点击数,跳转
// uri: /readings/{id:[0-9]+}
func IReadingHandler(rw http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
url := service.IReading(vars["id"])
util.Redirect(rw, req, url)
return
}