-
Notifications
You must be signed in to change notification settings - Fork 0
/
blog_new.go
169 lines (141 loc) · 3.75 KB
/
blog_new.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package blog
import (
"database/sql"
"gas"
_ "github.com/jbarham/gopgsqldriver"
"github.com/russross/blackfriday"
"log"
"net/http"
"strconv"
"time"
)
type Post struct {
Id int64
Time time.Time
Title string
Body string
Tag string
}
var (
DB *sql.DB
QuerySinglePost *sql.Stmt
QueryPage *sql.Stmt
QueryNewPost *sql.Stmt
QueryEditPost *sql.Stmt
)
func init() {
var err error
DB, err = sql.Open("postgres", "user=postgres dbname=postgres")
if err != nil {
panic(err)
}
QuerySinglePost, _ = DB.Prepare("SELECT * FROM posts WHERE id = $1")
QueryPage, _ = DB.Prepare("SELECT * FROM posts ORDER BY id DESC LIMIT 10 OFFSET $1")
QueryNewPost, _ = DB.Prepare("INSERT INTO posts (timestamp, title, body, tag) VALUES ($1, $2, $3, $4)")
QueryEditPost, _ = DB.Prepare("UPDATE posts SET body = $1 WHERE id = $2")
}
/////////////////////////////
// Data-getters
/////////////////////////////
//func getPost(g *gas.Gas) *Post {
//}
func getPosts(g *gas.Gas, postId interface{}) []*Post {
rows, err := QueryPage.Query(postId)
if err != nil {
g.ErrorPage(http.StatusServiceUnavailable)
panic(err)
}
posts := []*Post{}
for rows.Next() {
// TODO: better way to do this?
var (
id int64
stamp string
title string
body []byte
tag string
)
err = rows.Scan(&id, &stamp, &title, &body, &tag)
if err != nil {
g.ErrorPage(http.StatusServiceUnavailable)
}
timestamp, _ := time.Parse("2006-01-02 15:04:05-07", stamp)
posts = append(posts, &Post{id, timestamp, title, string(blackfriday.MarkdownCommon(body)), tag})
}
return posts
}
/////////////////////////////
// Actions
/////////////////////////////
func NewPost(g *gas.Gas) {
switch g.Method {
case "GET":
g.Render("blog/newpost", nil)
case "POST":
now := time.Now().Format("2006-01-02 15:04:05-07")
_, err := QueryNewPost.Exec(now, g.FormValue("title"), g.FormValue("body"), g.FormValue("tag"))
if err != nil {
log.Print(err)
return
}
http.Redirect(g.ResponseWriter, g.Request, "/blog/", http.StatusFound)
}
}
//func PreviewPost(g *gas.Gas) {
func EditPost(g *gas.Gas, postId string) {
switch g.Method {
case "GET":
row := QuerySinglePost.QueryRow(postId)
var (
id int64
tag string
stamp, title string
body string
)
err := row.Scan(&id, &stamp, &title, &body, &tag)
if err != nil {
log.Printf("blog.EditPost(): %v", err)
g.ErrorPage(http.StatusServiceUnavailable)
}
timestamp, _ := time.Parse("2006-01-02 15:04:05-07", stamp)
g.Render("blog/editpost", &Post{id, timestamp, title, body, tag})
case "POST":
QueryEditPost.Exec(g.FormValue("body"), postId)
http.Redirect(g.ResponseWriter, g.Request, "/blog/", http.StatusFound)
}
}
func SinglePost(g *gas.Gas, postId string) {
row := QuerySinglePost.QueryRow(postId)
var (
id int64
stamp string
title string
body []byte
tag string
)
err := row.Scan(&id, &stamp, &title, &body, &tag)
if err != nil {
log.Printf("blog.SinglePost(): %v", err)
g.ErrorPage(http.StatusServiceUnavailable)
}
timestamp, _ := time.Parse("2006-01-02 15:04:05-07", stamp)
g.Render("blog/onepost", &Post{id, timestamp, title, string(blackfriday.MarkdownCommon(body)), tag})
}
func Page(g *gas.Gas, page string) {
pageId, _ := strconv.Atoi(page)
posts := getPosts(g, pageId)
if len(posts) < 1 {
g.ErrorPage(http.StatusNotFound)
}
g.Render("blog/index", posts)
}
// TODO: add pagination
func Index(g *gas.Gas) {
// Note to self: use `SELECT setval('posts_id_seq', currval('posts_id_seq')-1);`
// when deleting a row so that the index gets set properly
g.Render("blog/index", getPosts(g, "0"))
}
func RSS(g *gas.Gas) {
g.ResponseWriter.Header().Set("Content-Type", "application/rss+xml")
g.Render("blog/rss", getPosts(g, "0"))
}