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,16 +80,16 @@ 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
|
||||
}
|
||||
|
||||
func createBlogPost(w http.ResponseWriter, r *http.Request) {
|
||||
returnError := ReturnError{}
|
||||
newBlogPost := &NewBlogPost{}
|
||||
// basic checks
|
||||
// basic checks
|
||||
_, claims, _ := jwtauth.FromContext(r.Context())
|
||||
username := claims["username"].(string)
|
||||
err := json.NewDecoder(r.Body).Decode(newBlogPost)
|
||||
|
@ -113,7 +113,7 @@ func createBlogPost(w http.ResponseWriter, r *http.Request) {
|
|||
as := slug.Make(newBlogPost.Title)
|
||||
slugCheck := DB.QueryRow("SELECT id FROM posts WHERE slug=$1", as)
|
||||
// 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
|
||||
err = slugCheck.Scan(&scr)
|
||||
if err == nil {
|
||||
|
@ -133,7 +133,7 @@ func createBlogPost(w http.ResponseWriter, r *http.Request) {
|
|||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
RETURNING id`
|
||||
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)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
|
@ -147,7 +147,7 @@ func createBlogPost(w http.ResponseWriter, r *http.Request) {
|
|||
render.JSON(w, r, returnError)
|
||||
return
|
||||
}
|
||||
// if the article has tags
|
||||
// if the article has tags
|
||||
if newBlogPost.Tags != "" {
|
||||
t := `INSERT INTO tags (tag, article_id)
|
||||
VALUES ($1, $2)`
|
||||
|
@ -190,7 +190,7 @@ func updateBlogPostById(w http.ResponseWriter, r *http.Request) {
|
|||
w.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
// update the post struct
|
||||
// update the post struct
|
||||
err = json.NewDecoder(r.Body).Decode(&post)
|
||||
s := `
|
||||
UPDATE posts SET
|
||||
|
@ -199,8 +199,8 @@ func updateBlogPostById(w http.ResponseWriter, r *http.Request) {
|
|||
updated = $3,
|
||||
update_time = $4
|
||||
WHERE id = $5`
|
||||
// write the row update
|
||||
_ , err = DB.Exec(s, post.Title, post.Content, true, time.Now().UTC(), post.ID)
|
||||
// write the row update
|
||||
_, 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,14 +217,14 @@ func getBlogPosts(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
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{}
|
||||
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)
|
||||
fmt.Println(err)
|
||||
returnError.Message = "post not found"
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
render.JSON(w, r, returnError)
|
||||
|
@ -235,15 +235,29 @@ func getBlogPostById(w http.ResponseWriter, r *http.Request) {
|
|||
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
|
||||
}
|
||||
|
||||
func getBlogPostByTag(w http.ResponseWriter, r *http.Request) {
|
||||
func getBlogPostsByTag(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