Merge branch 'comments' of Asara/sudoscientist-go-backend into master
This commit is contained in:
commit
2a537d2924
6 changed files with 201 additions and 32 deletions
3
migrations/1579998283_create_comments_table.down.sql
Normal file
3
migrations/1579998283_create_comments_table.down.sql
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
BEGIN;
|
||||||
|
DROP TABLE IF EXISTS comments;
|
||||||
|
COMMIT;
|
11
migrations/1579998283_create_comments_table.up.sql
Normal file
11
migrations/1579998283_create_comments_table.up.sql
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
BEGIN;
|
||||||
|
CREATE TABLE IF NOT EXISTS comments (
|
||||||
|
id SERIAL PRIMARY KEY,
|
||||||
|
parent INTEGER REFERENCES posts (id),
|
||||||
|
author text REFERENCES users (username),
|
||||||
|
content text,
|
||||||
|
time_published timestamp,
|
||||||
|
modified bool,
|
||||||
|
last_modified timestamp
|
||||||
|
);
|
||||||
|
COMMIT;
|
16
packages/blog/base.go
Normal file
16
packages/blog/base.go
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
package blog
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"github.com/go-chi/jwtauth"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ReturnMessage struct {
|
||||||
|
Message string `json:"msg"`
|
||||||
|
Error bool `json:"error"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
DB *sql.DB
|
||||||
|
TokenAuth *jwtauth.JWTAuth
|
||||||
|
)
|
134
packages/blog/comments.go
Normal file
134
packages/blog/comments.go
Normal file
|
@ -0,0 +1,134 @@
|
||||||
|
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 []Comment
|
||||||
|
|
||||||
|
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, "post_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.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
|
||||||
|
}
|
||||||
|
|
||||||
|
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 ASC
|
||||||
|
`
|
||||||
|
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
|
||||||
|
}
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.minhas.io/asara/sudoscientist-go-backend/packages/middleware"
|
|
||||||
"github.com/go-chi/chi"
|
"github.com/go-chi/chi"
|
||||||
"github.com/go-chi/jwtauth"
|
"github.com/go-chi/jwtauth"
|
||||||
"github.com/go-chi/render"
|
"github.com/go-chi/render"
|
||||||
|
@ -14,11 +13,6 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
DB *sql.DB
|
|
||||||
TokenAuth *jwtauth.JWTAuth
|
|
||||||
)
|
|
||||||
|
|
||||||
type BlogPost struct {
|
type BlogPost struct {
|
||||||
ID int `json:"id",db:"id"`
|
ID int `json:"id",db:"id"`
|
||||||
Title string `json:"title",db:"title"`
|
Title string `json:"title",db:"title"`
|
||||||
|
@ -43,37 +37,16 @@ type NewBlogPost struct {
|
||||||
Author string `json:"author",db:"author"`
|
Author string `json:"author",db:"author"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ReturnMessage struct {
|
|
||||||
Message string `json:"msg"`
|
|
||||||
Error bool `json:"error"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type ReturnSuccess struct {
|
type ReturnSuccess struct {
|
||||||
Message string `json:"success"`
|
Message string `json:"success"`
|
||||||
ID int `json:"id"`
|
ID int `json:"id"`
|
||||||
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"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func Routes() *chi.Mux {
|
|
||||||
r := chi.NewRouter()
|
|
||||||
r.Group(func(r chi.Router) {
|
|
||||||
r.Use(jwtauth.Verify(TokenAuth, auth_middleware.TokenFromSplitCookie))
|
|
||||||
r.Use(jwtauth.Authenticator)
|
|
||||||
r.Post("/", createBlogPost)
|
|
||||||
r.Patch("/by-id/{id}", updateBlogPostById)
|
|
||||||
})
|
|
||||||
r.Get("/", getBlogPosts)
|
|
||||||
r.Get("/by-id/{id}", getBlogPostById)
|
|
||||||
r.Get("/by-tag/{tag}", getBlogPostsByTag)
|
|
||||||
r.Get("/by-author/{author}", getBlogPostsByAuthor)
|
|
||||||
r.Get("/by-slug/{slug}", getBlogPostBySlug)
|
|
||||||
return r
|
|
||||||
}
|
|
||||||
|
|
||||||
func createBlogPost(w http.ResponseWriter, r *http.Request) {
|
func createBlogPost(w http.ResponseWriter, r *http.Request) {
|
||||||
returnMessage := ReturnMessage{}
|
returnMessage := ReturnMessage{}
|
||||||
returnMessage.Error = false
|
returnMessage.Error = false
|
||||||
|
@ -177,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)
|
||||||
|
@ -237,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 {
|
||||||
|
@ -326,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 {
|
||||||
|
@ -380,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 {
|
32
packages/blog/routes.go
Normal file
32
packages/blog/routes.go
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
package blog
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.minhas.io/asara/sudoscientist-go-backend/packages/middleware"
|
||||||
|
"github.com/go-chi/chi"
|
||||||
|
"github.com/go-chi/jwtauth"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Routes() *chi.Mux {
|
||||||
|
r := chi.NewRouter()
|
||||||
|
r.Group(func(r chi.Router) {
|
||||||
|
r.Use(jwtauth.Verify(TokenAuth, auth_middleware.TokenFromSplitCookie))
|
||||||
|
r.Use(jwtauth.Authenticator)
|
||||||
|
r.Post("/posts", createBlogPost)
|
||||||
|
r.Patch("/posts/by-id/{id}", updateBlogPostById)
|
||||||
|
r.Post("/comments/{post_id}", postComment)
|
||||||
|
/*
|
||||||
|
r.Patch("/comments/{id}", updateComment)
|
||||||
|
*/
|
||||||
|
})
|
||||||
|
r.Get("/posts", getBlogPosts)
|
||||||
|
r.Get("/posts/by-id/{id}", getBlogPostById)
|
||||||
|
r.Get("/posts/by-tag/{tag}", getBlogPostsByTag)
|
||||||
|
r.Get("/posts/by-author/{author}", getBlogPostsByAuthor)
|
||||||
|
r.Get("/posts/by-slug/{slug}", getBlogPostBySlug)
|
||||||
|
r.Get("/comments/{post_id}", getCommentsByParentId)
|
||||||
|
/*
|
||||||
|
r.Get("/by-author/{author}", getCommentsByAuthor)
|
||||||
|
r.Get("/by-parent/{post_id}", getCommentsByPost)
|
||||||
|
*/
|
||||||
|
return r
|
||||||
|
}
|
Reference in a new issue