Skip to content

Commit f7de486

Browse files
author
Chen Quan
authored
Add Feature comment (#43)
* Add comment * Add 删除自己的评论 * Update 删除自己的评论 * Fix 用户可以使用其他用户的身份评论bug * Add 帖子评论 * Update 返回某主题的全部评论[分页] * Update 返回的Json Format
1 parent d331481 commit f7de486

35 files changed

+1580
-155
lines changed

api/api.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@ import (
66
)
77

88
type Api struct {
9-
BoardApi *v1.BoardApi
10-
PostApi *v1.PostApi
11-
UserAPi *UserApi
9+
BoardApi *v1.BoardApi
10+
PostApi *v1.PostApi
11+
UserAPi *UserApi
12+
CommentAPi *v1.CommentApi
1213
}
1314

1415
var providerApi = wire.NewSet(
1516
v1.ProviderBoard,
1617
v1.ProviderPost,
18+
v1.ProviderComment,
1719
ProviderAuth,
1820
)
1921
var Provider = wire.NewSet(providerApi, wire.Struct(new(Api), "*"))

api/router.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ func NewApi() *gin.Engine {
3737
v1.NewBoardRouter(r)
3838
// 主题
3939
v1.NewPostRouter(r)
40+
// 评论
41+
v1.NewCommentRouter(r)
4042

4143
return r
4244
}

api/v1/comment_api.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package v1
2+
3+
import (
4+
"fmt"
5+
"github.com/gin-gonic/gin"
6+
"github.com/google/wire"
7+
"github.com/opensourceai/go-api-service/api/v1/dto"
8+
"github.com/opensourceai/go-api-service/internal/models"
9+
"github.com/opensourceai/go-api-service/internal/service"
10+
"github.com/opensourceai/go-api-service/middleware/jwt"
11+
"github.com/opensourceai/go-api-service/pkg/app"
12+
)
13+
14+
type CommentApi struct {
15+
}
16+
17+
var ProviderComment = wire.NewSet(NewCommentService, service.ProviderComment)
18+
var commentService service.CommentService
19+
20+
func NewCommentService(comment service.CommentService) (*CommentApi, error) {
21+
commentService = comment
22+
return &CommentApi{}, nil
23+
}
24+
func NewCommentRouter(router *gin.Engine) {
25+
comment := router.Group("/v1/comment")
26+
// 认证
27+
comment.Use(jwt.JWT())
28+
{
29+
comment.POST("", addComment)
30+
comment.DELETE("", deleteComment)
31+
comment.PUT("", updateComment)
32+
}
33+
}
34+
35+
// @Summary 修改评论
36+
// @Tags Comment
37+
// @Produce json
38+
// @Param comment body dto.CommentUpdate true "comment"
39+
// @Success 200 {object} app.Response
40+
// @Failure 500 {object} app.Response
41+
// @Security ApiKeyAuth
42+
// @Router /v1/comment [put]
43+
func updateComment(context *gin.Context) {
44+
appG := app.Gin{C: context}
45+
// 获取当前用户信息
46+
userInfo := app.GetUserInfo(context)
47+
comment := &dto.CommentUpdate{}
48+
app.BindAndValid(context, comment)
49+
err := commentService.ServiceUpdate(userInfo.UserId, comment)
50+
if err != nil {
51+
fmt.Println(err)
52+
appG.Fail(nil)
53+
return
54+
}
55+
56+
appG.Success(nil)
57+
58+
}
59+
60+
// @Summary 新增评论
61+
// @Tags Comment
62+
// @Produce json
63+
// @Param comment body models.Comment true "comment"
64+
// @Success 200 {object} app.Response
65+
// @Failure 500 {object} app.Response
66+
// @Security ApiKeyAuth
67+
// @Router /v1/comment [post]
68+
func addComment(context *gin.Context) {
69+
appG := app.Gin{C: context}
70+
comment := &models.Comment{}
71+
app.BindAndValid(context, comment)
72+
info := app.GetUserInfo(context)
73+
if err := commentService.ServiceAdd(info.UserId, comment); err != nil {
74+
appG.Fail(nil)
75+
return
76+
}
77+
appG.Success(nil)
78+
}
79+
80+
// @Summary 删除评论
81+
// @Tags Comment
82+
// @Produce json
83+
// @Param ids body dto.Ids true "ids"
84+
// @Success 200 {object} app.Response
85+
// @Failure 500 {object} app.Response
86+
// @Security ApiKeyAuth
87+
// @Router /v1/comment [delete]
88+
func deleteComment(context *gin.Context) {
89+
appG := app.Gin{C: context}
90+
ids := &dto.Ids{}
91+
app.BindAndValid(context, ids)
92+
userInfo := app.GetUserInfo(context)
93+
if err := commentService.ServiceDeleteByIds(userInfo.UserId, ids); err != nil {
94+
appG.Fail(nil)
95+
return
96+
}
97+
appG.Success(nil)
98+
}

api/v1/dto/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# DTO(数据传输层)
2+
3+
用于视图层将数据传输服务层

api/v1/dto/comment_dto.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2020 opensourceai
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/*
18+
* @Package dto
19+
* @Author Quan Chen
20+
* @Date 2020/3/19
21+
* @Description
22+
*
23+
*/
24+
package dto
25+
26+
type CommentUpdate struct {
27+
ID int `json:"id"` // 评论ID
28+
CommentContent string `json:"comment_content"` // 评论的内容
29+
}
30+
type Comment struct {
31+
ID int `json:"id"` // 评论ID
32+
CommentContent string `json:"comment_content"` // 评论内容
33+
FromUser User `json:"from_user"` // 评论者
34+
ToUser User `json:"to_user"` // 被评论者
35+
}

api/v1/dto/common_dto.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2020 opensourceai
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/*
18+
* @Package dto
19+
* @Author Quan Chen
20+
* @Date 2020/3/19
21+
* @Description 公共的数据传输结构体
22+
*
23+
*/
24+
package dto
25+
26+
type Ids struct {
27+
Ids []int `json:"ids"` // 帖子IDs
28+
}

api/v1/dto/user_dto.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2020 opensourceai
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/*
18+
* @Package dto
19+
* @Author Quan Chen
20+
* @Date 2020/3/19
21+
* @Description
22+
*
23+
*/
24+
package dto
25+
26+
type User struct {
27+
ID int `json:"id"` // 用户ID
28+
Username string `json:"username"` // 用户名
29+
Name string `json:"name"` // 名称
30+
}

api/v1/post.go renamed to api/v1/post_api.go

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2020 opensourceai
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package v1
218

319
import (
@@ -10,6 +26,7 @@ import (
1026
"github.com/opensourceai/go-api-service/pkg/app"
1127
"github.com/opensourceai/go-api-service/pkg/e"
1228
"github.com/opensourceai/go-api-service/pkg/page"
29+
"github.com/unknwon/com"
1330
"net/http"
1431
"strconv"
1532
)
@@ -31,6 +48,8 @@ func NewPostRouter(router *gin.Engine) {
3148
// 无需认证
3249
{
3350
post.GET("/:id", getPost)
51+
post.GET("/:id/comments", getPostComments)
52+
3453
}
3554
// 需认证
3655
post.Use(jwt.JWT())
@@ -43,13 +62,43 @@ func NewPostRouter(router *gin.Engine) {
4362

4463
}
4564

65+
// @Summary 获取帖子的评论
66+
// @Tags Post
67+
// @Produce json
68+
// @Param id path string true "ID"
69+
// @Param page query page.Page false "page"
70+
// @Success 200 {object} app.Response
71+
// @Failure 500 {object} app.Response
72+
// @Router /v1/post/{id}/comments [get]
73+
func getPostComments(context *gin.Context) {
74+
appG := app.Gin{C: context}
75+
bindPage := page.BindPage(context)
76+
id := context.Param("id")
77+
if id == "" {
78+
79+
return
80+
}
81+
var idInt int // 主题ID
82+
var err error // 错误
83+
idInt, err = strconv.Atoi(id)
84+
if err != nil {
85+
return
86+
}
87+
88+
comments, err := postService.ServiceGetPostComments(idInt, bindPage)
89+
if err != nil {
90+
appG.Fail(nil)
91+
return
92+
}
93+
appG.Success(comments)
94+
}
95+
4696
// @Summary 获取帖子信息
4797
// @Tags Post
4898
// @Produce json
4999
// @Param id path string true "id"
50100
// @Success 200 {object} app.Response
51101
// @Failure 500 {object} app.Response
52-
// @Security ApiKeyAuth
53102
// @Router /v1/post/{id} [get]
54103
func getPost(context *gin.Context) {
55104
appG := app.Gin{C: context}
@@ -90,7 +139,7 @@ func updatePost(context *gin.Context) {
90139
return
91140
}
92141
userInfo := app.GetUserInfo(context)
93-
if err := postService.UpdatePost(userInfo.UserId, &post); err == gorm.ErrRecordNotFound {
142+
if err := postService.UpdatePost(com.ToStr(userInfo.UserId), &post); err == gorm.ErrRecordNotFound {
94143
appG.Response(http.StatusNotFound, e.NOT_FOUND, nil)
95144
return
96145
} else if err != nil {
@@ -162,7 +211,7 @@ func getPostList(context *gin.Context) {
162211
}
163212
info := app.GetUserInfo(context)
164213

165-
if post, err := postService.GetOwnPost(pageObj, info.UserId); err != nil {
214+
if post, err := postService.GetOwnPost(pageObj, com.ToStr(info.UserId)); err != nil {
166215
appG.Response(http.StatusBadRequest, e.ERROR, post)
167216
return
168217
} else {
@@ -197,7 +246,7 @@ func deletePost(context *gin.Context) {
197246
//token := context.GetHeader("Authorization")
198247
//userId, exists := context.Get("userId")
199248
userInfo := app.GetUserInfo(context)
200-
if err := postService.DeletePost(userInfo.UserId, postIds.Ids...); err != nil {
249+
if err := postService.DeletePost(com.ToStr(userInfo.UserId), postIds.Ids...); err != nil {
201250
if err == gorm.ErrRecordNotFound {
202251
appG.Response(http.StatusBadRequest, e.ERROR_POST_NOT_EXIST, nil)
203252
return

api/v1/vo/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# VO(视图对象)
2+
3+
用于服务层传输数据给视图层

api/v1/vo/comment_vo.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2020 opensourceai
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/*
18+
* @Package vo
19+
* @Author Quan Chen
20+
* @Date 2020/3/19
21+
* @Description
22+
*
23+
*/
24+
package vo

api/v1/vo/common_vo.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2020 opensourceai
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/*
18+
* @Package vo
19+
* @Author Quan Chen
20+
* @Date 2020/3/19
21+
* @Description
22+
*
23+
*/
24+
25+
package vo

0 commit comments

Comments
 (0)