From f7b74041d7fa639d8ea6ba3e6bdffa5197b11c8a Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Fri, 8 May 2015 08:30:35 -0700 Subject: [PATCH] prep for saving post --- route_handlers.go | 9 +++++++-- tags/tags.go | 19 ++++++++++--------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/route_handlers.go b/route_handlers.go index a708fbc..273a589 100644 --- a/route_handlers.go +++ b/route_handlers.go @@ -120,17 +120,22 @@ 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, "tags": tags.Tags, "title": strings.TrimSpace(title[1])}) + ShowTemplate("add", w, map[string]interface{}{"user": user, "link": url, "tags": tags.TagsTree, "title": strings.TrimSpace(title[1])}) return } } } - ShowTemplate("add", w, map[string]interface{}{"user": user, "link": url, "tags": tags.Tags}) + ShowTemplate("add", w, map[string]interface{}{"user": user, "link": url, "tags": tags.TagsTree}) } func addPostHandler(w http.ResponseWriter, r *http.Request, user *user.User) { + //.Name = r.FormValue("name") + //desc := r.FormValue("desc") + //pid, perr := strconv.Atoi(r.FormValue("pid")) + + } func templateFormHandler(w http.ResponseWriter, r *http.Request, user *user.User) { diff --git a/tags/tags.go b/tags/tags.go index 17b6abe..72f13bb 100644 --- a/tags/tags.go +++ b/tags/tags.go @@ -13,18 +13,19 @@ type Tag struct { Children []*Tag } -var Tags []*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) { - Tags = []*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") + 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 - } - - var flatMap map[int]*Tag = make(map[int]*Tag) + } for rows.Next() { tag := &Tag{Children: []*Tag{}} @@ -34,13 +35,13 @@ func LoadTags(db *sql.DB) { fmt.Println("tags DB Error: ", err) } - flatMap[tag.Id] = tag + TagsFlat[tag.Id] = tag if tag.Parent.Valid { pid := int(tag.Parent.Int64) - flatMap[pid].Children = append(flatMap[pid].Children, tag) + TagsFlat[pid].Children = append(TagsFlat[pid].Children, tag) } else { - Tags = append(Tags, tag) + TagsTree = append(TagsTree, tag) } } } \ No newline at end of file