almost save news and error handling for

This commit is contained in:
Dan Ballard 2015-05-10 14:55:21 -07:00
parent f7b74041d7
commit 41936e57bb
6 changed files with 91 additions and 13 deletions

View File

@ -4,7 +4,7 @@
CREATE TABLE tags(tag_id SERIAL PRIMARY KEY, name varchar(255), parent_id INTEGER DEFAULT NULL); 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)); CREATE TABLE users(username VARCHAR(128) PRIMARY KEY, password VARCHAR(128));

View File

@ -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

26
news/news.go Normal file
View File

@ -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
}

View File

@ -10,7 +10,9 @@ import (
"io/ioutil" "io/ioutil"
"regexp" "regexp"
"strings" "strings"
"strconv"
"github.com/dballard/transmet/tags" "github.com/dballard/transmet/tags"
"github.com/dballard/transmet/news"
) )
func GetFlashes(session *sessions.Session) map[string]interface{} { func GetFlashes(session *sessions.Session) map[string]interface{} {
@ -104,6 +106,23 @@ 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) 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") 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)) {
@ -120,26 +139,45 @@ 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, "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 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) { 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") err = (&news).Insert(db)
//desc := r.FormValue("desc") if err != nil {
//pid, perr := strconv.Atoi(r.FormValue("pid")) 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) { 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) { func templatePostHandler(w http.ResponseWriter, r *http.Request, user *user.User) {
@ -160,4 +198,4 @@ func init_route_handlers() {
http.Handle("/", r) http.Handle("/", r)
} }

View File

@ -5,14 +5,14 @@
<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"> <div class="col-xs-2">Tag:</div><div class="col-xs-10">
<select class="form-control" name="path" placeholder="Path"> <select class="form-control" name="tag" placeholder="Tag">
{{range $tag := .tags}} {{range $tag := .tags}}
{{template "option-tag" $tag}} {{template "option-tag" $tag}}
{{end}} {{end}}
</select> </select>
</div> </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">{{.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>

View File

@ -0,0 +1,5 @@
{{define "body"}}
<h2 class="form-add-heading">List</h2>
{{template "flashes" .}}
{{end}}