59 lines
1.6 KiB
Go

package controllers
import (
"html/template"
"net/http"
"git.wicak.co/arif/budgl/models"
)
type IndexData struct {
Title string
Summary []*models.Summary
Categories []*models.Category
People []*models.People
}
func Index(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
err := r.ParseForm()
if err != nil {
panic(err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
models.AddExpense(r.PostFormValue("desc"), r.PostFormValue("date"), r.PostFormValue("cat"), r.PostFormValue("payer"), r.PostFormValue("value"))
r.Method = "GET"
http.Redirect(w, r, "/", http.StatusFound)
} else {
cats, err := models.GetCategories()
if err != nil {
panic(err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
ppls, err := models.GetPeople()
if err != nil {
panic(err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
smrys, err := models.GetSummary()
if err != nil {
panic(err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
data := IndexData{
Title: "Summary of Expenses",
Summary: smrys,
Categories: cats,
People: ppls,
}
tmpl := template.Must(template.ParseFiles("views/views.html"))
tmpl.Execute(w, data)
}
}