Add history description (#98)
All checks were successful
Backend Deploy / build (push) Successful in 3m1s
Backend Build and Test / build (push) Successful in 3m5s

Closes #20

Reviewed-on: #98
This commit is contained in:
Sebastiaan de Schaetzen 2025-05-27 10:55:13 +02:00
parent 56a19acd0f
commit db2f518cc2
7 changed files with 51 additions and 21 deletions

View File

@ -437,9 +437,9 @@ func TestPutTaskInvalidTaskId(t *testing.T) {
func TestPostHistory(t *testing.T) {
e := startServer(t)
e.POST("/user/1/history").WithJSON(PostHistory{Allowance: 100}).Expect().Status(200)
e.POST("/user/1/history").WithJSON(PostHistory{Allowance: 20}).Expect().Status(200)
e.POST("/user/1/history").WithJSON(PostHistory{Allowance: -10}).Expect().Status(200)
e.POST("/user/1/history").WithJSON(PostHistory{Allowance: 100, Description: "Add a 100"}).Expect().Status(200)
e.POST("/user/1/history").WithJSON(PostHistory{Allowance: 20, Description: "Lolol"}).Expect().Status(200)
e.POST("/user/1/history").WithJSON(PostHistory{Allowance: -10, Description: "Subtracting"}).Expect().Status(200)
response := e.GET("/user/1").Expect().Status(200).JSON().Object()
response.Value("allowance").Number().IsEqual(100 + 20 - 10)
@ -448,23 +448,36 @@ func TestPostHistory(t *testing.T) {
func TestPostHistoryInvalidUserId(t *testing.T) {
e := startServer(t)
e.POST("/user/999/history").WithJSON(PostHistory{Allowance: 100}).Expect().
e.POST("/user/999/history").WithJSON(PostHistory{Allowance: 100, Description: "Good"}).Expect().
Status(404)
}
func TestPostHistoryInvalidDescription(t *testing.T) {
e := startServer(t)
e.POST("/user/1/history").WithJSON(PostHistory{Allowance: 100}).Expect().
Status(400)
}
func TestGetHistory(t *testing.T) {
e := startServer(t)
e.POST("/user/1/history").WithJSON(PostHistory{Allowance: 100}).Expect().Status(200)
e.POST("/user/1/history").WithJSON(PostHistory{Allowance: 20}).Expect().Status(200)
e.POST("/user/1/history").WithJSON(PostHistory{Allowance: -10}).Expect().Status(200)
e.POST("/user/1/history").WithJSON(PostHistory{Allowance: 100, Description: "Add 100"}).Expect().Status(200)
e.POST("/user/1/history").WithJSON(PostHistory{Allowance: 20, Description: "Add 20"}).Expect().Status(200)
e.POST("/user/1/history").WithJSON(PostHistory{Allowance: -10, Description: "Subtract 10"}).Expect().Status(200)
response := e.GET("/user/1/history").Expect().Status(200).JSON().Array()
response.Length().IsEqual(3)
response.Value(0).Object().Length().IsEqual(3)
response.Value(0).Object().Value("allowance").Number().IsEqual(100)
response.Value(0).Object().Value("timestamp").String().AsDateTime().InRange(getDelta(time.Now(), 2.0))
response.Value(0).Object().Value("description").String().IsEqual("Add 100")
response.Value(1).Object().Value("allowance").Number().IsEqual(20)
response.Value(1).Object().Value("description").String().IsEqual("Add 20")
response.Value(2).Object().Value("allowance").Number().IsEqual(-10)
response.Value(2).Object().Value("description").String().IsEqual("Subtract 10")
}
func TestGetUserAllowanceById(t *testing.T) {
@ -690,10 +703,15 @@ func TestCompleteAllowance(t *testing.T) {
// Verify history is updated
history := e.GET("/user/1/history").Expect().Status(200).JSON().Array()
history.Length().IsEqual(2)
history.Value(0).Object().Length().IsEqual(3)
history.Value(0).Object().Value("allowance").Number().IsEqual(100)
history.Value(0).Object().Value("timestamp").String().AsDateTime().InRange(getDelta(time.Now(), 2.0))
history.Value(0).Object().Value("description").String().IsEqual("Task completed: Test Task")
history.Value(1).Object().Length().IsEqual(3)
history.Value(1).Object().Value("allowance").Number().IsEqual(-100)
history.Value(1).Object().Value("timestamp").String().AsDateTime().InRange(getDelta(time.Now(), 2.0))
history.Value(1).Object().Value("description").String().IsEqual("Allowance completed: Test Allowance 1")
}
func TestCompleteAllowanceInvalidUserId(t *testing.T) {

View File

@ -2,6 +2,7 @@ package main
import (
"errors"
"fmt"
"log"
"math"
"time"
@ -206,8 +207,9 @@ func (db *Db) CompleteAllowance(userId int, allowanceId int) error {
// Get the cost of the allowance
var cost int
err = tx.Query("select balance from allowances where id = ? and user_id = ?").
Bind(allowanceId, userId).ScanSingle(&cost)
var allowanceName string
err = tx.Query("select balance, name from allowances where id = ? and user_id = ?").
Bind(allowanceId, userId).ScanSingle(&cost, &allowanceName)
if err != nil {
return err
}
@ -220,8 +222,8 @@ func (db *Db) CompleteAllowance(userId int, allowanceId int) error {
}
// Add a history entry
err = tx.Query("insert into history (user_id, timestamp, amount) values (?, ?, ?)").
Bind(userId, time.Now().Unix(), -cost).
err = tx.Query("insert into history (user_id, timestamp, amount, description) values (?, ?, ?, ?)").
Bind(userId, time.Now().Unix(), -cost, fmt.Sprintf("Allowance completed: %s", allowanceName)).
Exec()
if err != nil {
return err
@ -420,7 +422,8 @@ func (db *Db) CompleteTask(taskId int) error {
defer tx.MustRollback()
var reward int
err = tx.Query("select reward from tasks where id = ?").Bind(taskId).ScanSingle(&reward)
var rewardName string
err = tx.Query("select reward, name from tasks where id = ?").Bind(taskId).ScanSingle(&reward, &rewardName)
if err != nil {
return err
}
@ -434,8 +437,8 @@ func (db *Db) CompleteTask(taskId int) error {
}
// Add the history entry
err = tx.Query("insert into history (user_id, timestamp, amount) values (?, ?, ?)").
Bind(userId, time.Now().Unix(), reward).
err = tx.Query("insert into history (user_id, timestamp, amount, description) values (?, ?, ?, ?)").
Bind(userId, time.Now().Unix(), reward, fmt.Sprintf("Task completed: %s", rewardName)).
Exec()
if err != nil {
return err
@ -499,8 +502,8 @@ func (db *Db) AddHistory(userId int, allowance *PostHistory) error {
defer tx.MustRollback()
amount := int(math.Round(allowance.Allowance * 100.0))
err = tx.Query("insert into history (user_id, timestamp, amount) values (?, ?, ?)").
Bind(userId, time.Now().Unix(), amount).
err = tx.Query("insert into history (user_id, timestamp, amount, description) values (?, ?, ?, ?)").
Bind(userId, time.Now().Unix(), amount, allowance.Description).
Exec()
if err != nil {
return err
@ -512,11 +515,11 @@ func (db *Db) GetHistory(userId int) ([]History, error) {
history := make([]History, 0)
var err error
for row := range db.db.Query("select amount, `timestamp` from history where user_id = ? order by `timestamp` desc").
for row := range db.db.Query("select amount, `timestamp`, description from history where user_id = ? order by `timestamp` desc").
Bind(userId).Range(&err) {
allowance := History{}
var timestamp, amount int64
err = row.Scan(&amount, &timestamp)
err = row.Scan(&amount, &timestamp, &allowance.Description)
if err != nil {
return nil, err
}

View File

@ -14,12 +14,14 @@ type UserWithAllowance struct {
}
type History struct {
Allowance float64 `json:"allowance"`
Timestamp time.Time `json:"timestamp"`
Allowance float64 `json:"allowance"`
Timestamp time.Time `json:"timestamp"`
Description string `json:"description"`
}
type PostHistory struct {
Allowance float64 `json:"allowance"`
Allowance float64 `json:"allowance"`
Description string `json:"description"`
}
// Task represents a task in the system.

View File

@ -539,6 +539,11 @@ func postHistory(c *gin.Context) {
return
}
if historyRequest.Description == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "Description cannot be empty"})
return
}
exists, err := db.UserExists(userId)
if err != nil {
log.Printf(ErrCheckingUserExist, err)

View File

@ -0,0 +1,2 @@
alter table history
add column description text;