diff --git a/packages/blog/blog.go b/packages/blog/blog.go index a08612a..3292467 100644 --- a/packages/blog/blog.go +++ b/packages/blog/blog.go @@ -94,6 +94,7 @@ func createBlogPost(w http.ResponseWriter, r *http.Request) { username := claims["username"].(string) err := json.NewDecoder(r.Body).Decode(newBlogPost) if err != nil { + returnError.Message = "unknown error, try again later" w.WriteHeader(http.StatusBadRequest) return } @@ -128,12 +129,12 @@ func createBlogPost(w http.ResponseWriter, r *http.Request) { return } } - s := `INSERT INTO posts (title, slug, author, content, date) - VALUES ($1, $2, $3, $4, $5) + s := `INSERT INTO posts (title, slug, author, content, date, updated, update_time) + VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING id` article_id := 0 // write the row and get back the id - err = DB.QueryRow(s, newBlogPost.Title, as, username, newBlogPost.Content, 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 == sql.ErrNoRows { returnError.Message = "something is super broken..." @@ -201,7 +202,6 @@ func updateBlogPostById(w http.ResponseWriter, r *http.Request) { // write the row update _ , err = DB.Exec(s, post.Title, post.Content, true, time.Now().UTC(), post.ID) if err != nil { - fmt.Println(err) returnError.Message = "something is super broken..." w.WriteHeader(http.StatusInternalServerError) render.JSON(w, r, returnError) @@ -218,6 +218,20 @@ func getBlogPosts(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 }