Compare commits
1 Commits
aba4f850b7
...
4c68612ea0
Author | SHA1 | Date | |
---|---|---|---|
4c68612ea0 |
@ -516,7 +516,6 @@ func TestPutAllowanceById(t *testing.T) {
|
||||
"name": TestAllowanceName,
|
||||
"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())
|
||||
@ -526,7 +525,6 @@ func TestPutAllowanceById(t *testing.T) {
|
||||
"name": "Updated Allowance",
|
||||
"target": 6000,
|
||||
"weight": 15,
|
||||
"colour": "#3357FF",
|
||||
}
|
||||
e.PUT("/user/1/allowance/" + strconv.Itoa(allowanceId)).WithJSON(updateRequest).Expect().Status(200)
|
||||
|
||||
@ -536,7 +534,6 @@ func TestPutAllowanceById(t *testing.T) {
|
||||
result.Value("name").IsEqual("Updated Allowance")
|
||||
result.Value("target").IsEqual(6000)
|
||||
result.Value("weight").IsEqual(15)
|
||||
result.Value("colour").IsEqual("#3357FF")
|
||||
}
|
||||
|
||||
func TestCompleteTask(t *testing.T) {
|
||||
|
@ -1,30 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func ConvertStringToColour(colourStr string) (int, error) {
|
||||
if len(colourStr) == 0 {
|
||||
return 0xFF0000, nil // Default colour if no string is provided
|
||||
}
|
||||
if colourStr[0] == '#' {
|
||||
colourStr = colourStr[1:]
|
||||
}
|
||||
if len(colourStr) != 6 && len(colourStr) != 3 {
|
||||
return 0, errors.New("colour must be a valid hex string")
|
||||
}
|
||||
var colour int
|
||||
_, err := fmt.Sscanf(colourStr, "%x", &colour)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("invalid colour format: %v", err)
|
||||
}
|
||||
if len(colourStr) == 3 {
|
||||
r := (colour & 0xF00) >> 8
|
||||
g := (colour & 0x0F0) >> 4
|
||||
b := (colour & 0x00F) >> 0
|
||||
colour = (r << 16 << 4) | (g << 8 << 4) | (b << 0 << 4)
|
||||
}
|
||||
return colour, nil
|
||||
}
|
@ -1,30 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/require"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestConvertStringToColourWithSign(t *testing.T) {
|
||||
colour, err := ConvertStringToColour("#123456")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 0x123456, colour)
|
||||
}
|
||||
|
||||
func TestConvertStringToColourWithoutSign(t *testing.T) {
|
||||
colour, err := ConvertStringToColour("123456")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 0x123456, colour)
|
||||
}
|
||||
|
||||
func TestConvertStringToColourWithSignThreeDigits(t *testing.T) {
|
||||
colour, err := ConvertStringToColour("#ABC")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 0xA0B0C0, colour)
|
||||
}
|
||||
|
||||
func TestConvertStringToColourWithoutSignThreeDigits(t *testing.T) {
|
||||
colour, err := ConvertStringToColour("ABC")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 0xA0B0C0, colour)
|
||||
}
|
@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"math"
|
||||
"time"
|
||||
@ -113,13 +112,12 @@ func (db *Db) GetUserAllowanceById(userId int, allowanceId int) (*Allowance, err
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
var target, progress, colour int64
|
||||
err := db.db.Query("select id, name, target, balance, weight, colour from allowances where user_id = ? and id = ?").
|
||||
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, &target, &progress, &allowance.Weight, &colour)
|
||||
ScanSingle(&allowance.ID, &allowance.Name, &target, &progress, &allowance.Weight)
|
||||
allowance.Target = float64(target) / 100.0
|
||||
allowance.Progress = float64(progress) / 100.0
|
||||
allowance.Colour = fmt.Sprintf("#%06X", colour)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -143,15 +141,9 @@ 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, colour) values (?, ?, ?, ?, ?)").
|
||||
Bind(userId, allowance.Name, int(math.Round(allowance.Target*100.0)), allowance.Weight, colour).
|
||||
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).
|
||||
Exec()
|
||||
|
||||
if err != nil {
|
||||
@ -263,14 +255,9 @@ 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=?, colour=? where id = ? and user_id = ?").
|
||||
Bind(allowance.Name, target, allowance.Weight, colour, allowanceId, userId).
|
||||
err = tx.Query("update allowances set name=?, target=?, weight=? where id = ? and user_id = ?").
|
||||
Bind(allowance.Name, target, allowance.Weight, allowanceId, userId).
|
||||
Exec()
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -36,21 +36,18 @@ type Allowance struct {
|
||||
Target float64 `json:"target"`
|
||||
Progress float64 `json:"progress"`
|
||||
Weight float64 `json:"weight"`
|
||||
Colour string `json:"colour"`
|
||||
}
|
||||
|
||||
type CreateAllowanceRequest struct {
|
||||
Name string `json:"name"`
|
||||
Target float64 `json:"target"`
|
||||
Weight float64 `json:"weight"`
|
||||
Colour string `json:"colour"`
|
||||
}
|
||||
|
||||
type UpdateAllowanceRequest struct {
|
||||
Name string `json:"name"`
|
||||
Target float64 `json:"target"`
|
||||
Weight float64 `json:"weight"`
|
||||
Colour string `json:"colour"`
|
||||
}
|
||||
|
||||
type BulkUpdateAllowanceRequest struct {
|
||||
|
@ -1,2 +0,0 @@
|
||||
alter table allowances
|
||||
add column colour integer not null;
|
Loading…
x
Reference in New Issue
Block a user