Simplify the get post logic
This commit is contained in:
parent
26b5f57451
commit
b38ae48013
1 changed files with 11 additions and 9 deletions
|
@ -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) {
|
||||
|
|
Reference in a new issue