102 lines
3 KiB
Go
102 lines
3 KiB
Go
|
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"`
|
||
|
}
|
||
|
|
||
|
type Comments []Comments
|
||
|
|
||
|
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)
|
||
|
post_id := chi.URLParam(r, "id")
|
||
|
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.Parent == "" {
|
||
|
returnMessage.Message = "title is required"
|
||
|
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
|
||
|
}
|