-
Notifications
You must be signed in to change notification settings - Fork 0
/
tax_api.go
96 lines (84 loc) · 1.87 KB
/
tax_api.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 main
import (
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"github.com/smtc/glog"
"io/ioutil"
//"net/http"
)
// GET /api/tax/list?taxonomy=xxxx
// todo: modify this api
func getCategoryList(c *gin.Context) {
taxes, err := getAllCategory()
if err != nil {
c.JSON(200, RespResult{ErrCodeDBQuery, err.Error(), nil})
return
}
c.JSON(200, RespResult{0, "ok", taxes})
return
}
// 新建taxonomy
// POST /api/tax/create/:id
/*
{
"id":,
"name":,
"slug":,
"taxonomy":,
"description":,
"parent",
}
*/
func createCategory(c *gin.Context) {
var tax Taxonomy
user := getCurrent(c)
if user == nil {
c.JSON(200, RespResult{ErrCodeNeeAuthen, "need login to create category", nil})
return
}
id := c.Param("id")
defer c.Request.Body.Close()
body, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
c.JSON(200, RespResult{ErrCodeReadRequest, err.Error(), nil})
return
}
if err = json.Unmarshal(body, &tax); err != nil {
c.JSON(200, RespResult{ErrCodeUnmarshaLTax, err.Error(), nil})
return
}
if id != tax.Id {
c.JSON(200, RespResult{ErrCodeParam,
fmt.Sprintf("path param id %s Not equal with json param id %s", id, tax.Id), nil})
return
}
err = createTaxonomy(&tax, user)
if err != nil {
c.JSON(200, RespResult{ErrCodeDBQuery, err.Error(), nil})
return
}
c.JSON(200, RespResult{0, "ok", nil})
return
}
/*
获取特定taxonomy的文章
目前使用join来获取文章
GET /api/cat/topics?catname=xxx&start=xx&count=xx
*/
func getTaxonomyTopics(c *gin.Context) {
tax := c.Query("catname")
if tax == "" {
glog.Warn("no categroy param found!\n")
getTopics(c)
return
}
start := getQueryIntDefault(c, "start", 0)
count := getQueryIntDefault(c, "count", 20)
posts, err := getTopicsByTaxonomy(tax, start, count)
if err != nil {
c.JSON(200, RespResult{ErrCodeDBQuery, err.Error(), nil})
return
}
c.JSON(200, RespResult{0, "ok", posts})
}