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/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
@ -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) {