2019-04-15 22:40:50 +00:00
|
|
|
package blog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2019-04-16 04:44:20 +00:00
|
|
|
"fmt"
|
2019-04-15 22:40:50 +00:00
|
|
|
"github.com/go-chi/chi"
|
|
|
|
"github.com/go-chi/jwtauth"
|
|
|
|
_ "github.com/go-chi/render"
|
2019-04-16 04:44:20 +00:00
|
|
|
"github.com/gosimple/slug"
|
2019-04-15 22:40:50 +00:00
|
|
|
"net/http"
|
2019-04-16 04:44:20 +00:00
|
|
|
"strings"
|
2019-04-15 22:40:50 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
DB *sql.DB
|
|
|
|
TokenAuth *jwtauth.JWTAuth
|
|
|
|
)
|
|
|
|
|
|
|
|
type BlogPost struct {
|
|
|
|
Title string `json:"title",db:"title"`
|
|
|
|
Slug string `json:"slug",db:"slug"`
|
|
|
|
Author string `json:"author",db:"author"`
|
2019-04-16 04:44:20 +00:00
|
|
|
Content string `json:"content",db:"content"`
|
2019-04-15 22:40:50 +00:00
|
|
|
DatePublished time.Time `json:"date", db:"date"`
|
|
|
|
}
|
|
|
|
|
2019-04-16 04:44:20 +00:00
|
|
|
type Tag struct {
|
|
|
|
Tag string `json:"tag",db:"tag"`
|
|
|
|
}
|
|
|
|
|
2019-04-15 22:40:50 +00:00
|
|
|
func Init() {
|
2019-04-16 04:44:20 +00:00
|
|
|
createPostsTable := `
|
|
|
|
CREATE TABLE IF NOT EXISTS posts
|
2019-04-15 22:40:50 +00:00
|
|
|
(id SERIAL PRIMARY KEY,
|
|
|
|
title text,
|
|
|
|
slug text,
|
|
|
|
author text REFERENCES users (username),
|
2019-04-16 04:44:20 +00:00
|
|
|
content text
|
2019-04-15 22:40:50 +00:00
|
|
|
date timestamp)`
|
2019-04-16 04:44:20 +00:00
|
|
|
DB.Exec(createPostsTable)
|
|
|
|
|
|
|
|
createTagsTable := `
|
|
|
|
CREATE TABLE IF NOT EXISTS tags
|
|
|
|
(id SERIAL PRIMARY KEY,
|
|
|
|
tag string,
|
|
|
|
article_id REFERENCES posts (id))`
|
|
|
|
DB.Exec(createTagsTable)
|
2019-04-15 22:40:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Routes() *chi.Mux {
|
|
|
|
r := chi.NewRouter()
|
|
|
|
r.Group(func(r chi.Router) {
|
|
|
|
r.Use(jwtauth.Verifier(TokenAuth))
|
|
|
|
r.Use(jwtauth.Authenticator)
|
|
|
|
r.Post("/", createBlogPost)
|
2019-04-16 04:44:20 +00:00
|
|
|
r.Patch("/{slug}", updateBlogPostBySlug)
|
|
|
|
r.Patch("/id/{id}", updateBlogPostById)
|
2019-04-15 22:40:50 +00:00
|
|
|
})
|
2019-04-16 04:44:20 +00:00
|
|
|
r.Get("/", getBlogPosts)
|
|
|
|
r.Get("/{slug}", getBlogPostBySlug)
|
|
|
|
r.Get("/tag/{slug}", getBlogPostByTag)
|
|
|
|
r.Get("/id/{id}", getBlogPostById)
|
2019-04-15 22:40:50 +00:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func createBlogPost(w http.ResponseWriter, r *http.Request) {
|
2019-04-16 04:44:20 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateBlogPostBySlug(w http.ResponseWriter, r *http.Request) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateBlogPostById(w http.ResponseWriter, r *http.Request) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func getBlogPosts(w http.ResponseWriter, r *http.Request) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func getBlogPostById(w http.ResponseWriter, r *http.Request) {
|
2019-04-15 22:40:50 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-16 04:44:20 +00:00
|
|
|
func getBlogPostBySlug(w http.ResponseWriter, r *http.Request) {
|
2019-04-15 22:40:50 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-16 04:44:20 +00:00
|
|
|
func getBlogPostBySlug(w http.ResponseWriter, r *http.Request) {
|
2019-04-15 22:40:50 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-16 04:44:20 +00:00
|
|
|
func getRssFeed(w http.ResponseWriter, r *http.Request) {
|
2019-04-15 22:40:50 +00:00
|
|
|
return
|
|
|
|
}
|