diff options
Diffstat (limited to 'database.go')
-rw-r--r-- | database.go | 66 |
1 files changed, 64 insertions, 2 deletions
diff --git a/database.go b/database.go index 9d5d721..037fde2 100644 --- a/database.go +++ b/database.go @@ -7,11 +7,12 @@ import "time" import "os" import "strings" import "sort" +import "container/list" func GetActorFromDB(db *sql.DB, id string) Actor { - var nActor Actor + var nActor Actor - query :=`select type, id, name, preferedusername, inbox, outbox, following, followers, restricted, summary, publickeypem from actor where id=$1` + query :=`select type, id, name, preferedusername, inbox, outbox, following, followers, restricted, summary, publickeypem from actor where id=$1` rows, err := db.Query(query, id) @@ -1488,3 +1489,64 @@ 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` + + rows, err := db.Query(query) + + if CheckError(err, "could not get news from db query") != nil { + return make([]NewsItem, 0) + } + + defer rows.Close() + for rows.Next() { + n := NewsItem{} + err = rows.Scan(&n.Title, &n.Content, &n.Time) + if CheckError(err, "error scanning news from db") != nil { + return make([]NewsItem, 0) + } + news.PushBack(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 +} + +func getNewsItemFromDB(db *sql.DB, timestamp int) (NewsItem, error) { + var news NewsItem + query := `select title, content, time from newsItem where time=$1 limit 1` + + rows, err := db.Query(query, timestamp) + + if err != nil { + return news, err + } + + defer rows.Close() + rows.Next() + err = rows.Scan(&news.Title, &news.Content, &news.Time) + + if err != nil { + return news, err + } + + return news, nil +} + +func WriteNewsToDB(db *sql.DB, news NewsItem) { + query := `insert into newsItem (title, content, time) values ($1, $2, $3)` + + _, err := db.Exec(query, news.Title, news.Content, time.Now().Unix()) + + CheckError(err, "error writing news item") +} |