2020-01-26 03:56:11 +00:00
|
|
|
package blog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"github.com/go-chi/chi"
|
|
|
|
"github.com/go-chi/jwtauth"
|
|
|
|
"github.com/go-chi/render"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Comment struct {
|
|
|
|
ID int `json:"id",db:"id"`
|
|
|
|
Parent string `json:"post_id",db:"parent"`
|
|
|
|
Author string `json:"author",db:"author"`
|
|
|
|
Content string `json:"content",db:"content"`
|
|
|
|
TimePublished time.Time `json:"time_published", db:"time_published"`
|
|
|
|
Modified bool `json:"modified", db:"modified"`
|
|
|
|
TimeModified time.Time `json:"last_modified", db:"last_modified"`
|
|
|
|
}
|
|
|
|
|
2020-01-26 04:19:39 +00:00
|
|
|
type Comments []Comment
|
2020-01-26 03:56:11 +00:00
|
|
|
|
|
|
|
func getCommentById(w http.ResponseWriter, r *http.Request) {
|
|
|
|
returnMessage := ReturnMessage{}
|
|
|
|
returnMessage.Error = false
|
|
|
|
id := chi.URLParam(r, "id")
|
|
|
|
result := DB.QueryRow("SELECT id, parent, author, content, time_published, modified, last_modified FROM comments WHERE id=$1", id)
|
|
|
|
comment := Comment{}
|
|
|
|
err := result.Scan(&comment.ID, &comment.Parent, &comment.Author, &comment.Content, &comment.TimePublished, &comment.Modified, &comment.TimeModified)
|
|
|
|
if err != nil {
|
|
|
|
returnMessage.Message = "comment not found"
|
|
|
|
returnMessage.Error = true
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
render.JSON(w, r, returnMessage)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
render.JSON(w, r, comment)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func postComment(w http.ResponseWriter, r *http.Request) {
|
|
|
|
returnMessage := ReturnMessage{}
|
|
|
|
returnMessage.Error = false
|
|
|
|
newComment := &Comment{}
|
|
|
|
// basic checks
|
|
|
|
_, claims, _ := jwtauth.FromContext(r.Context())
|
|
|
|
|
|
|
|
username := claims["username"].(string)
|
2020-01-30 05:37:14 +00:00
|
|
|
post_id := chi.URLParam(r, "post_id")
|
2020-01-26 03:56:11 +00:00
|
|
|
err := json.NewDecoder(r.Body).Decode(newComment)
|
|
|
|
if err != nil {
|
|
|
|
returnMessage.Message = "unknown error, try again later"
|
|
|
|
returnMessage.Error = true
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
render.JSON(w, r, returnMessage)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if newComment.Content == "" {
|
|
|
|
returnMessage.Message = "content is required"
|
|
|
|
returnMessage.Error = true
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
render.JSON(w, r, returnMessage)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c := `INSERT INTO comments (parent, author, content, time_published, modified, last_modified)
|
|
|
|
VALUES ($1, $2, $3, $4, $5, $6)
|
|
|
|
RETURNING id`
|
|
|
|
comment_id := 0
|
|
|
|
err = DB.QueryRow(c, post_id, username, newComment.Content, time.Now().UTC(), false, time.Now().UTC()).Scan(&comment_id)
|
|
|
|
if err != nil {
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
returnMessage.Message = "something is super broken..."
|
|
|
|
returnMessage.Error = true
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
render.JSON(w, r, returnMessage)
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
returnMessage.Message = "something is super broken..."
|
|
|
|
returnMessage.Error = true
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
render.JSON(w, r, returnMessage)
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
returnMessage.Message = "comment added"
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
|
|
render.JSON(w, r, returnMessage)
|
|
|
|
return
|
|
|
|
}
|
2020-01-26 04:19:39 +00:00
|
|
|
|
|
|
|
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
|
2020-01-26 06:26:56 +00:00
|
|
|
ORDER BY id ASC
|
2020-01-26 04:19:39 +00:00
|
|
|
`
|
|
|
|
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
|
|
|
|
}
|