Print out the news in the front page
This commit is contained in:
parent
9a98cfd793
commit
54763b04df
5 changed files with 29 additions and 13 deletions
|
@ -30,6 +30,7 @@ const (
|
|||
SEARCH_ITEMS_PAGE = 20
|
||||
NEW_ITEMS_PAGE = 50
|
||||
NUM_NEWS = 10
|
||||
DAYS_NEWS_INDEXPAGE = 15
|
||||
|
||||
TEMPLATE_PATH = "templates/"
|
||||
CSS_PATH = "css/"
|
||||
|
|
11
database.go
11
database.go
|
@ -96,8 +96,15 @@ func (d *DB) AddNews(text string) error {
|
|||
return d.news.Insert(news)
|
||||
}
|
||||
|
||||
func (d *DB) GetNews(num int) (news []News, err error) {
|
||||
err = d.news.Find(bson.M{}).Sort("-date").Limit(num).All(&news)
|
||||
func (d *DB) GetNews(num int, days int) (news []News, err error) {
|
||||
query := bson.M{}
|
||||
if days != 0 {
|
||||
duration := time.Duration(-24*days) * time.Hour
|
||||
date := time.Now().Add(duration)
|
||||
query = bson.M{"date": bson.M{"$gt": date}}
|
||||
}
|
||||
q := d.news.Find(query).Sort("-date").Limit(num)
|
||||
err = q.All(&news)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
21
news.go
21
news.go
|
@ -6,10 +6,10 @@ import (
|
|||
|
||||
type newsData struct {
|
||||
S Status
|
||||
News []news
|
||||
News []newsEntry
|
||||
}
|
||||
|
||||
type news struct {
|
||||
type newsEntry struct {
|
||||
Date string
|
||||
Text string
|
||||
}
|
||||
|
@ -18,12 +18,7 @@ func newsHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
|
|||
var data newsData
|
||||
data.S = GetStatus(w, r)
|
||||
data.S.News = true
|
||||
newsEntries, _ := db.GetNews(NUM_NEWS)
|
||||
data.News = make([]news, len(newsEntries))
|
||||
for i, n := range newsEntries {
|
||||
data.News[i].Text = n.Text
|
||||
data.News[i].Date = n.Date.Format("Jan 31, 2006")
|
||||
}
|
||||
data.News = getNews(NUM_NEWS, 0)
|
||||
loadTemplate(w, "news", data)
|
||||
}
|
||||
|
||||
|
@ -49,3 +44,13 @@ func postNewsHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
|
|||
db.AddNews(text)
|
||||
http.Redirect(w, r, "/news/", http.StatusFound)
|
||||
}
|
||||
|
||||
func getNews(num int, days int) []newsEntry {
|
||||
dbnews, _ := db.GetNews(num, days)
|
||||
news := make([]newsEntry, len(dbnews))
|
||||
for i, n := range dbnews {
|
||||
news[i].Text = n.Text
|
||||
news[i].Date = n.Date.Format("Jan 31, 2006")
|
||||
}
|
||||
return news
|
||||
}
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
{{template "header.html" .S}}
|
||||
|
||||
<div class="alert alert-info">
|
||||
<button type="button" class="close" data-dismiss="alert">×</button>
|
||||
<strong>News!</strong> The <a href="/about/">about</a> has being updated with pgp key and bitcoin address for donations.
|
||||
{{range .News}}
|
||||
<div class="offset2 span8 alert alert-info">
|
||||
<a href="/news/"><strong>News!</strong></a> {{.Text}}
|
||||
</div>
|
||||
{{end}}
|
||||
|
||||
<div class="row">
|
||||
<div class="span8">
|
||||
|
|
|
@ -121,6 +121,7 @@ type indexData struct {
|
|||
DownloadedBooks []Book
|
||||
Count int
|
||||
Tags []string
|
||||
News []newsEntry
|
||||
}
|
||||
|
||||
func indexHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
|
||||
|
@ -132,6 +133,7 @@ func indexHandler(w http.ResponseWriter, r *http.Request, sess *Session) {
|
|||
data.Books, data.Count, _ = db.GetBooks(bson.M{"active": true}, 6)
|
||||
data.VisitedBooks, _ = db.GetVisitedBooks(6)
|
||||
data.DownloadedBooks, _ = db.GetDownloadedBooks(6)
|
||||
data.News = getNews(1, DAYS_NEWS_INDEXPAGE)
|
||||
loadTemplate(w, "index", data)
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue