-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcomment_api.go
140 lines (112 loc) · 2.53 KB
/
comment_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
package blog
import (
"errors"
"strconv"
"github.com/gin-gonic/gin"
"github.com/uptrace/go-realworld-example-app/org"
"github.com/uptrace/go-realworld-example-app/rwe"
)
func listArticleComments(c *gin.Context) {
ctx := c.Request.Context()
article, err := SelectArticle(ctx, c.Param("slug"))
if err != nil {
c.Error(err)
return
}
var userID uint64
user, ok := c.Get("user")
if ok {
userID = user.(*org.User).ID
}
articles := make([]*Comment, 0)
if err := rwe.PGMain().ModelContext(ctx, &articles).
ColumnExpr("c.*").
Relation("Author").
Apply(authorFollowingColumn(userID)).
Where("article_id = ?", article.ID).
Select(); err != nil {
c.Error(err)
return
}
c.JSON(200, gin.H{"comments": articles})
}
func showComment(c *gin.Context) {
ctx := c.Request.Context()
article, err := SelectArticle(ctx, c.Param("slug"))
if err != nil {
c.Error(err)
return
}
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
if err != nil {
c.Error(err)
return
}
var userID uint64
user, ok := c.Get("user")
if ok {
userID = user.(*org.User).ID
}
comment := new(Comment)
if err := rwe.PGMain().ModelContext(ctx, comment).
ColumnExpr("c.*").
Relation("Author").
Apply(authorFollowingColumn(userID)).
Where("c.id = ?", id).
Where("article_id = ?", article.ID).
Select(); err != nil {
c.Error(err)
return
}
c.JSON(200, gin.H{"comment": comment})
}
func createComment(c *gin.Context) {
ctx := c.Request.Context()
user := c.MustGet("user").(*org.User)
article, err := SelectArticle(ctx, c.Param("slug"))
if err != nil {
c.Error(err)
return
}
var in struct {
Comment *Comment `json:"comment"`
}
if err := c.BindJSON(&in); err != nil {
return
}
if in.Comment == nil {
c.Error(errors.New(`JSON field "comment" is required`))
return
}
comment := in.Comment
comment.AuthorID = user.ID
comment.ArticleID = article.ID
comment.CreatedAt = rwe.Clock.Now()
comment.UpdatedAt = rwe.Clock.Now()
if _, err := rwe.PGMain().
ModelContext(ctx, comment).
Insert(); err != nil {
c.Error(err)
return
}
comment.Author = org.NewProfile(user)
c.JSON(200, gin.H{"comment": comment})
}
func deleteComment(c *gin.Context) {
ctx := c.Request.Context()
user := c.MustGet("user").(*org.User)
article, err := SelectArticle(ctx, c.Param("slug"))
if err != nil {
c.Error(err)
return
}
if _, err := rwe.PGMain().
ModelContext(ctx, (*Comment)(nil)).
Where("author_id = ?", user.ID).
Where("article_id = ?", article.ID).
Delete(); err != nil {
c.Error(err)
return
}
c.JSON(200, nil)
}