Add support for colour attribute on allowances in backend (#77)

Closes #76

Reviewed-on: #77
This commit was merged in pull request #77.
This commit is contained in:
2025-05-25 13:43:24 +02:00
parent f8d1f195de
commit a8e3332723
6 changed files with 88 additions and 7 deletions

View File

@@ -2,6 +2,7 @@ package main
import (
"errors"
"fmt"
"log"
"math"
"time"
@@ -112,12 +113,13 @@ func (db *Db) GetUserAllowanceById(userId int, allowanceId int) (*Allowance, err
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 = ?").
var target, progress, colour int64
err := db.db.Query("select id, name, target, balance, weight, colour from allowances where user_id = ? and id = ?").
Bind(userId, allowanceId).
ScanSingle(&allowance.ID, &allowance.Name, &target, &progress, &allowance.Weight)
ScanSingle(&allowance.ID, &allowance.Name, &target, &progress, &allowance.Weight, &colour)
allowance.Target = float64(target) / 100.0
allowance.Progress = float64(progress) / 100.0
allowance.Colour = fmt.Sprintf("#%06X", colour)
if err != nil {
return nil, err
}
@@ -141,9 +143,15 @@ func (db *Db) CreateAllowance(userId int, allowance *CreateAllowanceRequest) (in
}
defer tx.MustRollback()
// Convert string colour to a valid hex format
colour, err := ConvertStringToColour(allowance.Colour)
if err != nil {
return 0, err
}
// Insert the new allowance
err = tx.Query("insert into allowances (user_id, name, target, weight) values (?, ?, ?, ?)").
Bind(userId, allowance.Name, int(math.Round(allowance.Target*100.0)), allowance.Weight).
err = tx.Query("insert into allowances (user_id, name, target, weight, colour) values (?, ?, ?, ?, ?)").
Bind(userId, allowance.Name, int(math.Round(allowance.Target*100.0)), allowance.Weight, colour).
Exec()
if err != nil {
@@ -255,9 +263,14 @@ func (db *Db) UpdateAllowance(userId int, allowanceId int, allowance *UpdateAllo
}
defer tx.MustRollback()
colour, err := ConvertStringToColour(allowance.Colour)
if err != nil {
return err
}
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, target, allowance.Weight, allowanceId, userId).
err = tx.Query("update allowances set name=?, target=?, weight=?, colour=? where id = ? and user_id = ?").
Bind(allowance.Name, target, allowance.Weight, colour, allowanceId, userId).
Exec()
if err != nil {
return err