This repository has been archived on 2023-07-09. You can view files and clone it, but cannot push or open issues or pull requests.
sudoscientist-go-backend/packages/blog/posts.go

409 lines
12 KiB
Go
Raw Permalink Normal View History

2019-04-15 22:40:50 +00:00
package blog
import (
"database/sql"
"encoding/json"
"fmt"
2019-04-15 22:40:50 +00:00
"github.com/go-chi/chi"
"github.com/go-chi/jwtauth"
"github.com/go-chi/render"
"github.com/gosimple/slug"
2019-04-15 22:40:50 +00:00
"net/http"
"strings"
2019-04-15 22:40:50 +00:00
"time"
)
type BlogPost struct {
2019-04-22 19:22:14 +00:00
ID int `json:"id",db:"id"`
2019-04-15 22:40:50 +00:00
Title string `json:"title",db:"title"`
Slug string `json:"slug",db:"slug"`
Author string `json:"author",db:"author"`
Content string `json:"content",db:"content"`
TimePublished time.Time `json:"time_published", db:"time_published"`
Modified bool `json:"modified", db:"modified"`
2019-04-22 20:28:36 +00:00
TimeModified time.Time `json:"last_modified", db:"last_modified"`
2019-04-15 22:40:50 +00:00
}
2019-04-22 23:01:28 +00:00
type BlogPosts []BlogPost
type Tag struct {
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 ReturnSuccess struct {
Message string `json:"success"`
ID int `json:"id"`
2019-10-26 15:29:58 +00:00
Slug string `json:"slug",db:"slug"`
}
2020-01-26 04:19:39 +00:00
type ReferenceId struct {
2019-04-22 23:46:42 +00:00
LastID int `json:"last_id"`
}
2019-04-15 22:40:50 +00:00
func createBlogPost(w http.ResponseWriter, r *http.Request) {
2020-01-25 23:04:15 +00:00
returnMessage := ReturnMessage{}
returnMessage.Error = false
newBlogPost := &NewBlogPost{}
2019-04-22 20:16:31 +00:00
// basic checks
_, claims, _ := jwtauth.FromContext(r.Context())
2020-01-22 03:19:35 +00:00
is_admin := claims["admin"].(bool)
if !is_admin {
2020-01-25 23:04:15 +00:00
returnMessage.Message = "sorry only admins are allowed to create blog posts"
returnMessage.Error = true
2020-01-22 03:19:35 +00:00
w.WriteHeader(http.StatusUnauthorized)
2020-01-25 23:04:15 +00:00
render.JSON(w, r, returnMessage)
2020-01-22 03:19:35 +00:00
return
}
username := claims["username"].(string)
err := json.NewDecoder(r.Body).Decode(newBlogPost)
if err != nil {
2020-01-25 23:04:15 +00:00
returnMessage.Message = "unknown error, try again later"
returnMessage.Error = true
w.WriteHeader(http.StatusBadRequest)
2020-01-25 23:04:15 +00:00
render.JSON(w, r, returnMessage)
return
}
if newBlogPost.Title == "" {
2020-01-25 23:04:15 +00:00
returnMessage.Message = "title is required"
returnMessage.Error = true
w.WriteHeader(http.StatusBadRequest)
2020-01-25 23:04:15 +00:00
render.JSON(w, r, returnMessage)
return
}
if newBlogPost.Content == "" {
2020-01-25 23:04:15 +00:00
returnMessage.Message = "content is required"
returnMessage.Error = true
w.WriteHeader(http.StatusBadRequest)
2020-01-25 23:04:15 +00:00
render.JSON(w, r, returnMessage)
return
}
as := slug.Make(newBlogPost.Title)
slugCheck := DB.QueryRow("SELECT id FROM posts WHERE slug=$1", as)
// wow this is ugly. someone pls send help.
2019-04-22 20:16:31 +00:00
// checking to ensure the same slug doesn't exist...
scr := 0
err = slugCheck.Scan(&scr)
if err == nil {
2020-01-25 23:04:15 +00:00
returnMessage.Message = "slug already exists"
returnMessage.Error = true
w.WriteHeader(http.StatusBadRequest)
2020-01-25 23:04:15 +00:00
render.JSON(w, r, returnMessage)
return
}
if err != nil {
if err != sql.ErrNoRows {
2020-01-25 23:04:15 +00:00
returnMessage.Message = "something is super broken..."
returnMessage.Error = true
w.WriteHeader(http.StatusInternalServerError)
2020-01-25 23:04:15 +00:00
render.JSON(w, r, returnMessage)
2019-04-22 23:46:42 +00:00
fmt.Println(err)
return
}
}
2019-04-22 20:28:36 +00:00
s := `INSERT INTO posts (title, slug, author, content, time_published, modified, last_modified)
VALUES ($1, $2, $3, $4, $5, $6, $7)
RETURNING id`
article_id := 0
2019-04-22 20:16:31 +00:00
// write the row and get back the id
err = DB.QueryRow(s, newBlogPost.Title, as, username, newBlogPost.Content, time.Now().UTC(), false, time.Now().UTC()).Scan(&article_id)
if err != nil {
if err == sql.ErrNoRows {
2020-01-25 23:04:15 +00:00
returnMessage.Message = "something is super broken..."
returnMessage.Error = true
w.WriteHeader(http.StatusInternalServerError)
2020-01-25 23:04:15 +00:00
render.JSON(w, r, returnMessage)
2019-04-22 23:46:42 +00:00
fmt.Println(err)
return
}
2020-01-25 23:04:15 +00:00
returnMessage.Message = "something is super broken..."
returnMessage.Error = true
w.WriteHeader(http.StatusInternalServerError)
2020-01-25 23:04:15 +00:00
render.JSON(w, r, returnMessage)
2019-04-22 23:01:28 +00:00
fmt.Println(err)
return
}
2019-04-22 20:16:31 +00:00
// if the article has tags
if newBlogPost.Tags != "" {
t := `INSERT INTO tags (tag, article_id)
VALUES ($1, $2)`
tags := strings.Split(newBlogPost.Tags, ",")
for i := range tags {
DB.Exec(t, tags[i], article_id)
}
}
2019-10-26 15:29:58 +00:00
returnSuccess := ReturnSuccess{Message: "post created", ID: article_id, Slug: as}
w.WriteHeader(http.StatusCreated)
render.JSON(w, r, returnSuccess)
return
}
func updateBlogPostById(w http.ResponseWriter, r *http.Request) {
2020-01-25 23:04:15 +00:00
returnMessage := ReturnMessage{}
returnMessage.Error = false
2019-04-22 19:22:14 +00:00
// Get the actual post
2020-01-26 04:19:39 +00:00
id := chi.URLParam(r, "post_id")
result := DB.QueryRow("SELECT id, title, slug, author, content, time_published FROM posts WHERE id=$1", id)
2019-04-22 19:22:14 +00:00
post := BlogPost{}
err := result.Scan(&post.ID, &post.Title, &post.Slug, &post.Author, &post.Content, &post.TimePublished)
2019-04-22 19:22:14 +00:00
if err != nil {
if err == sql.ErrNoRows {
2020-01-25 23:04:15 +00:00
returnMessage.Message = "blog post requested for update not found"
returnMessage.Error = true
2019-04-22 19:22:14 +00:00
w.WriteHeader(http.StatusInternalServerError)
2020-01-25 23:04:15 +00:00
render.JSON(w, r, returnMessage)
2019-04-22 23:46:42 +00:00
fmt.Println(err)
2019-04-22 19:22:14 +00:00
return
}
2020-01-25 23:04:15 +00:00
returnMessage.Message = "something is super broken..."
returnMessage.Error = true
2019-04-22 19:22:14 +00:00
w.WriteHeader(http.StatusInternalServerError)
2020-01-25 23:04:15 +00:00
render.JSON(w, r, returnMessage)
2019-04-22 23:01:28 +00:00
fmt.Println(err)
2019-04-22 19:22:14 +00:00
return
}
// Verify the post belongs to the requester
_, claims, _ := jwtauth.FromContext(r.Context())
username := claims["username"].(string)
if username != post.Author {
2020-01-25 23:04:15 +00:00
returnMessage.Message = "unauthorized..."
returnMessage.Error = true
2019-04-22 19:22:14 +00:00
w.WriteHeader(http.StatusUnauthorized)
2020-01-25 23:04:15 +00:00
render.JSON(w, r, returnMessage)
2019-04-22 19:22:14 +00:00
return
}
2019-04-22 20:16:31 +00:00
// update the post struct
2019-04-22 19:22:14 +00:00
err = json.NewDecoder(r.Body).Decode(&post)
s := `
UPDATE posts SET
title = $1,
content = $2,
modified = $3,
2019-04-22 20:28:36 +00:00
last_modified = $4
2019-04-22 19:22:14 +00:00
WHERE id = $5`
2019-04-22 20:16:31 +00:00
// write the row update
_, err = DB.Exec(s, post.Title, post.Content, true, time.Now().UTC(), post.ID)
2019-04-22 19:22:14 +00:00
if err != nil {
2020-01-25 23:04:15 +00:00
returnMessage.Message = "something is super broken..."
returnMessage.Error = true
2019-04-22 19:22:14 +00:00
w.WriteHeader(http.StatusInternalServerError)
2020-01-25 23:04:15 +00:00
render.JSON(w, r, returnMessage)
2019-04-22 23:01:28 +00:00
fmt.Println(err)
2019-04-22 19:22:14 +00:00
return
}
2019-10-26 15:29:58 +00:00
returnSuccess := ReturnSuccess{Message: "post updated", ID: post.ID, Slug: post.Slug}
2019-04-22 19:22:14 +00:00
w.WriteHeader(http.StatusOK)
render.JSON(w, r, returnSuccess)
return
}
2019-04-22 23:01:28 +00:00
// This will search by the last id seen, descending.
func getBlogPosts(w http.ResponseWriter, r *http.Request) {
2020-01-25 23:04:15 +00:00
returnMessage := ReturnMessage{}
returnMessage.Error = false
2020-01-26 04:19:39 +00:00
referenceID := &ReferenceId{}
2019-04-22 23:46:42 +00:00
err := json.NewDecoder(r.Body).Decode(referenceID)
// hardcode 9001 for cool kid points
2019-04-22 23:01:28 +00:00
if err != nil {
2019-04-22 23:46:42 +00:00
referenceID.LastID = 9001
2019-04-22 23:01:28 +00:00
}
2019-04-22 23:46:42 +00:00
search_id := referenceID.LastID
// if someone is lame and sends up a negative number...
2019-04-22 23:01:28 +00:00
if search_id < 1 {
search_id = 9001
}
search := `
SELECT id, title, slug, author, content, time_published, modified, last_modified
FROM posts
WHERE id < $1
ORDER BY id DESC
FETCH FIRST 10 ROWS ONLY
`
rows, err := DB.Query(search, search_id)
if err != nil {
2020-01-25 23:04:15 +00:00
returnMessage.Message = "something is super broken..."
returnMessage.Error = true
2019-04-22 23:01:28 +00:00
w.WriteHeader(http.StatusInternalServerError)
2020-01-25 23:04:15 +00:00
render.JSON(w, r, returnMessage)
2019-04-22 23:01:28 +00:00
fmt.Println(err)
return
}
defer rows.Close()
post := BlogPost{}
posts := make(BlogPosts, 0)
for rows.Next() {
if err := rows.Scan(&post.ID, &post.Title, &post.Slug, &post.Author, &post.Content, &post.TimePublished, &post.Modified, &post.TimeModified); err != nil {
}
posts = append(posts, post)
}
if err := rows.Err(); err != nil {
2020-01-25 23:04:15 +00:00
returnMessage.Message = "something is super broken..."
returnMessage.Error = true
2019-04-22 23:01:28 +00:00
w.WriteHeader(http.StatusInternalServerError)
2020-01-25 23:04:15 +00:00
render.JSON(w, r, returnMessage)
2019-04-22 23:01:28 +00:00
fmt.Println(err)
return
}
w.WriteHeader(http.StatusOK)
render.JSON(w, r, posts)
return
}
2019-04-22 20:16:31 +00:00
func getBlogPostBySlug(w http.ResponseWriter, r *http.Request) {
2020-01-25 23:04:15 +00:00
returnMessage := ReturnMessage{}
returnMessage.Error = false
2019-04-22 20:16:31 +00:00
slug := chi.URLParam(r, "slug")
2019-04-22 20:28:36 +00:00
result := DB.QueryRow("SELECT id, title, slug, author, content, time_published, modified, last_modified FROM posts WHERE slug=$1", slug)
2019-04-22 20:16:31 +00:00
post := BlogPost{}
err := result.Scan(&post.ID, &post.Title, &post.Slug, &post.Author, &post.Content, &post.TimePublished, &post.Modified, &post.TimeModified)
if err != nil {
2020-01-25 23:04:15 +00:00
returnMessage.Message = "post not found"
returnMessage.Error = true
w.WriteHeader(http.StatusBadRequest)
2020-01-25 23:04:15 +00:00
render.JSON(w, r, returnMessage)
return
}
w.WriteHeader(http.StatusOK)
render.JSON(w, r, post)
2019-04-15 22:40:50 +00:00
return
}
2019-04-22 20:16:31 +00:00
func getBlogPostById(w http.ResponseWriter, r *http.Request) {
2020-01-25 23:04:15 +00:00
returnMessage := ReturnMessage{}
returnMessage.Error = false
2019-04-22 20:16:31 +00:00
id := chi.URLParam(r, "id")
2019-04-22 20:28:36 +00:00
result := DB.QueryRow("SELECT id, title, slug, author, content, time_published, modified, last_modified FROM posts WHERE id=$1", id)
2019-04-22 20:16:31 +00:00
post := BlogPost{}
err := result.Scan(&post.ID, &post.Title, &post.Slug, &post.Author, &post.Content, &post.TimePublished, &post.Modified, &post.TimeModified)
2019-04-22 20:16:31 +00:00
if err != nil {
2020-01-25 23:04:15 +00:00
returnMessage.Message = "post not found"
returnMessage.Error = true
2019-04-22 20:16:31 +00:00
w.WriteHeader(http.StatusBadRequest)
2020-01-25 23:04:15 +00:00
render.JSON(w, r, returnMessage)
2019-04-22 20:16:31 +00:00
return
}
w.WriteHeader(http.StatusOK)
render.JSON(w, r, post)
2019-04-15 22:40:50 +00:00
return
}
2019-04-22 20:16:31 +00:00
func getBlogPostsByTag(w http.ResponseWriter, r *http.Request) {
2020-01-25 23:04:15 +00:00
returnMessage := ReturnMessage{}
returnMessage.Error = false
2020-01-26 04:19:39 +00:00
referenceID := &ReferenceId{}
2019-04-23 00:20:00 +00:00
err := json.NewDecoder(r.Body).Decode(referenceID)
// hardcode 9001 for cool kid points
if err != nil {
referenceID.LastID = 9001
}
search_id := referenceID.LastID
// if someone is lame and sends up a negative number...
if search_id < 1 {
search_id = 9001
}
tag := chi.URLParam(r, "tag")
search := `
SELECT posts.id, posts.title, posts.slug, posts.author, posts.content, posts.time_published, posts.modified, posts.last_modified
FROM posts
INNER JOIN tags on posts .id = tags.article_id
WHERE posts.id < $1
AND tags.tag = $2
ORDER BY id DESC
FETCH FIRST 10 ROWS ONLY
`
rows, err := DB.Query(search, search_id, tag)
if err != nil {
2020-01-25 23:04:15 +00:00
returnMessage.Message = "something is super broken..."
returnMessage.Error = true
2019-04-23 00:20:00 +00:00
w.WriteHeader(http.StatusInternalServerError)
2020-01-25 23:04:15 +00:00
render.JSON(w, r, returnMessage)
2019-04-23 00:20:00 +00:00
fmt.Println(err)
return
}
defer rows.Close()
post := BlogPost{}
posts := make(BlogPosts, 0)
for rows.Next() {
if err := rows.Scan(&post.ID, &post.Title, &post.Slug, &post.Author, &post.Content, &post.TimePublished, &post.Modified, &post.TimeModified); err != nil {
}
posts = append(posts, post)
}
if err := rows.Err(); err != nil {
2020-01-25 23:04:15 +00:00
returnMessage.Message = "something is super broken..."
returnMessage.Error = true
2019-04-23 00:20:00 +00:00
w.WriteHeader(http.StatusInternalServerError)
2020-01-25 23:04:15 +00:00
render.JSON(w, r, returnMessage)
2019-04-23 00:20:00 +00:00
fmt.Println(err)
return
}
w.WriteHeader(http.StatusOK)
render.JSON(w, r, posts)
return
}
2019-04-22 20:16:31 +00:00
func getBlogPostsByAuthor(w http.ResponseWriter, r *http.Request) {
2020-01-25 23:04:15 +00:00
returnMessage := ReturnMessage{}
returnMessage.Error = false
2020-01-26 04:19:39 +00:00
referenceID := &ReferenceId{}
err := json.NewDecoder(r.Body).Decode(referenceID)
// hardcode 9001 for cool kid points
if err != nil {
referenceID.LastID = 9001
}
search_id := referenceID.LastID
// if someone is lame and sends up a negative number...
if search_id < 1 {
search_id = 9001
}
author := chi.URLParam(r, "author")
search := `
SELECT id, title, slug, author, content, time_published, modified, last_modified
FROM posts
WHERE id < $1
AND author = $2
ORDER BY id DESC
FETCH FIRST 10 ROWS ONLY
`
rows, err := DB.Query(search, search_id, author)
if err != nil {
2020-01-25 23:04:15 +00:00
returnMessage.Message = "something is super broken..."
returnMessage.Error = true
w.WriteHeader(http.StatusInternalServerError)
2020-01-25 23:04:15 +00:00
render.JSON(w, r, returnMessage)
fmt.Println(err)
return
}
defer rows.Close()
post := BlogPost{}
posts := make(BlogPosts, 0)
for rows.Next() {
if err := rows.Scan(&post.ID, &post.Title, &post.Slug, &post.Author, &post.Content, &post.TimePublished, &post.Modified, &post.TimeModified); err != nil {
}
posts = append(posts, post)
}
if err := rows.Err(); err != nil {
2020-01-25 23:04:15 +00:00
returnMessage.Message = "something is super broken..."
returnMessage.Error = true
w.WriteHeader(http.StatusInternalServerError)
2020-01-25 23:04:15 +00:00
render.JSON(w, r, returnMessage)
fmt.Println(err)
return
}
w.WriteHeader(http.StatusOK)
render.JSON(w, r, posts)
2019-04-15 22:40:50 +00:00
return
}
func getRssFeed(w http.ResponseWriter, r *http.Request) {
2019-04-15 22:40:50 +00:00
return
}