getting close to edit working

This commit is contained in:
Dan Ballard 2015-09-21 08:26:31 -07:00
parent 9c127555eb
commit 4e632602f4
3 changed files with 87 additions and 68 deletions

View File

@ -37,6 +37,12 @@ type NewsContainer struct {
Children map[int]*NewsContainer
}
func New(id int) *News {
news := &News{}
news.id = id
return news
}
// Insert News item into DB
func (news *News) Insert(db *sql.DB) error {
_, err := db.Exec("INSERT INTO news (url, title, category_id, notes) VALUES($1, $2, $3, $4)", news.Url, news.Title, news.Category_id, news.Notes)
@ -76,6 +82,54 @@ func Get(db *sql.DB, id int) (*News, error) {
return news, err
}
// Load and return in NewsContainer format all the unexported news items
func LoadPage(db *sql.DB, offset, amount int) ([]*News, int, error) {
categories.LoadCategories(db) // required by addContainer
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
}
defer rows.Close()
return convertSqlToNews(rows)
}
// Load and return in NewsContainer format all the unexported news items
func Unexported(db *sql.DB) (map[int]*NewsContainer, int, error) {
categories.LoadCategories(db) // required by addContainer
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
}
defer rows.Close()
return convertSqlToNewsContainer(rows)
}
// Helper fn - formating - math
func (this *NewsContainer) HeaderDepth(start int) int {
return start + this.Category.Depth()
}
// Mark the current batch (news.exported is null) as exported in this batch (exported = now())
func MarkExported(db *sql.DB) error {
now := time.Now()
_, err := db.Exec("UPDATE news SET exported=$1 WHERE exported is null", now)
if err != nil {
fmt.Println("DB errror: news.MarkExported():", err)
}
return err
}
func (news *News) Id() int {
return news.id
}
func nullStringToString(str *sql.NullString) string {
if str.Valid {
return str.String
@ -107,42 +161,6 @@ func addContainer(category_id int, flat, tree map[int]*NewsContainer) {
}
}
// Load and return in NewsContainer format all the unexported news items
func LoadPage(db *sql.DB, offset, amount int) ([]*News, int, error) {
categories.LoadCategories(db) // required by addContainer
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
}
defer rows.Close()
return convertSqlToNews(rows)
}
// Load and return in NewsContainer format all the unexported news items
func Unexported(db *sql.DB) (map[int]*NewsContainer, int, error) {
categories.LoadCategories(db) // required by addContainer
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
}
defer rows.Close()
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
@ -207,22 +225,4 @@ func convertSqlToNewsContainer(rows *sql.Rows) (map[int]*NewsContainer, int, err
return newsTree, count, nil
}
// Helper fn - formating - math
func (this *NewsContainer) HeaderDepth(start int) int {
return start + this.Category.Depth()
}
// Mark the current batch (news.exported is null) as exported in this batch (exported = now())
func MarkExported(db *sql.DB) error {
now := time.Now()
_, err := db.Exec("UPDATE news SET exported=$1 WHERE exported is null", now)
if err != nil {
fmt.Println("DB errror: news.MarkExported():", err)
}
return err
}
func (news *News) Id() int {
return news.id
}

View File

@ -146,7 +146,7 @@ func addFormHandler(w http.ResponseWriter, r *http.Request, user *user.User, ses
popup := r.URL.Query().Get("popup")
ShowTemplate("add", w, map[string]interface{}{"user": user, "flashes": flashes, "link": url, "categories": categories.CategoriesTree, "title": title, "popup": popup})
ShowTemplate("post", w, map[string]interface{}{"user": user, "flashes": flashes, "link": url, "categories": categories.CategoriesTree, "title": title, "popup": popup})
}
func addPostHandler(w http.ResponseWriter, r *http.Request, user *user.User, session *sessions.Session) {
@ -158,7 +158,10 @@ func addPostHandler(w http.ResponseWriter, r *http.Request, user *user.User, ses
popup := r.FormValue("popup")
category_id, err := strconv.Atoi(r.FormValue("category"))
if err != nil {
category_id = -1
var flashes = make(map[string]interface{})
flashes["error"] = []string{ "Category required: " +err.Error() }
ShowTemplate("post", 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})
return
}
news.Category_id = category_id
@ -166,7 +169,8 @@ func addPostHandler(w http.ResponseWriter, r *http.Request, user *user.User, ses
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})
ShowTemplate("post", 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})
return
} else {
session.AddFlash("Added news \""+news.Title+"\"", flash_info)
session.Save(r, w)
@ -178,13 +182,11 @@ func addPostHandler(w http.ResponseWriter, r *http.Request, user *user.User, ses
}
}
//
func editFormHandler(w http.ResponseWriter, r *http.Request, user *user.User, session *sessions.Session) {
categories.LoadCategories(db)
flashes := GetFlashes(session)
id, idErr := strconv.Atoi(r.FormValue("id"))
if idErr != nil {
session.AddFlash("Could not understand news id", flash_err)
@ -201,23 +203,39 @@ func editFormHandler(w http.ResponseWriter, r *http.Request, user *user.User, se
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})
ShowTemplate("post", 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, "id": newsItem.Id()})
return
}
func editPostHandler(w http.ResponseWriter, r *http.Request, user *user.User, session *sessions.Session) {
news := news.News{}
news_id, err := strconv.Atoi(r.FormValue("id"))
if err != nil {
session.AddFlash("Error trying to save news item", flash_err)
session.Save(r, w)
http.Redirect(w, r, "/news", http.StatusFound)
return
}
news := news.New(news_id)
news.Title = r.FormValue("title")
news.Notes = r.FormValue("notes")
news.Url = r.FormValue("link")
popup := r.FormValue("popup")
category_id, err := strconv.Atoi(r.FormValue("category"))
if err != nil {
category_id = -1
var flashes = make(map[string]interface{})
flashes["error"] = []string{ "Category required: " +err.Error() }
ShowTemplate("post", 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})
return
}
news.Category_id = category_id
err = news.Insert(db)

View File

@ -2,7 +2,8 @@
<h2 class="form-add-heading">Add Link</h2>
{{template "flashes" .}}
<form class="form-add" action="/add" method="post" role="form" class="container col-form">
<input type="hidden" name="popup" value="{{.popup}}" />
<input type="hidden" name="popup" value="{{.popup}}" />
<input type="hidden" name="id" value="{{.id}}" />
<div class="row">
<div class="col-xs-2">Link:</div><div class="col-xs-10"><input type="text" class="form-control" name="link" placeholder="Link" value="{{.link}}"/></div>
<div class="col-xs-2">Title:</div><div class="col-xs-10"><input type="text" class="form-control" name="title" placeholder="Title" value="{{.title}}"/></div>
@ -10,7 +11,7 @@
<select class="form-control add-category" name="category" placeholder="Category">
<option value="-1">-- Pick a Category --</option>
{{range $category := .categories}}
{{template "option-category" $category}}
{{template "option-category" dict "category" $category "category_id" .category_id}}
{{end}}
</select>
</div>
@ -23,8 +24,8 @@
{{define "option-category"}}
<option value="{{.Id}}">{{.ToString}}</option>
{{range $child := .Children}}
{{template "option-category" $child}}
<option value="{{$category.Id}}" {{if eq $category.Id $category_id}}selected="true"{{end}} >{{$category.ToString}}</option>
{{range $child := $category.Children}}
{{template "option-category" dict "category" $child "category_id" .category_id}}
{{end}}
{{end}}