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

@ -1,18 +1,18 @@
package categories
import (
"database/sql"
_ "github.com/lib/pq"
"fmt"
"strings"
"database/sql"
"errors"
"fmt"
_ "github.com/lib/pq"
"strings"
)
type Category struct {
Id int
Name string
Parent sql.NullInt64
Children []*Category
Id int
Name string
Parent sql.NullInt64
Children []*Category
}
var CategoriesTree []*Category
@ -20,15 +20,15 @@ var CategoriesFlat map[int]*Category
// Cavet: slight cheat. All parents must have Category_id < their children.
func LoadCategories(db *sql.DB) {
CategoriesTree = []*Category{}
CategoriesFlat = make(map[int]*Category)
CategoriesTree = []*Category{}
CategoriesFlat = make(map[int]*Category)
rows, err := db.Query("select categories.id, categories.name, categories.parent_id from categories order by coalesce(parent_id, id), parent_id is not null")
if err != nil {
fmt.Println("DB Error loading Categories:", err)
return
}
defer rows.Close()
rows, err := db.Query("select categories.id, categories.name, categories.parent_id from categories order by coalesce(parent_id, id), parent_id is not null")
if err != nil {
fmt.Println("DB Error loading Categories:", err)
return
}
defer rows.Close()
for rows.Next() {
category := &Category{Children: []*Category{}}
@ -41,10 +41,10 @@ func LoadCategories(db *sql.DB) {
CategoriesFlat[category.Id] = category
if category.Parent.Valid {
pid := int(category.Parent.Int64)
CategoriesFlat[pid].Children = append(CategoriesFlat[pid].Children, category)
pid := int(category.Parent.Int64)
CategoriesFlat[pid].Children = append(CategoriesFlat[pid].Children, category)
} else {
CategoriesTree = append(CategoriesTree, category)
CategoriesTree = append(CategoriesTree, category)
}
}
}
@ -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
}
@ -67,65 +67,64 @@ func Add(db *sql.DB, name string, parent int) error {
// set category of news to parent
// finallly delete
func Delete(db *sql.DB, id int) error {
rows, err := db.Query("SELECT parent_id FROM categories WHERE id=$1", id)
if err != nil {
fmt.Println("Categories DB Error loading category parent id: ", err)
return err
}
rows, err := db.Query("SELECT parent_id FROM categories WHERE id=$1", id)
if err != nil {
fmt.Println("Categories DB Error loading category parent id: ", err)
return err
}
if !rows.Next() {
fmt.Println("Categories DB Error loading category parent id: no category")
return errors.New("No category")
}
if !rows.Next() {
fmt.Println("Categories DB Error loading category parent id: no category")
return errors.New("No category")
}
var parent_id sql.NullInt64
err = rows.Scan(&parent_id)
_, err = db.Exec("UPDATE categories SET parent_id =$2 WHERE parent_id=$1", id, parent_id)
if err != nil {
fmt.Println("Categories DB error changing child parent: ", err)
return err
}
_, err = db.Exec("UPDATE categories SET parent_id =$2 WHERE parent_id=$1", id, parent_id)
if err != nil {
fmt.Println("Categories DB error changing child parent: ", err)
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)
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)
return err
}
_, err = db.Exec("DELETE FROM categories WHERE id=$1", id)
if err != nil {
fmt.Println("Categories DB Error Delete(): ", err)
}
return err
_, err = db.Exec("DELETE FROM categories WHERE id=$1", id)
if err != nil {
fmt.Println("Categories DB Error Delete(): ", err)
}
return err
}
func (category *Category) ChangeParent(db *sql.DB, parent *Category) error {
var err error
if parent == nil {
_, err = db.Exec("UPDATE categories SET parent_id = NULL WHERE id = $1", category.Id)
} else {
_, err = db.Exec("UPDATE categories SET parent_id = $2 WHERE id = $1", category.Id, parent.Id)
}
var err error
if parent == nil {
_, err = db.Exec("UPDATE categories SET parent_id = NULL WHERE id = $1", category.Id)
} else {
_, err = db.Exec("UPDATE categories SET parent_id = $2 WHERE id = $1", category.Id, parent.Id)
}
if err != nil {
fmt.Println("Categories DB Error: ", err)
}
return err
if err != nil {
fmt.Println("Categories DB Error: ", err)
}
return err
}
// 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
depth := 0
current := category
for current.Parent.Valid {
current = CategoriesFlat[int(current.Parent.Int64)]
depth++
}
return depth
}
// Helper function for templates (add form select list)
func (category *Category) ToString() string {
return strings.Repeat("- ", category.Depth()) + category.Name
return strings.Repeat("- ", category.Depth()) + category.Name
}

