Add logic for get posts by author, aka copy pasta pro
This commit is contained in:
parent
b38ae48013
commit
c4b3a6bbc5
1 changed files with 49 additions and 5 deletions
|
@ -84,10 +84,7 @@ func Routes() *chi.Mux {
|
||||||
r.Post("/", createBlogPost)
|
r.Post("/", createBlogPost)
|
||||||
r.Patch("/by-id/{id}", updateBlogPostById)
|
r.Patch("/by-id/{id}", updateBlogPostById)
|
||||||
})
|
})
|
||||||
r.Route("/", func(r chi.Router) {
|
|
||||||
r.Get("/{last_id}", getBlogPosts)
|
|
||||||
r.Get("/", getBlogPosts)
|
r.Get("/", getBlogPosts)
|
||||||
})
|
|
||||||
r.Get("/by-slug/{slug}", getBlogPostBySlug)
|
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)
|
||||||
|
@ -237,11 +234,12 @@ func getBlogPosts(w http.ResponseWriter, r *http.Request) {
|
||||||
returnError := ReturnError{}
|
returnError := ReturnError{}
|
||||||
referenceID := &ReferenceID{}
|
referenceID := &ReferenceID{}
|
||||||
err := json.NewDecoder(r.Body).Decode(referenceID)
|
err := json.NewDecoder(r.Body).Decode(referenceID)
|
||||||
|
// hardcode 9001 for cool kid points
|
||||||
if err != nil {
|
if err != nil {
|
||||||
referenceID.LastID = 9001
|
referenceID.LastID = 9001
|
||||||
}
|
}
|
||||||
search_id := referenceID.LastID
|
search_id := referenceID.LastID
|
||||||
// if someone is cool and sends up a negative number...
|
// if someone is lame and sends up a negative number...
|
||||||
if search_id < 1 {
|
if search_id < 1 {
|
||||||
search_id = 9001
|
search_id = 9001
|
||||||
}
|
}
|
||||||
|
@ -319,6 +317,52 @@ func getBlogPostsByTag(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func getBlogPostsByAuthor(w http.ResponseWriter, r *http.Request) {
|
func getBlogPostsByAuthor(w http.ResponseWriter, r *http.Request) {
|
||||||
|
returnError := ReturnError{}
|
||||||
|
referenceID := &ReferenceID{}
|
||||||
|
err := json.NewDecoder(r.Body).Decode(referenceID)
|
||||||
|
// hardcode 9001 for cool kid points
|
||||||
|
if err != nil {
|
||||||
|
referenceID.LastID = 9001
|
||||||
|
}
|
||||||
|
search_id := referenceID.LastID
|
||||||
|
// if someone is lame and sends up a negative number...
|
||||||
|
if search_id < 1 {
|
||||||
|
search_id = 9001
|
||||||
|
}
|
||||||
|
author := chi.URLParam(r, "author")
|
||||||
|
search := `
|
||||||
|
SELECT id, title, slug, author, content, time_published, modified, last_modified
|
||||||
|
FROM posts
|
||||||
|
WHERE id < $1
|
||||||
|
AND author = $2
|
||||||
|
ORDER BY id DESC
|
||||||
|
FETCH FIRST 10 ROWS ONLY
|
||||||
|
`
|
||||||
|
rows, err := DB.Query(search, search_id, author)
|
||||||
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in a new issue