From 756f1aa1610343b1422fe941136bd42edfac318e Mon Sep 17 00:00:00 2001 From: Asara Date: Sat, 25 Jan 2020 23:19:39 -0500 Subject: [PATCH] Flesh out basic commenting functions --- packages/blog/comments.go | 42 ++++++++++++++++++++++++++++++++++++++- packages/blog/posts.go | 10 +++++----- packages/blog/routes.go | 2 +- 3 files changed, 47 insertions(+), 7 deletions(-) diff --git a/packages/blog/comments.go b/packages/blog/comments.go index f86a78c..95c8f37 100644 --- a/packages/blog/comments.go +++ b/packages/blog/comments.go @@ -21,7 +21,7 @@ type Comment struct { TimeModified time.Time `json:"last_modified", db:"last_modified"` } -type Comments []Comments +type Comments []Comment func getCommentById(w http.ResponseWriter, r *http.Request) { returnMessage := ReturnMessage{} @@ -99,3 +99,43 @@ func postComment(w http.ResponseWriter, r *http.Request) { render.JSON(w, r, returnMessage) return } + +func getCommentsByParentId(w http.ResponseWriter, r *http.Request) { + returnMessage := ReturnMessage{} + returnMessage.Error = false + post_id := chi.URLParam(r, "post_id") + search := ` + SELECT id, author, content, time_published, modified, last_modified + FROM comments + WHERE parent = $1 + ORDER BY id DESC + ` + rows, err := DB.Query(search, post_id) + if err != nil { + returnMessage.Message = "something is super broken..." + returnMessage.Error = true + w.WriteHeader(http.StatusInternalServerError) + render.JSON(w, r, returnMessage) + fmt.Println(err) + return + } + defer rows.Close() + comment := Comment{} + comments := make(Comments, 0) + for rows.Next() { + if err := rows.Scan(&comment.ID, &comment.Author, &comment.Content, &comment.TimePublished, &comment.Modified, &comment.TimeModified); err != nil { + } + comments = append(comments, comment) + } + if err := rows.Err(); err != nil { + returnMessage.Message = "something is super broken..." + returnMessage.Error = true + w.WriteHeader(http.StatusInternalServerError) + render.JSON(w, r, returnMessage) + fmt.Println(err) + return + } + w.WriteHeader(http.StatusOK) + render.JSON(w, r, comments) + return +} diff --git a/packages/blog/posts.go b/packages/blog/posts.go index 7edb9ae..9317df6 100644 --- a/packages/blog/posts.go +++ b/packages/blog/posts.go @@ -43,7 +43,7 @@ type ReturnSuccess struct { Slug string `json:"slug",db:"slug"` } -type ReferenceID struct { +type ReferenceId struct { LastID int `json:"last_id"` } @@ -150,7 +150,7 @@ func updateBlogPostById(w http.ResponseWriter, r *http.Request) { returnMessage := ReturnMessage{} returnMessage.Error = false // Get the actual post - id := chi.URLParam(r, "id") + id := chi.URLParam(r, "post_id") result := DB.QueryRow("SELECT id, title, slug, author, content, time_published FROM posts WHERE id=$1", id) post := BlogPost{} err := result.Scan(&post.ID, &post.Title, &post.Slug, &post.Author, &post.Content, &post.TimePublished) @@ -210,7 +210,7 @@ func updateBlogPostById(w http.ResponseWriter, r *http.Request) { func getBlogPosts(w http.ResponseWriter, r *http.Request) { returnMessage := ReturnMessage{} returnMessage.Error = false - referenceID := &ReferenceID{} + referenceID := &ReferenceId{} err := json.NewDecoder(r.Body).Decode(referenceID) // hardcode 9001 for cool kid points if err != nil { @@ -299,7 +299,7 @@ func getBlogPostById(w http.ResponseWriter, r *http.Request) { func getBlogPostsByTag(w http.ResponseWriter, r *http.Request) { returnMessage := ReturnMessage{} returnMessage.Error = false - referenceID := &ReferenceID{} + referenceID := &ReferenceId{} err := json.NewDecoder(r.Body).Decode(referenceID) // hardcode 9001 for cool kid points if err != nil { @@ -353,7 +353,7 @@ func getBlogPostsByTag(w http.ResponseWriter, r *http.Request) { func getBlogPostsByAuthor(w http.ResponseWriter, r *http.Request) { returnMessage := ReturnMessage{} returnMessage.Error = false - referenceID := &ReferenceID{} + referenceID := &ReferenceId{} err := json.NewDecoder(r.Body).Decode(referenceID) // hardcode 9001 for cool kid points if err != nil { diff --git a/packages/blog/routes.go b/packages/blog/routes.go index cf73a05..51c639b 100644 --- a/packages/blog/routes.go +++ b/packages/blog/routes.go @@ -23,7 +23,7 @@ func Routes() *chi.Mux { r.Get("/posts/by-tag/{tag}", getBlogPostsByTag) r.Get("/posts/by-author/{author}", getBlogPostsByAuthor) r.Get("/posts/by-slug/{slug}", getBlogPostBySlug) - r.Get("/comments/{id}", getCommentById) + r.Get("/comments/{post_id}", getCommentsByParentId) /* r.Get("/by-author/{author}", getCommentsByAuthor) r.Get("/by-parent/{post_id}", getCommentsByPost)