load tags, start showing in template drop down
This commit is contained in:
parent
2d6a58f5e7
commit
605fc93409
|
@ -10,6 +10,7 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
"github.com/dballard/transmet/tags"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetFlashes(session *sessions.Session) map[string]interface{} {
|
func GetFlashes(session *sessions.Session) map[string]interface{} {
|
||||||
|
@ -102,6 +103,7 @@ func LoginPostHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
// ?url=
|
// ?url=
|
||||||
func addFormHandler(w http.ResponseWriter, r *http.Request, user *user.User) {
|
func addFormHandler(w http.ResponseWriter, r *http.Request, user *user.User) {
|
||||||
|
tags.LoadTags(db)
|
||||||
var url = r.URL.Query().Get("url")
|
var url = r.URL.Query().Get("url")
|
||||||
reHttp := regexp.MustCompile("^http://")
|
reHttp := regexp.MustCompile("^http://")
|
||||||
if ! reHttp.Match([]byte(url)) {
|
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] *>(.*)</ *[Tt][Ii][Tt][Ll][Ee] *>")
|
re := regexp.MustCompile("< *[Tt][Ii][Tt][Ll][Ee] *>(.*)</ *[Tt][Ii][Tt][Ll][Ee] *>")
|
||||||
title := re.FindStringSubmatch(string(body))
|
title := re.FindStringSubmatch(string(body))
|
||||||
if title != nil {
|
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
|
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) {
|
func addPostHandler(w http.ResponseWriter, r *http.Request, user *user.User) {
|
||||||
|
|
17
tags/tags.go
17
tags/tags.go
|
@ -10,22 +10,26 @@ import (
|
||||||
|
|
||||||
type Tag struct {
|
type Tag struct {
|
||||||
Name string
|
Name string
|
||||||
Parent int
|
Parent sql.NullInt64
|
||||||
Children []int
|
Children []int
|
||||||
}
|
}
|
||||||
|
|
||||||
var Tags map[int]*Tag
|
var Tags map[int]*Tag
|
||||||
|
|
||||||
func LoadTags(db *sql.DB) {
|
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() {
|
for rows.Next() {
|
||||||
tag := &Tag{Children: nil}
|
tag := &Tag{Children: nil}
|
||||||
|
|
||||||
var children sql.NullString
|
var children sql.NullString
|
||||||
var tag_id int
|
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 {
|
if err != nil {
|
||||||
fmt.Println("tags DB Error: ", err)
|
fmt.Println("tags DB Error: ", err)
|
||||||
}
|
}
|
||||||
|
@ -36,12 +40,11 @@ func LoadTags(db *sql.DB) {
|
||||||
pids := strings.Split(childrenStr, ",")
|
pids := strings.Split(childrenStr, ",")
|
||||||
for _, spid := range pids {
|
for _, spid := range pids {
|
||||||
pid, _ := strconv.Atoi(spid)
|
pid, _ := strconv.Atoi(spid)
|
||||||
|
tag.Children = append(tag.Children, pid)
|
||||||
tags.C
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
tags[tag_d] = tag
|
Tags[tag_id] = tag
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -5,10 +5,21 @@
|
||||||
<div class="row">
|
<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">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>
|
<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>
|
||||||
<div class="col-xs-2">Path:</div><div class="col-xs-10"><input type="text" class="form-control" name="path" placeholder="Path"/></div>
|
<div class="col-xs-2">Path:</div><div class="col-xs-10">
|
||||||
|
<select class="form-control" name="path" placeholder="Path">
|
||||||
|
{{range $tag := .tags}}
|
||||||
|
{{template "option-tag" $tag}}
|
||||||
|
{{end}}
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
<div class="col-xs-2">Description:</div><div class="col-xs-10"><textarea class="form-control" name="description" placeholder="Description" rows="3" cols="80">{{.task.Description}}</textarea></div>
|
<div class="col-xs-2">Description:</div><div class="col-xs-10"><textarea class="form-control" name="description" placeholder="Description" rows="3" cols="80">{{.task.Description}}</textarea></div>
|
||||||
<div class="col-xs-2"></div><div class="col-xs-10"><input class="btn btn-lg btn-primary btn-block" type="submit" value="Add Link" /></div>
|
<div class="col-xs-2"></div><div class="col-xs-10"><input class="btn btn-lg btn-primary btn-block" type="submit" value="Add Link" /></div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
</form>
|
</form>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
||||||
|
|
||||||
|
{{define "option-tag"}}
|
||||||
|
<option>{{.Name}}</option>
|
||||||
|
{{end}}
|
Loading…
Reference in New Issue