This repository has been archived on 2025-03-01. You can view files and clone it, but cannot push or open issues or pull requests.
trantor/news.go
Las Zenow 41b376992a Database stops being a global variable
With it now every handler creates it's own copy of the session.
2013-09-23 16:27:31 +02:00

68 lines
1.2 KiB
Go

package main
import (
"net/http"
)
type newsData struct {
S Status
News []newsEntry
}
type newsEntry struct {
Date string
Text string
}
func newsHandler(h handler) {
err := h.r.ParseForm()
if err != nil {
http.Error(h.w, err.Error(), http.StatusInternalServerError)
return
}
var data newsData
data.S = GetStatus(h)
data.S.News = true
data.News = getNews(NUM_NEWS, 0, h.db)
format := h.r.Form["fmt"]
if (len(format) > 0) && (format[0] == "rss") {
loadTxtTemplate(h.w, "news_rss.xml", data)
} else {
loadTemplate(h.w, "news", data)
}
}
func editNewsHandler(h handler) {
if !h.sess.IsAdmin() {
notFound(h)
return
}
var data statusData
data.S = GetStatus(h)
data.S.News = true
loadTemplate(h.w, "edit_news", data)
}
func postNewsHandler(h handler) {
if !h.sess.IsAdmin() {
notFound(h)
return
}
text := h.r.FormValue("text")
h.db.AddNews(text)
http.Redirect(h.w, h.r, "/news/", http.StatusFound)
}
func getNews(num int, days int, db *DB) []newsEntry {
dbnews, _ := db.GetNews(num, days)
news := make([]newsEntry, len(dbnews))
for i, n := range dbnews {
news[i].Text = n.Text
news[i].Date = n.Date.Format("Jan 2, 2006")
}
return news
}