diff --git a/src/TODO.md b/src/TODO.md new file mode 100644 index 0000000..c956af0 --- /dev/null +++ b/src/TODO.md @@ -0,0 +1,4 @@ +# TODO +- [ ] On the categories page, add a column of how many times a given category is +used +- [ ] Make all the other items update/create page be like expenses diff --git a/src/assets/css/aesthetic.css b/src/assets/css/aesthetic.css index fc52541..b23f980 100644 --- a/src/assets/css/aesthetic.css +++ b/src/assets/css/aesthetic.css @@ -30,7 +30,7 @@ opacity: 1; } 20% { - opacity: 1.1; + opacity: 1; } 40% { opacity: 1; @@ -39,10 +39,10 @@ opacity: 1; } 80% { - opacity: 1.1; + opacity: 1; } 100% { - opacity: 0.9; + opacity: 1; } } @keyframes beam { diff --git a/src/assets/htmx/categories.html b/src/assets/htmx/categories.html index 841acb2..0910f03 100644 --- a/src/assets/htmx/categories.html +++ b/src/assets/htmx/categories.html @@ -1,3 +1,4 @@ +{{ define "categories" }}
 

@@ -50,3 +51,4 @@ }); }) +{{ end }} diff --git a/src/assets/htmx/category.html b/src/assets/htmx/category.html index ec951dd..0a3e48d 100644 --- a/src/assets/htmx/category.html +++ b/src/assets/htmx/category.html @@ -1,3 +1,4 @@ +{{ define "category" }}
@@ -8,3 +9,4 @@
ID:{{ .category.CategoryName }}
+{{ end }} diff --git a/src/assets/htmx/categoryNew.html b/src/assets/htmx/categoryNew.html index 220987a..f5e9377 100644 --- a/src/assets/htmx/categoryNew.html +++ b/src/assets/htmx/categoryNew.html @@ -1,3 +1,4 @@ +{{ define "categoryNew" }}
@@ -19,3 +20,4 @@
+{{ end }} diff --git a/src/assets/htmx/overview.html b/src/assets/htmx/overview.html index 50fc6c3..f484b0e 100644 --- a/src/assets/htmx/overview.html +++ b/src/assets/htmx/overview.html @@ -1,3 +1,4 @@ +{{ define "overview" }}

@@ -24,3 +25,4 @@

+{{ end }} diff --git a/src/assets/htmx/overviewResults.html b/src/assets/htmx/overviewResults.html index 44e805f..0b9d257 100644 --- a/src/assets/htmx/overviewResults.html +++ b/src/assets/htmx/overviewResults.html @@ -1,3 +1,4 @@ +{{ define "overviewResults" }} {{ range .expenses }} @@ -42,3 +43,4 @@ {{ end }}
+{{ end }} diff --git a/src/assets/htmx/panic.html b/src/assets/htmx/panic.html index f6f5239..865061d 100644 --- a/src/assets/htmx/panic.html +++ b/src/assets/htmx/panic.html @@ -1,3 +1,4 @@ +{{ define "panic" }} {{ .msg }}
@@ -31,3 +32,4 @@ /usr/lib/go/src/runtime/asm_amd64.s:1695 (0x472d80) goexit: BYTE $0x90 // NOP
+{{ end }} diff --git a/src/assets/htmx/store.html b/src/assets/htmx/store.html new file mode 100644 index 0000000..380c734 --- /dev/null +++ b/src/assets/htmx/store.html @@ -0,0 +1,12 @@ +{{ define "store" }} + + + + + + + + + +
ID:{{ .store.StoreID }}
Name:{{ .store.StoreName }}
+{{ end }} diff --git a/src/assets/htmx/storeNew.html b/src/assets/htmx/storeNew.html index 9bc79ec..c47ac22 100644 --- a/src/assets/htmx/storeNew.html +++ b/src/assets/htmx/storeNew.html @@ -1,7 +1,8 @@ +{{ define "storeNew" }}
- CATEGORY FORM + STORE FORM

@@ -24,3 +25,4 @@
+{{ end }} diff --git a/src/assets/htmx/stores.html b/src/assets/htmx/stores.html index 8a91b83..436627c 100644 --- a/src/assets/htmx/stores.html +++ b/src/assets/htmx/stores.html @@ -1,3 +1,4 @@ +{{ define "stores" }}
 

