From dddcf38d89bb148fe2faadce53c756436d55c626 Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Mon, 11 May 2015 22:06:28 -0700 Subject: [PATCH] renameing for UX and adding now works --- README.md | 2 + categories/categories.go | 47 +++++++++++++++++++ .../20150426212641_initial_schema.sql | 2 +- news/news.go | 6 +-- route_handlers.go | 30 +++++++----- tags/tags.go | 47 ------------------- templates/pages/add.html | 14 +++--- 7 files changed, 77 insertions(+), 71 deletions(-) create mode 100644 categories/categories.go delete mode 100644 tags/tags.go diff --git a/README.md b/README.md index 316d493..9ab262a 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,8 @@ Create postgress DB and put details in db/dbconf.yml on that DB +sudo apt-get install postgresql-contrib + CREATE EXTENSION pgcrypto; go get bitbucket.org/liamstask/goose/cmd/goose diff --git a/categories/categories.go b/categories/categories.go new file mode 100644 index 0000000..65d93d1 --- /dev/null +++ b/categories/categories.go @@ -0,0 +1,47 @@ +package categories + +import ( + "database/sql" + _ "github.com/lib/pq" + "fmt" +) + +type Category struct { + Id int + Name string + Parent sql.NullInt64 + Children []*Category +} + +var CategoriesTree []*Category +var CategoriesFlat map[int]*Category + +// Cavet: slight cheat. All parents must have Category_id < their children. +func LoadCategories(db *sql.DB) { + CategoriesTree = []*Category{} + CategoriesFlat = make(map[int]*Category) + + rows, err := db.Query("select categories.id, categories.name, categories.parent_id from categories order by id,Categories.parent_id desc") + if err != nil { + fmt.Println("DB Error loading Categories:", err) + return + } + + for rows.Next() { + category := &Category{Children: []*Category{}} + + err = rows.Scan(&category.Id, &category.Name, &category.Parent) + if err != nil { + fmt.Println("Categories DB Error: ", err) + } + + CategoriesFlat[category.Id] = category + + if category.Parent.Valid { + pid := int(category.Parent.Int64) + CategoriesFlat[pid].Children = append(CategoriesFlat[pid].Children, category) + } else { + CategoriesTree = append(CategoriesTree, category) + } + } +} \ No newline at end of file diff --git a/db/migrations/20150426212641_initial_schema.sql b/db/migrations/20150426212641_initial_schema.sql index 3aed496..3066f27 100644 --- a/db/migrations/20150426212641_initial_schema.sql +++ b/db/migrations/20150426212641_initial_schema.sql @@ -11,6 +11,6 @@ CREATE TABLE users(username VARCHAR(128) PRIMARY KEY, password VARCHAR(128)); -- +goose Down -- SQL section 'Down' is executed when this migration is rolled back -DROP TABLE tags; DROP TABLE news; +DROP TABLE tags; DROP TABLE users; diff --git a/news/news.go b/news/news.go index 35de3d5..366e10f 100644 --- a/news/news.go +++ b/news/news.go @@ -11,13 +11,13 @@ type News struct { news_id int Url string Title string - Tag_id int + Category_id int Date time.Time - Description string + Notes string } func (news *News) Insert(db *sql.DB) error { - _, err := db.Exec("INSERT INTO news (url, title, tag_id, description) VALUES($1, $2, $3, $4)", news.Url, news.Title, news.Tag_id, news.Description ); + _, 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 ); if err != nil { fmt.Println("Error inserting news: ", err) return err diff --git a/route_handlers.go b/route_handlers.go index 717f43f..1d1bf02 100644 --- a/route_handlers.go +++ b/route_handlers.go @@ -11,7 +11,7 @@ import ( "regexp" "strings" "strconv" - "github.com/dballard/transmet/tags" + "github.com/dballard/transmet/categories" "github.com/dballard/transmet/news" ) @@ -105,7 +105,7 @@ func LoginPostHandler(w http.ResponseWriter, r *http.Request) { // ?url= func addFormHandler(w http.ResponseWriter, r *http.Request, user *user.User) { - tags.LoadTags(db) + categories.LoadCategories(db) session, _ := store.Get(r, "c_user") flashes := GetFlashes(session) @@ -117,9 +117,10 @@ func addFormHandler(w http.ResponseWriter, r *http.Request, user *user.User) { delete(session.Values, "description") session.Save(r, w) - if link != "" { - //TODO tag_id - ShowTemplate("add", w, map[string]interface{}{"user": user, "flashes": flashes, "tags": tags.TagsTree, "link": link, "title": title, "description": description}) + if link != nil { + fmt.Println("link: '" + link.(string) + "'") + //TODO category_id + ShowTemplate("add", w, map[string]interface{}{"user": user, "flashes": flashes, "categories": categories.CategoriesTree, "link": link, "title": title, "description": description}) return } @@ -139,33 +140,33 @@ func addFormHandler(w http.ResponseWriter, r *http.Request, user *user.User) { re := regexp.MustCompile("< *[Tt][Ii][Tt][Ll][Ee] *>(.*)") title := re.FindStringSubmatch(string(body)) if title != nil { - ShowTemplate("add", w, map[string]interface{}{"user": user, "flashes": flashes, "link": url, "tags": tags.TagsTree, "title": strings.TrimSpace(title[1])}) + ShowTemplate("add", w, map[string]interface{}{"user": user, "flashes": flashes, "link": url, "categories": categories.CategoriesTree, "title": strings.TrimSpace(title[1])}) return } } } - ShowTemplate("add", w, map[string]interface{}{"user": user, "link": url, "tags": tags.TagsTree, "flashes": flashes}) + ShowTemplate("add", w, map[string]interface{}{"user": user, "link": url, "categories": categories.CategoriesTree, "flashes": flashes}) } func addPostHandler(w http.ResponseWriter, r *http.Request, user *user.User) { session, _ := store.Get(r, "c_user") var news news.News news.Title = r.FormValue("title") - news.Description = r.FormValue("description") + news.Notes = r.FormValue("notes") news.Url = r.FormValue("link") - tag_id, err := strconv.Atoi(r.FormValue("tag")) + category_id, err := strconv.Atoi(r.FormValue("category")) if err != nil { - tag_id = -1 + category_id = -1 } - news.Tag_id = tag_id + news.Category_id = category_id err = (&news).Insert(db) if err != nil { session.AddFlash("Error saving news: " + err.Error(), flash_err) session.Values["title"] = news.Title session.Values["link"] = news.Url - session.Values["description"] = news.Description + session.Values["notes"] = news.Notes session.Save(r, w) http.Redirect(w, r, "/add", http.StatusFound) } else { @@ -177,7 +178,10 @@ func addPostHandler(w http.ResponseWriter, r *http.Request, user *user.User) { } func templateFormHandler(w http.ResponseWriter, r *http.Request, user *user.User) { - ShowTemplate("list", w, map[string]interface{}{"user": user}) + session, _ := store.Get(r, "c_user") + flashes := GetFlashes(session) + session.Save(r, w) + ShowTemplate("list", w, map[string]interface{}{"user": user, "flashes": flashes}) } func templatePostHandler(w http.ResponseWriter, r *http.Request, user *user.User) { diff --git a/tags/tags.go b/tags/tags.go deleted file mode 100644 index 72f13bb..0000000 --- a/tags/tags.go +++ /dev/null @@ -1,47 +0,0 @@ -package tags - -import ( - "database/sql" - _ "github.com/lib/pq" - "fmt" -) - -type Tag struct { - Id int - Name string - Parent sql.NullInt64 - Children []*Tag -} - -var TagsTree []*Tag -var TagsFlat map[int]*Tag - -// Cavet: slight cheat. All parents must have tag_id < their children. -func LoadTags(db *sql.DB) { - TagsTree = []*Tag{} - TagsFlat = make(map[int]*Tag) - - rows, err := db.Query("select tags.tag_id, tags.name, tags.parent_id from tags order by tag_id,tags.parent_id desc") - if err != nil { - fmt.Println("DB Error loading tags:", err) - return - } - - for rows.Next() { - tag := &Tag{Children: []*Tag{}} - - err = rows.Scan(&tag.Id, &tag.Name, &tag.Parent) - if err != nil { - fmt.Println("tags DB Error: ", err) - } - - TagsFlat[tag.Id] = tag - - if tag.Parent.Valid { - pid := int(tag.Parent.Int64) - TagsFlat[pid].Children = append(TagsFlat[pid].Children, tag) - } else { - TagsTree = append(TagsTree, tag) - } - } -} \ No newline at end of file diff --git a/templates/pages/add.html b/templates/pages/add.html index a0e7cbf..69212f2 100644 --- a/templates/pages/add.html +++ b/templates/pages/add.html @@ -5,14 +5,14 @@
Link:
Title:
-
Tag:
- + {{range $category := .categories}} + {{template "option-category" $category}} {{end}}
-
Description:
+
Notes:
@@ -20,9 +20,9 @@ {{end}} -{{define "option-tag"}} +{{define "option-category"}} {{range $child := .Children}} - {{template "option-tag" $child}} + {{template "option-category" $child}} {{end}} {{end}} \ No newline at end of file