-
Notifications
You must be signed in to change notification settings - Fork 0
/
topic.go
80 lines (63 loc) · 1.83 KB
/
topic.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
package controllers
import (
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"twreporter.org/go-api/models"
)
// GetTopics receive HTTP GET method request, and return the topics.
// `query`, `limit`, `offset` and `sort` are the url query params,
// which define the rule we retrieve topics from storage.
func (nc *NewsController) GetTopics(c *gin.Context) {
var total int
var topics []models.Topic
var err error
mq, limit, offset, sort, full := nc.GetQueryParam(c)
if limit == 0 {
limit = 10
}
if sort == "" {
sort = "-publishedDate"
}
if full {
topics, total, err = nc.Storage.GetFullTopics(mq, limit, offset, sort, nil)
} else {
topics, total, err = nc.Storage.GetMetaOfTopics(mq, limit, offset, sort, nil)
}
if err != nil {
appErr := err.(*models.AppError)
c.JSON(appErr.StatusCode, gin.H{"status": appErr.Message, "error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"status": "ok", "records": topics, "meta": models.MetaOfResponse{
Total: total,
Offset: offset,
Limit: limit,
}})
return
}
// GetATopic receive HTTP GET method request, and return the certain post.
func (nc *NewsController) GetATopic(c *gin.Context) {
var topics []models.Topic
var err error
slug := c.Param("slug")
full, _ := strconv.ParseBool(c.Query("full"))
mq := models.MongoQuery{
Slug: slug,
}
if full {
topics, _, err = nc.Storage.GetFullTopics(mq, 1, 0, "-publishedDate", nil)
} else {
topics, _, err = nc.Storage.GetMetaOfTopics(mq, 1, 0, "-publishedDate", nil)
}
if err != nil {
appErr := err.(*models.AppError)
c.JSON(appErr.StatusCode, gin.H{"status": appErr.Message, "error": err.Error()})
return
}
if len(topics) == 0 {
c.JSON(http.StatusNotFound, gin.H{"status": "Record Not Found", "error": "Record Not Found"})
return
}
c.JSON(http.StatusOK, gin.H{"status": "ok", "record": topics[0]})
}