2019-04-15 22:40:50 +00:00
|
|
|
package blog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2019-04-20 21:17:20 +00:00
|
|
|
"encoding/json"
|
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"
|
2019-04-20 21:17:20 +00:00
|
|
|
"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 {
|
2019-04-20 21:17:20 +00:00
|
|
|
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"`
|
2019-04-16 04:44:20 +00:00
|
|
|
}
|
|
|
|
|
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-20 21:17: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,
|
2019-04-20 21:17:20 +00:00
|
|
|
tag text,
|
|
|
|
article_id int REFERENCES posts (id))`
|
2019-04-16 04:44:20 +00:00
|
|
|
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)
|
2019-04-20 21:17:20 +00:00
|
|
|
r.Get("/by-tag/{tag}", getBlogPostByTag)
|
|
|
|
r.Get("/by-id/{id}", getBlogPostById)
|
|
|
|
r.Get("/by-author/{author}", getBlogPostByAuthor)
|
2019-04-15 22:40:50 +00:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func createBlogPost(w http.ResponseWriter, r *http.Request) {
|
2019-04-20 21:17:20 +00:00
|
|
|
returnError := ReturnError{}
|
|
|
|
newBlogPost := &NewBlogPost{}
|
2019-04-16 04:44:20 +00:00
|
|
|
_, claims, _ := jwtauth.FromContext(r.Context())
|
|
|
|
username := claims["username"].(string)
|
2019-04-20 21:17:20 +00:00
|
|
|
err := json.NewDecoder(r.Body).Decode(newBlogPost)
|
2019-04-16 04:44:20 +00:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2019-04-20 21:17:20 +00:00
|
|
|
if newBlogPost.Title == "" {
|
|
|
|
returnError.Message = "title is required"
|
2019-04-16 04:44:20 +00:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
2019-04-20 21:17:20 +00:00
|
|
|
render.JSON(w, r, returnError)
|
2019-04-16 04:44:20 +00:00
|
|
|
return
|
|
|
|
}
|
2019-04-20 21:17:20 +00:00
|
|
|
if newBlogPost.Content == "" {
|
|
|
|
returnError.Message = "content is required"
|
2019-04-16 04:44:20 +00:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
render.JSON(w, r, returnError)
|
|
|
|
return
|
|
|
|
}
|
2019-04-20 21:17:20 +00:00
|
|
|
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"
|
2019-04-16 04:44:20 +00:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
render.JSON(w, r, returnError)
|
|
|
|
return
|
|
|
|
}
|
2019-04-20 21:17:20 +00:00
|
|
|
if err != nil {
|
|
|
|
if err != sql.ErrNoRows {
|
|
|
|
returnError.Message = "something is super broken..."
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2019-04-16 04:44:20 +00:00
|
|
|
s := `INSERT INTO posts (title, slug, author, content, date)
|
2019-04-20 21:17:20 +00:00
|
|
|
VALUES ($1, $2, $3, $4, $5)
|
|
|
|
RETURNING id`
|
|
|
|
article_id := 0
|
|
|
|
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)
|
2019-04-16 04:44:20 +00:00
|
|
|
VALUES ($1, $2)`
|
2019-04-20 21:17:20 +00:00
|
|
|
tags := strings.Split(newBlogPost.Tags, ",")
|
|
|
|
for i := range tags {
|
|
|
|
DB.Exec(t, tags[i], article_id)
|
2019-04-16 04:44:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-04-20 21:17:20 +00:00
|
|
|
returnSuccess := ReturnSuccess{Message: "post created", ID: article_id}
|
2019-04-16 04:44:20 +00:00
|
|
|
w.WriteHeader(http.StatusCreated)
|
2019-04-20 21:17:20 +00:00
|
|
|
render.JSON(w, r, returnSuccess)
|
2019-04-16 04:44:20 +00:00
|
|
|
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-20 21:17:20 +00:00
|
|
|
func getBlogPostByTag(w http.ResponseWriter, r *http.Request) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func getBlogPostByAuthor(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
|
|
|
|
}
|