Support decimal currency amounts (#74)
Reviewed-on: #74
This commit was merged in pull request #74.
This commit is contained in:
@@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
"math"
|
||||
"time"
|
||||
|
||||
"gitea.seeseepuff.be/seeseemelk/mysqlite"
|
||||
@@ -49,8 +50,10 @@ func (db *Db) GetUsers() ([]User, error) {
|
||||
func (db *Db) GetUser(id int) (*UserWithAllowance, error) {
|
||||
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 = ?").
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
@@ -70,18 +73,23 @@ func (db *Db) UserExists(userId int) (bool, error) {
|
||||
func (db *Db) GetUserAllowances(userId int) ([]Allowance, error) {
|
||||
allowances := make([]Allowance, 0)
|
||||
var err error
|
||||
var progress int64
|
||||
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
totalAllowance.Progress = float64(progress) / 100.0
|
||||
allowances = append(allowances, totalAllowance)
|
||||
|
||||
for row := range db.db.Query("select id, name, target, balance, weight from allowances where user_id = ?").
|
||||
Bind(userId).Range(&err) {
|
||||
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 {
|
||||
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) {
|
||||
allowance := &Allowance{}
|
||||
if allowanceId == 0 {
|
||||
var progress int64
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
var target, progress int64
|
||||
err := db.db.Query("select id, name, target, balance, weight from allowances where user_id = ? and id = ?").
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
@@ -130,7 +143,7 @@ func (db *Db) CreateAllowance(userId int, allowance *CreateAllowanceRequest) (in
|
||||
|
||||
// Insert the new allowance
|
||||
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()
|
||||
|
||||
if err != nil {
|
||||
@@ -242,8 +255,9 @@ func (db *Db) UpdateAllowance(userId int, allowanceId int, allowance *UpdateAllo
|
||||
}
|
||||
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 = ?").
|
||||
Bind(allowance.Name, allowance.Target, allowance.Weight, allowanceId, userId).
|
||||
Bind(allowance.Name, target, allowance.Weight, allowanceId, userId).
|
||||
Exec()
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -284,8 +298,9 @@ func (db *Db) CreateTask(task *CreateTaskRequest) (int, error) {
|
||||
defer tx.MustRollback()
|
||||
|
||||
// Insert the new task
|
||||
reward := int(math.Round(task.Reward * 100.0))
|
||||
err = tx.Query("insert into tasks (name, reward, assigned) values (?, ?, ?)").
|
||||
Bind(task.Name, task.Reward, task.Assigned).
|
||||
Bind(task.Name, reward, task.Assigned).
|
||||
Exec()
|
||||
|
||||
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) {
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
@@ -329,8 +346,10 @@ func (db *Db) GetTasks() ([]Task, error) {
|
||||
func (db *Db) GetTask(id int) (Task, error) {
|
||||
task := Task{}
|
||||
|
||||
var reward int64
|
||||
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 {
|
||||
return Task{}, err
|
||||
}
|
||||
@@ -369,8 +388,9 @@ func (db *Db) UpdateTask(id int, task *CreateTaskRequest) error {
|
||||
}
|
||||
defer tx.MustRollback()
|
||||
|
||||
reward := int(math.Round(task.Reward * 100.0))
|
||||
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()
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -464,8 +484,9 @@ 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(), allowance.Allowance).
|
||||
Bind(userId, time.Now().Unix(), amount).
|
||||
Exec()
|
||||
if err != nil {
|
||||
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").
|
||||
Bind(userId).Range(&err) {
|
||||
allowance := History{}
|
||||
var timestamp int64
|
||||
err = row.Scan(&allowance.Allowance, ×tamp)
|
||||
var timestamp, amount int64
|
||||
err = row.Scan(&amount, ×tamp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
allowance.Allowance = float64(amount) / 100.0
|
||||
allowance.Timestamp = time.Unix(timestamp, 0)
|
||||
history = append(history, allowance)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user