diff --git a/backend/main.go b/backend/main.go index 0e84a94..1375cd1 100644 --- a/backend/main.go +++ b/backend/main.go @@ -593,8 +593,7 @@ func start(ctx context.Context, config *ServerConfig) { router.Use(cors.New(corsConfig)) // Web endpoints - router.LoadHTMLFiles("lite.gohtml") - router.GET("/", renderLite) + loadWebEndpoints(router) // API endpoints router.GET("/api/users", getUsers) router.GET("/api/user/:userId", getUser) diff --git a/backend/lite.go b/backend/web.go similarity index 56% rename from backend/lite.go rename to backend/web.go index 050c542..97b906a 100644 --- a/backend/lite.go +++ b/backend/web.go @@ -15,13 +15,21 @@ type ViewModel struct { History []History } -func renderLite(c *gin.Context) { +func loadWebEndpoints(router *gin.Engine) { + router.LoadHTMLFiles("web.gohtml") + router.GET("/", renderIndex) + router.GET("/login", renderLogin) + router.POST("/createTask", renderCreateTask) +} + +func renderLogin(c *gin.Context) { if c.Query("user") != "" { c.SetCookie("user", c.Query("user"), 3600, "/", "localhost", false, true) - c.Redirect(http.StatusFound, "/") - return } + c.Redirect(http.StatusFound, "/") +} +func renderIndex(c *gin.Context) { currentUserStr, err := c.Cookie("user") if errors.Is(err, http.ErrNoCookie) { renderNoUser(c) @@ -45,6 +53,59 @@ func renderLite(c *gin.Context) { renderWithUser(c, currentUser) } +func renderCreateTask(c *gin.Context) { + currentUserStr, err := c.Cookie("user") + if errors.Is(err, http.ErrNoCookie) { + c.HTML(http.StatusBadRequest, "error.gohtml", gin.H{ + "error": "User not logged in", + }) + 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 + } + + name := c.PostForm("name") + rewardStr := c.PostForm("reward") + reward, err := strconv.Atoi(rewardStr) + if err != nil { + c.HTML(http.StatusBadRequest, "error.gohtml", gin.H{ + "error": "Invalid reward value", + }) + return + } + if name == "" || reward <= 0 { + c.HTML(http.StatusBadRequest, "error.gohtml", gin.H{ + "error": "Name and reward must be provided", + }) + return + } + + _, err = db.CreateTask(&CreateTaskRequest{ + Name: name, + Reward: reward, + }) + if err != nil { + c.HTML(http.StatusInternalServerError, "error.gohtml", gin.H{ + "error": err.Error(), + }) + return + } + + c.Redirect(http.StatusFound, "/") +} + func unsetUserCookie(c *gin.Context) { c.SetCookie("user", "", -1, "/", "localhost", false, true) c.Redirect(http.StatusFound, "/") @@ -59,7 +120,7 @@ func renderNoUser(c *gin.Context) { return } - c.HTML(http.StatusOK, "lite.gohtml", ViewModel{ + c.HTML(http.StatusOK, "web.gohtml", ViewModel{ Users: users, }) } @@ -97,7 +158,7 @@ func renderWithUser(c *gin.Context, currentUser int) { return } - c.HTML(http.StatusOK, "lite.gohtml", ViewModel{ + c.HTML(http.StatusOK, "web.gohtml", ViewModel{ Users: users, CurrentUser: currentUser, Allowances: allowances, diff --git a/backend/lite.gohtml b/backend/web.gohtml similarity index 65% rename from backend/lite.gohtml rename to backend/web.gohtml index 9d764b6..11272a5 100644 --- a/backend/lite.gohtml +++ b/backend/web.gohtml @@ -10,7 +10,7 @@ {{if eq $.CurrentUser .ID}} {{.Name}} {{else}} - {{.Name}} + {{.Name}} {{end}} {{end}} @@ -47,24 +47,34 @@

Tasks

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

History