Add getpost by id and slug

This commit is contained in:
Amarpreet Minhas 2019-04-22 16:16:31 -04:00
parent ca6cace78d
commit 63c8a24f38

View file

@ -80,9 +80,9 @@ func Routes() *chi.Mux {
}) })
r.Get("/", getBlogPosts) r.Get("/", getBlogPosts)
r.Get("/{slug}", getBlogPostBySlug) r.Get("/{slug}", getBlogPostBySlug)
r.Get("/by-tag/{tag}", getBlogPostByTag)
r.Get("/by-id/{id}", getBlogPostById) r.Get("/by-id/{id}", getBlogPostById)
r.Get("/by-author/{author}", getBlogPostByAuthor) r.Get("/by-tag/{tag}", getBlogPostsByTag)
r.Get("/by-author/{author}", getBlogPostsByAuthor)
return r return r
} }
@ -200,7 +200,7 @@ func updateBlogPostById(w http.ResponseWriter, r *http.Request) {
update_time = $4 update_time = $4
WHERE id = $5` WHERE id = $5`
// write the row update // write the row update
_ , err = DB.Exec(s, post.Title, post.Content, true, time.Now().UTC(), post.ID) _, err = DB.Exec(s, post.Title, post.Content, true, time.Now().UTC(), post.ID)
if err != nil { if err != nil {
returnError.Message = "something is super broken..." returnError.Message = "something is super broken..."
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
@ -217,6 +217,24 @@ func getBlogPosts(w http.ResponseWriter, r *http.Request) {
return return
} }
func getBlogPostBySlug(w http.ResponseWriter, r *http.Request) {
returnError := ReturnError{}
slug := chi.URLParam(r, "slug")
result := DB.QueryRow("SELECT id, title, slug, author, content, date, updated, update_time FROM posts WHERE slug=$1", slug)
post := BlogPost{}
err := result.Scan(&post.ID, &post.Title, &post.Slug, &post.Author, &post.Content, &post.DatePublished, &post.Updated, &post.UpdateTime)
if err != nil {
fmt.Println(err)
returnError.Message = "post not found"
w.WriteHeader(http.StatusBadRequest)
render.JSON(w, r, returnError)
return
}
w.WriteHeader(http.StatusOK)
render.JSON(w, r, post)
return
}
func getBlogPostById(w http.ResponseWriter, r *http.Request) { func getBlogPostById(w http.ResponseWriter, r *http.Request) {
returnError := ReturnError{} returnError := ReturnError{}
id := chi.URLParam(r, "id") id := chi.URLParam(r, "id")
@ -235,15 +253,11 @@ func getBlogPostById(w http.ResponseWriter, r *http.Request) {
return return
} }
func getBlogPostBySlug(w http.ResponseWriter, r *http.Request) { func getBlogPostsByTag(w http.ResponseWriter, r *http.Request) {
return return
} }
func getBlogPostByTag(w http.ResponseWriter, r *http.Request) { func getBlogPostsByAuthor(w http.ResponseWriter, r *http.Request) {
return
}
func getBlogPostByAuthor(w http.ResponseWriter, r *http.Request) {
return return
} }