Flesh out basic commenting functions

This commit is contained in:
Amarpreet Minhas 2020-01-25 23:19:39 -05:00
parent 705c442114
commit 756f1aa161
3 changed files with 47 additions and 7 deletions

View file

@ -21,7 +21,7 @@ type Comment struct {
TimeModified time.Time `json:"last_modified", db:"last_modified"` TimeModified time.Time `json:"last_modified", db:"last_modified"`
} }
type Comments []Comments type Comments []Comment
func getCommentById(w http.ResponseWriter, r *http.Request) { func getCommentById(w http.ResponseWriter, r *http.Request) {
returnMessage := ReturnMessage{} returnMessage := ReturnMessage{}
@ -99,3 +99,43 @@ func postComment(w http.ResponseWriter, r *http.Request) {
render.JSON(w, r, returnMessage) render.JSON(w, r, returnMessage)
return 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
}

View file

@ -43,7 +43,7 @@ type ReturnSuccess struct {
Slug string `json:"slug",db:"slug"` Slug string `json:"slug",db:"slug"`
} }
type ReferenceID struct { type ReferenceId struct {
LastID int `json:"last_id"` LastID int `json:"last_id"`
} }
@ -150,7 +150,7 @@ func updateBlogPostById(w http.ResponseWriter, r *http.Request) {
returnMessage := ReturnMessage{} returnMessage := ReturnMessage{}
returnMessage.Error = false returnMessage.Error = false
// Get the actual post // 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) result := DB.QueryRow("SELECT id, title, slug, author, content, time_published FROM posts WHERE id=$1", id)
post := BlogPost{} post := BlogPost{}
err := result.Scan(&post.ID, &post.Title, &post.Slug, &post.Author, &post.Content, &post.TimePublished) 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) { func getBlogPosts(w http.ResponseWriter, r *http.Request) {
returnMessage := ReturnMessage{} returnMessage := ReturnMessage{}
returnMessage.Error = false returnMessage.Error = false
referenceID := &ReferenceID{} referenceID := &ReferenceId{}
err := json.NewDecoder(r.Body).Decode(referenceID) err := json.NewDecoder(r.Body).Decode(referenceID)
// hardcode 9001 for cool kid points // hardcode 9001 for cool kid points
if err != nil { if err != nil {
@ -299,7 +299,7 @@ func getBlogPostById(w http.ResponseWriter, r *http.Request) {
func getBlogPostsByTag(w http.ResponseWriter, r *http.Request) { func getBlogPostsByTag(w http.ResponseWriter, r *http.Request) {
returnMessage := ReturnMessage{} returnMessage := ReturnMessage{}
returnMessage.Error = false returnMessage.Error = false
referenceID := &ReferenceID{} referenceID := &ReferenceId{}
err := json.NewDecoder(r.Body).Decode(referenceID) err := json.NewDecoder(r.Body).Decode(referenceID)
// hardcode 9001 for cool kid points // hardcode 9001 for cool kid points
if err != nil { if err != nil {
@ -353,7 +353,7 @@ func getBlogPostsByTag(w http.ResponseWriter, r *http.Request) {
func getBlogPostsByAuthor(w http.ResponseWriter, r *http.Request) { func getBlogPostsByAuthor(w http.ResponseWriter, r *http.Request) {
returnMessage := ReturnMessage{} returnMessage := ReturnMessage{}
returnMessage.Error = false returnMessage.Error = false
referenceID := &ReferenceID{} referenceID := &ReferenceId{}
err := json.NewDecoder(r.Body).Decode(referenceID) err := json.NewDecoder(r.Body).Decode(referenceID)
// hardcode 9001 for cool kid points // hardcode 9001 for cool kid points
if err != nil { if err != nil {

View file

@ -23,7 +23,7 @@ func Routes() *chi.Mux {
r.Get("/posts/by-tag/{tag}", getBlogPostsByTag) r.Get("/posts/by-tag/{tag}", getBlogPostsByTag)
r.Get("/posts/by-author/{author}", getBlogPostsByAuthor) r.Get("/posts/by-author/{author}", getBlogPostsByAuthor)
r.Get("/posts/by-slug/{slug}", getBlogPostBySlug) 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-author/{author}", getCommentsByAuthor)
r.Get("/by-parent/{post_id}", getCommentsByPost) r.Get("/by-parent/{post_id}", getCommentsByPost)