editing may not work (load mostly) but at least its all unbroken now
This commit is contained in:
parent
0263f55073
commit
9c127555eb
106
news/news.go
106
news/news.go
|
@ -6,6 +6,8 @@ import (
|
||||||
"github.com/dballard/transmet/categories"
|
"github.com/dballard/transmet/categories"
|
||||||
_ "github.com/lib/pq"
|
_ "github.com/lib/pq"
|
||||||
"time"
|
"time"
|
||||||
|
"errors"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
type News struct {
|
type News struct {
|
||||||
|
@ -18,6 +20,10 @@ type News struct {
|
||||||
Expoerted bool
|
Expoerted bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
SQL_NEWS_FIELDS = "id, url, title, category_id, timestamp, notes, exported"
|
||||||
|
)
|
||||||
|
|
||||||
/* Storage Node containing:
|
/* Storage Node containing:
|
||||||
* Name - categry name
|
* Name - categry name
|
||||||
* News children
|
* News children
|
||||||
|
@ -51,6 +57,25 @@ func Delete(db *sql.DB, id int) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Get(db *sql.DB, id int) (*News, error) {
|
||||||
|
rows, err := db.Query("SELECT " + SQL_NEWS_FIELDS + " FROM news WHERE id=$1", id)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error getting news (", id, "): ", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
var news *News
|
||||||
|
if rows.Next() {
|
||||||
|
news, err = scanNews(rows)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return news, err
|
||||||
|
}
|
||||||
|
|
||||||
func nullStringToString(str *sql.NullString) string {
|
func nullStringToString(str *sql.NullString) string {
|
||||||
if str.Valid {
|
if str.Valid {
|
||||||
return str.String
|
return str.String
|
||||||
|
@ -59,6 +84,14 @@ func nullStringToString(str *sql.NullString) string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func nullBoolToBool(b *sql.NullBool) bool {
|
||||||
|
if b.Valid {
|
||||||
|
return true
|
||||||
|
} else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Init and add a news container to the Data Structs
|
// Init and add a news container to the Data Structs
|
||||||
func addContainer(category_id int, flat, tree map[int]*NewsContainer) {
|
func addContainer(category_id int, flat, tree map[int]*NewsContainer) {
|
||||||
container := &NewsContainer{Category: categories.CategoriesFlat[category_id], Name: categories.CategoriesFlat[category_id].Name, News: []News{}, Children: map[int]*NewsContainer{}}
|
container := &NewsContainer{Category: categories.CategoriesFlat[category_id], Name: categories.CategoriesFlat[category_id].Name, News: []News{}, Children: map[int]*NewsContainer{}}
|
||||||
|
@ -78,7 +111,7 @@ func addContainer(category_id int, flat, tree map[int]*NewsContainer) {
|
||||||
func LoadPage(db *sql.DB, offset, amount int) ([]*News, int, error) {
|
func LoadPage(db *sql.DB, offset, amount int) ([]*News, int, error) {
|
||||||
categories.LoadCategories(db) // required by addContainer
|
categories.LoadCategories(db) // required by addContainer
|
||||||
|
|
||||||
rows, err := db.Query("SELECT id, url, title, category_id, timestamp, notes FROM news WHERE exported is null order by timestamp DESC")
|
rows, err := db.Query("SELECT "+SQL_NEWS_FIELDS+" FROM news WHERE exported is null order by timestamp DESC")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("DB errpr reading LoadPage news: ", err)
|
fmt.Println("DB errpr reading LoadPage news: ", err)
|
||||||
return nil, 0, err
|
return nil, 0, err
|
||||||
|
@ -92,7 +125,7 @@ func LoadPage(db *sql.DB, offset, amount int) ([]*News, int, error) {
|
||||||
func Unexported(db *sql.DB) (map[int]*NewsContainer, int, error) {
|
func Unexported(db *sql.DB) (map[int]*NewsContainer, int, error) {
|
||||||
categories.LoadCategories(db) // required by addContainer
|
categories.LoadCategories(db) // required by addContainer
|
||||||
|
|
||||||
rows, err := db.Query("SELECT id, url, title, category_id, timestamp, notes FROM news WHERE exported is null order by category_id ASC")
|
rows, err := db.Query("SELECT "+SQL_NEWS_FIELDS+" FROM news WHERE exported is null order by category_id ASC")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("DB errpr reading unexported news: ", err)
|
fmt.Println("DB errpr reading unexported news: ", err)
|
||||||
return nil, 0, err
|
return nil, 0, err
|
||||||
|
@ -102,29 +135,49 @@ func Unexported(db *sql.DB) (map[int]*NewsContainer, int, error) {
|
||||||
return convertSqlToNewsContainer(rows)
|
return convertSqlToNewsContainer(rows)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//id int
|
||||||
|
//Url string
|
||||||
|
//Title string
|
||||||
|
//Category_id int
|
||||||
|
//Date time.Time
|
||||||
|
//Notes string
|
||||||
|
//Expoerted bool
|
||||||
|
//SQL_NEWS_FIELDS = "id, url, title, category_id, timestamp, notes, exported"
|
||||||
|
func scanNews(rows *sql.Rows) (*News, error) {
|
||||||
|
news := &News{}
|
||||||
|
var url, title, notes sql.NullString
|
||||||
|
var category_id sql.NullInt64
|
||||||
|
var exported sql.NullBool
|
||||||
|
err := rows.Scan(&news.id, &url, &title, &category_id, &news.Date, ¬es, &exported)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error reading news from DB: " + err.Error())
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
news.Url = nullStringToString(&url)
|
||||||
|
news.Title = nullStringToString(&title)
|
||||||
|
news.Notes = nullStringToString(¬es)
|
||||||
|
news.Expoerted = nullBoolToBool(&exported)
|
||||||
|
|
||||||
|
if category_id.Valid {
|
||||||
|
news.Category_id = int(category_id.Int64)
|
||||||
|
} else {
|
||||||
|
return nil, errors.New("Error reading news item from DB, no category_id for " + strconv.Itoa(news.Id()))
|
||||||
|
}
|
||||||
|
|
||||||
|
return news, nil
|
||||||
|
}
|
||||||
|
|
||||||
func convertSqlToNews(rows *sql.Rows) ([]*News, int, error) {
|
func convertSqlToNews(rows *sql.Rows) ([]*News, int, error) {
|
||||||
news := []*News{}
|
news := []*News{}
|
||||||
count := 0
|
count := 0
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
newsItem := &News{}
|
newsItem, err := scanNews(rows)
|
||||||
var url, title, notes sql.NullString
|
|
||||||
var category_id sql.NullInt64
|
|
||||||
err := rows.Scan(&newsItem.id, &url, &title, &category_id, &newsItem.Date, ¬es)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error reading news from DB: " + err.Error())
|
|
||||||
return nil, 0, err
|
return nil, 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
newsItem.Url = nullStringToString(&url)
|
|
||||||
newsItem.Title = nullStringToString(&title)
|
|
||||||
newsItem.Notes = nullStringToString(¬es)
|
|
||||||
|
|
||||||
if category_id.Valid {
|
|
||||||
newsItem.Category_id = int(category_id.Int64)
|
|
||||||
} else {
|
|
||||||
continue // needs a category id
|
|
||||||
}
|
|
||||||
|
|
||||||
news = append(news, newsItem)
|
news = append(news, newsItem)
|
||||||
count++
|
count++
|
||||||
}
|
}
|
||||||
|
@ -137,30 +190,17 @@ func convertSqlToNewsContainer(rows *sql.Rows) (map[int]*NewsContainer, int, err
|
||||||
newsFlat := map[int]*NewsContainer{}
|
newsFlat := map[int]*NewsContainer{}
|
||||||
count := 0
|
count := 0
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
news := News{}
|
news, err := scanNews(rows)
|
||||||
var url, title, notes sql.NullString
|
|
||||||
var category_id sql.NullInt64
|
|
||||||
err := rows.Scan(&news.id, &url, &title, &category_id, &news.Date, ¬es)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error reading news from DB: " + err.Error())
|
|
||||||
return nil, 0, err
|
return nil, 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
news.Url = nullStringToString(&url)
|
|
||||||
news.Title = nullStringToString(&title)
|
|
||||||
news.Notes = nullStringToString(¬es)
|
|
||||||
|
|
||||||
if category_id.Valid {
|
|
||||||
news.Category_id = int(category_id.Int64)
|
|
||||||
} else {
|
|
||||||
continue // needs a category id
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, ok := newsFlat[news.Category_id]; !ok {
|
if _, ok := newsFlat[news.Category_id]; !ok {
|
||||||
addContainer(news.Category_id, newsFlat, newsTree)
|
addContainer(news.Category_id, newsFlat, newsTree)
|
||||||
}
|
}
|
||||||
container := newsFlat[news.Category_id]
|
container := newsFlat[news.Category_id]
|
||||||
container.News = append(container.News, news)
|
container.News = append(container.News, *news)
|
||||||
count++
|
count++
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,7 +16,6 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
txtTemplate "text/template"
|
txtTemplate "text/template"
|
||||||
"time"
|
"time"
|
||||||
"errors"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetFlashes(session *sessions.Session) map[string]interface{} {
|
func GetFlashes(session *sessions.Session) map[string]interface{} {
|
||||||
|
@ -151,7 +150,7 @@ func addFormHandler(w http.ResponseWriter, r *http.Request, user *user.User, ses
|
||||||
}
|
}
|
||||||
|
|
||||||
func addPostHandler(w http.ResponseWriter, r *http.Request, user *user.User, session *sessions.Session) {
|
func addPostHandler(w http.ResponseWriter, r *http.Request, user *user.User, session *sessions.Session) {
|
||||||
var news news.News
|
news := news.News{}
|
||||||
|
|
||||||
news.Title = r.FormValue("title")
|
news.Title = r.FormValue("title")
|
||||||
news.Notes = r.FormValue("notes")
|
news.Notes = r.FormValue("notes")
|
||||||
|
@ -163,13 +162,12 @@ func addPostHandler(w http.ResponseWriter, r *http.Request, user *user.User, ses
|
||||||
}
|
}
|
||||||
news.Category_id = category_id
|
news.Category_id = category_id
|
||||||
|
|
||||||
err = (&news).Insert(db)
|
err = news.Insert(db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
var flashes = make(map[string]interface{})
|
var flashes = make(map[string]interface{})
|
||||||
flashes["error"] = []string{ "Error saving news: "+err.Error() }
|
flashes["error"] = []string{ "Error saving news: "+err.Error() }
|
||||||
ShowTemplate("add", w, map[string]interface{}{"user": user, "flashes": flashes, "link": news.Url, "categories": categories.CategoriesTree, "title": news.Title, "popup": popup, "notes": news.Notes, "category_id": news.Category_id})
|
ShowTemplate("add", w, map[string]interface{}{"user": user, "flashes": flashes, "link": news.Url, "categories": categories.CategoriesTree, "title": news.Title, "popup": popup, "notes": news.Notes, "category_id": news.Category_id})
|
||||||
} else {
|
} else {
|
||||||
// TODO auto close? redirect
|
|
||||||
session.AddFlash("Added news \""+news.Title+"\"", flash_info)
|
session.AddFlash("Added news \""+news.Title+"\"", flash_info)
|
||||||
session.Save(r, w)
|
session.Save(r, w)
|
||||||
if popup == "1" {
|
if popup == "1" {
|
||||||
|
@ -185,24 +183,32 @@ func editFormHandler(w http.ResponseWriter, r *http.Request, user *user.User, se
|
||||||
categories.LoadCategories(db)
|
categories.LoadCategories(db)
|
||||||
|
|
||||||
flashes := GetFlashes(session)
|
flashes := GetFlashes(session)
|
||||||
session.Save(r, w)
|
|
||||||
|
|
||||||
id, idErr := strconv.Atoi(r.FormValue("id"))
|
id, idErr := strconv.Atoi(r.FormValue("id"))
|
||||||
if idErr != nil {
|
if idErr != nil {
|
||||||
|
session.AddFlash("Could not understand news id", flash_err)
|
||||||
}
|
session.Save(r, w)
|
||||||
|
http.Redirect(w, r, "/news", http.StatusFound)
|
||||||
newsItem := news.Get(id)
|
|
||||||
|
|
||||||
if link != nil {
|
|
||||||
//TODO category_id
|
|
||||||
ShowTemplate("add", w, map[string]interface{}{"user": user, "flashes": flashes, "categories": categories.CategoriesTree, "link": link, "title": title, "notes": notes, "popup": false, "category": category})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
newsItem, err := news.Get(db, id)
|
||||||
|
if err != nil {
|
||||||
|
session.AddFlash("Could not load news item " + strconv.Itoa(id), flash_err)
|
||||||
|
session.Save(r, w)
|
||||||
|
http.Redirect(w, r, "/news", http.StatusFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
session.Save(r, w)
|
||||||
|
ShowTemplate("add", w, map[string]interface{}{"user": user, "flashes": flashes, "categories": categories.CategoriesTree, "link": newsItem.Url, "title": newsItem.Title, "notes": newsItem.Notes, "popup": false, "category": newsItem.Category_id})
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func editPostHandler(w http.ResponseWriter, r *http.Request, user *user.User, session *sessions.Session) {
|
func editPostHandler(w http.ResponseWriter, r *http.Request, user *user.User, session *sessions.Session) {
|
||||||
var news news.News
|
news := news.News{}
|
||||||
|
|
||||||
news.Title = r.FormValue("title")
|
news.Title = r.FormValue("title")
|
||||||
news.Notes = r.FormValue("notes")
|
news.Notes = r.FormValue("notes")
|
||||||
|
@ -214,7 +220,7 @@ func editPostHandler(w http.ResponseWriter, r *http.Request, user *user.User, se
|
||||||
}
|
}
|
||||||
news.Category_id = category_id
|
news.Category_id = category_id
|
||||||
|
|
||||||
err = (&news).Insert(db)
|
err = news.Insert(db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
session.AddFlash("Error saving news: "+err.Error(), flash_err)
|
session.AddFlash("Error saving news: "+err.Error(), flash_err)
|
||||||
session.Values["title"] = news.Title
|
session.Values["title"] = news.Title
|
||||||
|
|
Loading…
Reference in New Issue