Support decimal currency amounts (#74)

Reviewed-on: #74
This commit is contained in:
Sebastiaan de Schaetzen 2025-05-24 06:28:07 +02:00
parent 426e456ba7
commit f8d1f195de
4 changed files with 57 additions and 35 deletions

View File

@ -568,9 +568,9 @@ func TestCompleteTask(t *testing.T) {
allowances := e.GET("/user/1/allowance").Expect().Status(200).JSON().Array() allowances := e.GET("/user/1/allowance").Expect().Status(200).JSON().Array()
allowances.Length().IsEqual(3) allowances.Length().IsEqual(3)
allowances.Value(0).Object().Value("id").Number().IsEqual(0) allowances.Value(0).Object().Value("id").Number().IsEqual(0)
allowances.Value(0).Object().Value("progress").Number().IsEqual(31) allowances.Value(0).Object().Value("progress").Number().InDelta(30.34, 0.01)
allowances.Value(1).Object().Value("id").Number().IsEqual(1) allowances.Value(1).Object().Value("id").Number().IsEqual(1)
allowances.Value(1).Object().Value("progress").Number().IsEqual(60) allowances.Value(1).Object().Value("progress").Number().InDelta(60.66, 0.01)
allowances.Value(2).Object().Value("id").Number().IsEqual(2) allowances.Value(2).Object().Value("id").Number().IsEqual(2)
allowances.Value(2).Object().Value("progress").Number().IsEqual(10) allowances.Value(2).Object().Value("progress").Number().IsEqual(10)
@ -696,7 +696,7 @@ func getDelta(base time.Time, delta float64) (time.Time, time.Time) {
return start, end return start, end
} }
func createTestAllowance(e *httpexpect.Expect, name string, target int, weight float64) { func createTestAllowance(e *httpexpect.Expect, name string, target float64, weight float64) {
e.POST("/user/1/allowance").WithJSON(CreateAllowanceRequest{ e.POST("/user/1/allowance").WithJSON(CreateAllowanceRequest{
Name: name, Name: name,
Target: target, Target: target,

View File

@ -3,6 +3,7 @@ package main
import ( import (
"errors" "errors"
"log" "log"
"math"
"time" "time"
"gitea.seeseepuff.be/seeseemelk/mysqlite" "gitea.seeseepuff.be/seeseemelk/mysqlite"
@ -49,8 +50,10 @@ func (db *Db) GetUsers() ([]User, error) {
func (db *Db) GetUser(id int) (*UserWithAllowance, error) { func (db *Db) GetUser(id int) (*UserWithAllowance, error) {
user := &UserWithAllowance{} user := &UserWithAllowance{}
var allowance int
err := db.db.Query("select u.id, u.name, (select ifnull(sum(h.amount), 0) from history h where h.user_id = u.id) from users u where u.id = ?"). err := db.db.Query("select u.id, u.name, (select ifnull(sum(h.amount), 0) from history h where h.user_id = u.id) from users u where u.id = ?").
Bind(id).ScanSingle(&user.ID, &user.Name, &user.Allowance) Bind(id).ScanSingle(&user.ID, &user.Name, &allowance)
user.Allowance = float64(allowance) / 100.0
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -70,18 +73,23 @@ func (db *Db) UserExists(userId int) (bool, error) {
func (db *Db) GetUserAllowances(userId int) ([]Allowance, error) { func (db *Db) GetUserAllowances(userId int) ([]Allowance, error) {
allowances := make([]Allowance, 0) allowances := make([]Allowance, 0)
var err error var err error
var progress int64
totalAllowance := Allowance{} totalAllowance := Allowance{}
err = db.db.Query("select balance, weight from users where id = ?").Bind(userId).ScanSingle(&totalAllowance.Progress, &totalAllowance.Weight) err = db.db.Query("select balance, weight from users where id = ?").Bind(userId).ScanSingle(&progress, &totalAllowance.Weight)
if err != nil { if err != nil {
return nil, err return nil, err
} }
totalAllowance.Progress = float64(progress) / 100.0
allowances = append(allowances, totalAllowance) allowances = append(allowances, totalAllowance)
for row := range db.db.Query("select id, name, target, balance, weight from allowances where user_id = ?"). for row := range db.db.Query("select id, name, target, balance, weight from allowances where user_id = ?").
Bind(userId).Range(&err) { Bind(userId).Range(&err) {
allowance := Allowance{} allowance := Allowance{}
err = row.Scan(&allowance.ID, &allowance.Name, &allowance.Target, &allowance.Progress, &allowance.Weight) var target, progress int
err = row.Scan(&allowance.ID, &allowance.Name, &target, &progress, &allowance.Weight)
allowance.Target = float64(target) / 100.0
allowance.Progress = float64(progress) / 100.0
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -96,15 +104,20 @@ func (db *Db) GetUserAllowances(userId int) ([]Allowance, error) {
func (db *Db) GetUserAllowanceById(userId int, allowanceId int) (*Allowance, error) { func (db *Db) GetUserAllowanceById(userId int, allowanceId int) (*Allowance, error) {
allowance := &Allowance{} allowance := &Allowance{}
if allowanceId == 0 { if allowanceId == 0 {
var progress int64
err := db.db.Query("select balance, weight from users where id = ?"). err := db.db.Query("select balance, weight from users where id = ?").
Bind(userId).ScanSingle(&allowance.Progress, &allowance.Weight) Bind(userId).ScanSingle(&progress, &allowance.Weight)
allowance.Progress = float64(progress) / 100.0
if err != nil { if err != nil {
return nil, err return nil, err
} }
} else { } else {
var target, progress int64
err := db.db.Query("select id, name, target, balance, weight from allowances where user_id = ? and id = ?"). err := db.db.Query("select id, name, target, balance, weight from allowances where user_id = ? and id = ?").
Bind(userId, allowanceId). Bind(userId, allowanceId).
ScanSingle(&allowance.ID, &allowance.Name, &allowance.Target, &allowance.Progress, &allowance.Weight) ScanSingle(&allowance.ID, &allowance.Name, &target, &progress, &allowance.Weight)
allowance.Target = float64(target) / 100.0
allowance.Progress = float64(progress) / 100.0
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -130,7 +143,7 @@ func (db *Db) CreateAllowance(userId int, allowance *CreateAllowanceRequest) (in
// Insert the new allowance // Insert the new allowance
err = tx.Query("insert into allowances (user_id, name, target, weight) values (?, ?, ?, ?)"). err = tx.Query("insert into allowances (user_id, name, target, weight) values (?, ?, ?, ?)").
Bind(userId, allowance.Name, allowance.Target, allowance.Weight). Bind(userId, allowance.Name, int(math.Round(allowance.Target*100.0)), allowance.Weight).
Exec() Exec()
if err != nil { if err != nil {
@ -242,8 +255,9 @@ func (db *Db) UpdateAllowance(userId int, allowanceId int, allowance *UpdateAllo
} }
defer tx.MustRollback() defer tx.MustRollback()
target := int(math.Round(allowance.Target * 100.0))
err = tx.Query("update allowances set name=?, target=?, weight=? where id = ? and user_id = ?"). err = tx.Query("update allowances set name=?, target=?, weight=? where id = ? and user_id = ?").
Bind(allowance.Name, allowance.Target, allowance.Weight, allowanceId, userId). Bind(allowance.Name, target, allowance.Weight, allowanceId, userId).
Exec() Exec()
if err != nil { if err != nil {
return err return err
@ -284,8 +298,9 @@ func (db *Db) CreateTask(task *CreateTaskRequest) (int, error) {
defer tx.MustRollback() defer tx.MustRollback()
// Insert the new task // Insert the new task
reward := int(math.Round(task.Reward * 100.0))
err = tx.Query("insert into tasks (name, reward, assigned) values (?, ?, ?)"). err = tx.Query("insert into tasks (name, reward, assigned) values (?, ?, ?)").
Bind(task.Name, task.Reward, task.Assigned). Bind(task.Name, reward, task.Assigned).
Exec() Exec()
if err != nil { if err != nil {
@ -314,7 +329,9 @@ func (db *Db) GetTasks() ([]Task, error) {
for row := range db.db.Query("select id, name, reward, assigned from tasks").Range(&err) { for row := range db.db.Query("select id, name, reward, assigned from tasks").Range(&err) {
task := Task{} task := Task{}
err = row.Scan(&task.ID, &task.Name, &task.Reward, &task.Assigned) var reward int64
err = row.Scan(&task.ID, &task.Name, &reward, &task.Assigned)
task.Reward = float64(reward) / 100.0
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -329,8 +346,10 @@ func (db *Db) GetTasks() ([]Task, error) {
func (db *Db) GetTask(id int) (Task, error) { func (db *Db) GetTask(id int) (Task, error) {
task := Task{} task := Task{}
var reward int64
err := db.db.Query("select id, name, reward, assigned from tasks where id = ?"). err := db.db.Query("select id, name, reward, assigned from tasks where id = ?").
Bind(id).ScanSingle(&task.ID, &task.Name, &task.Reward, &task.Assigned) Bind(id).ScanSingle(&task.ID, &task.Name, &reward, &task.Assigned)
task.Reward = float64(reward) / 100.0
if err != nil { if err != nil {
return Task{}, err return Task{}, err
} }
@ -369,8 +388,9 @@ func (db *Db) UpdateTask(id int, task *CreateTaskRequest) error {
} }
defer tx.MustRollback() defer tx.MustRollback()
reward := int(math.Round(task.Reward * 100.0))
err = tx.Query("update tasks set name=?, reward=?, assigned=? where id = ?"). err = tx.Query("update tasks set name=?, reward=?, assigned=? where id = ?").
Bind(task.Name, task.Reward, task.Assigned, id). Bind(task.Name, reward, task.Assigned, id).
Exec() Exec()
if err != nil { if err != nil {
return err return err
@ -464,8 +484,9 @@ func (db *Db) AddHistory(userId int, allowance *PostHistory) error {
} }
defer tx.MustRollback() defer tx.MustRollback()
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) values (?, ?, ?)").
Bind(userId, time.Now().Unix(), allowance.Allowance). Bind(userId, time.Now().Unix(), amount).
Exec() Exec()
if err != nil { if err != nil {
return err return err
@ -480,11 +501,12 @@ func (db *Db) GetHistory(userId int) ([]History, 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` from history where user_id = ? order by `timestamp` desc").
Bind(userId).Range(&err) { Bind(userId).Range(&err) {
allowance := History{} allowance := History{}
var timestamp int64 var timestamp, amount int64
err = row.Scan(&allowance.Allowance, &timestamp) err = row.Scan(&amount, &timestamp)
if err != nil { if err != nil {
return nil, err return nil, err
} }
allowance.Allowance = float64(amount) / 100.0
allowance.Timestamp = time.Unix(timestamp, 0) allowance.Timestamp = time.Unix(timestamp, 0)
history = append(history, allowance) history = append(history, allowance)
} }

View File

@ -8,45 +8,45 @@ type User struct {
} }
type UserWithAllowance struct { type UserWithAllowance struct {
ID int `json:"id"` ID int `json:"id"`
Name string `json:"name"` Name string `json:"name"`
Allowance int `json:"allowance"` Allowance float64 `json:"allowance"`
} }
type History struct { type History struct {
Allowance int `json:"allowance"` Allowance float64 `json:"allowance"`
Timestamp time.Time `json:"timestamp"` Timestamp time.Time `json:"timestamp"`
} }
type PostHistory struct { type PostHistory struct {
Allowance int `json:"allowance"` Allowance float64 `json:"allowance"`
} }
// Task represents a task in the system. // Task represents a task in the system.
type Task struct { type Task struct {
ID int `json:"id"` ID int `json:"id"`
Name string `json:"name"` Name string `json:"name"`
Reward int `json:"reward"` Reward float64 `json:"reward"`
Assigned *int `json:"assigned"` // Pointer to allow null Assigned *int `json:"assigned"` // Pointer to allow null
} }
type Allowance struct { type Allowance struct {
ID int `json:"id"` ID int `json:"id"`
Name string `json:"name"` Name string `json:"name"`
Target int `json:"target"` Target float64 `json:"target"`
Progress int `json:"progress"` Progress float64 `json:"progress"`
Weight float64 `json:"weight"` Weight float64 `json:"weight"`
} }
type CreateAllowanceRequest struct { type CreateAllowanceRequest struct {
Name string `json:"name"` Name string `json:"name"`
Target int `json:"target"` Target float64 `json:"target"`
Weight float64 `json:"weight"` Weight float64 `json:"weight"`
} }
type UpdateAllowanceRequest struct { type UpdateAllowanceRequest struct {
Name string `json:"name"` Name string `json:"name"`
Target int `json:"target"` Target float64 `json:"target"`
Weight float64 `json:"weight"` Weight float64 `json:"weight"`
} }
@ -60,9 +60,9 @@ type CreateGoalResponse struct {
} }
type CreateTaskRequest struct { type CreateTaskRequest struct {
Name string `json:"name" binding:"required"` Name string `json:"name" binding:"required"`
Reward int `json:"reward"` Reward float64 `json:"reward"`
Assigned *int `json:"assigned"` Assigned *int `json:"assigned"`
} }
type CreateTaskResponse struct { type CreateTaskResponse struct {

View File

@ -49,7 +49,7 @@ func renderCreateTask(c *gin.Context) {
name := c.PostForm("name") name := c.PostForm("name")
rewardStr := c.PostForm("reward") rewardStr := c.PostForm("reward")
reward, err := strconv.Atoi(rewardStr) reward, err := strconv.ParseFloat(rewardStr, 64)
if err != nil { if err != nil {
renderError(c, http.StatusBadRequest, err) renderError(c, http.StatusBadRequest, err)
return return
@ -96,7 +96,7 @@ func renderCreateAllowance(c *gin.Context) {
name := c.PostForm("name") name := c.PostForm("name")
targetStr := c.PostForm("target") targetStr := c.PostForm("target")
target, err := strconv.Atoi(targetStr) target, err := strconv.ParseFloat(targetStr, 64)
if err != nil { if err != nil {
renderError(c, http.StatusBadRequest, err) renderError(c, http.StatusBadRequest, err)
return return