Use the right date type for news
This commit is contained in:
parent
15e1802a00
commit
fd0b9cf53d
2 changed files with 17 additions and 6 deletions
|
@ -16,9 +16,7 @@ CREATE TABLE books (
|
|||
tsv tsvector
|
||||
);
|
||||
|
||||
--
|
||||
-- Books text search index
|
||||
--
|
||||
CREATE FUNCTION books_trigger() RETURNS trigger AS $$
|
||||
declare
|
||||
lang_config regconfig;
|
||||
|
@ -70,12 +68,14 @@ CREATE TRIGGER tsvectorupdate BEFORE INSERT OR UPDATE
|
|||
ON books FOR EACH ROW EXECUTE PROCEDURE books_trigger();
|
||||
CREATE INDEX books_idx ON books USING GIN (tsv);
|
||||
|
||||
|
||||
CREATE TABLE news (
|
||||
id serial unique,
|
||||
date time,
|
||||
date timestamp,
|
||||
text text
|
||||
);
|
||||
|
||||
|
||||
CREATE TABLE users (
|
||||
id serial unique,
|
||||
username varchar(255) unique,
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package database
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
@ -13,17 +14,27 @@ type New struct {
|
|||
|
||||
// AddNews creates a new entry
|
||||
func (db *pgDB) AddNews(text string) error {
|
||||
return db.addRawNews(text, time.Now())
|
||||
}
|
||||
|
||||
func (db *pgDB) addRawNews(text string, date time.Time) error {
|
||||
return db.sql.Create(&New{
|
||||
Text: text,
|
||||
Date: time.Now(),
|
||||
Date: date,
|
||||
})
|
||||
}
|
||||
|
||||
// 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).
|
||||
query := db.sql.Model(&news)
|
||||
|
||||
if days != 0 {
|
||||
daystr := fmt.Sprintf("%d days", days)
|
||||
query = query.Where("date > CURRENT_TIMESTAMP-?::interval", daystr)
|
||||
}
|
||||
|
||||
err := query.Limit(num).
|
||||
Order("date DESC").
|
||||
Select()
|
||||
return news, err
|
||||
|
|
Reference in a new issue