Skip to content

Commit 5bb7b35

Browse files
add swagger docs
1 parent 603ab1c commit 5bb7b35

File tree

2 files changed

+212
-0
lines changed

2 files changed

+212
-0
lines changed

internal/handlers/comment.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@ func NewCommentHandler(commentService *services.CommentService) *CommentHandler
2222
}
2323
}
2424

25+
26+
// @Summary Create a new comment
27+
// @Description Create a new comment for a specific post
28+
// @Tags Comments
29+
// @Accept json
30+
// @Produce json
31+
// @Param id path int true "Post ID"
32+
// @Param comment body models.CreateCommentRequest true "Comment data"
33+
// @Success 201 {object} map[string]interface{}
34+
// @Failure 400 {object} map[string]string
35+
// @Failure 401 {object} map[string]string
36+
// @Failure 500 {object} map[string]string
37+
// @Router /posts/{id}/comments [post]
2538
func (c *CommentHandler) CreateComment(ctx *gin.Context) {
2639
uidRaw, exists := ctx.Get("user_id")
2740
if !exists {
@@ -65,6 +78,17 @@ func (c *CommentHandler) CreateComment(ctx *gin.Context) {
6578
})
6679
}
6780

81+
// @Summary Get a comment by ID
82+
// @Description Retrieve a comment by its ID
83+
// @Tags Comments
84+
// @Accept json
85+
// @Produce json
86+
// @Param id path int true "Comment ID"
87+
// @Success 200 {object} map[string]interface{}
88+
// @Failure 400 {object} map[string]string
89+
// @Failure 404 {object} map[string]string
90+
// @Failure 500 {object} map[string]string
91+
// @Router /comments/{id} [get]
6892
func (c *CommentHandler) GetCommentByID(ctx *gin.Context) {
6993
idParam := ctx.Param("id")
7094
id, err := strconv.ParseUint(idParam, 10, 32)
@@ -88,6 +112,18 @@ func (c *CommentHandler) GetCommentByID(ctx *gin.Context) {
88112
})
89113
}
90114

115+
// @Summary Update a comment
116+
// @Description Update an existing comment by its ID
117+
// @Tags Comments
118+
// @Accept json
119+
// @Produce json
120+
// @Param id path int true "Comment ID"
121+
// @Param comment body models.UpdateCommentRequest true "Updated comment data"
122+
// @Success 200 {object} map[string]interface{}
123+
// @Failure 400 {object} map[string]string
124+
// @Failure 404 {object} map[string]string
125+
// @Failure 500 {object} map[string]string
126+
// @Router /comments/{id} [put]
91127
func (c *CommentHandler) UpdateComment(ctx *gin.Context) {
92128
idParam := ctx.Param("id")
93129
id, err := strconv.ParseUint(idParam, 10, 32)
@@ -123,6 +159,17 @@ func (c *CommentHandler) UpdateComment(ctx *gin.Context) {
123159
})
124160
}
125161

162+
// @Summary Delete a comment
163+
// @Description Delete a comment by its ID
164+
// @Tags Comments
165+
// @Accept json
166+
// @Produce json
167+
// @Param id path int true "Comment ID"
168+
// @Success 204 {object} nil
169+
// @Failure 400 {object} map[string]string
170+
// @Failure 404 {object} map[string]string
171+
// @Failure 500 {object} map[string]string
172+
// @Router /comments/{id} [delete]
126173
func (c *CommentHandler) DeleteComment(ctx *gin.Context) {
127174
idParam := ctx.Param("id")
128175
id, err := strconv.ParseUint(idParam, 10, 32)
@@ -144,6 +191,17 @@ func (c *CommentHandler) DeleteComment(ctx *gin.Context) {
144191
ctx.Status(http.StatusNoContent)
145192
}
146193

194+
// @Summary List comments by post ID
195+
// @Description Retrieve all comments for a specific post
196+
// @Tags Comments
197+
// @Accept json
198+
// @Produce json
199+
// @Param id path int true "Post ID"
200+
// @Success 200 {object} map[string]interface{}
201+
// @Failure 400 {object} map[string]string
202+
// @Failure 404 {object} map[string]string
203+
// @Failure 500 {object} map[string]string
204+
// @Router /posts/{id}/comments [get]
147205
func (c *CommentHandler) ListCommentsByPostID(ctx *gin.Context) {
148206
postidParam := ctx.Param("id")
149207
postID, err := strconv.ParseUint(postidParam, 10, 32)
@@ -167,6 +225,17 @@ func (c *CommentHandler) ListCommentsByPostID(ctx *gin.Context) {
167225
})
168226
}
169227

