Still not much to say here, just adding the basic functionaliy

This commit is contained in:
2025-07-13 22:07:39 +01:00
parent a56a277463
commit 83ffa57dbf
26 changed files with 488 additions and 135 deletions
+85
View File
@@ -0,0 +1,85 @@
package pages
import (
"expenses/config"
"expenses/expenses"
"expenses/templating"
"log"
"net/http"
fp "path/filepath"
"strconv"
"github.com/gin-gonic/gin"
)
const (
CategoriesPath = "/categories"
)
func categoriesContent() (map[string]any, error) {
log.Println("here1")
categories, err := expenses.GetAllCategories()
if err != nil {
return nil, err
}
log.Println("here2")
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")
return
}
content := templating.HtmlTemplate(
fp.Join(cfg.AssetsDir, "htmx/categories.html"),
categories,
)
c.HTML(http.StatusOK, "terminal.gotempl", gin.H{
"page": "categories",
"renderNavBar": true,
"content": content,
})
}
func categoryPage(c *gin.Context) {
cfg := config.GetInstance()
categoryID, err := strconv.Atoi(c.Param("id"))
if err != nil {
c.Redirect(404, "/404")
}
category, err := expenses.GetCategory(categoryID)
content := templating.HtmlTemplate(
fp.Join(cfg.AssetsDir, "/htmx/category.html"),
map[string]any{
"category": category,
},
)
c.HTML(http.StatusOK, "terminal.gotempl", gin.H{
"page": "category",
"renderNavBar": false,
"content": content,
})
}
func RouteCategories(router *gin.Engine) {
router.GET(CategoriesPath, categoriesGlobalPage)
router.POST(CategoriesPath, categoriesGlobalPage)
router.GET(CategoriesPath+"/:id", categoryPage)
}