Halfway towards the first step part of blogposts

This commit is contained in:
Amarpreet Minhas 2019-04-16 00:44:20 -04:00
parent 04fdad2bfb
commit 9f9beb2f0b

View file

@ -2,11 +2,13 @@ package blog
import ( import (
"database/sql" "database/sql"
_ "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"
"net/http" "net/http"
"strings"
"time" "time"
) )
@ -19,18 +21,31 @@ type BlogPost struct {
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"`
DatePublished time.Time `json:"date", db:"date"` DatePublished time.Time `json:"date", db:"date"`
} }
type Tag struct {
Tag string `json:"tag",db:"tag"`
}
func Init() { func Init() {
dbCreateStatement := ` createPostsTable := `
CREATE TABLE IF NOT EXISTS blog CREATE TABLE IF NOT EXISTS posts
(id SERIAL PRIMARY KEY, (id SERIAL PRIMARY KEY,
title text, title text,
slug text, slug text,
author text REFERENCES users (username), author text REFERENCES users (username),
content text
date timestamp)` date timestamp)`
DB.Exec(dbCreateStatement) DB.Exec(createPostsTable)
createTagsTable := `
CREATE TABLE IF NOT EXISTS tags
(id SERIAL PRIMARY KEY,
tag string,
article_id REFERENCES posts (id))`
DB.Exec(createTagsTable)
} }
func Routes() *chi.Mux { func Routes() *chi.Mux {
@ -39,25 +54,85 @@ 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("/{slug}", updateBlogPost) r.Patch("/{slug}", updateBlogPostBySlug)
r.Patch("/id/{id}", updateBlogPostById)
}) })
r.Get("/", getAllBlogPosts) r.Get("/", getBlogPosts)
r.Get("/{slug}", getBlogPost) r.Get("/{slug}", getBlogPostBySlug)
r.Get("/tag/{slug}", getBlogPostByTag)
r.Get("/id/{id}", getBlogPostById)
return r return r
} }
func createBlogPost(w http.ResponseWriter, r *http.Request) { func createBlogPost(w http.ResponseWriter, r *http.Request) {
blogPost := &BlogPost{}
tags := &Tag{}
_, claims, _ := jwtauth.FromContext(r.Context())
username := claims["username"].(string)
err := json.NewDecoder(r.Body).Decode(blogPost)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Println(err)
return
}
err := json.NewDecoder(r.Body).Decode(tags)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Println(err)
return
}
if blogPost.Title == "" {
returnError.Message = "title is required"
w.WriteHeader(http.StatusBadRequest)
render.JSON(w, r, returnError)
return
}
if blogPost.Content == "" {
returnError.Message = "content is required"
w.WriteHeader(http.StatusBadRequest)
render.JSON(w, r, returnError)
return
}
slug := slug.Make(blogPost.Content)
s := `INSERT INTO posts (title, slug, author, content, date)
VALUES ($1, $2, $3, $4, $5)`
db.Exec(s, blogPost.Title, slug, username, blogPost.Content, time.UTC())
if tags.Tag != "" {
t : = `INSERT INTO tags (tag, article_id)
VALUES ($1, $2)`
for i := range strings.Split(tags.Tag, ",") {
db.Exec(t, i, article_id)
}
}
w.WriteHeader(http.StatusCreated)
return return
} }
func updateBlogPost(w http.ResponseWriter, r *http.Request) { func updateBlogPostBySlug(w http.ResponseWriter, r *http.Request) {
return return
} }
func getAllBlogPosts(w http.ResponseWriter, r *http.Request) { func updateBlogPostById(w http.ResponseWriter, r *http.Request) {
return return
} }
func getBlogPost(w http.ResponseWriter, r *http.Request) { func getBlogPosts(w http.ResponseWriter, r *http.Request) {
return
}
func getBlogPostById(w http.ResponseWriter, r *http.Request) {
return
}
func getBlogPostBySlug(w http.ResponseWriter, r *http.Request) {
return
}
func getBlogPostBySlug(w http.ResponseWriter, r *http.Request) {
return
}
func getRssFeed(w http.ResponseWriter, r *http.Request) {
return return
} }