From b38ae480138e8a7eb602f5f5524d3af1b04e58d2 Mon Sep 17 00:00:00 2001 From: Asara Date: Mon, 22 Apr 2019 19:46:42 -0400 Subject: [PATCH] Simplify the get post logic --- packages/blog/blog.go | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/packages/blog/blog.go b/packages/blog/blog.go index b581bf2..e2029a4 100644 --- a/packages/blog/blog.go +++ b/packages/blog/blog.go @@ -9,7 +9,6 @@ import ( "github.com/go-chi/render" "github.com/gosimple/slug" "net/http" - "strconv" "strings" "time" ) @@ -52,6 +51,10 @@ type ReturnSuccess struct { ID int `json:"id"` } +type ReferenceID struct { + LastID int `json:"last_id"` +} + func Init() { createPostsTable := ` CREATE TABLE IF NOT EXISTS posts @@ -134,7 +137,7 @@ 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) + fmt.Println(err) return } } @@ -149,7 +152,7 @@ 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) + fmt.Println(err) return } returnError.Message = "something is super broken..." @@ -186,7 +189,7 @@ 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) + fmt.Println(err) return } returnError.Message = "something is super broken..." @@ -232,12 +235,12 @@ func updateBlogPostById(w http.ResponseWriter, r *http.Request) { // 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. + referenceID := &ReferenceID{} + err := json.NewDecoder(r.Body).Decode(referenceID) if err != nil { - search_id = 9001 + referenceID.LastID = 9001 } + search_id := referenceID.LastID // if someone is cool and sends up a negative number... if search_id < 1 { search_id = 9001 @@ -275,7 +278,6 @@ func getBlogPosts(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) render.JSON(w, r, posts) return - } func getBlogPostBySlug(w http.ResponseWriter, r *http.Request) {