Skip to content

Conversation

@grnd-alt
Copy link
Owner

@grnd-alt grnd-alt commented Jul 4, 2025

No description provided.

Signed-off-by: grnd-alt <[email protected]>
@grnd-alt grnd-alt requested a review from Copilot July 4, 2025 09:58
@grnd-alt grnd-alt merged commit 8146c7e into main Jul 4, 2025
1 check passed
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds pagination support for fetching comments, refactors post/comment types, and updates related SQL queries and routes.

  • Switch PostWithComments to use dbgen.Comment and update population logic.
  • Introduce CommentsService.GetComments with limit/offset, new controller action, and route.
  • Update SQL queries (GetCommentsByPost, GetCommentsByPosts) to include ORDER BY, LIMIT, and OFFSET, and regenerate corresponding Go code.

Reviewed Changes

Copilot reviewed 7 out of 8 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
services/posts_service.go Updated PostWithComments, changed comment type, removed mutex
services/comments_service.go Added GetComments(postID, page) with pagination logic
controllers/comments_controller.go Added GetCommentsByPost handler parsing page parameter
routes/routes.go Added GET /api/comments/:postId route
db/sql/queries/comments.sql Added pagination clauses to SQL queries
db/sql/dbgen/comments.sql.go Regenerated query methods to accept Limit/Offset
go.mod Bumped several indirect dependencies
Comments suppressed due to low confidence (1)

services/comments_service.go:20

  • New pagination logic should have unit tests verifying offset calculation, boundary conditions, and empty-page behavior to prevent regressions.
func (c *CommentsService) GetComments(postID string, page int32) ([]dbgen.Comment, error) {

commentsMap := make(map[string][]*dbgen.GetCommentsByPostsRow)
commentsMap := make(map[string][]*dbgen.Comment)
for _, comment := range comments {
commentsMap[comment.PostID] = append(commentsMap[comment.PostID], &comment)
Copy link

Copilot AI Jul 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Taking the address of the loop variable comment causes all pointers to reference the same memory. Instead, create a new variable in the loop (e.g., c := comment) and append &c.

Suggested change
commentsMap[comment.PostID] = append(commentsMap[comment.PostID], &comment)
c := comment
commentsMap[comment.PostID] = append(commentsMap[comment.PostID], &c)

Copilot uses AI. Check for mistakes.
}
post.Filepath = pgtype.Text{String: filepath, Valid: true}
mut.Lock()
postsWithComments[i] = PostWithComments{
Copy link

Copilot AI Jul 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing the mutex leads to a data race when writing to postsWithComments concurrently. Reintroduce synchronization or populate the slice sequentially to avoid races.

Copilot uses AI. Check for mistakes.

func (c *CommentsService) CreateComment(userId string, username string,content string, postID string) (*dbgen.Comment, error) {
func (c *CommentsService) GetComments(postID string, page int32) ([]dbgen.Comment, error) {
comments, err := c.Queries.GetCommentsByPost(context.Background(), dbgen.GetCommentsByPostParams{Offset: 10 * page, PostID: postID, Limit: 10})
Copy link

Copilot AI Jul 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Extract the magic numbers 10 (page size) into a named constant so it's easier to adjust pagination limits in the future.

Suggested change
comments, err := c.Queries.GetCommentsByPost(context.Background(), dbgen.GetCommentsByPostParams{Offset: 10 * page, PostID: postID, Limit: 10})
comments, err := c.Queries.GetCommentsByPost(context.Background(), dbgen.GetCommentsByPostParams{Offset: DefaultPageSize * page, PostID: postID, Limit: DefaultPageSize})

Copilot uses AI. Check for mistakes.
Comment on lines +24 to +28
page, err := strconv.Atoi(ctx.Query("page"))
if postID == "" || err != nil || page < 0 {
response := gin.H{"error": "incorrect parameters"}
ctx.JSON(http.StatusBadRequest, response)
return
Copy link

Copilot AI Jul 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Calling Atoi on an empty string returns an error. Consider defaulting to page 0 when the page query param is omitted so a first page request doesn't fail.

Suggested change
page, err := strconv.Atoi(ctx.Query("page"))
if postID == "" || err != nil || page < 0 {
response := gin.H{"error": "incorrect parameters"}
ctx.JSON(http.StatusBadRequest, response)
return
pageStr := ctx.Query("page")
page := 0
if pageStr != "" {
var err error
page, err = strconv.Atoi(pageStr)
if err != nil || page < 0 {
response := gin.H{"error": "incorrect parameters"}
ctx.JSON(http.StatusBadRequest, response)
return
}

Copilot uses AI. Check for mistakes.
select * from RankedComments where rn <= 10;
SELECT c.* from unnest($1::varchar[]) as post_ids
JOIN LATERAL (
SELECT * FROM comments WHERE post_id = post_ids ORDER BY created_at DESC LIMIT 2
Copy link

Copilot AI Jul 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The LIMIT 2 in the lateral join is effectively a magic number. Consider making the per-post comment limit configurable or use the same page-size constant.

Suggested change
SELECT * FROM comments WHERE post_id = post_ids ORDER BY created_at DESC LIMIT 2
SELECT * FROM comments WHERE post_id = post_ids ORDER BY created_at DESC LIMIT $2

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants