diff options
author | FChannel0 <77419041+FChannel0@users.noreply.github.com> | 2021-07-03 16:16:22 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-03 16:16:22 -0700 |
commit | 875ee4c53d79918ac87d1ce3e208ad27db6c0a90 (patch) | |
tree | fef9e3a94b09f356711fce887a42d405338dffb8 /database.go | |
parent | 9bd4e03d926c045dc82ce844cb02b078843ce787 (diff) | |
parent | fc8b4d6517213a371e26938ff2c0319d5e5f459a (diff) |
Merge pull request #27 from knotteye/master
Home page improvements and news system
Diffstat (limited to 'database.go')
-rw-r--r-- | database.go | 75 |
1 files changed, 73 insertions, 2 deletions
diff --git a/database.go b/database.go index 9d5d721..6a871ba 100644 --- a/database.go +++ b/database.go @@ -9,9 +9,9 @@ import "strings" import "sort" 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 +1488,74 @@ func MarkObjectSensitive(db *sql.DB, id string, sensitive bool) { CheckError(err, "error updating sensitive object in cacheactivitystream") } + +//if limit less than 1 return all news items +func getNewsFromDB(db *sql.DB, limit int) []NewsItem { + var news []NewsItem + + var query string + 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` + } + + var rows *sql.Rows + var err error + if(limit > 0) { + rows, err = db.Query(query, limit) + } else { + rows, err = db.Query(query) + } + + + if CheckError(err, "could not get news from db query") != nil { + return news + } + + 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 news + } + news = append(news, n) + } + + return news +} + +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 deleteNewsItemFromDB(db *sql.DB, timestamp int) { + query := `delete from newsItem where time=$1` + db.Exec(query, timestamp) +} + +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") +} |