This repository has been archived on 2025-03-01. You can view files and clone it, but cannot push or open issues or pull requests.
trantor/database/news.go

50 lines
875 B
Go
Raw Normal View History

2014-06-29 19:41:29 -05:00
package database
import (
2014-08-31 15:09:59 -05:00
log "github.com/cihub/seelog"
2014-08-30 13:17:50 -05:00
"time"
2014-08-18 19:27:53 -05:00
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
2014-06-29 19:41:29 -05:00
)
const (
news_coll = "news"
)
type News struct {
Date time.Time
Text string
}
2014-08-31 15:09:59 -05:00
func indexNews(coll *mgo.Collection) {
idx := mgo.Index{
Key: []string{"-date"},
Background: true,
}
err := coll.EnsureIndex(idx)
if err != nil {
log.Error("Error indexing news: ", err)
}
}
2014-06-29 19:41:29 -05:00
func addNews(coll *mgo.Collection, text string) error {
var news News
news.Text = text
news.Date = time.Now()
return coll.Insert(news)
}
func getNews(coll *mgo.Collection, 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 := coll.Find(query).Sort("-date").Limit(num)
err = q.All(&news)
return
}