diff --git a/README.md b/README.md index 9ab262a..f9f782b 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,14 @@ # transmet -Quick fast dirty link store that can dump them to an html template +Quick fast personal link store that exports to a HTML template for quick posting to a blog + +Usecase: storing interesting news articles you come across during a week with at the moment notes/commentary and + then exporting in a currated organized format for immediate blog posting at your convience + +## Note + +As this is a personal project, some of the niceities like user managment and category managment +(that are one time tasks) are left to be done in SQL. I needed a tool to store links and export +to html so that's what I've focused on. # Install @@ -14,6 +23,8 @@ CREATE EXTENSION pgcrypto; go get bitbucket.org/liamstask/goose/cmd/goose +edit db/dbconf.yaml + goose up ## Build and run @@ -22,3 +33,13 @@ go build sudo cp transmet.conf /etc/init sudo service transmet start + +## Setup environment + +### Adding a user + +INSERT INTO users (username, password) VALUES('USERNAME', crypt('PASSWORD', gen_salt('bf'))); + +### Adding Categories + +INSERT INTO categories (name, parent_id) VALUES ('NAME', [null or PARENT_ID]); diff --git a/categories/categories.go b/categories/categories.go index 65d93d1..5f8d7f6 100644 --- a/categories/categories.go +++ b/categories/categories.go @@ -4,6 +4,7 @@ import ( "database/sql" _ "github.com/lib/pq" "fmt" + "strings" ) type Category struct { @@ -44,4 +45,19 @@ func LoadCategories(db *sql.DB) { CategoriesTree = append(CategoriesTree, category) } } +} + +// get the number of parents a category has +func (category *Category) Depth() int { + depth := 0 + current := category + for current.Parent.Valid { + current = CategoriesFlat[int(current.Parent.Int64)] + depth ++ + } + return depth +} + +func (category *Category) ToString() string { + return strings.Repeat("- ", category.Depth()) + category.Name } \ No newline at end of file diff --git a/news/news.go b/news/news.go index 6d5889a..a73a6d2 100644 --- a/news/news.go +++ b/news/news.go @@ -17,6 +17,12 @@ type News struct { Expoerted bool } +type NewsContainer struct { + Name string + News []News + Children map[int]*NewsContainer +} + func (news *News) Insert(db *sql.DB) error { _, 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 { @@ -26,15 +32,21 @@ func (news *News) Insert(db *sql.DB) error { return nil } -func Unexported(db *sql.DB) ([]News, error) { - res, err := db.Query("SELECT url, title, category_id, timestamp, notes FROM news WHERE exported=false") +func Unexported(db *sql.DB) (map[int]*NewsContainer, error) { + rows, err := db.Query("SELECT url, title, category_id, timestamp, notes FROM news WHERE exported=false") if err != nil { fmt.Println("DB errpr reading unexported news: ", err) return nil, err } - news := []News{} + newsTree := map[int]*NewsContainer{} + newsFlat := map[int]*NewsContainer{} + + for rows.Next() { + news := News{} + var url sql.NullString + err := rows.Scan(&news.Url, &news.Title, &news.Category_id, &news.Date, &news.Notes) + } - - return news, nil + return newsTree, nil } \ No newline at end of file diff --git a/route_handlers.go b/route_handlers.go index 286d73b..53049c9 100644 --- a/route_handlers.go +++ b/route_handlers.go @@ -135,7 +135,7 @@ func addFormHandler(w http.ResponseWriter, r *http.Request, user *user.User) { var url = r.URL.Query().Get("url") reHttp := regexp.MustCompile("^http://") - if ! reHttp.Match([]byte(url)) { + if url != "" && ! reHttp.Match([]byte(url)) { url = "http://" + url } resp, err := http.Get(url) diff --git a/templates/pages/add.html b/templates/pages/add.html index 69212f2..e0f9bce 100644 --- a/templates/pages/add.html +++ b/templates/pages/add.html @@ -21,7 +21,7 @@ {{define "option-category"}} - + {{range $child := .Children}} {{template "option-category" $child}} {{end}}