Compare commits

..

No commits in common. "4549d2927cb6ef3cd209f69e60ab5e7009ce0d99" and "56a19acd0fb0ce4c4017b4f9af14a7d2acb0a40b" have entirely different histories.

7 changed files with 21 additions and 51 deletions

Binary file not shown.

Binary file not shown.

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, 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)
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)
response := e.GET("/user/1").Expect().Status(200).JSON().Object()
response.Value("allowance").Number().IsEqual(100 + 20 - 10)
@ -448,36 +448,23 @@ func TestPostHistory(t *testing.T) {
func TestPostHistoryInvalidUserId(t *testing.T) {
e := startServer(t)
e.POST("/user/999/history").WithJSON(PostHistory{Allowance: 100, Description: "Good"}).Expect().
e.POST("/user/999/history").WithJSON(PostHistory{Allowance: 100}).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, 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)
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)
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) {
@ -703,15 +690,10 @@ 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,7 +2,6 @@ package main
import (
"errors"
"fmt"
"log"
"math"
"time"
@ -207,9 +206,8 @@ func (db *Db) CompleteAllowance(userId int, allowanceId int) error {
// Get the cost of the allowance
var cost int
var allowanceName string
err = tx.Query("select balance, name from allowances where id = ? and user_id = ?").
Bind(allowanceId, userId).ScanSingle(&cost, &allowanceName)
err = tx.Query("select balance from allowances where id = ? and user_id = ?").
Bind(allowanceId, userId).ScanSingle(&cost)
if err != nil {
return err
}
@ -222,8 +220,8 @@ func (db *Db) CompleteAllowance(userId int, allowanceId int) error {
}
// Add a history entry
err = tx.Query("insert into history (user_id, timestamp, amount, description) values (?, ?, ?, ?)").
Bind(userId, time.Now().Unix(), -cost, fmt.Sprintf("Allowance completed: %s", allowanceName)).
err = tx.Query("insert into history (user_id, timestamp, amount) values (?, ?, ?)").
Bind(userId, time.Now().Unix(), -cost).
Exec()
if err != nil {
return err
@ -422,8 +420,7 @@ func (db *Db) CompleteTask(taskId int) error {
defer tx.MustRollback()
var reward int
var rewardName string
err = tx.Query("select reward, name from tasks where id = ?").Bind(taskId).ScanSingle(&reward, &rewardName)
err = tx.Query("select reward from tasks where id = ?").Bind(taskId).ScanSingle(&reward)
if err != nil {
return err
}
@ -437,8 +434,8 @@ func (db *Db) CompleteTask(taskId int) error {
}
// Add the history entry
err = tx.Query("insert into history (user_id, timestamp, amount, description) values (?, ?, ?, ?)").
Bind(userId, time.Now().Unix(), reward, fmt.Sprintf("Task completed: %s", rewardName)).
err = tx.Query("insert into history (user_id, timestamp, amount) values (?, ?, ?)").
Bind(userId, time.Now().Unix(), reward).
Exec()
if err != nil {
return err
@ -502,8 +499,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, description) values (?, ?, ?, ?)").
Bind(userId, time.Now().Unix(), amount, allowance.Description).
err = tx.Query("insert into history (user_id, timestamp, amount) values (?, ?, ?)").
Bind(userId, time.Now().Unix(), amount).
Exec()
if err != nil {
return err
@ -515,11 +512,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`, description from history where user_id = ? order by `timestamp` desc").
for row := range db.db.Query("select amount, `timestamp` from history where user_id = ? order by `timestamp` desc").
Bind(userId).Range(&err) {
allowance := History{}
var timestamp, amount int64
err = row.Scan(&amount, &timestamp, &allowance.Description)
err = row.Scan(&amount, &timestamp)
if err != nil {
return nil, err
}

View File

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

View File

@ -539,11 +539,6 @@ 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

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