Add return success codes, wrap up createBlogPost
This commit is contained in:
parent
9f9beb2f0b
commit
75ec2a02f5
3 changed files with 83 additions and 33 deletions
3
main.go
3
main.go
|
@ -25,10 +25,13 @@ func main() {
|
||||||
auth.Init()
|
auth.Init()
|
||||||
users.DB = db
|
users.DB = db
|
||||||
users.Init()
|
users.Init()
|
||||||
|
blog.DB = db
|
||||||
|
blog.Init()
|
||||||
|
|
||||||
// initiate jwt token
|
// initiate jwt token
|
||||||
auth.TokenAuth = jwtauth.New("HS256", []byte(os.Getenv("JWT_SECRET")), nil)
|
auth.TokenAuth = jwtauth.New("HS256", []byte(os.Getenv("JWT_SECRET")), nil)
|
||||||
users.TokenAuth = auth.TokenAuth
|
users.TokenAuth = auth.TokenAuth
|
||||||
|
blog.TokenAuth = auth.TokenAuth
|
||||||
// initiate the routes
|
// initiate the routes
|
||||||
router := Routes()
|
router := Routes()
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,7 @@ var (
|
||||||
TokenAuth *jwtauth.JWTAuth
|
TokenAuth *jwtauth.JWTAuth
|
||||||
)
|
)
|
||||||
|
|
||||||
type RegistrationError struct {
|
type ReturnError struct {
|
||||||
Message string `json:"error"`
|
Message string `json:"error"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,7 +61,7 @@ func Routes() *chi.Mux {
|
||||||
}
|
}
|
||||||
|
|
||||||
func register(w http.ResponseWriter, r *http.Request) {
|
func register(w http.ResponseWriter, r *http.Request) {
|
||||||
returnError := RegistrationError{}
|
returnError := ReturnError{}
|
||||||
creds := &SignUpCredentials{}
|
creds := &SignUpCredentials{}
|
||||||
err := json.NewDecoder(r.Body).Decode(creds)
|
err := json.NewDecoder(r.Body).Decode(creds)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -127,10 +127,6 @@ func signin(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
result := DB.QueryRow("SELECT password FROM users WHERE username=$1", creds.Username)
|
result := DB.QueryRow("SELECT password FROM users WHERE username=$1", creds.Username)
|
||||||
if err != nil {
|
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
storedCreds := &SignInCredentials{}
|
storedCreds := &SignInCredentials{}
|
||||||
err = result.Scan(&storedCreds.Password)
|
err = result.Scan(&storedCreds.Password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -2,10 +2,11 @@ package blog
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/go-chi/chi"
|
"github.com/go-chi/chi"
|
||||||
"github.com/go-chi/jwtauth"
|
"github.com/go-chi/jwtauth"
|
||||||
_ "github.com/go-chi/render"
|
"github.com/go-chi/render"
|
||||||
"github.com/gosimple/slug"
|
"github.com/gosimple/slug"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -26,7 +27,23 @@ type BlogPost struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Tag struct {
|
type Tag struct {
|
||||||
Tag string `json:"tag",db:"tag"`
|
TagList string `json:"tags"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type NewBlogPost struct {
|
||||||
|
Title string `json:"title",db:"title"`
|
||||||
|
Content string `json:"content",db:"content"`
|
||||||
|
Tags string `json:"tags"`
|
||||||
|
Author string `json:"author",db:"author"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ReturnError struct {
|
||||||
|
Message string `json:"error"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ReturnSuccess struct {
|
||||||
|
Message string `json:"success"`
|
||||||
|
ID int `json:"id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func Init() {
|
func Init() {
|
||||||
|
@ -36,15 +53,15 @@ func Init() {
|
||||||
title text,
|
title text,
|
||||||
slug text,
|
slug text,
|
||||||
author text REFERENCES users (username),
|
author text REFERENCES users (username),
|
||||||
content text
|
content text,
|
||||||
date timestamp)`
|
date timestamp)`
|
||||||
DB.Exec(createPostsTable)
|
DB.Exec(createPostsTable)
|
||||||
|
|
||||||
createTagsTable := `
|
createTagsTable := `
|
||||||
CREATE TABLE IF NOT EXISTS tags
|
CREATE TABLE IF NOT EXISTS tags
|
||||||
(id SERIAL PRIMARY KEY,
|
(id SERIAL PRIMARY KEY,
|
||||||
tag string,
|
tag text,
|
||||||
article_id REFERENCES posts (id))`
|
article_id int REFERENCES posts (id))`
|
||||||
DB.Exec(createTagsTable)
|
DB.Exec(createTagsTable)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -59,53 +76,83 @@ func Routes() *chi.Mux {
|
||||||
})
|
})
|
||||||
r.Get("/", getBlogPosts)
|
r.Get("/", getBlogPosts)
|
||||||
r.Get("/{slug}", getBlogPostBySlug)
|
r.Get("/{slug}", getBlogPostBySlug)
|
||||||
r.Get("/tag/{slug}", getBlogPostByTag)
|
r.Get("/by-tag/{tag}", getBlogPostByTag)
|
||||||
r.Get("/id/{id}", getBlogPostById)
|
r.Get("/by-id/{id}", getBlogPostById)
|
||||||
|
r.Get("/by-author/{author}", getBlogPostByAuthor)
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
func createBlogPost(w http.ResponseWriter, r *http.Request) {
|
func createBlogPost(w http.ResponseWriter, r *http.Request) {
|
||||||
blogPost := &BlogPost{}
|
returnError := ReturnError{}
|
||||||
tags := &Tag{}
|
newBlogPost := &NewBlogPost{}
|
||||||
_, claims, _ := jwtauth.FromContext(r.Context())
|
_, claims, _ := jwtauth.FromContext(r.Context())
|
||||||
username := claims["username"].(string)
|
username := claims["username"].(string)
|
||||||
err := json.NewDecoder(r.Body).Decode(blogPost)
|
err := json.NewDecoder(r.Body).Decode(newBlogPost)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
fmt.Println(err)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err := json.NewDecoder(r.Body).Decode(tags)
|
if newBlogPost.Title == "" {
|
||||||
if err != nil {
|
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
|
||||||
fmt.Println(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if blogPost.Title == "" {
|
|
||||||
returnError.Message = "title is required"
|
returnError.Message = "title is required"
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
render.JSON(w, r, returnError)
|
render.JSON(w, r, returnError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if blogPost.Content == "" {
|
if newBlogPost.Content == "" {
|
||||||
returnError.Message = "content is required"
|
returnError.Message = "content is required"
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
render.JSON(w, r, returnError)
|
render.JSON(w, r, returnError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
slug := slug.Make(blogPost.Content)
|
as := slug.Make(newBlogPost.Title)
|
||||||
|
slugCheck := DB.QueryRow("SELECT id FROM posts WHERE slug=$1", as)
|
||||||
|
// wow this is ugly. someone pls send help.
|
||||||
|
scr := 0
|
||||||
|
err = slugCheck.Scan(&scr)
|
||||||
|
if err == nil {
|
||||||
|
returnError.Message = "slug already exists"
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
render.JSON(w, r, returnError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
if err != sql.ErrNoRows {
|
||||||
|
returnError.Message = "something is super broken..."
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
s := `INSERT INTO posts (title, slug, author, content, date)
|
s := `INSERT INTO posts (title, slug, author, content, date)
|
||||||
VALUES ($1, $2, $3, $4, $5)`
|
VALUES ($1, $2, $3, $4, $5)
|
||||||
db.Exec(s, blogPost.Title, slug, username, blogPost.Content, time.UTC())
|
RETURNING id`
|
||||||
if tags.Tag != "" {
|
article_id := 0
|
||||||
t : = `INSERT INTO tags (tag, article_id)
|
err = DB.QueryRow(s, newBlogPost.Title, as, username, newBlogPost.Content, time.Now().UTC()).Scan(&article_id)
|
||||||
|
if err != nil {
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
returnError.Message = "something is super broken..."
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
render.JSON(w, r, returnError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Println(err)
|
||||||
|
returnError.Message = "something is super broken..."
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
render.JSON(w, r, returnError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Println(newBlogPost.Tags)
|
||||||
|
if newBlogPost.Tags != "" {
|
||||||
|
t := `INSERT INTO tags (tag, article_id)
|
||||||
VALUES ($1, $2)`
|
VALUES ($1, $2)`
|
||||||
for i := range strings.Split(tags.Tag, ",") {
|
tags := strings.Split(newBlogPost.Tags, ",")
|
||||||
db.Exec(t, i, article_id)
|
for i := range tags {
|
||||||
|
DB.Exec(t, tags[i], article_id)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
returnSuccess := ReturnSuccess{Message: "post created", ID: article_id}
|
||||||
w.WriteHeader(http.StatusCreated)
|
w.WriteHeader(http.StatusCreated)
|
||||||
|
render.JSON(w, r, returnSuccess)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -129,7 +176,11 @@ func getBlogPostBySlug(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func getBlogPostBySlug(w http.ResponseWriter, r *http.Request) {
|
func getBlogPostByTag(w http.ResponseWriter, r *http.Request) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func getBlogPostByAuthor(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in a new issue