228+
// @Summary List comments by user ID
229+
// @Description Retrieve all comments made by a specific user
230+
// @Tags Comments
231+
// @Accept json
232+
// @Produce json
233+
// @Param id path int true "User ID"
234+
// @Success 200 {object} map[string]interface{}
235+
// @Failure 400 {object} map[string]string
236+
// @Failure 404 {object} map[string]string
237+
// @Failure 500 {object} map[string]string
238+
// @Router /users/{id}/comments [get]
170239
func (c *CommentHandler) ListCommentsByUserID(ctx *gin.Context) {
171240
uidParam := ctx.Param("id")
172241
userID, err := strconv.ParseUint(uidParam, 10, 32)
@@ -190,6 +259,18 @@ func (c *CommentHandler) ListCommentsByUserID(ctx *gin.Context) {
190259
})
191260
}
192261

262+
// @Summary Like a comment
263+
// @Description Like a comment by its ID
264+
// @Tags Comments
265+
// @Accept json
266+
// @Produce json
267+
// @Param id path int true "Comment ID"
268+
// @Success 200 {object} map[string]interface{}
269+
// @Failure 400 {object} map[string]string
270+
// @Failure 401 {object} map[string]string
271+
// @Failure 404 {object} map[string]string
272+
// @Failure 500 {object} map[string]string
273+
// @Router /comments/{id}/like [post]
193274
func (c *CommentHandler) LikeComment(ctx *gin.Context) {
194275
uidRaw, exists := ctx.Get("user_id")
195276
if !exists {
@@ -231,6 +312,18 @@ func (c *CommentHandler) LikeComment(ctx *gin.Context) {
231312
ctx.JSON(http.StatusOK, gin.H{"message": "comment liked successfully"})
232313
}
233314

315+
// @Summary Unlike a comment
316+
// @Description Unlike a comment by its ID
317+
// @Tags Comments
318+
// @Accept json
319+
// @Produce json
320+
// @Param id path int true "Comment ID"
321+
// @Success 200 {object} map[string]interface{}
322+
// @Failure 400 {object} map[string]string
323+
// @Failure 401 {object} map[string]string
324+
// @Failure 404 {object} map[string]string
325+
// @Failure 500 {object} map[string]string
326+
// @Router /comments/{id}/unlike [post]
234327
func (c *CommentHandler) UnlikeComment(ctx *gin.Context) {
235328
uidRaw, exists := ctx.Get("user_id")
236329
if !exists {
@@ -272,6 +365,17 @@ func (c *CommentHandler) UnlikeComment(ctx *gin.Context) {
272365
ctx.JSON(http.StatusOK, gin.H{"message": "comment unliked successfully"})
273366
}
274367

368+
// @Summary List likes by comment ID
369+
// @Description Retrieve all likes for a specific comment
370+
// @Tags Comments
371+
// @Accept json
372+
// @Produce json
373+
// @Param id path int true "Comment ID"
374+
// @Success 200 {object} map[string]interface{}
375+
// @Failure 400 {object} map[string]string
376+
// @Failure 404 {object} map[string]string
377+
// @Failure 500 {object} map[string]string
378+
// @Router /comments/{id}/likes [get]
275379
func (c *CommentHandler) ListLikesByCommentID(ctx *gin.Context) {
276380
idParam := ctx.Param("id")
277381
commentID, err := strconv.ParseUint(idParam, 10, 32)

internal/handlers/post.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@ func NewPostHandler(postService *services.PostService) *PostHandler {
2323
}
2424
}
2525

26+
// @Summary Create a new post
27+
// @Description Create a new post associated with the authenticated user
28+
// @Tags Posts
29+
// @Accept json
30+
// @Produce json
31+
// @Param post body models.CreatePostRequest true "Post data"
32+
// @Success 201 {object} models.Post "Post created successfully"
33+
// @Failure 400 {object} gin.H "Bad request"
34+
// @Failure 401 {object} gin.H "Unauthorized"
35+
// @Failure 500 {object} gin.H "Internal server error"
36+
// @Router /posts [post]
2637
func (h *PostHandler) CreatePost(c *gin.Context) {
2738
useridRaw, exists := c.Get("user_id")
2839
if !exists {
@@ -59,6 +70,17 @@ func (h *PostHandler) CreatePost(c *gin.Context) {
5970
})
6071
}
6172

73+
// @Summary Get a post by ID
74+
// @Description Retrieve a post by its ID
75+
// @Tags Posts
76+
// @Accept json
77+
// @Produce json
78+
// @Param id path int true "Post ID"
79+
// @Success 200 {object} models.Post "Post retrieved successfully"
80+
// @Failure 400 {object} gin.H "Bad request"
81+
// @Failure 404 {object} gin.H "Post not found"
82+
// @Failure 500 {object} gin.H "Internal server error"
83+
// @Router /posts/{id} [get]
6284
func (h *PostHandler) GetPostByID(c *gin.Context) {
6385
idParam := c.Param("id")
6486
id, err := strconv.ParseUint(idParam, 10, 32)
@@ -80,6 +102,18 @@ func (h *PostHandler) GetPostByID(c *gin.Context) {
80102
c.JSON(http.StatusOK, gin.H{"post": post})
81103
}
82104

105+
// @Summary Update a post
106+
// @Description Update a post by its ID
107+
// @Tags Posts
108+
// @Accept json
109+
// @Produce json
110+
// @Param id path int true "Post ID"
111+
// @Param post body models.UpdatePostRequest true "Updated post data"
112+
// @Success 200 {object} models.Post "Post updated successfully"
113+
// @Failure 400 {object} gin.H "Bad request"
114+
// @Failure 404 {object} gin.H "Post not found"
115+
// @Failure 500 {object} gin.H "Internal server error"
116+
// @Router /posts/{id} [put]
83117
func (h *PostHandler) UpdatePost(c *gin.Context) {
84118
idParam := c.Param("id")
85119
id, err := strconv.ParseUint(idParam, 10, 32)
@@ -115,6 +149,17 @@ func (h *PostHandler) UpdatePost(c *gin.Context) {
115149
})
116150
}
117151

152+
// @Summary Delete a post
153+
// @Description Delete a post by its ID
154+
// @Tags Posts
155+
// @Accept json
156+
// @Produce json
157+
// @Param id path int true "Post ID"
158+
// @Success 204 {object} nil "Post deleted successfully"
159+
// @Failure 400 {object} gin.H "Bad request"
160+
// @Failure 404 {object} gin.H "Post not found"
161+
// @Failure 500 {object} gin.H "Internal server error"
162+
// @Router /posts/{id} [delete]
118163
func (h *PostHandler) DeletePost(c *gin.Context) {
119164
idParam := c.Param("id")
120165
id, err := strconv.ParseUint(idParam, 10, 32)
@@ -135,6 +180,16 @@ func (h *PostHandler) DeletePost(c *gin.Context) {
135180
c.Status(http.StatusNoContent)
136181
}
137182

183+
// @Summary List posts by user ID
184+
// @Description Retrieve all posts created by a specific user
185+
// @Tags Posts
186+
// @Accept json
187+
// @Produce json
188+
// @Param id path int true "User ID"
189+
// @Success 200 {array} models.Post "Posts retrieved successfully"
190+
// @Failure 400 {object} gin.H "Bad request"
191+
// @Failure 500 {object} gin.H "Internal server error"
192+
// @Router /users/{id}/posts [get]
138193
func (h *PostHandler) ListPostsByUserID(c *gin.Context) {
139194
userIDParam := c.Param("id")
140195
userID, err := strconv.ParseUint(userIDParam, 10, 32)
@@ -152,6 +207,16 @@ func (h *PostHandler) ListPostsByUserID(c *gin.Context) {
152207
c.JSON(http.StatusOK, gin.H{"posts": posts})
153208
}
154209

210+
// @Summary List posts by club ID
211+
// @Description Retrieve all posts associated with a specific club
212+
// @Tags Posts
213+
// @Accept json
214+
// @Produce json
215+
// @Param id path int true "Club ID"
216+
// @Success 200 {array} models.Post "Posts retrieved successfully"
217+
// @Failure 400 {object} gin.H "Bad request"
218+
// @Failure 500 {object} gin.H "Internal server error"
219+
// @Router /clubs/{id}/posts [get]
155220
func (h *PostHandler) ListPostsByClubID(c *gin.Context) {
156221
clubIDParam := c.Param("id")
157222
clubID, err := strconv.ParseUint(clubIDParam, 10, 32)
@@ -169,6 +234,14 @@ func (h *PostHandler) ListPostsByClubID(c *gin.Context) {
169234
c.JSON(http.StatusOK, gin.H{"posts": posts})
170235
}
171236

237+
// @Summary List all posts
238+
// @Description Retrieve all posts in the system
239+
// @Tags Posts
240+
// @Accept json
241+
// @Produce json
242+
// @Success 200 {array} models.Post "Posts retrieved successfully"
243+
// @Failure 500 {object} gin.H "Internal server error"
244+
// @Router /posts [get]
172245
func (h *PostHandler) ListAllPosts(c *gin.Context) {
173246
posts, err := h.postService.ListAllPosts()
174247
if err != nil {
@@ -179,6 +252,18 @@ func (h *PostHandler) ListAllPosts(c *gin.Context) {
179252
c.JSON(http.StatusOK, gin.H{"posts": posts})
180253
}
181254

255+
// @Summary Like a post
256+
// @Description Like a post by its ID
257+
// @Tags Posts
258+
// @Accept json
259+
// @Produce json
260+
// @Param id path int true "Post ID"
261+
// @Success 200 {object} gin.H "Post liked successfully"
262+
// @Failure 400 {object} gin.H "Bad request"
263+
// @Failure 401 {object} gin.H "Unauthorized"
264+
// @Failure 404 {object} gin.H "Post not found"
265+
// @Failure 500 {object} gin.H "Internal server error"
266+
// @Router /posts/{id}/like [post]
182267
func (h *PostHandler) LikePost(c *gin.Context) {
183268
postIdParam := c.Param("id")
184269
postID, err := strconv.ParseUint(postIdParam, 10, 32)
@@ -214,6 +299,18 @@ func (h *PostHandler) LikePost(c *gin.Context) {
214299
c.JSON(http.StatusOK, gin.H{"message": "post liked successfully"})
215300
}
216301

302+
// @Summary Unlike a post
303+
// @Description Unlike a post by its ID
304+
// @Tags Posts
305+
// @Accept json
306+
// @Produce json
307+
// @Param id path int true "Post ID"
308+
// @Success 200 {object} gin.H "Post unliked successfully"
309+
// @Failure 400 {object} gin.H "Bad request"
310+
// @Failure 401 {object} gin.H "Unauthorized"
311+
// @Failure 404 {object} gin.H "Post not found"
312+
// @Failure 500 {object} gin.H "Internal server error"
313+
// @Router /posts/{id}/unlike [post]
217314
func (h *PostHandler) UnlikePost(c *gin.Context) {
218315
postIdParam := c.Param("id")
219316
postID, err := strconv.ParseUint(postIdParam, 10, 32)
@@ -249,6 +346,17 @@ func (h *PostHandler) UnlikePost(c *gin.Context) {
249346
c.JSON(http.StatusOK, gin.H{"message": "post unliked successfully"})
250347
}
251348

349+
// @Summary List likes by post ID
350+
// @Description Retrieve all likes associated with a specific post
351+
// @Tags Posts
352+
// @Accept json
353+
// @Produce json
354+
// @Param id path int true "Post ID"
355+
// @Success 200 {array} models.Like "Likes retrieved successfully"
356+
// @Failure 400 {object} gin.H "Bad request"
357+
// @Failure 404 {object} gin.H "Post not found or no likes found"
358+
// @Failure 500 {object} gin.H "Internal server error"
359+
// @Router /posts/{id}/likes [get]
252360
func (h *PostHandler) ListLikesByPostID(c *gin.Context) {
253361
postIdParam := c.Param("id")
254362
postID, err := strconv.ParseUint(postIdParam, 10, 32)

0 commit comments

Comments
 (0)