renameing for UX and adding now works
This commit is contained in:
		
							parent
							
								
									41936e57bb
								
							
						
					
					
						commit
						dddcf38d89
					
				|  | @ -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 | ||||
|  |  | |||
|  | @ -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) | ||||
| 		}     | ||||
| 	} | ||||
| } | ||||
|  | @ -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; | ||||
|  |  | |||
|  | @ -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 | ||||
|  |  | |||
|  | @ -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] *>(.*)</ *[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) { | ||||
|  |  | |||
							
								
								
									
										47
									
								
								tags/tags.go
								
								
								
								
							
							
						
						
									
										47
									
								
								tags/tags.go
								
								
								
								
							|  | @ -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) | ||||
| 		}     | ||||
| 	} | ||||
| } | ||||
|  | @ -5,14 +5,14 @@ | |||
| <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">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">Tag:</div><div class="col-xs-10"> | ||||
|         <select class="form-control" name="tag" placeholder="Tag"> | ||||
|         {{range $tag := .tags}} | ||||
|             {{template "option-tag" $tag}} | ||||
|     <div class="col-xs-2">Category:</div><div class="col-xs-10"> | ||||
|         <select class="form-control" name="category" placeholder="Tag"> | ||||
|         {{range $category := .categories}} | ||||
|             {{template "option-category" $category}} | ||||
|         {{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">{{.description}}</textarea></div> | ||||
|     <div class="col-xs-2">Notes:</div><div class="col-xs-10"><textarea class="form-control" name="notes" placeholder="Notes"  rows="3" cols="80">{{.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> | ||||
| 
 | ||||
|  | @ -20,9 +20,9 @@ | |||
| {{end}} | ||||
| 
 | ||||
| 
 | ||||
| {{define "option-tag"}} | ||||
| {{define "option-category"}} | ||||
|     <option value="{{.Id}}">{{if .Parent.Valid}}-{{end}}{{.Name}}</option> | ||||
|     {{range $child := .Children}} | ||||
|         {{template "option-tag" $child}} | ||||
|         {{template "option-category" $child}} | ||||
|      {{end}}             | ||||
| {{end}} | ||||
		Loading…
	
		Reference in New Issue