[WIP] migration to psql

TODO:
[ ] stats
[ ] indexes
This commit is contained in:
Las Zenow 2016-07-30 07:10:33 -04:00
parent e1bd235785
commit e72de38725
24 changed files with 648 additions and 936 deletions

View file

@ -1,49 +1,30 @@
package database
import (
log "github.com/cihub/seelog"
"time"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
const (
news_coll = "news"
)
type News struct {
// New entry in the news table
type New struct {
ID int
Date time.Time
Text string
}
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)
}
// AddNews creates a new entry
func (db *pgDB) AddNews(text string) error {
return db.sql.Create(&New{
Text: text,
Date: time.Now(),
})
}
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
// GetNews returns all the news for the last 'days' limiting with a maximum of 'num' results
func (db *pgDB) GetNews(num int, days int) ([]New, error) {
var news []New
err := db.sql.Model(&news).
Limit(num).
Order("date DESC").
Select()
return news, err
}