From cbb9d4d1e9e0fd99d3d0ae0a59caffca7e6455da Mon Sep 17 00:00:00 2001 From: Ozoniuss Date: Sun, 13 Oct 2024 18:33:53 +0300 Subject: [PATCH] Add section for creating new entries This is really not a nice approach. I created a new section just because otherwise the entry creation part would have re-rendered meaning that you needed to set the values to the current period somehow. --- ui/index.html | 150 ++++++++++++++++++++++++++------------------------ ui/main.go | 75 +++++++++++++++++++++++-- 2 files changed, 149 insertions(+), 76 deletions(-) diff --git a/ui/index.html b/ui/index.html index c036c9b..5698a93 100644 --- a/ui/index.html +++ b/ui/index.html @@ -97,15 +97,15 @@

Available entries

Insert new entry here -
- - + + @@ -113,81 +113,89 @@

Available entries

+ + +
+ {{ block "all-entries-categorized-inner" . }} {{ range .CategorizedEntryList }} - {{ $expectedForCategory := 0 }} - {{ range $_, $entry := .Entries }} - {{ $expectedForCategory = (Sum $expectedForCategory $entry.TotalMoney) }} - {{ end }} + {{ $expectedForCategory := 0 }} + {{ range $_, $entry := .Entries }} + {{ $expectedForCategory = (Sum $expectedForCategory $entry.TotalMoney) }} + {{ end }} -

- {{ .Category }} [Expected: {{ $expectedForCategory }}] [Actual] -

- Insert new expense to this category -
- - - - - - - - - - - - - - -
- {{ end }} - + {{ end }} + + {{ end }} diff --git a/ui/main.go b/ui/main.go index e15a22d..eccfd8b 100644 --- a/ui/main.go +++ b/ui/main.go @@ -2,6 +2,7 @@ package main import ( "encoding/json" + "errors" "fmt" "io" "math" @@ -12,6 +13,7 @@ import ( "time" "github.com/Ozoniuss/casheer/client/httpclient" + "github.com/Ozoniuss/casheer/pkg/casheerapi" "golang.org/x/exp/maps" ) @@ -238,11 +240,79 @@ func main() { tmpl.ExecuteTemplate(w, "all-entries-categorized", templateData.CategorizedEntryList[idx]) } + handleCreateEntry := func(w http.ResponseWriter, r *http.Request) { + + category := r.FormValue("category") + subcategory := r.FormValue("subcategory") + expectedTotalStr := r.FormValue("expected-total") + recurringCheck := r.FormValue("recurring") + currency := r.FormValue("currency") + monthStr := r.FormValue("month") + yearStr := r.FormValue("year") + + recurring := false + if recurringCheck == "on" { + recurring = true + } + + // basic validation + if subcategory == "" || category == "" { + w.WriteHeader(400) + fmt.Fprint(w, "some fields should not be empty") + return + } + + if currency == "" { + currency = "RON" + } + + expectedTotal, err := strconv.Atoi(expectedTotalStr) + if err != nil { + w.WriteHeader(http.StatusBadRequest) + fmt.Fprintf(w, "Invalid expected total: %s", err.Error()) + return + } + month, err := strconv.Atoi(monthStr) + if err != nil { + w.WriteHeader(http.StatusBadRequest) + fmt.Fprintf(w, "Invalid month: %s", err.Error()) + return + } + year, err := strconv.Atoi(yearStr) + if err != nil { + w.WriteHeader(http.StatusBadRequest) + fmt.Fprintf(w, "Invalid year: %s", err.Error()) + return + } + + createdEntry, err := cl.CreateEntry(month, year, category, subcategory, expectedTotal, currency, recurring) + if err != nil { + var apierr casheerapi.ErrorResponse + if errors.As(err, &apierr) { + fmt.Printf("got error %v\n", apierr) + w.WriteHeader(apierr.Err.Status) + fmt.Fprintf(w, "could not create entry: %s", apierr.Err.Detail) + } else { + fmt.Printf("got error %v\n", err) + w.WriteHeader(http.StatusInternalServerError) + fmt.Fprintf(w, "could not create entry: %s", err) + } + } + + fmt.Printf("created entry %+v\n", createdEntry) + + templateData.CategorizedEntryList = loadCategorizedEntriesList(cl) + + tmpl := template.Must(template.New("index.html").Funcs(Funcs).ParseFiles("index.html")) + tmpl.ExecuteTemplate(w, "all-entries-categorized-inner", templateData) + } + http.HandleFunc("/", h1) http.HandleFunc("/deleteDebt", handleDeleteDebt) http.HandleFunc("/deleteExpense", handleDeleteExpense) http.HandleFunc("/createDebt", handleCreateDebt) http.HandleFunc("/createExpense", handleCreateExpense) + http.HandleFunc("/createEntry", handleCreateEntry) http.HandleFunc("/year", func(w http.ResponseWriter, r *http.Request) { // year=2023 yearstr, err := io.ReadAll(r.Body) @@ -361,11 +431,6 @@ func createCategoriesArray(entries []EntryListItem) []CategoryWithEntries { categories := make(map[string][]EntryListItem) for _, e := range entries { categories[e.Category] = append(categories[e.Category], e) - // if _, ok := categories[e.Category]; ok { - // categories[e.Category] = append(categories[e.Category], e) - // } else { - // categories[e.Category] = []EntryListItem{e} - // } } categoriesWithEntries := make([]CategoryWithEntries, 0)