diff --git a/db/migrations/20150426212641_initial_schema.sql b/db/migrations/20150426212641_initial_schema.sql index 13ab108..3aed496 100644 --- a/db/migrations/20150426212641_initial_schema.sql +++ b/db/migrations/20150426212641_initial_schema.sql @@ -4,7 +4,7 @@ CREATE TABLE tags(tag_id SERIAL PRIMARY KEY, name varchar(255), parent_id INTEGER DEFAULT NULL); -CREATE TABLE news(news_id INTEGER NOT NULL, url varchar(255), title varchar(255), tag_id integer REFERENCES tags, timestamp timestamp PRIMARY KEY, description text); +CREATE TABLE news(news_id SERIAL, url varchar(255), title varchar(255), tag_id integer REFERENCES tags, timestamp timestamp PRIMARY KEY, description text); CREATE TABLE users(username VARCHAR(128) PRIMARY KEY, password VARCHAR(128)); diff --git a/db/migrations/20150509095706_news_default_now.sql b/db/migrations/20150509095706_news_default_now.sql new file mode 100644 index 0000000..ac3ab41 --- /dev/null +++ b/db/migrations/20150509095706_news_default_now.sql @@ -0,0 +1,9 @@ + +-- +goose Up +-- SQL in section 'Up' is executed when this migration is applied + +ALTER TABLE news ALTER COLUMN timestamp SET DEFAULT now(); + +-- +goose Down +-- SQL section 'Down' is executed when this migration is rolled back + diff --git a/news/news.go b/news/news.go new file mode 100644 index 0000000..35de3d5 --- /dev/null +++ b/news/news.go @@ -0,0 +1,26 @@ +package news + +import ( + "time" + "database/sql" + _ "github.com/lib/pq" + "fmt" +) + +type News struct { + news_id int + Url string + Title string + Tag_id int + Date time.Time + Description 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 ); + if err != nil { + fmt.Println("Error inserting news: ", err) + return err + } + return nil +} \ No newline at end of file diff --git a/route_handlers.go b/route_handlers.go index 273a589..717f43f 100644 --- a/route_handlers.go +++ b/route_handlers.go @@ -10,7 +10,9 @@ import ( "io/ioutil" "regexp" "strings" + "strconv" "github.com/dballard/transmet/tags" + "github.com/dballard/transmet/news" ) func GetFlashes(session *sessions.Session) map[string]interface{} { @@ -104,6 +106,23 @@ func LoginPostHandler(w http.ResponseWriter, r *http.Request) { // ?url= func addFormHandler(w http.ResponseWriter, r *http.Request, user *user.User) { tags.LoadTags(db) + + session, _ := store.Get(r, "c_user") + flashes := GetFlashes(session) + title := session.Values["title"] + delete(session.Values, "title") + link := session.Values["link"] + delete(session.Values, "link") + description := session.Values["description"] + 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}) + return + } + var url = r.URL.Query().Get("url") reHttp := regexp.MustCompile("^http://") if ! reHttp.Match([]byte(url)) { @@ -120,26 +139,45 @@ 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.TagsTree, "title": strings.TrimSpace(title[1])}) + ShowTemplate("add", w, map[string]interface{}{"user": user, "flashes": flashes, "link": url, "tags": tags.TagsTree, "title": strings.TrimSpace(title[1])}) return } } } - ShowTemplate("add", w, map[string]interface{}{"user": user, "link": url, "tags": tags.TagsTree}) + ShowTemplate("add", w, map[string]interface{}{"user": user, "link": url, "tags": tags.TagsTree, "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.Url = r.FormValue("link") + tag_id, err := strconv.Atoi(r.FormValue("tag")) + if err != nil { + tag_id = -1 + } + news.Tag_id = tag_id - //.Name = r.FormValue("name") - //desc := r.FormValue("desc") - //pid, perr := strconv.Atoi(r.FormValue("pid")) - - + 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.Save(r, w) + http.Redirect(w, r, "/add", http.StatusFound) + } else { + // TODO auto close? redirect + session.AddFlash("Added news \""+news.Title+"\"", flash_info) + session.Save(r, w) + http.Redirect(w, r, "/", http.StatusFound) + } } func templateFormHandler(w http.ResponseWriter, r *http.Request, user *user.User) { - + ShowTemplate("list", w, map[string]interface{}{"user": user}) } func templatePostHandler(w http.ResponseWriter, r *http.Request, user *user.User) { @@ -160,4 +198,4 @@ func init_route_handlers() { http.Handle("/", r) -} \ No newline at end of file +} diff --git a/templates/pages/add.html b/templates/pages/add.html index 8f563ba..a0e7cbf 100644 --- a/templates/pages/add.html +++ b/templates/pages/add.html @@ -5,14 +5,14 @@
Link:
Title:
-
Path:
- {{range $tag := .tags}} {{template "option-tag" $tag}} {{end}}
-
Description:
+
Description:
diff --git a/templates/pages/list.html b/templates/pages/list.html new file mode 100644 index 0000000..cbfda19 --- /dev/null +++ b/templates/pages/list.html @@ -0,0 +1,5 @@ +{{define "body"}} +

List

+{{template "flashes" .}} + +{{end}} \ No newline at end of file