diff --git a/backend/allowance_planner.db3.backup.1 b/backend/allowance_planner.db3.backup.1 deleted file mode 100644 index dd398a8..0000000 Binary files a/backend/allowance_planner.db3.backup.1 and /dev/null differ diff --git a/backend/allowance_planner.db3.backup.2 b/backend/allowance_planner.db3.backup.2 deleted file mode 100644 index ab889f0..0000000 Binary files a/backend/allowance_planner.db3.backup.2 and /dev/null differ diff --git a/backend/api_test.go b/backend/api_test.go index d0fe142..c862102 100644 --- a/backend/api_test.go +++ b/backend/api_test.go @@ -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) { diff --git a/backend/db.go b/backend/db.go index 447bb82..6f31ea6 100644 --- a/backend/db.go +++ b/backend/db.go @@ -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, ×tamp) + err = row.Scan(&amount, ×tamp, &allowance.Description) if err != nil { return nil, err } diff --git a/backend/dto.go b/backend/dto.go index 5773f45..a79035c 100644 --- a/backend/dto.go +++ b/backend/dto.go @@ -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. diff --git a/backend/main.go b/backend/main.go index e584eb6..e9aa104 100644 --- a/backend/main.go +++ b/backend/main.go @@ -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) diff --git a/backend/migrations/4_add_history_description.sql b/backend/migrations/4_add_history_description.sql new file mode 100644 index 0000000..9c73876 --- /dev/null +++ b/backend/migrations/4_add_history_description.sql @@ -0,0 +1,2 @@ +alter table history +add column description text;