-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblog.go
49 lines (37 loc) · 1.08 KB
/
blog.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
package controllers
import (
"net/http"
"github.com/blastbeatsandcode/blastbeatsandcode-website/models"
"github.com/blastbeatsandcode/blastbeatsandcode-website/utils"
"github.com/jinzhu/gorm"
)
/* BlogHandler serves the blog page */
func BlogHandler(w http.ResponseWriter, r *http.Request) {
db := utils.GetDB()
defer db.Close()
m := getBlogPosts(db)
err := tpl.Get("blog").ExecuteTemplate(w, "base-tpl", m)
checkErr(err)
defer db.Close()
}
func getBlogPosts(db *gorm.DB) map[string]interface{} {
posts := []models.BlogPost{}
rows, err := db.Raw("SELECT * FROM blog_posts ORDER BY post_id DESC").Rows() // (*sql.Rows, error)
checkErr(err)
defer rows.Close()
for rows.Next() {
var post models.BlogPost
db.ScanRows(rows, &post)
postID := post.PostID
postTitle := post.Title
postContent := post.Content
postDate := post.Date
postAuthor := post.Author
newPost := models.BlogPost{PostID: postID, Title: postTitle, Content: postContent, Date: postDate, Author: postAuthor}
posts = append(posts, newPost)
}
m := map[string]interface{}{
"Posts": posts,
}
return m
}