Add getpost by id and slug
This commit is contained in:
parent
ca6cace78d
commit
63c8a24f38
1 changed files with 32 additions and 18 deletions
|
@ -80,9 +80,9 @@ func Routes() *chi.Mux {
|
|||
})
|
||||
r.Get("/", getBlogPosts)
|
||||
r.Get("/{slug}", getBlogPostBySlug)
|
||||
r.Get("/by-tag/{tag}", getBlogPostByTag)
|
||||
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
|
||||
}
|
||||
|
||||
|
@ -200,7 +200,7 @@ func updateBlogPostById(w http.ResponseWriter, r *http.Request) {
|
|||
update_time = $4
|
||||
WHERE id = $5`
|
||||
// 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 {
|
||||
returnError.Message = "something is super broken..."
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
|
@ -217,6 +217,24 @@ func getBlogPosts(w http.ResponseWriter, r *http.Request) {
|
|||
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) {
|
||||
returnError := ReturnError{}
|
||||
id := chi.URLParam(r, "id")
|
||||
|
@ -235,15 +253,11 @@ func getBlogPostById(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
func getBlogPostBySlug(w http.ResponseWriter, r *http.Request) {
|
||||
func getBlogPostsByTag(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
func getBlogPostByTag(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
func getBlogPostByAuthor(w http.ResponseWriter, r *http.Request) {
|
||||
func getBlogPostsByAuthor(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue