aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorknotteye <knotteye@airmail.cc>2021-07-02 13:56:38 -0500
committerknotteye <knotteye@airmail.cc>2021-07-02 13:56:38 -0500
commitad5f4e9ff7e7ab0abab4a964cbf1626d0cdc3365 (patch)
tree6b5439f05a8e70420f0b166d7629492b905ef9bc
parent3a1e1090b31772908ceed43380cd09bc41019270 (diff)
select a specific number of news items from the database
-rw-r--r--database.go32
1 files changed, 14 insertions, 18 deletions
diff --git a/database.go b/database.go
index 037fde2..7dc7f5c 100644
--- a/database.go
+++ b/database.go
@@ -7,7 +7,6 @@ import "time"
import "os"
import "strings"
import "sort"
-import "container/list"
func GetActorFromDB(db *sql.DB, id string) Actor {
var nActor Actor
@@ -1490,14 +1489,20 @@ func MarkObjectSensitive(db *sql.DB, id string, sensitive bool) {
CheckError(err, "error updating sensitive object in cacheactivitystream")
}
-func getNewsFromDB(db *sql.DB) []NewsItem {
- news := list.New()
- query :=`select title, content, time from newsItem order by time desc`
+//if limit less than 1 return all news items
+func getNewsFromDB(db *sql.DB, limit int) []NewsItem {
+ news := []NewsItem
+
+ if(limit > 0) {
+ query :=`select title, content, time from newsItem order by time desc limit $1`
+ } else {
+ query :=`select title, content, time from newsItem order by time desc`
+ }
- rows, err := db.Query(query)
+ rows, err := db.Query(query, limit)
if CheckError(err, "could not get news from db query") != nil {
- return make([]NewsItem, 0)
+ return news
}
defer rows.Close()
@@ -1505,21 +1510,12 @@ func getNewsFromDB(db *sql.DB) []NewsItem {
n := NewsItem{}
err = rows.Scan(&n.Title, &n.Content, &n.Time)
if CheckError(err, "error scanning news from db") != nil {
- return make([]NewsItem, 0)
+ return news
}
- news.PushBack(n)
+ append(news, n)
}
- anews := make([]NewsItem, news.Len())
-
- i:=0
- for e := news.Front(); e != nil; e = e.Next() {
- assert := e.Value.(NewsItem)
- anews[i] = assert
- i++
- }
-
- return anews
+ return news
}
func getNewsItemFromDB(db *sql.DB, timestamp int) (NewsItem, error) {