diff --git a/config.go b/config.go index 21f0612..f92f3a3 100644 --- a/config.go +++ b/config.go @@ -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/" diff --git a/database.go b/database.go index 9f96fd2..0dc0eec 100644 --- a/database.go +++ b/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 } diff --git a/news.go b/news.go index 640c46d..81ef543 100644 --- a/news.go +++ b/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 +} diff --git a/templates/index.html b/templates/index.html index ca8d66e..cd0f7a5 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,9 +1,10 @@ {{template "header.html" .S}} -
- - News! The about has being updated with pgp key and bitcoin address for donations. +{{range .News}} +
+ News! {{.Text}}
+{{end}}
diff --git a/trantor.go b/trantor.go index b44adb5..3c284f0 100644 --- a/trantor.go +++ b/trantor.go @@ -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) }