diff --git a/main.go b/main.go index 19d8d47..c69f72b 100644 --- a/main.go +++ b/main.go @@ -1,71 +1,104 @@ package main import ( + "bytes" "fmt" "io" "log" "net/http" "net/url" "os" + "strings" "text/template" "time" ) -var tpl *template.Template +var ledgerTpl *template.Template +var htmlTpl *template.Template const LEDGER_FILE = "test.txt" type TxData struct { - Date string - Amount string + Name string + Date string + Amount string + Account string } func init() { - var err error - tpl, err = template.ParseGlob("templates/*") - if err != nil { - panic(err) - } - log.Println(tpl.DefinedTemplates()) + ledgerTpl = template.Must(template.ParseGlob("tx/*.txt")) + log.Println(ledgerTpl.DefinedTemplates()) + htmlTpl = template.Must(template.ParseGlob("templates/*.html")) } func main() { - fmt.Println("Hi") - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - tpl.ExecuteTemplate(w, "index.html", tpl) + htmlTpl.ExecuteTemplate(w, "index.html", ledgerTpl) }) - http.HandleFunc("/action", func(w http.ResponseWriter, r *http.Request) { + http.HandleFunc("/new", func(w http.ResponseWriter, r *http.Request) { if err := r.ParseForm(); err != nil { - panic(err) + http.Error(w, err.Error(), 400) + return } fmt.Println(r.Form) - err := renderTx(w, r.Form) + tx, err := newTx(r.Form) if err != nil { - panic(err) + http.Error(w, err.Error(), 400) + log.Println(err, r.Form) + return + } + if err := htmlTpl.ExecuteTemplate(w, "new.html", struct { + Tx string + }{tx}); err != nil { + http.Error(w, err.Error(), 500) } }) + http.HandleFunc("/submit", func(w http.ResponseWriter, r *http.Request) { + if err := r.ParseForm(); err != nil { + http.Error(w, err.Error(), 400) + return + } + + tx := r.FormValue("tx") + if err := appendToFile(tx); err != nil { + http.Error(w, err.Error(), 500) + return + } + + if err := htmlTpl.ExecuteTemplate(w, "success.html", struct { + Tx string + }{tx}); err != nil { + http.Error(w, err.Error(), 500) + } + }) + + log.Println("Listen on http://localhost:8000") log.Fatal(http.ListenAndServe(":8000", nil)) } -func renderTx(w io.Writer, params url.Values) (err error) { +func newTx(params url.Values) (result string, err error) { name := params.Get("action") data := TxData{ - Date: time.Now().Format("2006/01/02"), - Amount: params.Get("amount"), + Date: time.Now().Format("2006/01/02"), + Amount: params.Get("amount"), + Account: params.Get("account"), + Name: params.Get("name"), } + var buf bytes.Buffer + err = ledgerTpl.ExecuteTemplate(&buf, name, data) + return buf.String(), nil +} + +func appendToFile(tx string) (err error) { f, err := os.OpenFile(LEDGER_FILE, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644) if err != nil { - panic(err) + return err } defer f.Close() - if err = tpl.ExecuteTemplate(f, name, data); err != nil { - return err - } - if err = tpl.ExecuteTemplate(w, name, data); err != nil { - return err - } - return nil + + buf := strings.NewReader(strings.ReplaceAll(tx, "\r", "")) // Remove CR generated from browser + _, err = io.Copy(f, buf) + return err } diff --git a/templates/index.html b/templates/index.html index 900d8b7..74b810f 100644 --- a/templates/index.html +++ b/templates/index.html @@ -5,15 +5,15 @@

Ledger Quick Note

-
+ - +
+
+
+
diff --git a/templates/new.html b/templates/new.html new file mode 100644 index 0000000..bb42f1f --- /dev/null +++ b/templates/new.html @@ -0,0 +1,13 @@ + + + + Ledger Quick Note + + +

Comfirm new Tx

+
+
+ +
+ + diff --git a/templates/success.html b/templates/success.html new file mode 100644 index 0000000..81c5ecd --- /dev/null +++ b/templates/success.html @@ -0,0 +1,12 @@ + + + + Ledger Quick Note + + +

Ledger Quick Note

+

Success

+
{{ .Tx }}
+

Back to home

+ + diff --git a/templates/cash.txt b/tx/cash.txt similarity index 100% rename from templates/cash.txt rename to tx/cash.txt diff --git a/tx/expense.txt b/tx/expense.txt new file mode 100644 index 0000000..a7cf3b5 --- /dev/null +++ b/tx/expense.txt @@ -0,0 +1,4 @@ +{{ .Date }} {{ with .Name }}{{ . }}{{ else }}{{ .Account }}{{ end }} + {{ .Account }} ${{ .Amount }} + cash + diff --git a/templates/withdraw.txt b/tx/withdraw.txt similarity index 100% rename from templates/withdraw.txt rename to tx/withdraw.txt