parent
56a19acd0f
commit
db2f518cc2
Binary file not shown.
Binary file not shown.
@ -437,9 +437,9 @@ func TestPutTaskInvalidTaskId(t *testing.T) {
|
|||||||
func TestPostHistory(t *testing.T) {
|
func TestPostHistory(t *testing.T) {
|
||||||
e := startServer(t)
|
e := startServer(t)
|
||||||
|
|
||||||
e.POST("/user/1/history").WithJSON(PostHistory{Allowance: 100}).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}).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}).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 := e.GET("/user/1").Expect().Status(200).JSON().Object()
|
||||||
response.Value("allowance").Number().IsEqual(100 + 20 - 10)
|
response.Value("allowance").Number().IsEqual(100 + 20 - 10)
|
||||||
@ -448,23 +448,36 @@ func TestPostHistory(t *testing.T) {
|
|||||||
func TestPostHistoryInvalidUserId(t *testing.T) {
|
func TestPostHistoryInvalidUserId(t *testing.T) {
|
||||||
e := startServer(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)
|
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) {
|
func TestGetHistory(t *testing.T) {
|
||||||
e := startServer(t)
|
e := startServer(t)
|
||||||
|
|
||||||
e.POST("/user/1/history").WithJSON(PostHistory{Allowance: 100}).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}).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}).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 := e.GET("/user/1/history").Expect().Status(200).JSON().Array()
|
||||||
response.Length().IsEqual(3)
|
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("allowance").Number().IsEqual(100)
|
||||||
response.Value(0).Object().Value("timestamp").String().AsDateTime().InRange(getDelta(time.Now(), 2.0))
|
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("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("allowance").Number().IsEqual(-10)
|
||||||
|
response.Value(2).Object().Value("description").String().IsEqual("Subtract 10")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetUserAllowanceById(t *testing.T) {
|
func TestGetUserAllowanceById(t *testing.T) {
|
||||||
@ -690,10 +703,15 @@ func TestCompleteAllowance(t *testing.T) {
|
|||||||
// Verify history is updated
|
// Verify history is updated
|
||||||
history := e.GET("/user/1/history").Expect().Status(200).JSON().Array()
|
history := e.GET("/user/1/history").Expect().Status(200).JSON().Array()
|
||||||
history.Length().IsEqual(2)
|
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("allowance").Number().IsEqual(100)
|
||||||
history.Value(0).Object().Value("timestamp").String().AsDateTime().InRange(getDelta(time.Now(), 2.0))
|
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("allowance").Number().IsEqual(-100)
|
||||||
history.Value(1).Object().Value("timestamp").String().AsDateTime().InRange(getDelta(time.Now(), 2.0))
|
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) {
|
func TestCompleteAllowanceInvalidUserId(t *testing.T) {
|
||||||
|
@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"math"
|
"math"
|
||||||
"time"
|
"time"
|
||||||
@ -206,8 +207,9 @@ func (db *Db) CompleteAllowance(userId int, allowanceId int) error {
|
|||||||
|
|
||||||
// Get the cost of the allowance
|
// Get the cost of the allowance
|
||||||
var cost int
|
var cost int
|
||||||
err = tx.Query("select balance from allowances where id = ? and user_id = ?").
|
var allowanceName string
|
||||||
Bind(allowanceId, userId).ScanSingle(&cost)
|
err = tx.Query("select balance, name from allowances where id = ? and user_id = ?").
|
||||||
|
Bind(allowanceId, userId).ScanSingle(&cost, &allowanceName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -220,8 +222,8 @@ func (db *Db) CompleteAllowance(userId int, allowanceId int) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Add a history entry
|
// Add a history entry
|
||||||
err = tx.Query("insert into history (user_id, timestamp, amount) values (?, ?, ?)").
|
err = tx.Query("insert into history (user_id, timestamp, amount, description) values (?, ?, ?, ?)").
|
||||||
Bind(userId, time.Now().Unix(), -cost).
|
Bind(userId, time.Now().Unix(), -cost, fmt.Sprintf("Allowance completed: %s", allowanceName)).
|
||||||
Exec()
|
Exec()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -420,7 +422,8 @@ func (db *Db) CompleteTask(taskId int) error {
|
|||||||
defer tx.MustRollback()
|
defer tx.MustRollback()
|
||||||
|
|
||||||
var reward int
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -434,8 +437,8 @@ func (db *Db) CompleteTask(taskId int) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Add the history entry
|
// Add the history entry
|
||||||
err = tx.Query("insert into history (user_id, timestamp, amount) values (?, ?, ?)").
|
err = tx.Query("insert into history (user_id, timestamp, amount, description) values (?, ?, ?, ?)").
|
||||||
Bind(userId, time.Now().Unix(), reward).
|
Bind(userId, time.Now().Unix(), reward, fmt.Sprintf("Task completed: %s", rewardName)).
|
||||||
Exec()
|
Exec()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -499,8 +502,8 @@ func (db *Db) AddHistory(userId int, allowance *PostHistory) error {
|
|||||||
defer tx.MustRollback()
|
defer tx.MustRollback()
|
||||||
|
|
||||||
amount := int(math.Round(allowance.Allowance * 100.0))
|
amount := int(math.Round(allowance.Allowance * 100.0))
|
||||||
err = tx.Query("insert into history (user_id, timestamp, amount) values (?, ?, ?)").
|
err = tx.Query("insert into history (user_id, timestamp, amount, description) values (?, ?, ?, ?)").
|
||||||
Bind(userId, time.Now().Unix(), amount).
|
Bind(userId, time.Now().Unix(), amount, allowance.Description).
|
||||||
Exec()
|
Exec()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -512,11 +515,11 @@ func (db *Db) GetHistory(userId int) ([]History, error) {
|
|||||||
history := make([]History, 0)
|
history := make([]History, 0)
|
||||||
var err error
|
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) {
|
Bind(userId).Range(&err) {
|
||||||
allowance := History{}
|
allowance := History{}
|
||||||
var timestamp, amount int64
|
var timestamp, amount int64
|
||||||
err = row.Scan(&amount, ×tamp)
|
err = row.Scan(&amount, ×tamp, &allowance.Description)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -14,12 +14,14 @@ type UserWithAllowance struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type History struct {
|
type History struct {
|
||||||
Allowance float64 `json:"allowance"`
|
Allowance float64 `json:"allowance"`
|
||||||
Timestamp time.Time `json:"timestamp"`
|
Timestamp time.Time `json:"timestamp"`
|
||||||
|
Description string `json:"description"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type PostHistory struct {
|
type PostHistory struct {
|
||||||
Allowance float64 `json:"allowance"`
|
Allowance float64 `json:"allowance"`
|
||||||
|
Description string `json:"description"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Task represents a task in the system.
|
// Task represents a task in the system.
|
||||||
|
@ -539,6 +539,11 @@ func postHistory(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if historyRequest.Description == "" {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Description cannot be empty"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
exists, err := db.UserExists(userId)
|
exists, err := db.UserExists(userId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf(ErrCheckingUserExist, err)
|
log.Printf(ErrCheckingUserExist, err)
|
||||||
|
2
backend/migrations/4_add_history_description.sql
Normal file
2
backend/migrations/4_add_history_description.sql
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
alter table history
|
||||||
|
add column description text;
|
Loading…
x
Reference in New Issue
Block a user