Fill out getting posts

This commit is contained in:
Amarpreet Minhas 2019-04-22 19:01:28 -04:00
parent 0fd73db5ae
commit 26b5f57451

View file

@ -9,6 +9,7 @@ import (
"github.com/go-chi/render"
"github.com/gosimple/slug"
"net/http"
"strconv"
"strings"
"time"
)
@ -29,6 +30,8 @@ type BlogPost struct {
TimeModified time.Time `json:"last_modified", db:"last_modified"`
}
type BlogPosts []BlogPost
type Tag struct {
TagList string `json:"tags"`
}
@ -78,8 +81,11 @@ func Routes() *chi.Mux {
r.Post("/", createBlogPost)
r.Patch("/by-id/{id}", updateBlogPostById)
})
r.Get("/", getBlogPosts)
r.Get("/{slug}", getBlogPostBySlug)
r.Route("/", func(r chi.Router) {
r.Get("/{last_id}", getBlogPosts)
r.Get("/", getBlogPosts)
})
r.Get("/by-slug/{slug}", getBlogPostBySlug)
r.Get("/by-id/{id}", getBlogPostById)
r.Get("/by-tag/{tag}", getBlogPostsByTag)
r.Get("/by-author/{author}", getBlogPostsByAuthor)
@ -96,6 +102,7 @@ func createBlogPost(w http.ResponseWriter, r *http.Request) {
if err != nil {
returnError.Message = "unknown error, try again later"
w.WriteHeader(http.StatusBadRequest)
render.JSON(w, r, returnError)
return
}
if newBlogPost.Title == "" {
@ -126,6 +133,8 @@ func createBlogPost(w http.ResponseWriter, r *http.Request) {
if err != sql.ErrNoRows {
returnError.Message = "something is super broken..."
w.WriteHeader(http.StatusInternalServerError)
render.JSON(w, r, returnError)
fmt.Println(err)
return
}
}
@ -140,11 +149,13 @@ func createBlogPost(w http.ResponseWriter, r *http.Request) {
returnError.Message = "something is super broken..."
w.WriteHeader(http.StatusInternalServerError)
render.JSON(w, r, returnError)
fmt.Println(err)
return
}
returnError.Message = "something is super broken..."
w.WriteHeader(http.StatusInternalServerError)
render.JSON(w, r, returnError)
fmt.Println(err)
return
}
// if the article has tags
@ -175,11 +186,13 @@ func updateBlogPostById(w http.ResponseWriter, r *http.Request) {
returnError.Message = "blog post requested for update not found"
w.WriteHeader(http.StatusInternalServerError)
render.JSON(w, r, returnError)
fmt.Println(err)
return
}
returnError.Message = "something is super broken..."
w.WriteHeader(http.StatusInternalServerError)
render.JSON(w, r, returnError)
fmt.Println(err)
return
}
@ -187,7 +200,9 @@ func updateBlogPostById(w http.ResponseWriter, r *http.Request) {
_, claims, _ := jwtauth.FromContext(r.Context())
username := claims["username"].(string)
if username != post.Author {
returnError.Message = "unauthorized..."
w.WriteHeader(http.StatusUnauthorized)
render.JSON(w, r, returnError)
return
}
// update the post struct
@ -205,6 +220,7 @@ func updateBlogPostById(w http.ResponseWriter, r *http.Request) {
returnError.Message = "something is super broken..."
w.WriteHeader(http.StatusInternalServerError)
render.JSON(w, r, returnError)
fmt.Println(err)
return
}
returnSuccess := ReturnSuccess{Message: "post updated", ID: post.ID}
@ -213,8 +229,53 @@ func updateBlogPostById(w http.ResponseWriter, r *http.Request) {
return
}
// This will search by the last id seen, descending.
func getBlogPosts(w http.ResponseWriter, r *http.Request) {
returnError := ReturnError{}
last_id := chi.URLParam(r, "last_id")
search_id, err := strconv.Atoi(last_id)
// hardcoding 9001 because i'm cool.
if err != nil {
search_id = 9001
}
// if someone is cool and sends up a negative number...
if search_id < 1 {
search_id = 9001
}
search := `
SELECT id, title, slug, author, content, time_published, modified, last_modified
FROM posts
WHERE id < $1
ORDER BY id DESC
FETCH FIRST 10 ROWS ONLY
`
rows, err := DB.Query(search, search_id)
if err != nil {
returnError.Message = "something is super broken..."
w.WriteHeader(http.StatusInternalServerError)
render.JSON(w, r, returnError)
fmt.Println(err)
return
}
defer rows.Close()
post := BlogPost{}
posts := make(BlogPosts, 0)
for rows.Next() {
if err := rows.Scan(&post.ID, &post.Title, &post.Slug, &post.Author, &post.Content, &post.TimePublished, &post.Modified, &post.TimeModified); err != nil {
}
posts = append(posts, post)
}
if err := rows.Err(); err != nil {
returnError.Message = "something is super broken..."
w.WriteHeader(http.StatusInternalServerError)
render.JSON(w, r, returnError)
fmt.Println(err)
return
}
w.WriteHeader(http.StatusOK)
render.JSON(w, r, posts)
return
}
func getBlogPostBySlug(w http.ResponseWriter, r *http.Request) {
@ -224,7 +285,6 @@ func getBlogPostBySlug(w http.ResponseWriter, r *http.Request) {
post := BlogPost{}
err := result.Scan(&post.ID, &post.Title, &post.Slug, &post.Author, &post.Content, &post.TimePublished, &post.Modified, &post.TimeModified)
if err != nil {
fmt.Println(err)
returnError.Message = "post not found"
w.WriteHeader(http.StatusBadRequest)
render.JSON(w, r, returnError)
@ -242,7 +302,6 @@ func getBlogPostById(w http.ResponseWriter, r *http.Request) {
post := BlogPost{}
err := result.Scan(&post.ID, &post.Title, &post.Slug, &post.Author, &post.Content, &post.TimePublished, &post.Modified, &post.TimeModified)
if err != nil {
fmt.Println(err)
returnError.Message = "post not found"
w.WriteHeader(http.StatusBadRequest)
render.JSON(w, r, returnError)