13
main.go
View File

@ -3,16 +3,16 @@ package main
import (
"database/sql"
"encoding/json"
"flag"
"fmt"
"github.com/gorilla/sessions"
_ "github.com/lib/pq"
"net/http"
"os"
"flag"
)
const (
VERSION = "0.1"
VERSION = "0.1"
MAX_DB_CONNS = 10 // already excessive for personal app
)
@ -24,7 +24,7 @@ type Config struct {
Password string
}
Port string
Url string
Url string
}
const (
@ -33,9 +33,9 @@ const (
)
var (
config Config
db *sql.DB
store = sessions.NewCookieStore([]byte("key-store-auth-secret"))
config Config
db *sql.DB
store = sessions.NewCookieStore([]byte("key-store-auth-secret"))
)
func loadConfig(env string) {
@ -73,7 +73,6 @@ func main() {
envFlag := flag.String("env", "local", "load config/{env}.json")
flag.Parse()
fmt.Println("Loading...")
loadConfig(*envFlag)

View File

@ -1,22 +1,21 @@
package news
import (
"time"
"database/sql"
_ "github.com/lib/pq"
"database/sql"
"fmt"
"github.com/dballard/transmet/categories"
_ "github.com/lib/pq"
"time"
)
type News struct {
id int
Url string
Title string
Category_id int
Date time.Time
Notes string
Expoerted bool
id int
Url string
Title string
Category_id int
Date time.Time
Notes string
Expoerted bool
}
/* Storage Node containing:
@ -26,53 +25,58 @@ type News struct {
* Children - sub containers: mapped to sub categories
*/
type NewsContainer struct {
Name string
News []News
Category *categories.Category
Children map[int]*NewsContainer
Name string
News []News
Category *categories.Category
Children map[int]*NewsContainer
}
// 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 );
if err != nil {
fmt.Println("Error inserting news: ", err)
return err
}
return nil
_, 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
}
return nil
}
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
}
func nullStringToString(str *sql.NullString) string {
if str.Valid {
return str.String
} else {
return ""
}
if str.Valid {
return str.String
} else {
return ""
}
}
// 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{} }
flat[category_id] = container
parent := categories.CategoriesFlat[category_id].Parent
if parent.Valid {
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 {
if _, ok := flat[int(parent.Int64)]; !ok {
addContainer(int(parent.Int64), flat, tree)
addContainer(int(parent.Int64), flat, tree)
}
flat[int(parent.Int64)].Children[category_id] = container
} else {
tree[category_id] = container
}
} else {
tree[category_id] = container
}
}
// Load and return in NewsContainer format all the unexported news items
func LoadPage(db *sql.DB, offset, amount int) ([]*News, int, error) {
categories.LoadCategories(db) // required by addContainer
categories.LoadCategories(db) // required by addContainer
rows, err := db.Query("SELECT id, url, title, category_id, timestamp, notes FROM news WHERE exported is null order by timestamp DESC")
if err != nil {
@ -86,7 +90,7 @@ func LoadPage(db *sql.DB, offset, amount int) ([]*News, int, error) {
// Load and return in NewsContainer format all the unexported news items
func Unexported(db *sql.DB) (map[int]*NewsContainer, int, error) {
categories.LoadCategories(db) // required by addContainer
categories.LoadCategories(db) // required by addContainer
rows, err := db.Query("SELECT id, url, title, category_id, timestamp, notes FROM news WHERE exported is null order by category_id ASC")
if err != nil {
@ -102,28 +106,27 @@ func convertSqlToNews(rows *sql.Rows) ([]*News, int, error) {
news := []*News{}
count := 0
for rows.Next() {
newsItem := &News{}
var url, title, notes sql.NullString
var category_id sql.NullInt64
err := rows.Scan(&newsItem.id, &url, &title, &category_id, &newsItem.Date, &notes)
if err != nil {
fmt.Println("Error reading news from DB: " + err.Error())
return nil, 0, err
}
newsItem := &News{}
var url, title, notes sql.NullString
var category_id sql.NullInt64
err := rows.Scan(&newsItem.id, &url, &title, &category_id, &newsItem.Date, &notes)
if err != nil {
fmt.Println("Error reading news from DB: " + err.Error())
return nil, 0, err
}
newsItem.Url = nullStringToString(&url)
newsItem.Title = nullStringToString(&title)
newsItem.Notes = nullStringToString(&notes)
newsItem.Url = nullStringToString(&url)
newsItem.Title = nullStringToString(&title)
newsItem.Notes = nullStringToString(&notes)
if category_id.Valid {
newsItem.Category_id = int(category_id.Int64)
} else {
continue // needs a category id
}
if category_id.Valid {
newsItem.Category_id = int(category_id.Int64)
} else {
continue // needs a category id
}
news = append(news, newsItem)
count++
news = append(news, newsItem)
count++
}
return news, count, nil
@ -134,31 +137,31 @@ func convertSqlToNewsContainer(rows *sql.Rows) (map[int]*NewsContainer, int, err
newsFlat := map[int]*NewsContainer{}
count := 0
for rows.Next() {
news := News{}
var url, title, notes sql.NullString
var category_id sql.NullInt64
err := rows.Scan(&news.id, &url, &title, &category_id, &news.Date, &notes)
if err != nil {
fmt.Println("Error reading news from DB: " + err.Error())
return nil, 0, err
}
news := News{}
var url, title, notes sql.NullString
var category_id sql.NullInt64
err := rows.Scan(&news.id, &url, &title, &category_id, &news.Date, &notes)
if err != nil {
fmt.Println("Error reading news from DB: " + err.Error())
return nil, 0, err
}
news.Url = nullStringToString(&url)
news.Title = nullStringToString(&title)
news.Notes = nullStringToString(&notes)
news.Url = nullStringToString(&url)
news.Title = nullStringToString(&title)
news.Notes = nullStringToString(&notes)
if category_id.Valid {
news.Category_id = int(category_id.Int64)
} else {
continue // needs a category id
}
if category_id.Valid {
news.Category_id = int(category_id.Int64)
} else {
continue // needs a category id
}
if _, ok := newsFlat[news.Category_id]; !ok {
addContainer(news.Category_id, newsFlat, newsTree)
}
container := newsFlat[news.Category_id]
container.News = append(container.News, news)
count++
if _, ok := newsFlat[news.Category_id]; !ok {
addContainer(news.Category_id, newsFlat, newsTree)
}
container := newsFlat[news.Category_id]
container.News = append(container.News, news)
count++
}
return newsTree, count, nil
@ -166,20 +169,20 @@ func convertSqlToNewsContainer(rows *sql.Rows) (map[int]*NewsContainer, int, err
// Helper fn - formating - math
func (this *NewsContainer) HeaderDepth(start int) int {
return start + this.Category.Depth()
return start + this.Category.Depth()
}
// Mark the current batch (news.exported is null) as exported in this batch (exported = now())
func MarkExported(db *sql.DB) error {
now := time.Now()
now := time.Now()
_, err := db.Exec("UPDATE news SET exported=$1 WHERE exported is null", now)
if err != nil {
fmt.Println("DB errror: news.MarkExported():", err)
}
return err
_, err := db.Exec("UPDATE news SET exported=$1 WHERE exported is null", now)
if err != nil {
fmt.Println("DB errror: news.MarkExported():", err)
}
return err
}
func (news *News) Id() int {
return news.id
return news.id
}

View File

@ -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 {
@ -107,31 +107,31 @@ func LogoutHandler(w http.ResponseWriter, r *http.Request, user *user.User, sess
}
func getUrlTitle(url string) string {
resp, err := http.Get(url)
if err != nil {
fmt.Println("Error looking up link", url, ":", err)
} else {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading link", url, ":", err)
} else {
re := regexp.MustCompile("< *[Tt][Ii][Tt][Ll][Ee] *>(.*)</ *[Tt][Ii][Tt][Ll][Ee] *>")
title := re.FindStringSubmatch(string(body))
if title != nil {
return strings.TrimSpace(title[1])
}
}
}
return ""
resp, err := http.Get(url)
if err != nil {
fmt.Println("Error looking up link", url, ":", err)
} else {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading link", url, ":", err)
} else {
re := regexp.MustCompile("< *[Tt][Ii][Tt][Ll][Ee] *>(.*)</ *[Tt][Ii][Tt][Ll][Ee] *>")
title := re.FindStringSubmatch(string(body))
if title != nil {
return strings.TrimSpace(title[1])
}
}
}
return ""
}
// ?url=
func addFormHandler(w http.ResponseWriter, r *http.Request, user *user.User, session *sessions.Session) {
categories.LoadCategories(db)
categories.LoadCategories(db)
flashes := GetFlashes(session)
popup := session.Values["popup"]
delete(session.Values, "popup")
flashes := GetFlashes(session)
popup := session.Values["popup"]
delete(session.Values, "popup")
title := session.Values["title"]
delete(session.Values, "title")
link := session.Values["link"]
@ -141,23 +141,23 @@ func addFormHandler(w http.ResponseWriter, r *http.Request, user *user.User, ses
session.Save(r, w)
if link != nil {
//TODO category_id
//TODO category_id
ShowTemplate("add", w, map[string]interface{}{"user": user, "flashes": flashes, "categories": categories.CategoriesTree, "link": link, "title": title, "description": description, "popup": popup})
return
}
var url = r.URL.Query().Get("url")
reHttp := regexp.MustCompile("^https?://")
if url != "" && ! reHttp.Match([]byte(url)) {
url = "http://" + url
}
var url = r.URL.Query().Get("url")
reHttp := regexp.MustCompile("^https?://")
if url != "" && !reHttp.Match([]byte(url)) {
url = "http://" + url
}
title = r.URL.Query().Get("title")
if title == "" && url != "" {
title = getUrlTitle(url)
}
title = r.URL.Query().Get("title")
if title == "" && url != "" {
title = getUrlTitle(url)
}
popup = r.URL.Query().Get("popup")
popup = r.URL.Query().Get("popup")
ShowTemplate("add", w, map[string]interface{}{"user": user, "flashes": flashes, "link": url, "categories": categories.CategoriesTree, "title": title, "popup": popup})
}
@ -171,44 +171,44 @@ func addPostHandler(w http.ResponseWriter, r *http.Request, user *user.User, ses
popup := r.FormValue("popup")
category_id, err := strconv.Atoi(r.FormValue("category"))
if err != nil {
category_id = -1
category_id = -1
}
news.Category_id = category_id
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["notes"] = news.Notes
session.Values["popup"] = popup
session.AddFlash("Error saving news: "+err.Error(), flash_err)
session.Values["title"] = news.Title
session.Values["link"] = news.Url
session.Values["notes"] = news.Notes
session.Values["popup"] = popup
session.Save(r, w)
http.Redirect(w, r, "/add", http.StatusFound)
http.Redirect(w, r, "/add", http.StatusFound)
} else {
// TODO auto close? redirect
session.AddFlash("Added news \""+news.Title+"\"", flash_info)
// TODO auto close? redirect
session.AddFlash("Added news \""+news.Title+"\"", flash_info)
session.Save(r, w)
if popup == "1" {
http.Redirect(w, r, "/added", http.StatusFound)
http.Redirect(w, r, "/added", http.StatusFound)
} else {
http.Redirect(w, r, "/", http.StatusFound)
}
http.Redirect(w, r, "/", http.StatusFound)
}
}
}
func templateFormHandler(w http.ResponseWriter, r *http.Request, user *user.User, session *sessions.Session) {
flashes := GetFlashes(session)
session.Save(r, w)
flashes := GetFlashes(session)
session.Save(r, w)
news, count, err := news.Unexported(db)
news, count, err := news.Unexported(db)
if err != nil {
return
return
}
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 {
@ -222,18 +222,18 @@ func exportHandler(w http.ResponseWriter, r *http.Request, user *user.User, sess
err := news.MarkExported(db)
if err != nil {
session.AddFlash("Error marking last batch of news exported", flash_err)
session.AddFlash("Error marking last batch of news exported", flash_err)
} else {
session.AddFlash("Last batch of news marked exported", flash_info)
}
session.Save(r, w)
http.Redirect(w, r, "/", http.StatusFound)
http.Redirect(w, r, "/", http.StatusFound)
}
func addedHandler(w http.ResponseWriter, r *http.Request, user *user.User, session *sessions.Session) {
flashes := GetFlashes(session)
session.Save(r, w)
ShowTemplate("added", w, map[string]interface{}{"user": user, "flashes": flashes})
flashes := GetFlashes(session)
session.Save(r, w)
ShowTemplate("added", w, map[string]interface{}{"user": user, "flashes": flashes})
}
func deleteHandler(w http.ResponseWriter, r *http.Request, user *user.User, session *sessions.Session) {
@ -242,39 +242,38 @@ func deleteHandler(w http.ResponseWriter, r *http.Request, user *user.User, sess
if idErr != nil {
session.AddFlash("Invalid news to delete", flash_err)
} else {
err := news.Delete(db, id)
if err != nil {
session.AddFlash("Error commiting to Database", flash_err)
} else {
session.AddFlash("Deleted news post", flash_info)
}
}
err := news.Delete(db, id)
if err != nil {
session.AddFlash("Error commiting to Database", flash_err)
} else {
session.AddFlash("Deleted news post", flash_info)
}
}
session.Save(r, w)
http.Redirect(w, r, "/", http.StatusFound)
session.Save(r, w)
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)
categories.LoadCategories(db)
flashes := GetFlashes(session)
session.Save(r, w)
categories.LoadCategories(db)
ShowTemplate("categories", w, map[string]interface{}{"user": user, "flashes": flashes, "categories": categories.CategoriesTree})
ShowTemplate("categories", w, map[string]interface{}{"user": user, "flashes": flashes, "categories": categories.CategoriesTree})
}
func categoriesPostHandler(w http.ResponseWriter, r *http.Request, user *user.User, session *sessions.Session) {
http.Redirect(w, r, "/categories", http.StatusFound)
http.Redirect(w, r, "/categories", http.StatusFound)
}
func categoryFromReqArg(arg string) *categories.Category {
if cid, err := strconv.Atoi(arg); err != nil {
return nil
} else if category, ok := categories.CategoriesFlat[cid]; !ok {
return nil
} else {
return category
}
if cid, err := strconv.Atoi(arg); err != nil {
return nil
} else if category, ok := categories.CategoriesFlat[cid]; !ok {
return nil
} else {
return category
}
}
func categoryChangeParentHandler(w http.ResponseWriter, r *http.Request, user *user.User, session *sessions.Session) {
@ -287,16 +286,16 @@ func categoryChangeParentHandler(w http.ResponseWriter, r *http.Request, user *u
} else if parent != nil && category.Id == parent.Id {
session.AddFlash("Cannot set category parent to itself", flash_err)
} else {
err := category.ChangeParent(db, parent)
if err != nil {
session.AddFlash("Error commiting to Database", flash_err)
} else {
session.AddFlash("Changed category parent", flash_info)
}
}
err := category.ChangeParent(db, parent)
if err != nil {
session.AddFlash("Error commiting to Database", flash_err)
} else {
session.AddFlash("Changed category parent", flash_info)
}
}
session.Save(r, w)
http.Redirect(w, r, "/categories", http.StatusFound)
session.Save(r, w)
http.Redirect(w, r, "/categories", http.StatusFound)
}
func categoryAddHandler(w http.ResponseWriter, r *http.Request, user *user.User, session *sessions.Session) {
@ -304,21 +303,21 @@ func categoryAddHandler(w http.ResponseWriter, r *http.Request, user *user.User,
parent, perr := strconv.Atoi(r.FormValue("parent"))
if perr != nil {
parent = -1
parent = -1
}
if name == "" {
session.AddFlash("Invalid category name", flash_err)
} else {
err := categories.Add(db, name, parent)
if err != nil {
session.AddFlash("Error commiting to Database", flash_err)
} else {
session.AddFlash("Added category", flash_info)
}
}
err := categories.Add(db, name, parent)
if err != nil {
session.AddFlash("Error commiting to Database", flash_err)
} else {
session.AddFlash("Added category", flash_info)
}
}
session.Save(r, w)
http.Redirect(w, r, "/categories", http.StatusFound)
session.Save(r, w)
http.Redirect(w, r, "/categories", http.StatusFound)
}
func categoryDeleteHandler(w http.ResponseWriter, r *http.Request, user *user.User, session *sessions.Session) {
@ -327,45 +326,45 @@ func categoryDeleteHandler(w http.ResponseWriter, r *http.Request, user *user.Us
if idErr != nil {
session.AddFlash("Invalid category to delete", flash_err)
} else {
err := categories.Delete(db, id)
if err != nil {
session.AddFlash("Error commiting to Database", flash_err)
} else {
session.AddFlash("Deleted category", flash_info)
}
}
err := categories.Delete(db, id)
if err != nil {
session.AddFlash("Error commiting to Database", flash_err)
} else {
session.AddFlash("Deleted category", flash_info)
}
}
session.Save(r, w)
http.Redirect(w, r, "/categories", http.StatusFound)
session.Save(r, w)
http.Redirect(w, r, "/categories", http.StatusFound)
}
func newsFormHandler(w http.ResponseWriter, r *http.Request, user *user.User, session *sessions.Session) {
flashes := GetFlashes(session)
session.Save(r, w)
var offset = 0
var amount = 100
flashes := GetFlashes(session)
session.Save(r, w)
var offset = 0
var amount = 100
argOffset, eOffset := strconv.Atoi(r.FormValue("offset"))
if eOffset == nil {
offset = amount * argOffset
}
news, count, err := news.LoadPage(db, offset, amount)
if err != nil {
session.AddFlash("Error loading news", flash_err)
argOffset, eOffset := strconv.Atoi(r.FormValue("offset"))
if eOffset == nil {
offset = amount * argOffset
}
ShowTemplate("news", w, map[string]interface{}{"user": user, "flashes": flashes, "news": news, "count": count, "categories": categories.CategoriesFlat})
news, count, err := news.LoadPage(db, offset, amount)
if err != nil {
session.AddFlash("Error loading news", flash_err)
}
ShowTemplate("news", w, map[string]interface{}{"user": user, "flashes": flashes, "news": news, "count": count, "categories": categories.CategoriesFlat})
}
func ServeFileHandler(res http.ResponseWriter, req *http.Request) {
fname := path.Base(req.URL.Path)
http.ServeFile(res, req, "./"+fname)
}
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/"))))
http.Handle("/js/", http.StripPrefix("/js/", http.FileServer(http.Dir("js/"))))
http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("css/"))))
http.Handle("/fonts/", http.StripPrefix("/fonts", http.FileServer(http.Dir("fonts/"))))
http.HandleFunc("/favicon.ico", ServeFileHandler)

View File

@ -1,81 +1,80 @@
package main
import (
"fmt"
"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 (
templates = map[string]*template.Template{}
templates = map[string]*template.Template{}
)
// 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 {
key, ok := values[i].(string)
if !ok {
return nil, errors.New("dict keys must be strings")
}
dict[key] = values[i+1]
}
return dict, nil
return nil, errors.New("invalid dict call")
}
dict := make(map[string]interface{}, len(values)/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")
}
dict[key] = values[i+1]
}
return dict, nil
}
// string multiplication
// stringTimes(3, "Foo") => "FooFooFoo"
func stringTimes(times int, str string) string {
result := ""
for i := 0; i < times; i ++ {
result += str
}
return result
result := ""
for i := 0; i < times; i++ {
result += str
}
return result
}
// Turns a Time into a formated string
func dateFormat(t time.Time) string {
return t.Format("2006.01.02 15:04:05")
return t.Format("2006.01.02 15:04:05")
}
// takes a category_id and returns "Root / Parent / Category"
func fullCategoryPath(categoriesFlat map[int]*categories.Category, category_id int) string {
var categoryNames []string = nil
for category := categoriesFlat[category_id]; category != nil; category = categoriesFlat[int(category.Parent.Int64)] {
categoryNames = append([]string{category.Name}, categoryNames...)
}
return strings.Join(categoryNames, " / ")
var categoryNames []string = nil
for category := categoriesFlat[category_id]; category != nil; category = categoriesFlat[int(category.Parent.Int64)] {
categoryNames = append([]string{category.Name}, categoryNames...)
}
return strings.Join(categoryNames, " / ")
}
// truncate a string
func truncate(str string, maxLen int) string {
if len(str) <= maxLen {
return str
}
return str[0:maxLen] + "..."
if len(str) <= maxLen {
return str
}
return str[0:maxLen] + "..."
}
// 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 },
"dict": dict,
"stringTimes": stringTimes,
"dateFormat": dateFormat,
"fullCategoryPath": fullCategoryPath,
"truncate": truncate,
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,
"fullCategoryPath": fullCategoryPath,
"truncate": truncate,
}
func initTemplates() {
files, _ := filepath.Glob("templates/pages/*.html")
re := regexp.MustCompile("templates/pages/(.*).html")

View File

@ -7,8 +7,8 @@ import (
)
type User struct {
Username string
db *sql.DB
Username string
db *sql.DB
}
func UsernameExists(db *sql.DB, username string) (bool, error) {
@ -39,7 +39,7 @@ func NewUserFromAuth(db *sql.DB, username, password string) *User {
fmt.Println("scan err: ", err)
}
} else {
fmt.Println("no match")
fmt.Println("no match")
return nil
}
return &user