From 605fc93409568717969f77700fd878f4aef1792b Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Thu, 7 May 2015 08:31:18 -0700 Subject: [PATCH] load tags, start showing in template drop down --- route_handlers.go | 6 ++++-- tags/tags.go | 17 ++++++++++------- templates/pages/add.html | 13 ++++++++++++- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/route_handlers.go b/route_handlers.go index a2b163c..a708fbc 100644 --- a/route_handlers.go +++ b/route_handlers.go @@ -10,6 +10,7 @@ import ( "io/ioutil" "regexp" "strings" + "github.com/dballard/transmet/tags" ) func GetFlashes(session *sessions.Session) map[string]interface{} { @@ -102,6 +103,7 @@ func LoginPostHandler(w http.ResponseWriter, r *http.Request) { // ?url= func addFormHandler(w http.ResponseWriter, r *http.Request, user *user.User) { + tags.LoadTags(db) var url = r.URL.Query().Get("url") reHttp := regexp.MustCompile("^http://") if ! reHttp.Match([]byte(url)) { @@ -118,13 +120,13 @@ 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, "link": url, "title": strings.TrimSpace(title[1])}) + ShowTemplate("add", w, map[string]interface{}{"user": user, "link": url, "tags": tags.Tags, "title": strings.TrimSpace(title[1])}) return } } } - ShowTemplate("add", w, map[string]interface{}{"user": user, "link": url}) + ShowTemplate("add", w, map[string]interface{}{"user": user, "link": url, "tags": tags.Tags}) } func addPostHandler(w http.ResponseWriter, r *http.Request, user *user.User) { diff --git a/tags/tags.go b/tags/tags.go index 2a7880e..73c9dee 100644 --- a/tags/tags.go +++ b/tags/tags.go @@ -10,22 +10,26 @@ import ( type Tag struct { Name string - Parent int + Parent sql.NullInt64 Children []int } var Tags map[int]*Tag func LoadTags(db *sql.DB) { - rows, err := db.Query("select tag_id, name, parent_id, array_agg(distinct(parent_of.tag_id)) from tags join tags as parent_of on tags.parent_id=parent_of.tag_id group by tags.tag_id") - + Tags = make(map[int]*Tag) + rows, err := db.Query("select tags.tag_id, tags.name, tags.parent_id, array_agg(distinct(parent_of.tag_id)) as children from tags left join tags as parent_of on parent_of.parent_id=tags.tag_id group by tags.tag_id") + if err != nil { + fmt.Println("DB Error loading tags:", err) + return + } for rows.Next() { tag := &Tag{Children: nil} var children sql.NullString var tag_id int - err = rows.Scan(&tag_id, &tag.Name, &tag.Name, &tag.Parent, &children) + err = rows.Scan(&tag_id, &tag.Name, &tag.Parent, &children) if err != nil { fmt.Println("tags DB Error: ", err) } @@ -36,12 +40,11 @@ func LoadTags(db *sql.DB) { pids := strings.Split(childrenStr, ",") for _, spid := range pids { pid, _ := strconv.Atoi(spid) - - tags.C + tag.Children = append(tag.Children, pid) } } } - tags[tag_d] = tag + Tags[tag_id] = tag } } \ No newline at end of file diff --git a/templates/pages/add.html b/templates/pages/add.html index e98f6b5..5b5c915 100644 --- a/templates/pages/add.html +++ b/templates/pages/add.html @@ -5,10 +5,21 @@
Link:
Title:
-
Path:
+
Path:
+ +
Description:
+{{end}} + + +{{define "option-tag"}} + {{end}} \ No newline at end of file