-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomment.go
More file actions
302 lines (258 loc) · 8.71 KB
/
Copy pathcomment.go
File metadata and controls
302 lines (258 loc) · 8.71 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package main
import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"net/http"
"github.com/raeperd/realworld.go/internal/sqlite"
)
func handlePostArticlesSlugComments(db *sql.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var request commentPostRequestBody
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
defer func() { _ = r.Body.Close() }()
if errs := request.Validate(); len(errs) > 0 {
encodeErrorResponse(r.Context(), http.StatusUnprocessableEntity, errs, w)
return
}
// Get authenticated user ID from context
userID, ok := r.Context().Value(userIDKey).(int64)
if !ok {
encodeErrorResponse(r.Context(), http.StatusUnauthorized, []error{errors.New("unauthorized")}, w)
return
}
// Get article slug from URL path
slug := r.PathValue("slug")
tx, err := db.BeginTx(r.Context(), nil)
if err != nil {
encodeErrorResponse(r.Context(), http.StatusInternalServerError, []error{err}, w)
return
}
defer func() { _ = tx.Rollback() }()
queries := sqlite.New(tx)
// Get article by slug to verify it exists
article, err := queries.GetArticleBySlug(r.Context(), slug)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
encodeErrorResponse(r.Context(), http.StatusNotFound, []error{errors.New("article not found")}, w)
return
}
encodeErrorResponse(r.Context(), http.StatusInternalServerError, []error{err}, w)
return
}
// Create comment
comment, err := queries.CreateComment(r.Context(), sqlite.CreateCommentParams{
Body: request.Comment.Body,
ArticleID: article.ID,
AuthorID: userID,
})
if err != nil {
encodeErrorResponse(r.Context(), http.StatusInternalServerError, []error{err}, w)
return
}
// Get comment with author details
commentWithAuthor, err := queries.GetCommentWithAuthor(r.Context(), comment.ID)
if err != nil {
encodeErrorResponse(r.Context(), http.StatusInternalServerError, []error{err}, w)
return
}
// Check if current user is following the comment author
following := false
if userID != commentWithAuthor.AuthorID {
isFollowingInt, err := queries.IsFollowing(r.Context(), sqlite.IsFollowingParams{
FollowerID: userID,
FollowedID: commentWithAuthor.AuthorID,
})
if err != nil {
encodeErrorResponse(r.Context(), http.StatusInternalServerError, []error{err}, w)
return
}
following = isFollowingInt == 1
}
if err := tx.Commit(); err != nil {
encodeErrorResponse(r.Context(), http.StatusInternalServerError, []error{err}, w)
return
}
// Build response
response := commentResponseBody{
Comment: commentPayload{
ID: commentWithAuthor.ID,
CreatedAt: commentWithAuthor.CreatedAt.Format("2006-01-02T15:04:05.999Z"),
UpdatedAt: commentWithAuthor.UpdatedAt.Format("2006-01-02T15:04:05.999Z"),
Body: commentWithAuthor.Body,
Author: authorProfile{
Username: commentWithAuthor.AuthorUsername,
Bio: commentWithAuthor.AuthorBio.String,
Image: commentWithAuthor.AuthorImage.String,
Following: following,
},
},
}
encodeResponse(r.Context(), http.StatusCreated, response, w)
}
}
type commentPostRequestBody struct {
Comment commentPostRequest `json:"comment"`
}
func (r commentPostRequestBody) Validate() []error {
var errs []error
if r.Comment.Body == "" {
errs = append(errs, errors.New("body is required"))
}
return errs
}
type commentPostRequest struct {
Body string `json:"body"`
}
type commentResponseBody struct {
Comment commentPayload `json:"comment"`
}
type commentPayload struct {
ID int64 `json:"id"`
CreatedAt string `json:"createdAt"`
UpdatedAt string `json:"updatedAt"`
Body string `json:"body"`
Author authorProfile `json:"author"`
}
func handleGetArticlesSlugComments(db *sql.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Get article slug from URL path
slug := r.PathValue("slug")
// Get optional user ID from context (for following status)
userID, _ := r.Context().Value(userIDKey).(int64)
queries := sqlite.New(db)
// Verify article exists first
_, err := queries.GetArticleBySlug(r.Context(), slug)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
encodeErrorResponse(r.Context(), http.StatusNotFound, []error{errors.New("article not found")}, w)
return
}
encodeErrorResponse(r.Context(), http.StatusInternalServerError, []error{err}, w)
return
}
// Get comments by article slug
comments, err := queries.GetCommentsByArticleSlug(r.Context(), slug)
if err != nil {
encodeErrorResponse(r.Context(), http.StatusInternalServerError, []error{err}, w)
return
}
// Build following status map to avoid N+1 queries
followingMap := make(map[int64]bool)
if userID != 0 && len(comments) > 0 {
// Collect unique author IDs (excluding current user)
authorIDsMap := make(map[int64]struct{})
for i := range comments {
if comments[i].AuthorID != userID {
authorIDsMap[comments[i].AuthorID] = struct{}{}
}
}
// Convert map to slice for batch query
if len(authorIDsMap) > 0 {
authorIDs := make([]int64, 0, len(authorIDsMap))
for id := range authorIDsMap {
authorIDs = append(authorIDs, id)
}
// Single batch query to get all following relationships
followedIDs, err := queries.GetFollowingByIDs(r.Context(), sqlite.GetFollowingByIDsParams{
FollowerID: userID,
FollowedIds: authorIDs,
})
if err != nil {
encodeErrorResponse(r.Context(), http.StatusInternalServerError, []error{err}, w)
return
}
// Build map for O(1) lookups
for _, followedID := range followedIDs {
followingMap[followedID] = true
}
}
}
// Build response with comments
commentPayloads := make([]commentPayload, len(comments))
for i := range comments {
// Get following status from map (defaults to false if not present)
following := followingMap[comments[i].AuthorID]
commentPayloads[i] = commentPayload{
ID: comments[i].ID,
CreatedAt: comments[i].CreatedAt.Format("2006-01-02T15:04:05.999Z"),
UpdatedAt: comments[i].UpdatedAt.Format("2006-01-02T15:04:05.999Z"),
Body: comments[i].Body,
Author: authorProfile{
Username: comments[i].AuthorUsername,
Bio: comments[i].AuthorBio.String,
Image: comments[i].AuthorImage.String,
Following: following,
},
}
}
response := commentsResponseBody{
Comments: commentPayloads,
}
encodeResponse(r.Context(), http.StatusOK, response, w)
}
}
type commentsResponseBody struct {
Comments []commentPayload `json:"comments"`
}
func handleDeleteArticlesSlugCommentsID(db *sql.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
slug := r.PathValue("slug")
commentIDStr := r.PathValue("id")
// Parse comment ID from path
var commentID int64
if _, err := fmt.Sscanf(commentIDStr, "%d", &commentID); err != nil {
encodeErrorResponse(r.Context(), http.StatusBadRequest, []error{errors.New("invalid comment ID")}, w)
return
}
// Get authenticated user ID from context
userID, ok := r.Context().Value(userIDKey).(int64)
if !ok {
encodeErrorResponse(r.Context(), http.StatusUnauthorized, []error{errors.New("unauthorized")}, w)
return
}
queries := sqlite.New(db)
// Verify article exists
article, err := queries.GetArticleBySlug(r.Context(), slug)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
encodeErrorResponse(r.Context(), http.StatusNotFound, []error{errors.New("article not found")}, w)
return
}
encodeErrorResponse(r.Context(), http.StatusInternalServerError, []error{err}, w)
return
}
// Get comment by ID
comment, err := queries.GetCommentByID(r.Context(), commentID)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
encodeErrorResponse(r.Context(), http.StatusNotFound, []error{errors.New("comment not found")}, w)
return
}
encodeErrorResponse(r.Context(), http.StatusInternalServerError, []error{err}, w)
return
}
// Verify comment belongs to the article (security check)
if comment.ArticleID != article.ID {
encodeErrorResponse(r.Context(), http.StatusNotFound, []error{errors.New("comment not found")}, w)
return
}
// Verify user is the comment author (authorization check)
if comment.AuthorID != userID {
encodeErrorResponse(r.Context(), http.StatusForbidden, []error{errors.New("not authorized to delete this comment")}, w)
return
}
// Delete the comment
err = queries.DeleteComment(r.Context(), commentID)
if err != nil {
encodeErrorResponse(r.Context(), http.StatusInternalServerError, []error{err}, w)
return
}
// Return 204 No Content
w.WriteHeader(http.StatusNoContent)
}
}