add ability to update posts
This commit is contained in:
parent
5d265d9753
commit
104edaf90a
1 changed files with 58 additions and 4 deletions
|
@ -19,11 +19,14 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
type BlogPost struct {
|
type BlogPost struct {
|
||||||
|
ID int `json:"id",db:"id"`
|
||||||
Title string `json:"title",db:"title"`
|
Title string `json:"title",db:"title"`
|
||||||
Slug string `json:"slug",db:"slug"`
|
Slug string `json:"slug",db:"slug"`
|
||||||
Author string `json:"author",db:"author"`
|
Author string `json:"author",db:"author"`
|
||||||
Content string `json:"content",db:"content"`
|
Content string `json:"content",db:"content"`
|
||||||
DatePublished time.Time `json:"date", db:"date"`
|
DatePublished time.Time `json:"date", db:"date"`
|
||||||
|
Updated bool `json:"updated", db:"updated"`
|
||||||
|
UpdateTime time.Time `json:"update_time", db:"update_time"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Tag struct {
|
type Tag struct {
|
||||||
|
@ -54,7 +57,9 @@ func Init() {
|
||||||
slug text,
|
slug text,
|
||||||
author text REFERENCES users (username),
|
author text REFERENCES users (username),
|
||||||
content text,
|
content text,
|
||||||
date timestamp)`
|
date timestamp,
|
||||||
|
updated bool,
|
||||||
|
update_time timestamp)`
|
||||||
DB.Exec(createPostsTable)
|
DB.Exec(createPostsTable)
|
||||||
|
|
||||||
createTagsTable := `
|
createTagsTable := `
|
||||||
|
@ -71,7 +76,7 @@ func Routes() *chi.Mux {
|
||||||
r.Use(jwtauth.Verifier(TokenAuth))
|
r.Use(jwtauth.Verifier(TokenAuth))
|
||||||
r.Use(jwtauth.Authenticator)
|
r.Use(jwtauth.Authenticator)
|
||||||
r.Post("/", createBlogPost)
|
r.Post("/", createBlogPost)
|
||||||
r.Patch("/id/{id}", updateBlogPostById)
|
r.Patch("/by-id/{id}", updateBlogPostById)
|
||||||
})
|
})
|
||||||
r.Get("/", getBlogPosts)
|
r.Get("/", getBlogPosts)
|
||||||
r.Get("/{slug}", getBlogPostBySlug)
|
r.Get("/{slug}", getBlogPostBySlug)
|
||||||
|
@ -84,6 +89,7 @@ func Routes() *chi.Mux {
|
||||||
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
|
||||||
_, 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)
|
||||||
|
@ -106,6 +112,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...
|
||||||
scr := 0
|
scr := 0
|
||||||
err = slugCheck.Scan(&scr)
|
err = slugCheck.Scan(&scr)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
@ -125,6 +132,7 @@ func createBlogPost(w http.ResponseWriter, r *http.Request) {
|
||||||
VALUES ($1, $2, $3, $4, $5)
|
VALUES ($1, $2, $3, $4, $5)
|
||||||
RETURNING id`
|
RETURNING id`
|
||||||
article_id := 0
|
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()).Scan(&article_id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == sql.ErrNoRows {
|
if err == sql.ErrNoRows {
|
||||||
|
@ -133,13 +141,12 @@ func createBlogPost(w http.ResponseWriter, r *http.Request) {
|
||||||
render.JSON(w, r, returnError)
|
render.JSON(w, r, returnError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Println(err)
|
|
||||||
returnError.Message = "something is super broken..."
|
returnError.Message = "something is super broken..."
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
render.JSON(w, r, returnError)
|
render.JSON(w, r, returnError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Println(newBlogPost.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)`
|
||||||
|
@ -156,6 +163,53 @@ func createBlogPost(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateBlogPostById(w http.ResponseWriter, r *http.Request) {
|
func updateBlogPostById(w http.ResponseWriter, r *http.Request) {
|
||||||
|
returnError := ReturnError{}
|
||||||
|
// Get the actual post
|
||||||
|
id := chi.URLParam(r, "id")
|
||||||
|
result := DB.QueryRow("SELECT id, title, slug, author, content, date FROM posts WHERE id=$1", id)
|
||||||
|
post := BlogPost{}
|
||||||
|
err := result.Scan(&post.ID, &post.Title, &post.Slug, &post.Author, &post.Content, &post.DatePublished)
|
||||||
|
if err != nil {
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
returnError.Message = "blog post requested for update not found"
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
render.JSON(w, r, returnError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
returnError.Message = "something is super broken..."
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
render.JSON(w, r, returnError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify the post belongs to the requester
|
||||||
|
_, claims, _ := jwtauth.FromContext(r.Context())
|
||||||
|
username := claims["username"].(string)
|
||||||
|
if username != post.Author {
|
||||||
|
w.WriteHeader(http.StatusUnauthorized)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// update the post struct
|
||||||
|
err = json.NewDecoder(r.Body).Decode(&post)
|
||||||
|
s := `
|
||||||
|
UPDATE posts SET
|
||||||
|
title = $1,
|
||||||
|
content = $2,
|
||||||
|
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)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
returnError.Message = "something is super broken..."
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
render.JSON(w, r, returnError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
returnSuccess := ReturnSuccess{Message: "post updated", ID: post.ID}
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
render.JSON(w, r, returnSuccess)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in a new issue