-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreply_api.go
66 lines (60 loc) · 1.35 KB
/
reply_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
package main
import (
"fmt"
"io/ioutil"
"github.com/gin-gonic/gin"
)
/*
# 回复:
获取回复:
GET /api/reply/:id
*/
func getReply(c *gin.Context) {
reply, err := getReplyById(c.Param("id"))
if err != nil {
c.JSON(200, RespResult{ErrCodeNotFound, err.Error(), nil})
return
}
c.JSON(200, RespResult{0, "ok", reply})
return
}
/*
id is the post's id
POST /api/reply/t/:id
*/
func createReplyAPI(c *gin.Context) {
user := getCurrent(c)
if user == nil {
c.JSON(200, RespResult{ErrCodeNeedLogin, "login to reply", nil})
return
}
pid := c.Param("id")
if pid == "" {
c.JSON(200, RespResult{ErrCodeParam, "param id is empty.", nil})
return
}
post, err := getPostById(pid, false)
if err != nil || post == nil {
c.JSON(200, RespResult{ErrCodeGetPost, fmt.Sprintf("get post by id %s failed: %v", pid, err), nil})
return
}
// read http post header
body, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
c.JSON(200, RespResult{ErrCodeReadRequest, err.Error(), nil})
return
}
// 读取reply
reply, err := readPostFromRequest(body)
if err != nil {
c.JSON(200, RespResult{ErrCodeReadReqPost, err.Error(), nil})
return
}
setReplyDefaultValue(reply, post)
if err = createReply(reply, post, user); err != nil {
c.JSON(200, RespResult{ErrCodeCreateReply, err.Error(), nil})
return
}
c.JSON(200, RespResult{0, "ok", ""})
return
}