Fix colour not being sent it get /allowance
Some checks failed
Backend Build and Test / build (push) Has been cancelled

This commit is contained in:
Sebastiaan de Schaetzen 2025-05-25 14:32:42 +02:00
parent 7727bedbbd
commit 1bbf6b99e3
3 changed files with 23 additions and 27 deletions

View File

@ -475,6 +475,7 @@ func TestGetUserAllowanceById(t *testing.T) {
"name": TestHistoryName,
"target": 5000,
"weight": 10,
"colour": "#FF5733",
}
resp := e.POST("/user/1/allowance").WithJSON(requestBody).Expect().Status(201).JSON().Object()
allowanceId := int(resp.Value("id").Number().Raw())
@ -486,6 +487,17 @@ func TestGetUserAllowanceById(t *testing.T) {
result.Value("target").IsEqual(5000)
result.Value("weight").IsEqual(10)
result.Value("progress").IsEqual(0)
result.Value("colour").IsEqual("#FF5733")
resultArray := e.GET("/user/1/allowance").Expect().Status(200).JSON().Array()
resultArray.Length().IsEqual(2)
result = resultArray.Value(1).Object()
result.Value("id").IsEqual(allowanceId)
result.Value("name").IsEqual(TestHistoryName)
result.Value("target").IsEqual(5000)
result.Value("weight").IsEqual(10)
result.Value("progress").IsEqual(0)
result.Value("colour").IsEqual("#FF5733")
}
func TestGetUserByAllowanceIdInvalidAllowance(t *testing.T) {
@ -508,27 +520,6 @@ func TestGetUserByAllowanceByIdBadAllowanceId(t *testing.T) {
e.GET("/user/1/allowance/bad").Expect().Status(400)
}
func TestPostAllowance(t *testing.T) {
e := startServer(t)
// Create a new allowance
requestBody := map[string]interface{}{
"name": TestHistoryName,
"target": 5000,
"weight": 10,
"colour": "#FF5733",
}
resp := e.POST("/user/1/allowance").WithJSON(requestBody).Expect().Status(201).JSON().Object()
resp.Value("id").Number().IsEqual(1)
result := e.GET("/user/1/allowance/1").Expect().Status(200).JSON().Object()
result.Value("id").IsEqual(1)
result.Value("name").IsEqual("Test History")
result.Value("target").IsEqual(5000)
result.Value("weight").IsEqual(10)
result.Value("colour").IsEqual("#FF5733")
}
func TestPutAllowanceById(t *testing.T) {
e := startServer(t)

View File

@ -28,3 +28,7 @@ func ConvertStringToColour(colourStr string) (int, error) {
}
return colour, nil
}
func ConvertColourToString(colour int) string {
return fmt.Sprintf("#%06X", colour)
}

View File

@ -2,7 +2,6 @@ package main
import (
"errors"
"fmt"
"log"
"math"
"time"
@ -84,13 +83,14 @@ func (db *Db) GetUserAllowances(userId int) ([]Allowance, error) {
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 = ?").
for row := range db.db.Query("select id, name, target, balance, weight, colour from allowances where user_id = ?").
Bind(userId).Range(&err) {
allowance := Allowance{}
var target, progress int
err = row.Scan(&allowance.ID, &allowance.Name, &target, &progress, &allowance.Weight)
var target, progress, colour int
err = row.Scan(&allowance.ID, &allowance.Name, &target, &progress, &allowance.Weight, &colour)
allowance.Target = float64(target) / 100.0
allowance.Progress = float64(progress) / 100.0
allowance.Colour = ConvertColourToString(colour)
if err != nil {
return nil, err
}
@ -113,13 +113,14 @@ func (db *Db) GetUserAllowanceById(userId int, allowanceId int) (*Allowance, err
return nil, err
}
} else {
var target, progress, colour int64
var target, progress int64
var colour int
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, &colour)
allowance.Target = float64(target) / 100.0
allowance.Progress = float64(progress) / 100.0
allowance.Colour = fmt.Sprintf("#%06X", colour)
allowance.Colour = ConvertColourToString(colour)
if err != nil {
return nil, err
}