Compare commits
2 Commits
5330cdd988
...
f4c8ae33dd
Author | SHA1 | Date | |
---|---|---|---|
f4c8ae33dd | |||
885455454c |
@ -623,38 +623,6 @@ func TestCompleteTaskInvalidId(t *testing.T) {
|
|||||||
e.POST("/task/999/complete").Expect().Status(404)
|
e.POST("/task/999/complete").Expect().Status(404)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCompleteTaskAllowanceWeightsSumTo0(t *testing.T) {
|
|
||||||
e := startServer(t)
|
|
||||||
taskId := createTestTaskWithAmount(e, 101)
|
|
||||||
|
|
||||||
e.GET("/tasks").Expect().Status(200).JSON().Array().Length().IsEqual(1)
|
|
||||||
|
|
||||||
// Update rest allowance
|
|
||||||
e.PUT("/user/1/allowance/0").WithJSON(UpdateAllowanceRequest{
|
|
||||||
Weight: 0,
|
|
||||||
}).Expect().Status(200)
|
|
||||||
// Create two allowance goals
|
|
||||||
e.POST("/user/1/allowance").WithJSON(CreateAllowanceRequest{
|
|
||||||
Name: "Test Allowance 1",
|
|
||||||
Target: 1000,
|
|
||||||
Weight: 0,
|
|
||||||
}).Expect().Status(201)
|
|
||||||
|
|
||||||
// Complete the task
|
|
||||||
e.POST("/task/" + strconv.Itoa(taskId) + "/complete").Expect().Status(200)
|
|
||||||
|
|
||||||
// Verify the task is marked as completed
|
|
||||||
e.GET("/task/" + strconv.Itoa(taskId)).Expect().Status(404)
|
|
||||||
|
|
||||||
// Verify the allowances are updated for user 1
|
|
||||||
allowances := e.GET("/user/1/allowance").Expect().Status(200).JSON().Array()
|
|
||||||
allowances.Length().IsEqual(2)
|
|
||||||
allowances.Value(0).Object().Value("id").Number().IsEqual(0)
|
|
||||||
allowances.Value(0).Object().Value("progress").Number().IsEqual(101)
|
|
||||||
allowances.Value(1).Object().Value("id").Number().IsEqual(1)
|
|
||||||
allowances.Value(1).Object().Value("progress").Number().IsEqual(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCompleteAllowance(t *testing.T) {
|
func TestCompleteAllowance(t *testing.T) {
|
||||||
e := startServer(t)
|
e := startServer(t)
|
||||||
createTestTaskWithAmount(e, 100)
|
createTestTaskWithAmount(e, 100)
|
||||||
|
@ -593,8 +593,7 @@ func start(ctx context.Context, config *ServerConfig) {
|
|||||||
router.Use(cors.New(corsConfig))
|
router.Use(cors.New(corsConfig))
|
||||||
|
|
||||||
// Web endpoints
|
// Web endpoints
|
||||||
router.LoadHTMLFiles("lite.gohtml")
|
loadWebEndpoints(router)
|
||||||
router.GET("/", renderLite)
|
|
||||||
// API endpoints
|
// API endpoints
|
||||||
router.GET("/api/users", getUsers)
|
router.GET("/api/users", getUsers)
|
||||||
router.GET("/api/user/:userId", getUser)
|
router.GET("/api/user/:userId", getUser)
|
||||||
|
@ -15,13 +15,21 @@ type ViewModel struct {
|
|||||||
History []History
|
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") != "" {
|
if c.Query("user") != "" {
|
||||||
c.SetCookie("user", c.Query("user"), 3600, "/", "localhost", false, true)
|
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")
|
currentUserStr, err := c.Cookie("user")
|
||||||
if errors.Is(err, http.ErrNoCookie) {
|
if errors.Is(err, http.ErrNoCookie) {
|
||||||
renderNoUser(c)
|
renderNoUser(c)
|
||||||
@ -45,6 +53,59 @@ func renderLite(c *gin.Context) {
|
|||||||
renderWithUser(c, currentUser)
|
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) {
|
func unsetUserCookie(c *gin.Context) {
|
||||||
c.SetCookie("user", "", -1, "/", "localhost", false, true)
|
c.SetCookie("user", "", -1, "/", "localhost", false, true)
|
||||||
c.Redirect(http.StatusFound, "/")
|
c.Redirect(http.StatusFound, "/")
|
||||||
@ -59,7 +120,7 @@ func renderNoUser(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.HTML(http.StatusOK, "lite.gohtml", ViewModel{
|
c.HTML(http.StatusOK, "web.gohtml", ViewModel{
|
||||||
Users: users,
|
Users: users,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -97,7 +158,7 @@ func renderWithUser(c *gin.Context, currentUser int) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.HTML(http.StatusOK, "lite.gohtml", ViewModel{
|
c.HTML(http.StatusOK, "web.gohtml", ViewModel{
|
||||||
Users: users,
|
Users: users,
|
||||||
CurrentUser: currentUser,
|
CurrentUser: currentUser,
|
||||||
Allowances: allowances,
|
Allowances: allowances,
|
@ -10,7 +10,7 @@
|
|||||||
{{if eq $.CurrentUser .ID}}
|
{{if eq $.CurrentUser .ID}}
|
||||||
<strong>{{.Name}}</strong>
|
<strong>{{.Name}}</strong>
|
||||||
{{else}}
|
{{else}}
|
||||||
<a href="?user={{.ID}}">{{.Name}}</a>
|
<a href="/login?user={{.ID}}">{{.Name}}</a>
|
||||||
{{end}}
|
{{end}}
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
||||||
@ -47,24 +47,34 @@
|
|||||||
</table>
|
</table>
|
||||||
|
|
||||||
<h2>Tasks</h2>
|
<h2>Tasks</h2>
|
||||||
<table border="1">
|
<form method="post" action="/createTask">
|
||||||
<thead>
|
<table border="1">
|
||||||
<tr>
|
<thead>
|
||||||
<th>Name</th>
|
|
||||||
<th>Assigned</th>
|
|
||||||
<th>Reward</th>
|
|
||||||
</tr>
|
|
||||||
</thead>
|
|
||||||
<tbody>
|
|
||||||
{{range .Tasks}}
|
|
||||||
<tr>
|
<tr>
|
||||||
<td>{{.Name}}</td>
|
<th>Name</th>
|
||||||
<td>{{.Assigned}}</td>
|
<th>Assigned</th>
|
||||||
<td>{{.Reward}}</td>
|
<th>Reward</th>
|
||||||
</tr>
|
</tr>
|
||||||
{{end}}
|
</thead>
|
||||||
</tbody>
|
<tbody>
|
||||||
</table>
|
{{range .Tasks}}
|
||||||
|
<tr>
|
||||||
|
<td>{{.Name}}</td>
|
||||||
|
<td>{{.Assigned}}</td>
|
||||||
|
<td>{{.Reward}}</td>
|
||||||
|
</tr>
|
||||||
|
{{end}}
|
||||||
|
<tr>
|
||||||
|
<td><label><input type="text" placeholder="Name"></label></td>
|
||||||
|
<td></td>
|
||||||
|
<td>
|
||||||
|
<label><input type="number" placeholder="Reward"></label>
|
||||||
|
<button>Create</button>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</form>
|
||||||
|
|
||||||
<h2>History</h2>
|
<h2>History</h2>
|
||||||
<table border="1">
|
<table border="1">
|
Loading…
x
Reference in New Issue
Block a user