diff --git a/api/article_handler.go b/api/article_handler.go index 014c192f14..0e999021b5 100644 --- a/api/article_handler.go +++ b/api/article_handler.go @@ -918,10 +918,10 @@ func newCommentsResponse( }, len(comments)), } for i, comment := range comments { - res.Comments[i].ID = comment.ID - res.Comments[i].CreatedAt = comment.CreatedAt - res.Comments[i].UpdatedAt = comment.UpdatedAt - res.Comments[i].Body = comment.Body + res.Comments[i].ID = &comment.ID + res.Comments[i].CreatedAt = &comment.CreatedAt + res.Comments[i].UpdatedAt = &comment.UpdatedAt + res.Comments[i].Body = &comment.Body res.Comments[i].Author.Username = comment.Username res.Comments[i].Author.Bio = comment.Bio res.Comments[i].Author.Image = comment.Image diff --git a/db/query/article.sql b/db/query/article.sql index 27907fadd2..889d1d6264 100644 --- a/db/query/article.sql +++ b/db/query/article.sql @@ -130,14 +130,11 @@ SELECT u.id AS author_id, u.username, u.bio, - u.image -FROM ( - SELECT id - FROM articles - WHERE slug = $1 -) a -LEFT JOIN comments c ON a.id = c.article_id -LEFT JOIN users u ON c.author_id = u.id; + u.image +FROM comments c +LEFT JOIN articles a ON a.id = c.article_id +LEFT JOIN users u ON u.id = c.author_id +WHERE a.slug = $1; -- name: GetArticlesByTag :many WITH article_tags_cte AS ( diff --git a/db/sqlc/article.sql.go b/db/sqlc/article.sql.go index c09855122a..6f57ec508b 100644 --- a/db/sqlc/article.sql.go +++ b/db/sqlc/article.sql.go @@ -1042,25 +1042,22 @@ SELECT u.id AS author_id, u.username, u.bio, - u.image -FROM ( - SELECT id - FROM articles - WHERE slug = $1 -) a -LEFT JOIN comments c ON a.id = c.article_id -LEFT JOIN users u ON c.author_id = u.id + u.image +FROM comments c +LEFT JOIN articles a ON a.id = c.article_id +LEFT JOIN users u ON u.id = c.author_id +WHERE a.slug = $1 ` type GetCommentsBySlugRow struct { - ID *string `json:"id"` - Body *string `json:"body"` - CreatedAt *time.Time `json:"created_at"` - UpdatedAt *time.Time `json:"updated_at"` - AuthorID *string `json:"author_id"` - Username *string `json:"username"` - Bio *string `json:"bio"` - Image *string `json:"image"` + ID string `json:"id"` + Body string `json:"body"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` + AuthorID *string `json:"author_id"` + Username *string `json:"username"` + Bio *string `json:"bio"` + Image *string `json:"image"` } func (q *Queries) GetCommentsBySlug(ctx context.Context, slug string) ([]*GetCommentsBySlugRow, error) {