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/go-chi/render"
"github.com/gosimple/slug" "github.com/gosimple/slug"
"net/http" "net/http"
"strconv"
"strings" "strings"
"time" "time"
) )
@ -29,6 +30,8 @@ type BlogPost struct {
TimeModified time.Time `json:"last_modified", db:"last_modified"` TimeModified time.Time `json:"last_modified", db:"last_modified"`
} }
type BlogPosts []BlogPost
type Tag struct { type Tag struct {
TagList string `json:"tags"` TagList string `json:"tags"`
} }
@ -78,8 +81,11 @@ func Routes() *chi.Mux {
r.Post("/", createBlogPost) r.Post("/", createBlogPost)
r.Patch("/by-id/{id}", updateBlogPostById) r.Patch("/by-id/{id}", updateBlogPostById)
}) })
r.Get("/", getBlogPosts) r.Route("/", func(r chi.Router) {
r.Get("/{slug}", getBlogPostBySlug) r.Get("/{last_id}", getBlogPosts)
r.Get("/", getBlogPosts)
})
r.Get("/by-slug/{slug}", getBlogPostBySlug)
r.Get("/by-id/{id}", getBlogPostById) r.Get("/by-id/{id}", getBlogPostById)
r.Get("/by-tag/{tag}", getBlogPostsByTag) r.Get("/by-tag/{tag}", getBlogPostsByTag)
r.Get("/by-author/{author}", getBlogPostsByAuthor) r.Get("/by-author/{author}", getBlogPostsByAuthor)
@ -96,6 +102,7 @@ func createBlogPost(w http.ResponseWriter, r *http.Request) {
if err != nil { if err != nil {
returnError.Message = "unknown error, try again later" returnError.Message = "unknown error, try again later"
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
render.JSON(w, r, returnError)
return return
} }
if newBlogPost.Title == "" { if newBlogPost.Title == "" {
@ -126,6 +133,8 @@ func createBlogPost(w http.ResponseWriter, r *http.Request) {
if err != sql.ErrNoRows { if err != sql.ErrNoRows {
returnError.Message = "something is super broken..." returnError.Message = "something is super broken..."
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
render.JSON(w, r, returnError)
fmt.Println(err)
return return
} }
} }
@ -140,11 +149,13 @@ func createBlogPost(w http.ResponseWriter, r *http.Request) {
returnError.Message = "something is super broken..." returnError.Message = "something is super broken..."
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
render.JSON(w, r, returnError) render.JSON(w, r, returnError)
fmt.Println(err)
return return
} }
returnError.Message = "something is super broken..." returnError.Message = "something is super broken..."
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
render.JSON(w, r, returnError) render.JSON(w, r, returnError)
fmt.Println(err)
return return
} }
// if the article has tags // 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" returnError.Message = "blog post requested for update not found"
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
render.JSON(w, r, returnError) render.JSON(w, r, returnError)
fmt.Println(err)
return return
} }
returnError.Message = "something is super broken..." returnError.Message = "something is super broken..."
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
render.JSON(w, r, returnError) render.JSON(w, r, returnError)
fmt.Println(err)
return return
} }
@ -187,7 +200,9 @@ func updateBlogPostById(w http.ResponseWriter, r *http.Request) {
_, claims, _ := jwtauth.FromContext(r.Context()) _, claims, _ := jwtauth.FromContext(r.Context())
username := claims["username"].(string) username := claims["username"].(string)
if username != post.Author { if username != post.Author {
returnError.Message = "unauthorized..."
w.WriteHeader(http.StatusUnauthorized) w.WriteHeader(http.StatusUnauthorized)
render.JSON(w, r, returnError)
return return
} }
// update the post struct // update the post struct
@ -205,6 +220,7 @@ func updateBlogPostById(w http.ResponseWriter, r *http.Request) {
returnError.Message = "something is super broken..." returnError.Message = "something is super broken..."
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
render.JSON(w, r, returnError) render.JSON(w, r, returnError)
fmt.Println(err)
return return
} }
returnSuccess := ReturnSuccess{Message: "post updated", ID: post.ID} returnSuccess := ReturnSuccess{Message: "post updated", ID: post.ID}
@ -213,8 +229,53 @@ func updateBlogPostById(w http.ResponseWriter, r *http.Request) {
return return
} }
// This will search by the last id seen, descending.
func getBlogPosts(w http.ResponseWriter, r *http.Request) { 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 return
} }
func getBlogPostBySlug(w http.ResponseWriter, r *http.Request) { func getBlogPostBySlug(w http.ResponseWriter, r *http.Request) {
@ -224,7 +285,6 @@ func getBlogPostBySlug(w http.ResponseWriter, r *http.Request) {
post := BlogPost{} post := BlogPost{}
err := result.Scan(&post.ID, &post.Title, &post.Slug, &post.Author, &post.Content, &post.TimePublished, &post.Modified, &post.TimeModified) err := result.Scan(&post.ID, &post.Title, &post.Slug, &post.Author, &post.Content, &post.TimePublished, &post.Modified, &post.TimeModified)
if err != nil { if err != nil {
fmt.Println(err)
returnError.Message = "post not found" returnError.Message = "post not found"
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
render.JSON(w, r, returnError) render.JSON(w, r, returnError)
@ -242,7 +302,6 @@ func getBlogPostById(w http.ResponseWriter, r *http.Request) {
post := BlogPost{} post := BlogPost{}
err := result.Scan(&post.ID, &post.Title, &post.Slug, &post.Author, &post.Content, &post.TimePublished, &post.Modified, &post.TimeModified) err := result.Scan(&post.ID, &post.Title, &post.Slug, &post.Author, &post.Content, &post.TimePublished, &post.Modified, &post.TimeModified)
if err != nil { if err != nil {
fmt.Println(err)
returnError.Message = "post not found" returnError.Message = "post not found"
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
render.JSON(w, r, returnError) render.JSON(w, r, returnError)