gofmt, delete works
This commit is contained in:
parent
8f9969b4cc
commit
bc7fcdde54
|
@ -2,10 +2,10 @@ package categories
|
|||
|
||||
import (
|
||||
"database/sql"
|
||||
_ "github.com/lib/pq"
|
||||
"fmt"
|
||||
"strings"
|
||||
"errors"
|
||||
"fmt"
|
||||
_ "github.com/lib/pq"
|
||||
"strings"
|
||||
)
|
||||
|
||||
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)
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Println("Categories DB Error Add(): " , err)
|
||||
fmt.Println("Categories DB Error Add(): ", err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
@ -86,7 +86,6 @@ func Delete(db *sql.DB, id int) error {
|
|||
return err
|
||||
}
|
||||
|
||||
|
||||
_, err = db.Exec("UPDATE news SET category_id =$2 WHERE category_id=$1", id, parent_id)
|
||||
if err != nil {
|
||||
fmt.Println("Categories DB error changing category of news: ", err)
|
||||
|
@ -120,7 +119,7 @@ func (category *Category) Depth() int {
|
|||
current := category
|
||||
for current.Parent.Valid {
|
||||
current = CategoriesFlat[int(current.Parent.Int64)]
|
||||
depth ++
|
||||
depth++
|
||||
}
|
||||
return depth
|
||||
}
|
||||
|
|
3
main.go
3
main.go
|
@ -3,12 +3,12 @@ package main
|
|||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/gorilla/sessions"
|
||||
_ "github.com/lib/pq"
|
||||
"net/http"
|
||||
"os"
|
||||
"flag"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -73,7 +73,6 @@ func main() {
|
|||
envFlag := flag.String("env", "local", "load config/{env}.json")
|
||||
flag.Parse()
|
||||
|
||||
|
||||
fmt.Println("Loading...")
|
||||
|
||||
loadConfig(*envFlag)
|
||||
|
|
15
news/news.go
15
news/news.go
|
@ -1,12 +1,11 @@
|
|||
package news
|
||||
|
||||
import (
|
||||
"time"
|
||||
"database/sql"
|
||||
_ "github.com/lib/pq"
|
||||
"fmt"
|
||||
"github.com/dballard/transmet/categories"
|
||||
|
||||
_ "github.com/lib/pq"
|
||||
"time"
|
||||
)
|
||||
|
||||
type News struct {
|
||||
|
@ -34,7 +33,7 @@ type NewsContainer struct {
|
|||
|
||||
// Insert News item into DB
|
||||
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 {
|
||||
fmt.Println("Error inserting news: ", err)
|
||||
return err
|
||||
|
@ -43,6 +42,11 @@ func (news *News) Insert(db *sql.DB) 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
|
||||
}
|
||||
|
@ -57,7 +61,7 @@ func nullStringToString(str *sql.NullString) string {
|
|||
|
||||
// Init and add a news container to the Data Structs
|
||||
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
|
||||
parent := categories.CategoriesFlat[category_id].Parent
|
||||
if parent.Valid {
|
||||
|
@ -121,7 +125,6 @@ func convertSqlToNews(rows *sql.Rows) ([]*News, int, error) {
|
|||
continue // needs a category id
|
||||
}
|
||||
|
||||
|
||||
news = append(news, newsItem)
|
||||
count++
|
||||
}
|
||||
|
|
|
@ -1,21 +1,21 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/gorilla/sessions"
|
||||
"net/http"
|
||||
"github.com/dballard/transmet/user"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"time"
|
||||
"io/ioutil"
|
||||
"regexp"
|
||||
"strings"
|
||||
"strconv"
|
||||
"github.com/dballard/transmet/categories"
|
||||
"github.com/dballard/transmet/news"
|
||||
"bytes"
|
||||
txtTemplate "text/template"
|
||||
"github.com/dballard/transmet/user"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/gorilla/sessions"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"path"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
txtTemplate "text/template"
|
||||
"time"
|
||||
)
|
||||
|
||||
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.Save(r, w)
|
||||
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)
|
||||
} else {
|
||||
|
@ -148,7 +148,7 @@ func addFormHandler(w http.ResponseWriter, r *http.Request, user *user.User, ses
|
|||
|
||||
var url = r.URL.Query().Get("url")
|
||||
reHttp := regexp.MustCompile("^https?://")
|
||||
if url != "" && ! reHttp.Match([]byte(url)) {
|
||||
if url != "" && !reHttp.Match([]byte(url)) {
|
||||
url = "http://" + url
|
||||
}
|
||||
|
||||
|
@ -177,7 +177,7 @@ func addPostHandler(w http.ResponseWriter, r *http.Request, user *user.User, ses
|
|||
|
||||
err = (&news).Insert(db)
|
||||
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["link"] = news.Url
|
||||
session.Values["notes"] = news.Notes
|
||||
|
@ -208,7 +208,7 @@ func templateFormHandler(w http.ResponseWriter, r *http.Request, user *user.User
|
|||
var templateBuf bytes.Buffer
|
||||
template, err := txtTemplate.ParseFiles("templates/html_template.txt")
|
||||
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})
|
||||
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)
|
||||
}
|
||||
|
||||
|
||||
func categoriesFormHandler(w http.ResponseWriter, r *http.Request, user *user.User, session *sessions.Session) {
|
||||
flashes := GetFlashes(session)
|
||||
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) {
|
||||
fname := path.Base(req.URL.Path)
|
||||
http.ServeFile(res, req, "./"+fname)
|
||||
}
|
||||
}
|
||||
|
||||
func init_route_handlers() {
|
||||
http.Handle("/js/", http.StripPrefix("/js/", http.FileServer(http.Dir("js/"))))
|
||||
|
|
21
templates.go
21
templates.go
|
@ -1,15 +1,15 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/dballard/transmet/categories"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"net/http"
|
||||
"errors"
|
||||
"time"
|
||||
"strings"
|
||||
"github.com/dballard/transmet/categories"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -17,12 +17,12 @@ var (
|
|||
)
|
||||
|
||||
// template helper function
|
||||
func dict (values ...interface{}) (map[string]interface{}, error) {
|
||||
func dict(values ...interface{}) (map[string]interface{}, error) {
|
||||
if len(values)%2 != 0 {
|
||||
return nil, errors.New("invalid dict call")
|
||||
}
|
||||
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)
|
||||
if !ok {
|
||||
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"
|
||||
func stringTimes(times int, str string) string {
|
||||
result := ""
|
||||
for i := 0; i < times; i ++ {
|
||||
for i := 0; i < times; i++ {
|
||||
result += str
|
||||
}
|
||||
return result
|
||||
|
@ -65,9 +65,9 @@ func truncate(str string, maxLen int) string {
|
|||
}
|
||||
|
||||
// Tempalte helper functions
|
||||
var funcMap = template.FuncMap {
|
||||
"add": func (x, y int) int { return x + y },
|
||||
"minus": func (x, y int) int { return x - y },
|
||||
var funcMap = template.FuncMap{
|
||||
"add": func(x, y int) int { return x + y },
|
||||
"minus": func(x, y int) int { return x - y },
|
||||
"dict": dict,
|
||||
"stringTimes": stringTimes,
|
||||
"dateFormat": dateFormat,
|
||||
|
@ -75,7 +75,6 @@ var funcMap = template.FuncMap {
|
|||
"truncate": truncate,
|
||||
}
|
||||
|
||||
|
||||
func initTemplates() {
|
||||
files, _ := filepath.Glob("templates/pages/*.html")
|
||||
re := regexp.MustCompile("templates/pages/(.*).html")
|
||||
|
|
Loading…
Reference in New Issue