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,16 +80,16 @@ 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
} }
func createBlogPost(w http.ResponseWriter, r *http.Request) { func createBlogPost(w http.ResponseWriter, r *http.Request) {
returnError := ReturnError{} returnError := ReturnError{}
newBlogPost := &NewBlogPost{} newBlogPost := &NewBlogPost{}
// basic checks // basic checks
_, claims, _ := jwtauth.FromContext(r.Context()) _, claims, _ := jwtauth.FromContext(r.Context())
username := claims["username"].(string) username := claims["username"].(string)
err := json.NewDecoder(r.Body).Decode(newBlogPost) err := json.NewDecoder(r.Body).Decode(newBlogPost)
@ -113,7 +113,7 @@ func createBlogPost(w http.ResponseWriter, r *http.Request) {
as := slug.Make(newBlogPost.Title) as := slug.Make(newBlogPost.Title)
slugCheck := DB.QueryRow("SELECT id FROM posts WHERE slug=$1", as) slugCheck := DB.QueryRow("SELECT id FROM posts WHERE slug=$1", as)
// wow this is ugly. someone pls send help. // wow this is ugly. someone pls send help.
// checking to ensure the same slug doesn't exist... // checking to ensure the same slug doesn't exist...
scr := 0 scr := 0
err = slugCheck.Scan(&scr) err = slugCheck.Scan(&scr)
if err == nil { if err == nil {
@ -133,7 +133,7 @@ func createBlogPost(w http.ResponseWriter, r *http.Request) {
VALUES ($1, $2, $3, $4, $5, $6, $7) VALUES ($1, $2, $3, $4, $5, $6, $7)
RETURNING id` RETURNING id`
article_id := 0 article_id := 0
// write the row and get back the id // write the row and get back the id
err = DB.QueryRow(s, newBlogPost.Title, as, username, newBlogPost.Content, time.Now().UTC(), false, time.Now().UTC()).Scan(&article_id) err = DB.QueryRow(s, newBlogPost.Title, as, username, newBlogPost.Content, time.Now().UTC(), false, time.Now().UTC()).Scan(&article_id)
if err != nil { if err != nil {
if err == sql.ErrNoRows { if err == sql.ErrNoRows {
@ -147,7 +147,7 @@ func createBlogPost(w http.ResponseWriter, r *http.Request) {
render.JSON(w, r, returnError) render.JSON(w, r, returnError)
return return
} }
// if the article has tags // if the article has tags
if newBlogPost.Tags != "" { if newBlogPost.Tags != "" {
t := `INSERT INTO tags (tag, article_id) t := `INSERT INTO tags (tag, article_id)
VALUES ($1, $2)` VALUES ($1, $2)`
@ -190,7 +190,7 @@ func updateBlogPostById(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusUnauthorized) w.WriteHeader(http.StatusUnauthorized)
return return
} }
// update the post struct // update the post struct
err = json.NewDecoder(r.Body).Decode(&post) err = json.NewDecoder(r.Body).Decode(&post)
s := ` s := `
UPDATE posts SET UPDATE posts SET
@ -199,8 +199,8 @@ func updateBlogPostById(w http.ResponseWriter, r *http.Request) {
updated = $3, updated = $3,
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,14 +217,14 @@ func getBlogPosts(w http.ResponseWriter, r *http.Request) {
return return
} }
func getBlogPostById(w http.ResponseWriter, r *http.Request) { func getBlogPostBySlug(w http.ResponseWriter, r *http.Request) {
returnError := ReturnError{} returnError := ReturnError{}
id := chi.URLParam(r, "id") slug := chi.URLParam(r, "slug")
result := DB.QueryRow("SELECT id, title, slug, author, content, date, updated, update_time FROM posts WHERE id=$1", id) result := DB.QueryRow("SELECT id, title, slug, author, content, date, updated, update_time FROM posts WHERE slug=$1", slug)
post := BlogPost{} post := BlogPost{}
err := result.Scan(&post.ID, &post.Title, &post.Slug, &post.Author, &post.Content, &post.DatePublished, &post.Updated, &post.UpdateTime) err := result.Scan(&post.ID, &post.Title, &post.Slug, &post.Author, &post.Content, &post.DatePublished, &post.Updated, &post.UpdateTime)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
returnError.Message = "post not found" returnError.Message = "post not found"
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
render.JSON(w, r, returnError) render.JSON(w, r, returnError)
@ -235,15 +235,29 @@ func getBlogPostById(w http.ResponseWriter, r *http.Request) {
return return
} }
func getBlogPostBySlug(w http.ResponseWriter, r *http.Request) { func getBlogPostById(w http.ResponseWriter, r *http.Request) {
returnError := ReturnError{}
id := chi.URLParam(r, "id")
result := DB.QueryRow("SELECT id, title, slug, author, content, date, updated, update_time FROM posts WHERE id=$1", id)
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 return
} }
func getBlogPostByTag(w http.ResponseWriter, r *http.Request) { func getBlogPostsByTag(w http.ResponseWriter, r *http.Request) {
return return
} }
func getBlogPostByAuthor(w http.ResponseWriter, r *http.Request) { func getBlogPostsByAuthor(w http.ResponseWriter, r *http.Request) {
return return
} }