gofmt, delete works

This commit is contained in:
Dan Ballard 2015-09-13 14:57:22 -07:00
parent 8f9969b4cc
commit bc7fcdde54
6 changed files with 392 additions and 393 deletions

View File

@ -2,10 +2,10 @@ package categories
import ( import (
"database/sql" "database/sql"
_ "github.com/lib/pq"
"fmt"
"strings"
"errors" "errors"
"fmt"
_ "github.com/lib/pq"
"strings"
) )
type Category struct { type Category struct {
@ -57,7 +57,7 @@ func Add(db *sql.DB, name string, parent int) error {
_, err = db.Exec("INSERT INTO categories (name, parent_id) VALUES ($1, $2)", name, parent) _, err = db.Exec("INSERT INTO categories (name, parent_id) VALUES ($1, $2)", name, parent)
} }
if err != nil { if err != nil {
fmt.Println("Categories DB Error Add(): " , err) fmt.Println("Categories DB Error Add(): ", err)
} }
return err return err
} }
@ -86,7 +86,6 @@ func Delete(db *sql.DB, id int) error {
return err return err
} }
_, err = db.Exec("UPDATE news SET category_id =$2 WHERE category_id=$1", id, parent_id) _, err = db.Exec("UPDATE news SET category_id =$2 WHERE category_id=$1", id, parent_id)
if err != nil { if err != nil {
fmt.Println("Categories DB error changing category of news: ", err) fmt.Println("Categories DB error changing category of news: ", err)
@ -120,7 +119,7 @@ func (category *Category) Depth() int {
current := category current := category
for current.Parent.Valid { for current.Parent.Valid {
current = CategoriesFlat[int(current.Parent.Int64)] current = CategoriesFlat[int(current.Parent.Int64)]
depth ++ depth++
} }
return depth return depth
} }

View File

@ -3,12 +3,12 @@ package main
import ( import (
"database/sql" "database/sql"
"encoding/json" "encoding/json"
"flag"
"fmt" "fmt"
"github.com/gorilla/sessions" "github.com/gorilla/sessions"
_ "github.com/lib/pq" _ "github.com/lib/pq"
"net/http" "net/http"
"os" "os"
"flag"
) )
const ( const (
@ -73,7 +73,6 @@ func main() {
envFlag := flag.String("env", "local", "load config/{env}.json") envFlag := flag.String("env", "local", "load config/{env}.json")
flag.Parse() flag.Parse()
fmt.Println("Loading...") fmt.Println("Loading...")
loadConfig(*envFlag) loadConfig(*envFlag)

View File

@ -1,12 +1,11 @@
package news package news
import ( import (
"time"
"database/sql" "database/sql"
_ "github.com/lib/pq"
"fmt" "fmt"
"github.com/dballard/transmet/categories" "github.com/dballard/transmet/categories"
_ "github.com/lib/pq"
"time"
) )
type News struct { type News struct {
@ -34,7 +33,7 @@ type NewsContainer struct {
// Insert News item into DB // Insert News item into DB
func (news *News) Insert(db *sql.DB) error { 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 ); _, 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 { if err != nil {
fmt.Println("Error inserting news: ", err) fmt.Println("Error inserting news: ", err)
return err return err
@ -43,6 +42,11 @@ func (news *News) Insert(db *sql.DB) error {
} }
func Delete(db *sql.DB, id int) error { func Delete(db *sql.DB, id int) error {
_, err := db.Exec("DELETE FROM news WHERE id = $1", id)
if err != nil {
fmt.Println("Error deleting news: ", err)
return err
}
return nil return nil
} }
@ -57,7 +61,7 @@ func nullStringToString(str *sql.NullString) string {
// Init and add a news container to the Data Structs // Init and add a news container to the Data Structs
func addContainer(category_id int, flat, tree map[int]*NewsContainer) { func addContainer(category_id int, flat, tree map[int]*NewsContainer) {
container := &NewsContainer{ Category: categories.CategoriesFlat[category_id], Name: categories.CategoriesFlat[category_id].Name, News: []News{}, Children: map[int]*NewsContainer{} } container := &NewsContainer{Category: categories.CategoriesFlat[category_id], Name: categories.CategoriesFlat[category_id].Name, News: []News{}, Children: map[int]*NewsContainer{}}
flat[category_id] = container flat[category_id] = container
parent := categories.CategoriesFlat[category_id].Parent parent := categories.CategoriesFlat[category_id].Parent
if parent.Valid { if parent.Valid {
@ -121,7 +125,6 @@ func convertSqlToNews(rows *sql.Rows) ([]*News, int, error) {
continue // needs a category id continue // needs a category id
} }
news = append(news, newsItem) news = append(news, newsItem)
count++ count++
} }

View File

@ -1,21 +1,21 @@
package main package main
import ( import (
"github.com/gorilla/mux" "bytes"
"github.com/gorilla/sessions"
"net/http"
"github.com/dballard/transmet/user"
"fmt" "fmt"
"time"
"io/ioutil"
"regexp"
"strings"
"strconv"
"github.com/dballard/transmet/categories" "github.com/dballard/transmet/categories"
"github.com/dballard/transmet/news" "github.com/dballard/transmet/news"
"bytes" "github.com/dballard/transmet/user"
txtTemplate "text/template" "github.com/gorilla/mux"
"github.com/gorilla/sessions"
"io/ioutil"
"net/http"
"path" "path"
"regexp"
"strconv"
"strings"
txtTemplate "text/template"
"time"
) )
func GetFlashes(session *sessions.Session) map[string]interface{} { func GetFlashes(session *sessions.Session) map[string]interface{} {
@ -86,7 +86,7 @@ func LoginPostHandler(w http.ResponseWriter, r *http.Request) {
session.Values["username"] = user.Username session.Values["username"] = user.Username
session.Save(r, w) session.Save(r, w)
if r.URL.Query().Get("url") != "" { if r.URL.Query().Get("url") != "" {
http.Redirect(w, r, "/add?" + r.URL.RawQuery, http.StatusFound) http.Redirect(w, r, "/add?"+r.URL.RawQuery, http.StatusFound)
} }
http.Redirect(w, r, "/", http.StatusFound) http.Redirect(w, r, "/", http.StatusFound)
} else { } else {
@ -148,7 +148,7 @@ func addFormHandler(w http.ResponseWriter, r *http.Request, user *user.User, ses
var url = r.URL.Query().Get("url") var url = r.URL.Query().Get("url")
reHttp := regexp.MustCompile("^https?://") reHttp := regexp.MustCompile("^https?://")
if url != "" && ! reHttp.Match([]byte(url)) { if url != "" && !reHttp.Match([]byte(url)) {
url = "http://" + url url = "http://" + url
} }
@ -177,7 +177,7 @@ func addPostHandler(w http.ResponseWriter, r *http.Request, user *user.User, ses
err = (&news).Insert(db) err = (&news).Insert(db)
if err != nil { if err != nil {
session.AddFlash("Error saving news: " + err.Error(), flash_err) session.AddFlash("Error saving news: "+err.Error(), flash_err)
session.Values["title"] = news.Title session.Values["title"] = news.Title
session.Values["link"] = news.Url session.Values["link"] = news.Url
session.Values["notes"] = news.Notes session.Values["notes"] = news.Notes
@ -208,7 +208,7 @@ func templateFormHandler(w http.ResponseWriter, r *http.Request, user *user.User
var templateBuf bytes.Buffer var templateBuf bytes.Buffer
template, err := txtTemplate.ParseFiles("templates/html_template.txt") template, err := txtTemplate.ParseFiles("templates/html_template.txt")
if err != nil { if err != nil {
fmt.Println("Error processing html_tempalte:" , err) fmt.Println("Error processing html_tempalte:", err)
} }
err = template.Execute(&templateBuf, map[string]interface{}{"news": news}) err = template.Execute(&templateBuf, map[string]interface{}{"news": news})
if err != nil { if err != nil {
@ -254,7 +254,6 @@ func deleteHandler(w http.ResponseWriter, r *http.Request, user *user.User, sess
http.Redirect(w, r, "/", http.StatusFound) http.Redirect(w, r, "/", http.StatusFound)
} }
func categoriesFormHandler(w http.ResponseWriter, r *http.Request, user *user.User, session *sessions.Session) { func categoriesFormHandler(w http.ResponseWriter, r *http.Request, user *user.User, session *sessions.Session) {
flashes := GetFlashes(session) flashes := GetFlashes(session)
session.Save(r, w) session.Save(r, w)
@ -362,7 +361,7 @@ func newsFormHandler(w http.ResponseWriter, r *http.Request, user *user.User, se
func ServeFileHandler(res http.ResponseWriter, req *http.Request) { func ServeFileHandler(res http.ResponseWriter, req *http.Request) {
fname := path.Base(req.URL.Path) fname := path.Base(req.URL.Path)
http.ServeFile(res, req, "./"+fname) http.ServeFile(res, req, "./"+fname)
} }
func init_route_handlers() { func init_route_handlers() {
http.Handle("/js/", http.StripPrefix("/js/", http.FileServer(http.Dir("js/")))) http.Handle("/js/", http.StripPrefix("/js/", http.FileServer(http.Dir("js/"))))

View File

@ -1,15 +1,15 @@
package main package main
import ( import (
"errors"
"fmt" "fmt"
"github.com/dballard/transmet/categories"
"html/template" "html/template"
"net/http"
"path/filepath" "path/filepath"
"regexp" "regexp"
"net/http"
"errors"
"time"
"strings" "strings"
"github.com/dballard/transmet/categories" "time"
) )
var ( var (
@ -17,12 +17,12 @@ var (
) )
// template helper function // template helper function
func dict (values ...interface{}) (map[string]interface{}, error) { func dict(values ...interface{}) (map[string]interface{}, error) {
if len(values)%2 != 0 { if len(values)%2 != 0 {
return nil, errors.New("invalid dict call") return nil, errors.New("invalid dict call")
} }
dict := make(map[string]interface{}, len(values)/2) dict := make(map[string]interface{}, len(values)/2)
for i := 0; i < len(values); i+=2 { for i := 0; i < len(values); i += 2 {
key, ok := values[i].(string) key, ok := values[i].(string)
if !ok { if !ok {
return nil, errors.New("dict keys must be strings") return nil, errors.New("dict keys must be strings")
@ -36,7 +36,7 @@ func dict (values ...interface{}) (map[string]interface{}, error) {
// stringTimes(3, "Foo") => "FooFooFoo" // stringTimes(3, "Foo") => "FooFooFoo"
func stringTimes(times int, str string) string { func stringTimes(times int, str string) string {
result := "" result := ""
for i := 0; i < times; i ++ { for i := 0; i < times; i++ {
result += str result += str
} }
return result return result
@ -65,9 +65,9 @@ func truncate(str string, maxLen int) string {
} }
// Tempalte helper functions // Tempalte helper functions
var funcMap = template.FuncMap { var funcMap = template.FuncMap{
"add": func (x, y int) int { return x + y }, "add": func(x, y int) int { return x + y },
"minus": func (x, y int) int { return x - y }, "minus": func(x, y int) int { return x - y },
"dict": dict, "dict": dict,
"stringTimes": stringTimes, "stringTimes": stringTimes,
"dateFormat": dateFormat, "dateFormat": dateFormat,
@ -75,7 +75,6 @@ var funcMap = template.FuncMap {
"truncate": truncate, "truncate": truncate,
} }
func initTemplates() { func initTemplates() {
files, _ := filepath.Glob("templates/pages/*.html") files, _ := filepath.Glob("templates/pages/*.html")
re := regexp.MustCompile("templates/pages/(.*).html") re := regexp.MustCompile("templates/pages/(.*).html")