@@ -50,3 +51,4 @@ }); }) +{{ end }} diff --git a/src/assets/htmx/terminal.html b/src/assets/htmx/terminal.html index 5a97e5e..8707e11 100644 --- a/src/assets/htmx/terminal.html +++ b/src/assets/htmx/terminal.html @@ -1,3 +1,4 @@ +{{ define "terminal" }} @@ -53,6 +54,26 @@ {{ template "expenses" . }} {{ else if eq .content "newExpense" }} {{ template "expense" . }} + {{ else if eq .content "panic" }} + {{ template "panic" . }} + {{ else if eq .content "categories" }} + {{ template "categories" . }} + {{ else if eq .content "category" }} + {{ template "category" . }} + {{ else if eq .content "categoryNew" }} + {{ template "categoryNew" . }} + {{ else if eq .content "overview" }} + {{ template "overview" . }} + {{ else if eq .content "stores" }} + {{ template "stores" . }} + {{ else if eq .content "store" }} + {{ template "store" . }} + {{ else if eq .content "storeNew" }} + {{ template "storeNew" . }} + {{ else if eq .content "types" }} + {{ template "types" . }} + {{ else if eq .content "typeNew" }} + {{ template "typeNew" . }} {{ else }}
Unknown page
{{ end }} @@ -65,3 +86,4 @@ +{{ end }} diff --git a/src/assets/htmx/type.html b/src/assets/htmx/type.html index c38e564..584a213 100644 --- a/src/assets/htmx/type.html +++ b/src/assets/htmx/type.html @@ -1,3 +1,4 @@ +{{ define "type" }}
@@ -33,3 +34,4 @@
+{{ end }} diff --git a/src/assets/htmx/typeNew.html b/src/assets/htmx/typeNew.html index 480ff64..7517c09 100644 --- a/src/assets/htmx/typeNew.html +++ b/src/assets/htmx/typeNew.html @@ -1,3 +1,4 @@ +{{ define "typeNew" }}
@@ -19,3 +20,4 @@
+{{ end }} diff --git a/src/assets/htmx/types.html b/src/assets/htmx/types.html index baeb68e..5cb2115 100644 --- a/src/assets/htmx/types.html +++ b/src/assets/htmx/types.html @@ -1,3 +1,4 @@ +{{ define "types" }}
 

