Initial commit
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"git.wicak.co/arif/budgl/models"
|
||||
)
|
||||
|
||||
func Expenses(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)
|
||||
}
|
||||
|
||||
err = models.AddExpense(r.PostFormValue("desc"), r.PostFormValue("date"), r.PostFormValue("cat"), r.PostFormValue("payer"), r.PostFormValue("value"))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
r.Method = "GET"
|
||||
http.Redirect(w, r, "/", http.StatusFound)
|
||||
} else {
|
||||
query := r.URL.Query()
|
||||
for key, val := range query {
|
||||
fmt.Println("key: ", key)
|
||||
fmt.Println("val: ", val)
|
||||
}
|
||||
expid := query.Get("delete-id")
|
||||
if expid != "" {
|
||||
expidint, err := strconv.Atoi(expid)
|
||||
if err != nil {
|
||||
log.Panic("Unable to convert expense information")
|
||||
log.Panic(err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
}
|
||||
err = models.DeleteExpense(expidint)
|
||||
if err != nil {
|
||||
log.Panic("Unable to delete the requested expense")
|
||||
log.Panic(err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
http.Redirect(w, r, "/", http.StatusFound)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user