From a82040720aa5141a07c4e22bd05eb8eb5086698f Mon Sep 17 00:00:00 2001 From: Sebastiaan de Schaetzen Date: Sun, 25 May 2025 14:20:38 +0200 Subject: [PATCH 1/4] Add gitea workflow (#78) Reviewed-on: https://gitea.seeseepuff.be/seeseemelk/allowance_planner_2000/pulls/78 --- .gitea/workflows/build.yml | 24 ++++++++++++++++++++++++ .gitea/workflows/deploy.yml | 25 +++++++++++++++++++++++++ backend/Dockerfile | 14 ++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 .gitea/workflows/build.yml create mode 100644 .gitea/workflows/deploy.yml create mode 100644 backend/Dockerfile diff --git a/.gitea/workflows/build.yml b/.gitea/workflows/build.yml new file mode 100644 index 0000000..9de519f --- /dev/null +++ b/.gitea/workflows/build.yml @@ -0,0 +1,24 @@ +name: Backend Build and Test +on: [push] + +jobs: + build: + runs-on: standard-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Setup Go + uses: actions/setup-go@v5 + with: + go-version: '>=1.24' + + - name: Build + run: | + cd backend + go build . + + - name: Test + run: | + cd backend + go test . -v diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml new file mode 100644 index 0000000..634f65f --- /dev/null +++ b/.gitea/workflows/deploy.yml @@ -0,0 +1,25 @@ +name: Backend Deploy +on: + push: [main] + +jobs: + build: + runs-on: standard-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Login + with: + package_rw: ${{ secrets.PACKAGE_RW }} + run: docker login gitea.seeseepuff.be -u seeseemelk -p ${{ secrets.PACKAGE_RW }} + + - name: Build + run: | + cd backend + docker build -t gitea.seeseepuff.be/seeseemelk/wolproxy:$(git rev-parse --short HEAD) . + + - name: Push + run: | + cd backend + docker push gitea.seeseepuff.be/seeseemelk/wolproxy:$(git rev-parse --short HEAD) diff --git a/backend/Dockerfile b/backend/Dockerfile new file mode 100644 index 0000000..95f19db --- /dev/null +++ b/backend/Dockerfile @@ -0,0 +1,14 @@ +FROM golang:1.24.2-alpine3.21 + +WORKDIR /app +COPY go.mod go.sum ./ +RUN go mod download + +COPY migrations ./migrations/ +COPY *.go ./ +COPY *.gohtml ./ +RUN go build -o /allowance_planner + +EXPOSE 8080 +ENV GIN_MODE=release +CMD ["/allowance_planner"] From cd23e72882267ca0af0c22970c3a74240ed30c4c Mon Sep 17 00:00:00 2001 From: Sebastiaan de Schaetzen Date: Sun, 25 May 2025 14:22:58 +0200 Subject: [PATCH 2/4] Allow null colour (#79) Reviewed-on: https://gitea.seeseepuff.be/seeseemelk/allowance_planner_2000/pulls/79 --- backend/migrations/2_add_colour.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/migrations/2_add_colour.sql b/backend/migrations/2_add_colour.sql index 2381df8..6dcb3bb 100644 --- a/backend/migrations/2_add_colour.sql +++ b/backend/migrations/2_add_colour.sql @@ -1,2 +1,2 @@ alter table allowances -add column colour integer not null; +add column colour integer; From 5a233073c7383dc9dd96bd8ff3696826be33d23f Mon Sep 17 00:00:00 2001 From: Sebastiaan de Schaetzen Date: Sun, 25 May 2025 14:24:25 +0200 Subject: [PATCH 3/4] Do deploy on main branch (#80) Reviewed-on: https://gitea.seeseepuff.be/seeseemelk/allowance_planner_2000/pulls/80 --- .gitea/workflows/deploy.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml index 634f65f..3d4e6d5 100644 --- a/.gitea/workflows/deploy.yml +++ b/.gitea/workflows/deploy.yml @@ -1,6 +1,8 @@ name: Backend Deploy on: - push: [main] + push: + branches: + - main jobs: build: From f9fb956efd7205e3ace2dffe2fb07d5a35d82b1a Mon Sep 17 00:00:00 2001 From: Sebastiaan de Schaetzen Date: Sun, 25 May 2025 14:36:17 +0200 Subject: [PATCH 4/4] Fix colour not being sent properly by backend (#81) Reviewed-on: https://gitea.seeseepuff.be/seeseemelk/allowance_planner_2000/pulls/81 --- .gitea/workflows/deploy.yml | 4 ++-- backend/api_test.go | 38 ++++++++++++++++++++++++------------- backend/colour.go | 4 ++++ backend/db.go | 13 +++++++------ 4 files changed, 38 insertions(+), 21 deletions(-) diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml index 3d4e6d5..d82960c 100644 --- a/.gitea/workflows/deploy.yml +++ b/.gitea/workflows/deploy.yml @@ -19,9 +19,9 @@ jobs: - name: Build run: | cd backend - docker build -t gitea.seeseepuff.be/seeseemelk/wolproxy:$(git rev-parse --short HEAD) . + docker build -t gitea.seeseepuff.be/seeseemelk/allowance-planner:$(git rev-parse --short HEAD) . - name: Push run: | cd backend - docker push gitea.seeseepuff.be/seeseemelk/wolproxy:$(git rev-parse --short HEAD) + docker push gitea.seeseepuff.be/seeseemelk/allowance-planner:$(git rev-parse --short HEAD) diff --git a/backend/api_test.go b/backend/api_test.go index 15e8480..06d4628 100644 --- a/backend/api_test.go +++ b/backend/api_test.go @@ -9,7 +9,7 @@ import ( ) const ( - TestAllowanceName = "Test History" + TestHistoryName = "Test History" ) func startServer(t *testing.T) *httpexpect.Expect { @@ -62,7 +62,7 @@ func TestGetUserAllowance(t *testing.T) { // Create a new allowance requestBody := map[string]interface{}{ - "name": TestAllowanceName, + "name": TestHistoryName, "target": 5000, "weight": 10, } @@ -73,7 +73,7 @@ func TestGetUserAllowance(t *testing.T) { result.Length().IsEqual(2) item := result.Value(1).Object() item.Value("id").IsEqual(1) - item.Value("name").IsEqual(TestAllowanceName) + item.Value("name").IsEqual(TestHistoryName) item.Value("target").IsEqual(5000) item.Value("weight").IsEqual(10) item.Value("progress").IsEqual(0) @@ -95,7 +95,7 @@ func TestCreateUserAllowance(t *testing.T) { // Create a new allowance requestBody := map[string]interface{}{ - "name": TestAllowanceName, + "name": TestHistoryName, "target": 5000, "weight": 10, } @@ -120,7 +120,7 @@ func TestCreateUserAllowance(t *testing.T) { allowance := allowances.Value(1).Object() allowance.Value("id").IsEqual(allowanceId) - allowance.Value("name").IsEqual(TestAllowanceName) + allowance.Value("name").IsEqual(TestHistoryName) allowance.Value("target").IsEqual(5000) allowance.Value("weight").IsEqual(10) allowance.Value("progress").IsEqual(0) @@ -130,7 +130,7 @@ func TestCreateUserAllowanceNoUser(t *testing.T) { e := startServer(t) requestBody := map[string]interface{}{ - "name": TestAllowanceName, + "name": TestHistoryName, "target": 5000, "weight": 10, } @@ -171,7 +171,7 @@ func TestCreateUserAllowanceBadId(t *testing.T) { e := startServer(t) requestBody := map[string]interface{}{ - "name": TestAllowanceName, + "name": TestHistoryName, "target": 5000, "weight": 10, } @@ -187,7 +187,7 @@ func TestDeleteUserAllowance(t *testing.T) { // Create a new allowance to delete createRequest := map[string]interface{}{ - "name": TestAllowanceName, + "name": TestHistoryName, "target": 1000, "weight": 5, } @@ -434,7 +434,7 @@ func TestPutTaskInvalidTaskId(t *testing.T) { e.PUT("/task/999").WithJSON(requestBody).Expect().Status(404) } -func TestPostAllowance(t *testing.T) { +func TestPostHistory(t *testing.T) { e := startServer(t) e.POST("/user/1/history").WithJSON(PostHistory{Allowance: 100}).Expect().Status(200) @@ -445,7 +445,7 @@ func TestPostAllowance(t *testing.T) { response.Value("allowance").Number().IsEqual(100 + 20 - 10) } -func TestPostAllowanceInvalidUserId(t *testing.T) { +func TestPostHistoryInvalidUserId(t *testing.T) { e := startServer(t) e.POST("/user/999/history").WithJSON(PostHistory{Allowance: 100}).Expect(). @@ -472,9 +472,10 @@ func TestGetUserAllowanceById(t *testing.T) { // Create a new allowance requestBody := map[string]interface{}{ - "name": TestAllowanceName, + "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()) @@ -482,10 +483,21 @@ func TestGetUserAllowanceById(t *testing.T) { // Retrieve the created allowance by ID result := e.GET("/user/1/allowance/" + strconv.Itoa(allowanceId)).Expect().Status(200).JSON().Object() result.Value("id").IsEqual(allowanceId) - result.Value("name").IsEqual(TestAllowanceName) + 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") + + 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) { @@ -513,7 +525,7 @@ func TestPutAllowanceById(t *testing.T) { // Create a new allowance requestBody := map[string]interface{}{ - "name": TestAllowanceName, + "name": TestHistoryName, "target": 5000, "weight": 10, "colour": "#FF5733", diff --git a/backend/colour.go b/backend/colour.go index 0886ad4..515de12 100644 --- a/backend/colour.go +++ b/backend/colour.go @@ -28,3 +28,7 @@ func ConvertStringToColour(colourStr string) (int, error) { } return colour, nil } + +func ConvertColourToString(colour int) string { + return fmt.Sprintf("#%06X", colour) +} diff --git a/backend/db.go b/backend/db.go index 151594d..447bb82 100644 --- a/backend/db.go +++ b/backend/db.go @@ -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 }