-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_api.go
206 lines (179 loc) · 4.48 KB
/
user_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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package main
import (
"encoding/json"
"github.com/gin-gonic/gin"
"io/ioutil"
"strings"
)
/*
用户:
GET /api/user/current
*/
func userCurrent(c *gin.Context) {
v, ok := c.Get(CurrentUserKey)
if !ok {
c.JSON(200, RespResult{ErrCodeVisitor, ErrMsgVisitor, nil})
return
}
c.JSON(200, RespResult{0, "ok", v.(*User)})
}
// 用户名是否存在:
// GET /api/user/exist?name=xxx
func userNameExist(c *gin.Context) {
exist, err := nameHasExist(c.Query("name"))
if err != nil {
c.JSON(200, RespResult{ErrCodeDBQuery, err.Error(), nil})
return
}
c.JSON(200, RespResult{0, "ok", exist})
}
/*
用户资料:
GET /api/user/info/{username}
*/
func getUserInfo(c *gin.Context) {
name := strings.TrimSpace(c.Param("username"))
if name == "" {
c.JSON(200, RespResult{ErrCodeParam, "param username is empty", nil})
return
}
u := getUserByName(name)
if u == nil {
c.JSON(200, RespResult{ErrCodeNotFound, "Not found User by username: " + name, nil})
return
}
// 不显示email
u.Email = ""
c.JSON(200, RespResult{0, "ok", u})
}
/*
修改用户资料:
POST /api/user/info/{username}
*/
func modifyUserInfo(c *gin.Context) {
var u User
ct := c.Request.Header.Get("Content-Type")
// content-type必须包含"application/json"
// "application/json"
// "application/json; charset=utf-8"
if strings.Contains(ct, "application/json") == false {
c.JSON(200, RespResult{ErrCodeContentType, "Post Content-Type invalid: " + ct, nil})
return
}
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, &u); err != nil {
c.JSON(200, RespResult{ErrCodeUnmarshalUser, err.Error(), nil})
return
}
// 写入数据库中
if err = updateUserById(&u); err != nil {
c.JSON(200, RespResult{ErrCodeUpdateUser, err.Error(), nil})
return
}
c.JSON(200, RespResult{0, "ok", nil})
return
}
// getUserTopics
// getUserReplies
func getUserPostByType(c *gin.Context, typ string) {
var (
u *User
start int
count int
)
name := strings.TrimSpace(c.Param("username"))
if name == "" {
c.JSON(200, RespResult{ErrCodeParam, "param username is empty", nil})
return
}
if u = getUserByName(name); u == nil {
c.JSON(200, RespResult{ErrCodeNotFound, "Not found User by username: " + name, nil})
return
}
start = getQueryIntDefault(c, "start", 0)
count = getQueryIntDefault(c, "count", 10)
posts, err := getPostsByUser(u, start, count, map[string]string{"object_type": typ})
if err != nil {
c.JSON(200, RespResult{ErrCodeGetUserPosts, err.Error(), "object type: " + typ})
return
}
c.JSON(200, RespResult{0, "ok", posts})
return
}
/*
获取用户文章
GET /api/user/t/{username}/topics?start=x&count=y
*/
func getUserTopics(c *gin.Context) {
getUserPostByType(c, "post")
}
/*
获取用户评论
GET /api/user/t/{username}/reply?start=x&count=y
*/
func getUserReplies(c *gin.Context) {
getUserPostByType(c, "comment")
}
/*
获取用户收藏
GET /api/user/a/{username}/favor?start=x&count=y
获取用户点赞
GET /api/user/a/{username}/up?start=x&count=y
获取用户反对
GET /api/user/a/{username}/down?start=x&count=y
获得用户打赏:
GET /api/user/a/{username}/pay?start=x&count=y
*/
func getUserFavor(c *gin.Context) {
getUserPostByAction(c, "favor")
}
func getUserUp(c *gin.Context) {
getUserPostByAction(c, "up")
}
func getUserDown(c *gin.Context) {
getUserPostByAction(c, "down")
}
func getUserPay(c *gin.Context) {
getUserPostByAction(c, "pay")
}
func getUserPostByAction(c *gin.Context, action string) {
var (
u *User
start int
count int
)
name := strings.TrimSpace(c.Param("username"))
if name == "" {
c.JSON(200, RespResult{ErrCodeParam, "param username is empty", nil})
return
}
if u = getUserByName(name); u == nil {
c.JSON(200, RespResult{ErrCodeNotFound, "Not found User by username: " + name, nil})
return
}
start = getQueryIntDefault(c, "start", 0)
count = getQueryIntDefault(c, "count", 10)
posts, err := getPostsByUserAction(u, action, start, count)
if err != nil {
c.JSON(200, RespResult{ErrCodeGetPostsByAction, err.Error(), "action type: " + action})
return
}
c.JSON(200, RespResult{0, "ok", posts})
return
}
/*
** 获取用户关注列表:
GET /api/user/{username}/follow?start=x&count=y
** 获取block列表
GET /api/user/{username}/block?start=x&count=y
用户动作:
block用户/unblock 用户
POST/DELETE /api/block/{uid1, uid2,uid3...}
follow用户
POST/DELETE /api/block/{uid1,uid2,uid3...}
*/