@@ -50,3 +51,4 @@ }); }) +{{ end }} diff --git a/src/main.go b/src/main.go index 97c2652..075db9d 100644 --- a/src/main.go +++ b/src/main.go @@ -3,12 +3,13 @@ package main import ( "expenses/config" "expenses/pages" - "expenses/templating" + "fmt" "html/template" "log" "net/http" fp "path/filepath" + "time" "github.com/gin-gonic/gin" ) @@ -16,6 +17,15 @@ import ( // This can be set in the compilation command - in the Makefile var ginMode = "debug" +var tmplFuncMap = template.FuncMap{ + "formatDate": func(ts int64) string { + return time.Unix(ts, 0).Format("02-Jan-2006") + }, + "formatPrice": func(v float32) string { + return fmt.Sprintf("%.2f", v) + }, +} + func main() { config.SetConfig("./config.yaml") cfg := config.GetInstance() @@ -23,8 +33,8 @@ func main() { gin.SetMode(ginMode) templates := template.Must( template.New(""). - Funcs(templating.TmplFuncMap). - ParseGlob(fp.Join(cfg.AssetsDir, "htmx/*.html")), + Funcs(tmplFuncMap). + ParseGlob(fp.Join(cfg.AssetsDir, "htmx/*.html")), ) router := gin.Default() diff --git a/src/pages/400.go b/src/pages/400.go index 0be17d9..8a922fa 100644 --- a/src/pages/400.go +++ b/src/pages/400.go @@ -1,27 +1,17 @@ package pages import ( - "expenses/config" - "expenses/templating" - "net/http" "github.com/gin-gonic/gin" ) func NotFoundView(c *gin.Context, msg string) { - cfg := config.GetInstance() - - errHtml := templating.HtmlTemplate( - cfg.AssetsDir+"htmx/panic.html", map[string]any{ - "msg": msg, - }, - ) - - c.HTML(404, "terminal.html", gin.H{ + c.HTML(404, "terminal", gin.H{ "warning": true, "renderNavBar": false, - "content": errHtml, + "content": "panic", + "msg": msg, }) } diff --git a/src/pages/500.go b/src/pages/500.go index 3135b0b..28ca1d2 100644 --- a/src/pages/500.go +++ b/src/pages/500.go @@ -1,25 +1,15 @@ package pages import ( - "expenses/config" - "expenses/templating" - "github.com/gin-gonic/gin" ) func ServerErrorView(c *gin.Context, msg string) { - cfg := config.GetInstance() - - errHtml := templating.HtmlTemplate( - cfg.AssetsDir+"htmx/panic.html", map[string]any{ - "msg": msg, - }, - ) - - c.HTML(500, "terminal.html", gin.H{ + c.HTML(500, "terminal", gin.H{ "warning": true, "renderNavBar": false, - "content": errHtml, + "content": "panic", + "msg": msg, }) } diff --git a/src/pages/categories.go b/src/pages/categories.go index 534c9af..79e9946 100644 --- a/src/pages/categories.go +++ b/src/pages/categories.go @@ -1,13 +1,10 @@ package pages import ( - "expenses/config" "expenses/expenses" - "expenses/templating" "fmt" "net/http" - fp "path/filepath" "strconv" "github.com/gin-gonic/gin" @@ -17,75 +14,47 @@ const ( CategoriesPath = "/categories" ) -func categoriesContent() (map[string]any, error) { +func categoriesGlobalPage(c *gin.Context) { categories, err := expenses.GetAllCategories() if err != nil { - return nil, err - } - - data := map[string]any{ - "size": len(categories) - 1, - "categories": categories, - "resource": "categories", - } - - return data, nil -} - -func categoriesGlobalPage(c *gin.Context) { - cfg := config.GetInstance() - - categories, err := categoriesContent() - if err != nil { - c.Header("HX-Redirect", "/500") + ServerErrorView(c, "The server too makes mistakes") return } - content := templating.HtmlTemplate( - fp.Join(cfg.AssetsDir, "htmx/categories.html"), - categories, - ) - c.HTML(http.StatusOK, "terminal.html", gin.H{ + c.HTML(http.StatusOK, "terminal", gin.H{ "page": "categories", "renderNavBar": true, - "content": content, + "content": "categories", + "categories": categories, }) } func categoryPage(c *gin.Context) { - cfg := config.GetInstance() - categoryID, err := strconv.Atoi(c.Param("id")) if err != nil { - c.Redirect(404, "/404") + NotFoundView(c, "No such category") + return } category, err := expenses.GetCategory(categoryID) - content := templating.HtmlTemplate( - fp.Join(cfg.AssetsDir, "/htmx/category.html"), - map[string]any{ - "category": category, - }, - ) + if err != nil { + ServerErrorView(c, "The server too makes mistakes") + return + } - c.HTML(http.StatusOK, "terminal.html", gin.H{ + c.HTML(http.StatusOK, "terminal", gin.H{ "page": "category", "renderNavBar": false, - "content": content, + "content": "category", + "category": category, }) } func newCategoryPage(c *gin.Context) { - cfg := config.GetInstance() - - content := templating.HtmlTemplate( - fp.Join(cfg.AssetsDir, "/htmx/categoryNew.html"), map[string]any{}, - ) - - c.HTML(http.StatusOK, "terminal.html", gin.H{ + c.HTML(http.StatusOK, "terminal", gin.H{ "page": "expenseNew", "renderNavBar": false, - "content": content, + "content": "categoryNew", }) } @@ -107,7 +76,8 @@ func createCategory(c *gin.Context) { func deleteCategory(c *gin.Context) { categoryID, err := strconv.Atoi(c.Param("id")) if err != nil { - c.Redirect(404, "/404") + NotFoundView(c, "No such category") + return } category := expenses.Category{ diff --git a/src/pages/expenses.go b/src/pages/expenses.go index 147d224..7c9b7a9 100644 --- a/src/pages/expenses.go +++ b/src/pages/expenses.go @@ -34,7 +34,7 @@ func ExpensesGlobalPage(c *gin.Context) { return } - c.HTML(http.StatusOK, "terminal.html", gin.H{ + c.HTML(http.StatusOK, "terminal", gin.H{ "page": "expenses", "renderNavBar": true, "content": "expenses", @@ -96,7 +96,7 @@ func expensePage(c *gin.Context) { return } - c.HTML(http.StatusOK, "terminal.html", gin.H{ + c.HTML(http.StatusOK, "terminal", gin.H{ "page": "expense", "renderNavBar": false, "content": "expense", @@ -135,7 +135,7 @@ func newExpensePage(c *gin.Context) { return } - c.HTML(http.StatusOK, "terminal.html", gin.H{ + c.HTML(http.StatusOK, "terminal", gin.H{ "page": "expenseNew", "renderNavBar": false, "content": "newExpense", diff --git a/src/pages/overview.go b/src/pages/overview.go index 5a8f5af..e3266b2 100644 --- a/src/pages/overview.go +++ b/src/pages/overview.go @@ -1,15 +1,11 @@ package pages import ( - "expenses/config" "expenses/expenses" - "expenses/templating" "log" "time" - "html/template" "net/http" - fp "path/filepath" "github.com/gin-gonic/gin" ) @@ -24,35 +20,19 @@ type UserDebtSummary struct { Total float32 } -func overviewContent() template.HTML { - cfg := config.GetInstance() - - content := templating.HtmlTemplate( - fp.Join(cfg.AssetsDir, "htmx/overview.html"), - map[string]any{}, - ) - - return content -} - func overviewPartialPage(c *gin.Context) { - content := overviewContent() - c.String(http.StatusOK, string(content)) + c.HTML(http.StatusOK, "overview", gin.H{}) } func overviewPage(c *gin.Context) { - content := overviewContent() - - c.HTML(http.StatusOK, "terminal.html", gin.H{ + c.HTML(http.StatusOK, "terminal", gin.H{ "page": "overview", "renderNavBar": true, - "content": content, + "content": "overview", }) } func getResults(c *gin.Context) { - cfg := config.GetInstance() - startDateStr := c.PostForm("range-start") + " 00:00:00" endDateStr := c.PostForm("range-end") + " 23:59:59" @@ -115,15 +95,10 @@ func getResults(c *gin.Context) { } } - content := templating.HtmlTemplate( - fp.Join(cfg.AssetsDir, "htmx/overviewResults.html"), - map[string]any{ - "expenses": expensesWithDebts, - "summary": userDebtSummary, - }, - ) - - c.String(http.StatusOK, string(content)) + c.HTML(http.StatusOK, "overviewResults", gin.H{ + "expenses": expensesWithDebts, + "summary": userDebtSummary, + }) } func RouteOverview(router *gin.Engine) { diff --git a/src/pages/payments.go b/src/pages/payments.go index 9859d9b..ddc481e 100644 --- a/src/pages/payments.go +++ b/src/pages/payments.go @@ -12,7 +12,7 @@ import ( func deletePayment(c *gin.Context) { paymentID, err := strconv.Atoi(c.Param("id")) if err != nil { - c.Header("HX-Redirect", "/404") + NotFoundView(c, "failed to fetch payment") return } diff --git a/src/pages/stores.go b/src/pages/stores.go index 13f4244..7e3e75b 100644 --- a/src/pages/stores.go +++ b/src/pages/stores.go @@ -1,13 +1,10 @@ package pages import ( - "expenses/config" "expenses/expenses" - "expenses/templating" "fmt" "net/http" - fp "path/filepath" "strconv" "github.com/gin-gonic/gin" @@ -17,75 +14,43 @@ const ( StoresPath = "/stores" ) -func storesContent() (map[string]any, error) { +func storesGlobalPage(c *gin.Context) { stores, err := expenses.GetAllStores() if err != nil { - return nil, err - } - - data := map[string]any{ - "size": len(stores) - 1, - "stores": stores, - "resource": "stores", - } - - return data, nil -} - -func storesGlobalPage(c *gin.Context) { - cfg := config.GetInstance() - - stores, err := storesContent() - if err != nil { - c.Header("HX-Redirect", "/500") + ServerErrorView(c, "Failed to fetch stores content") return } - content := templating.HtmlTemplate( - fp.Join(cfg.AssetsDir, "htmx/stores.html"), - stores, - ) - c.HTML(http.StatusOK, "terminal.html", gin.H{ + c.HTML(http.StatusOK, "terminal", gin.H{ "page": "stores", "renderNavBar": true, - "content": content, + "content": "stores", + "stores": stores, }) } func storePage(c *gin.Context) { - cfg := config.GetInstance() - storeID, err := strconv.Atoi(c.Param("id")) if err != nil { - c.Redirect(404, "/404") + NotFoundView(c, "didn't find request store") + return } - store, err := expenses.GetCategory(storeID) - content := templating.HtmlTemplate( - fp.Join(cfg.AssetsDir, "/htmx/category.html"), - map[string]any{ - "store": store, - }, - ) + store, err := expenses.GetStore(storeID) - c.HTML(http.StatusOK, "terminal.html", gin.H{ + c.HTML(http.StatusOK, "terminal", gin.H{ "page": "store", "renderNavBar": false, - "content": content, + "content": "store", + "store": store, }) } func newStorePage(c *gin.Context) { - cfg := config.GetInstance() - - content := templating.HtmlTemplate( - fp.Join(cfg.AssetsDir, "/htmx/storeNew.html"), map[string]any{}, - ) - - c.HTML(http.StatusOK, "terminal.html", gin.H{ + c.HTML(http.StatusOK, "terminal", gin.H{ "page": "storeNew", "renderNavBar": false, - "content": content, + "content": "storeNew", }) } diff --git a/src/pages/types.go b/src/pages/types.go index 8187220..5875039 100644 --- a/src/pages/types.go +++ b/src/pages/types.go @@ -1,13 +1,10 @@ package pages import ( - "expenses/config" "expenses/expenses" - "expenses/templating" "fmt" "net/http" - fp "path/filepath" "strconv" "github.com/gin-gonic/gin" @@ -17,75 +14,46 @@ const ( TypesPath = "/types" ) -func typesContent() (map[string]any, error) { +func typesGlobalPage(c *gin.Context) { types, err := expenses.GetAllTypes() if err != nil { - return nil, err - } - - data := map[string]any{ - "size": len(types) - 1, - "types": types, - "resource": "types", - } - - return data, nil -} - -func typesGlobalPage(c *gin.Context) { - cfg := config.GetInstance() - - types, err := typesContent() - if err != nil { - c.Header("HX-Redirect", "/500") + ServerErrorView(c, "the server failed to get the types") return } - content := templating.HtmlTemplate( - fp.Join(cfg.AssetsDir, "htmx/types.html"), - types, - ) - c.HTML(http.StatusOK, "terminal.html", gin.H{ + c.HTML(http.StatusOK, "terminal", gin.H{ "page": "types", "renderNavBar": true, - "content": content, + "content": "types", + "types": types, }) } func typePage(c *gin.Context) { - cfg := config.GetInstance() - typeID, err := strconv.Atoi(c.Param("id")) if err != nil { - c.Redirect(404, "/404") + NotFoundView(c, "requested type doesn't exist") + return } typ, err := expenses.GetType(typeID) - content := templating.HtmlTemplate( - fp.Join(cfg.AssetsDir, "/htmx/type.html"), - map[string]any{ - "type": typ, - }, - ) + if err != nil { + ServerErrorView(c, "failed to retrieve requested type") + } - c.HTML(http.StatusOK, "terminal.html", gin.H{ + c.HTML(http.StatusOK, "terminal", gin.H{ "page": "type", "renderNavBar": false, - "content": content, + "content": "type", + "type": typ, }) } func newTypePage(c *gin.Context) { - cfg := config.GetInstance() - - content := templating.HtmlTemplate( - fp.Join(cfg.AssetsDir, "/htmx/typeNew.html"), map[string]any{}, - ) - - c.HTML(http.StatusOK, "terminal.html", gin.H{ + c.HTML(http.StatusOK, "terminal", gin.H{ "page": "typeNew", "renderNavBar": false, - "content": content, + "content": "typeNew", }) } diff --git a/src/templating/templating.go b/src/templating/templating.go deleted file mode 100644 index 36fc1f9..0000000 --- a/src/templating/templating.go +++ /dev/null @@ -1,58 +0,0 @@ -package templating - -import ( - "bytes" - "expenses/config" - "fmt" - "html/template" - "log" - fp "path/filepath" - "time" -) - -var ( - TmplFuncMap = template.FuncMap{ - "formatDate": func(ts int64) string { - return time.Unix(ts, 0).Format("02-Jan-2006") - }, - "formatPrice": func(v float32) string { - return fmt.Sprintf("%.2f", v) - }, - } -) - -func createTemplateEngine(name string) *template.Template { - cfg := config.GetInstance() - - - tmpl := template.New(name).Funcs(TmplFuncMap) - tmpl, err := tmpl.ParseGlob(fp.Join(cfg.AssetsDir, "htmx/*.html")) - if err != nil { - panic(err) - } - - return tmpl -} - -func HtmlTemplate(file string, data map[string]any) template.HTML { - tmplEngName := fp.Base(file) - tmplEngine := createTemplateEngine(tmplEngName) - - tmpl, err := tmplEngine.ParseFiles(file) - if err != nil { - log.Println("error in templating") - panic(err) - } - - // Use a bytes.Buffer to store the rendered output - var renderedOutput bytes.Buffer - - // Render the template - err = tmpl.ExecuteTemplate(&renderedOutput, tmplEngName, data) - if err != nil { - log.Fatalf("Template execution: %v", err) - } - - // Now renderedOutput holds the rendered template - return template.HTML(renderedOutput.String()) -}