Simplify the get post logic

This commit is contained in:
Amarpreet Minhas 2019-04-22 19:46:42 -04:00
parent 26b5f57451
commit b38ae48013

View file

@ -9,7 +9,6 @@ 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"
) )
@ -52,6 +51,10 @@ type ReturnSuccess struct {
ID int `json:"id"` ID int `json:"id"`
} }
type ReferenceID struct {
LastID int `json:"last_id"`
}
func Init() { func Init() {
createPostsTable := ` createPostsTable := `
CREATE TABLE IF NOT EXISTS posts CREATE TABLE IF NOT EXISTS posts
@ -134,7 +137,7 @@ 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) fmt.Println(err)
return return
} }
} }
@ -149,7 +152,7 @@ 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) fmt.Println(err)
return return
} }
returnError.Message = "something is super broken..." 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" 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) fmt.Println(err)
return return
} }
returnError.Message = "something is super broken..." 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. // 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{} returnError := ReturnError{}
last_id := chi.URLParam(r, "last_id") referenceID := &ReferenceID{}
search_id, err := strconv.Atoi(last_id) err := json.NewDecoder(r.Body).Decode(referenceID)
// hardcoding 9001 because i'm cool.
if err != nil { if err != nil {
search_id = 9001 referenceID.LastID = 9001
} }
search_id := referenceID.LastID
// if someone is cool and sends up a negative number... // if someone is cool and sends up a negative number...
if search_id < 1 { if search_id < 1 {
search_id = 9001 search_id = 9001
@ -275,7 +278,6 @@ func getBlogPosts(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
render.JSON(w, r, posts) render.JSON(w, r, posts)
return return
} }
func getBlogPostBySlug(w http.ResponseWriter, r *http.Request) { func getBlogPostBySlug(w http.ResponseWriter, r *http.Request) {