52 lines
1.7 KiB
Go
52 lines
1.7 KiB
Go
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)
|
|
}
|
|
}
|