Somewhat working first page
This commit is contained in:
parent
19292ec746
commit
5330cdd988
107
backend/lite.go
Normal file
107
backend/lite.go
Normal file
@ -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,
|
||||||
|
})
|
||||||
|
}
|
@ -1,3 +1,4 @@
|
|||||||
|
{{- /*gotype: allowance_planner.ViewModel*/}}
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<title>Allowance Planner 2000</title>
|
<title>Allowance Planner 2000</title>
|
||||||
@ -5,5 +6,83 @@
|
|||||||
<body>
|
<body>
|
||||||
<h1>Allowance Planner 2000</h1>
|
<h1>Allowance Planner 2000</h1>
|
||||||
<h2>Users</h2>
|
<h2>Users</h2>
|
||||||
|
{{range .Users}}
|
||||||
|
{{if eq $.CurrentUser .ID}}
|
||||||
|
<strong>{{.Name}}</strong>
|
||||||
|
{{else}}
|
||||||
|
<a href="?user={{.ID}}">{{.Name}}</a>
|
||||||
|
{{end}}
|
||||||
|
{{end}}
|
||||||
|
|
||||||
|
{{if ne .CurrentUser 0}}
|
||||||
|
<h2>Allowances</h2>
|
||||||
|
<table border="1">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Name</th>
|
||||||
|
<th>Progress</th>
|
||||||
|
<th>Target</th>
|
||||||
|
<th>Weight</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{{range .Allowances}}
|
||||||
|
{{if eq .ID 0}}
|
||||||
|
<tr>
|
||||||
|
<td>Total</td>
|
||||||
|
<td>{{.Progress}}</td>
|
||||||
|
<td></td>
|
||||||
|
<td>{{.Weight}}</td>
|
||||||
|
</tr>
|
||||||
|
{{else}}
|
||||||
|
<tr>
|
||||||
|
<td>{{.Name}}</td>
|
||||||
|
<td>{{.Progress}}</td>
|
||||||
|
<td>{{.Target}}</td>
|
||||||
|
<td>{{.Weight}}</td>
|
||||||
|
</tr>
|
||||||
|
{{end}}
|
||||||
|
{{end}}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
<h2>Tasks</h2>
|
||||||
|
<table border="1">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Name</th>
|
||||||
|
<th>Assigned</th>
|
||||||
|
<th>Reward</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{{range .Tasks}}
|
||||||
|
<tr>
|
||||||
|
<td>{{.Name}}</td>
|
||||||
|
<td>{{.Assigned}}</td>
|
||||||
|
<td>{{.Reward}}</td>
|
||||||
|
</tr>
|
||||||
|
{{end}}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
<h2>History</h2>
|
||||||
|
<table border="1">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Timestamp</th>
|
||||||
|
<th>Allowance</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{{range .History}}
|
||||||
|
<tr>
|
||||||
|
<td>{{.Timestamp}}</td>
|
||||||
|
<td>{{.Allowance}}</td>
|
||||||
|
</tr>
|
||||||
|
{{end}}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
{{end}}
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
@ -578,10 +578,6 @@ func getHistory(c *gin.Context) {
|
|||||||
c.IndentedJSON(http.StatusOK, history)
|
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.
|
Initialises the database, and then starts the server.
|
||||||
If the context gets cancelled, the server is shutdown and the database is closed.
|
If the context gets cancelled, the server is shutdown and the database is closed.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user