make news populate id, add edit del links

This commit is contained in:
Dan Ballard 2015-08-30 20:48:55 -07:00
parent 470b011ded
commit fddf4b5045
2 changed files with 15 additions and 11 deletions

View File

@ -10,7 +10,7 @@ import (
)
type News struct {
news_id int
id int
Url string
Title string
Category_id int
@ -66,10 +66,10 @@ 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) {
func LoadPage(db *sql.DB, offset, amount int) ([]*News, int, error) {
categories.LoadCategories(db) // required by addContainer
rows, err := db.Query("SELECT url, title, category_id, timestamp, notes FROM news WHERE exported is null order by timestamp DESC")
rows, err := db.Query("SELECT id, url, title, category_id, timestamp, notes 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
@ -83,7 +83,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 url, title, category_id, timestamp, notes FROM news WHERE exported is null order by category_id ASC")
rows, err := db.Query("SELECT id, url, title, category_id, timestamp, notes 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
@ -93,14 +93,14 @@ func Unexported(db *sql.DB) (map[int]*NewsContainer, int, error) {
return convertSqlToNewsContainer(rows)
}
func convertSqlToNews(rows *sql.Rows) ([]News, int, error) {
news := []News{}
func convertSqlToNews(rows *sql.Rows) ([]*News, int, error) {
news := []*News{}
count := 0
for rows.Next() {
newsItem := News{}
newsItem := &News{}
var url, title, notes sql.NullString
var category_id sql.NullInt64
err := rows.Scan(&url, &title, &category_id, &newsItem.Date, &notes)
err := rows.Scan(&newsItem.id, &url, &title, &category_id, &newsItem.Date, &notes)
if err != nil {
fmt.Println("Error reading news from DB: " + err.Error())
return nil, 0, err
@ -132,7 +132,7 @@ func convertSqlToNewsContainer(rows *sql.Rows) (map[int]*NewsContainer, int, err
news := News{}
var url, title, notes sql.NullString
var category_id sql.NullInt64
err := rows.Scan(&url, &title, &category_id, &news.Date, &notes)
err := rows.Scan(&news.id, &url, &title, &category_id, &news.Date, &notes)
if err != nil {
fmt.Println("Error reading news from DB: " + err.Error())
return nil, 0, err
@ -173,4 +173,8 @@ func MarkExported(db *sql.DB) error {
fmt.Println("DB errror: news.MarkExported():", err)
}
return err
}
func (news *News) Id() int {
return news.id
}

View File

@ -28,12 +28,12 @@
<div class="col-xs-8">
<a href="{{.post.Url}}">{{truncate .post.Url 100}}</a>
</div>
<div class="col-xs-2">&nbsp;</div>
<div class="col-xs-2"><a href="/edit/{{.post.Id}}">Edit</a></div>
</div>
<div class="row">
<div class="col-xs-2">&nbsp;</div>
<div class="col-xs-8">{{truncate .post.Notes 500}}</div>
<div class="col-xs-2">&nbsp;</div>
<div class="col-xs-2"><a class="delete" href="/delete/{{.post.Id}}">Delete</a></div>
</div>
</div>
{{end}}