From 5330cdd98888e91cadb98b4d2fc43cecaa93436c Mon Sep 17 00:00:00 2001 From: Sebastiaan de Schaetzen Date: Thu, 22 May 2025 15:09:52 +0200 Subject: [PATCH] Somewhat working first page --- backend/lite.go | 107 ++++++++++++++++++++++++++++++++++++++++++++ backend/lite.gohtml | 79 ++++++++++++++++++++++++++++++++ backend/main.go | 4 -- 3 files changed, 186 insertions(+), 4 deletions(-) create mode 100644 backend/lite.go diff --git a/backend/lite.go b/backend/lite.go new file mode 100644 index 0000000..050c542 --- /dev/null +++ b/backend/lite.go @@ -0,0 +1,107 @@ +package main + +import ( + "errors" + "github.com/gin-gonic/gin" + "net/http" + "strconv" +) + +type ViewModel struct { + Users []User + CurrentUser int + Allowances []Allowance + Tasks []Task + History []History +} + +func renderLite(c *gin.Context) { + if c.Query("user") != "" { + c.SetCookie("user", c.Query("user"), 3600, "/", "localhost", false, true) + c.Redirect(http.StatusFound, "/") + return + } + + currentUserStr, err := c.Cookie("user") + if errors.Is(err, http.ErrNoCookie) { + renderNoUser(c) + return + } + + if err != nil { + unsetUserCookie(c) + return + } + currentUser, err := strconv.Atoi(currentUserStr) + if err != nil { + unsetUserCookie(c) + return + } + userExists, err := db.UserExists(currentUser) + if !userExists || err != nil { + unsetUserCookie(c) + return + } + renderWithUser(c, currentUser) +} + +func unsetUserCookie(c *gin.Context) { + c.SetCookie("user", "", -1, "/", "localhost", false, true) + c.Redirect(http.StatusFound, "/") +} + +func renderNoUser(c *gin.Context) { + users, err := db.GetUsers() + if err != nil { + c.HTML(http.StatusInternalServerError, "error.gohtml", gin.H{ + "error": err.Error(), + }) + return + } + + c.HTML(http.StatusOK, "lite.gohtml", ViewModel{ + Users: users, + }) +} + +func renderWithUser(c *gin.Context, currentUser int) { + users, err := db.GetUsers() + if err != nil { + c.HTML(http.StatusInternalServerError, "error.gohtml", gin.H{ + "error": err.Error(), + }) + return + } + + allowances, err := db.GetUserAllowances(currentUser) + if err != nil { + c.HTML(http.StatusInternalServerError, "error.gohtml", gin.H{ + "error": err.Error(), + }) + return + } + + tasks, err := db.GetTasks() + if err != nil { + c.HTML(http.StatusInternalServerError, "error.gohtml", gin.H{ + "error": err.Error(), + }) + return + } + + history, err := db.GetHistory(currentUser) + if err != nil { + c.HTML(http.StatusInternalServerError, "error.gohtml", gin.H{ + "error": err.Error(), + }) + return + } + + c.HTML(http.StatusOK, "lite.gohtml", ViewModel{ + Users: users, + CurrentUser: currentUser, + Allowances: allowances, + Tasks: tasks, + History: history, + }) +} diff --git a/backend/lite.gohtml b/backend/lite.gohtml index 9bb9e0d..9d764b6 100644 --- a/backend/lite.gohtml +++ b/backend/lite.gohtml @@ -1,3 +1,4 @@ +{{- /*gotype: allowance_planner.ViewModel*/}} Allowance Planner 2000 @@ -5,5 +6,83 @@

Allowance Planner 2000

Users

+{{range .Users}} + {{if eq $.CurrentUser .ID}} + {{.Name}} + {{else}} + {{.Name}} + {{end}} +{{end}} + +{{if ne .CurrentUser 0}} +

Allowances

+ + + + + + + + + + + {{range .Allowances}} + {{if eq .ID 0}} + + + + + + + {{else}} + + + + + + + {{end}} + {{end}} + +
NameProgressTargetWeight
Total{{.Progress}}{{.Weight}}
{{.Name}}{{.Progress}}{{.Target}}{{.Weight}}
+ +

Tasks

+ + + + + + + + + + {{range .Tasks}} + + + + + + {{end}} + +
NameAssignedReward
{{.Name}}{{.Assigned}}{{.Reward}}
+ +

History

+ + + + + + + + + {{range .History}} + + + + + {{end}} + +
TimestampAllowance
{{.Timestamp}}{{.Allowance}}
+{{end}} diff --git a/backend/main.go b/backend/main.go index f496614..0e84a94 100644 --- a/backend/main.go +++ b/backend/main.go @@ -578,10 +578,6 @@ func getHistory(c *gin.Context) { c.IndentedJSON(http.StatusOK, history) } -func renderLite(c *gin.Context) { - c.HTML(http.StatusOK, "lite.gohtml", nil) -} - /* Initialises the database, and then starts the server. If the context gets cancelled, the server is shutdown and the database is closed.