From f77fb7342e7de47b87aa8be6f2e70c0f813c1748 Mon Sep 17 00:00:00 2001 From: Sebastiaan de Schaetzen Date: Tue, 27 May 2025 10:07:02 +0200 Subject: [PATCH 1/2] Remove accidentally commited db backup files --- backend/allowance_planner.db3.backup.1 | Bin 20480 -> 0 bytes backend/allowance_planner.db3.backup.2 | Bin 20480 -> 0 bytes 2 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 backend/allowance_planner.db3.backup.1 delete mode 100644 backend/allowance_planner.db3.backup.2 diff --git a/backend/allowance_planner.db3.backup.1 b/backend/allowance_planner.db3.backup.1 deleted file mode 100644 index dd398a849eec729f19198521e2947c096cc29947..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 20480 zcmeI%L2KJE6bEqGjY*b~Wzd5eVerDBG%!k`hh5u^j2*he*4~HOyrd$sT_l+_$9Ax9 zu@AAYv@fu)Fxg4tnQjfcwc-CjvXEl@-cP!SoP0V~j%l2kL^?VU&qWxDLn0wW=v(C5 zZ8ZF#XtxpgLDBAuQ0#phbo?(Gou25vfA+oe!w(P;fB*y_009U<00Izz00bVE!1Z>c z8OL#O6FGUN*-1OKWf#5sJUlub(&^FL;~|w(?cFx{P$N>Q;|UwOG%As1PE(%0F4t0- zFgd<*lxDu=S}*1e&!nl|t1n7x)gizTa6mE{Hpmi zNj}Lfn=gNqP*3Gw`8#PQ>{fNxUpkYzNbb%mjd(0`?P!02Fk>~jaAa8O6_2lVCv3*O zUU2iOto1@!mznwUu~n;bIk~39Vvp{x(y4^~E+>~XlKx&?uSO9F>n{ zqQxg{*@d+v%c&|we_sCgu>8%bvfNv0u2*jl$U39WU2!)LyFW$um+ue|fB*y_009U< z00Izz00bZa0SG)&fu~_4UbI>t^6^-6H;6>P)jDCe%n9phasT&!`Xe9!0SG_<0uX=z z1Rwwb2tWV=5ZGV=-2XRtb5R-uAOHafKmY;|fB*y_009U@@F| z+sP3-$yIVo+^tJd5dmKf+?o@t@fEQ)YA5>|p$Hath3mfL^@iuEn7**?)C8B}zAEeu zNGGTWxl`+&=#t~hnB!jMk55=Jkv9)hUo=-waN!$35hpzF4$hfWx}mAcr8+sT5O53OjRF8wzHRahUT z@!Zgmv2i)-bJY!{AFQ?C%$_l67ang^j+mCxtqC>mwN+kn)bsG(v8nlu`u@48hG_ko zu|0k6Ma|WB38NR<)6}SFcSF?*I&k?MI)M(F+ua2))>#5auNBpB9?HO7mbRK>lx-2V z#eSU9pC8-?0uX=z1Rwwb2tWV=5P$##AOL~C6UZ7dGMu1uLWDxp&P6%1ofF;)`8;FO z7#ZxNGmUcD6UA5h`9Hy)5%!aPV_(<@_Kv+~FH`{%1Rwwb2tWV=5P$##AOHafKmY9g9I^qEd5|1zIR#o8gANn4#z_Tw#{GTR}YNvmi6?FzHk zRfX!m0#SuJW3x}hVvh)W%f7Qsb%F!|2tWV=5P$##AOHafKmY;|fWXNJ3|I+b4AO*p zMz8p4-^#?q0wuo*xnQdDi&6QM+L2IIn{9O5dJ{zl3>D2$YM69h?OnK%&*U?v^Zcu7 zW51Czb#1!*DtUkVJk=jH>8_#&Pbm3S% Date: Tue, 27 May 2025 10:49:15 +0200 Subject: [PATCH 2/2] Add support for descriptions in history --- backend/api_test.go | 32 +++++++++++++++---- backend/db.go | 25 ++++++++------- backend/dto.go | 8 +++-- backend/main.go | 5 +++ .../migrations/4_add_history_description.sql | 2 ++ 5 files changed, 51 insertions(+), 21 deletions(-) create mode 100644 backend/migrations/4_add_history_description.sql 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; -- 2.47.2