editing may not work (load mostly) but at least its all unbroken now

This commit is contained in:
Dan Ballard 2015-09-20 07:51:36 -07:00
parent 0263f55073
commit 9c127555eb
2 changed files with 94 additions and 48 deletions

View File

@ -6,6 +6,8 @@ import (
"github.com/dballard/transmet/categories"
_ "github.com/lib/pq"
"time"
"errors"
"strconv"
)
type News struct {
@ -18,6 +20,10 @@ type News struct {
Expoerted bool
}
const (
SQL_NEWS_FIELDS = "id, url, title, category_id, timestamp, notes, exported"
)
/* Storage Node containing:
* Name - categry name
* News children
@ -51,6 +57,25 @@ func Delete(db *sql.DB, id int) error {
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 {
if str.Valid {
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
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{}}
@ -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) {
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 {
fmt.Println("DB errpr reading LoadPage news: ", 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) {
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 {
fmt.Println("DB errpr reading unexported news: ", err)
return nil, 0, err
@ -102,29 +135,49 @@ func Unexported(db *sql.DB) (map[int]*NewsContainer, int, error) {
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, &notes, &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(&notes)
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) {
news := []*News{}
count := 0
for rows.Next() {
newsItem := &News{}
var url, title, notes sql.NullString
var category_id sql.NullInt64
err := rows.Scan(&newsItem.id, &url, &title, &category_id, &newsItem.Date, &notes)
newsItem, err := scanNews(rows)
if err != nil {
fmt.Println("Error reading news from DB: " + err.Error())
return nil, 0, err
}
newsItem.Url = nullStringToString(&url)
newsItem.Title = nullStringToString(&title)
newsItem.Notes = nullStringToString(&notes)
if category_id.Valid {
newsItem.Category_id = int(category_id.Int64)
} else {
continue // needs a category id
}
news = append(news, newsItem)
count++
}
@ -137,30 +190,17 @@ func convertSqlToNewsContainer(rows *sql.Rows) (map[int]*NewsContainer, int, err
newsFlat := map[int]*NewsContainer{}
count := 0
for rows.Next() {
news := News{}
var url, title, notes sql.NullString
var category_id sql.NullInt64
err := rows.Scan(&news.id, &url, &title, &category_id, &news.Date, &notes)
news, err := scanNews(rows)
if err != nil {
fmt.Println("Error reading news from DB: " + err.Error())
return nil, 0, err
}
news.Url = nullStringToString(&url)
news.Title = nullStringToString(&title)
news.Notes = nullStringToString(&notes)
if category_id.Valid {
news.Category_id = int(category_id.Int64)
} else {
continue // needs a category id
}
if _, ok := newsFlat[news.Category_id]; !ok {
addContainer(news.Category_id, newsFlat, newsTree)
}
container := newsFlat[news.Category_id]
container.News = append(container.News, news)
container.News = append(container.News, *news)
count++
}

View File

@ -16,7 +16,6 @@ import (
"strings"
txtTemplate "text/template"
"time"
"errors"
)
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) {
var news news.News
news := news.News{}
news.Title = r.FormValue("title")
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
err = (&news).Insert(db)
err = news.Insert(db)
if err != nil {
var flashes = make(map[string]interface{})
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})
} else {
// TODO auto close? redirect
session.AddFlash("Added news \""+news.Title+"\"", flash_info)
session.Save(r, w)
if popup == "1" {
@ -185,24 +183,32 @@ func editFormHandler(w http.ResponseWriter, r *http.Request, user *user.User, se
categories.LoadCategories(db)
flashes := GetFlashes(session)
session.Save(r, w)
id, idErr := strconv.Atoi(r.FormValue("id"))
if idErr != nil {
}
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})
session.AddFlash("Could not understand news id", flash_err)
session.Save(r, w)
http.Redirect(w, r, "/news", http.StatusFound)
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) {
var news news.News
news := news.News{}
news.Title = r.FormValue("title")
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
err = (&news).Insert(db)
err = news.Insert(db)
if err != nil {
session.AddFlash("Error saving news: "+err.Error(), flash_err)
session.Values["title"] = news.Title