Transaction entity and DB Handle

This patch introduces the Transaction entity.
It also creates a placeholder Handle type with a Transactions method as
a placeholder for the future DB adaptation.

Incorporates the new types into the retriveTransactions HTTP handler.
This commit is contained in:
Luís Murta 2024-02-05 23:47:27 +00:00
parent 771e4cad0c
commit a7359aacea
Signed by: satprog
GPG Key ID: 169EF1BBD7049F94
5 changed files with 56 additions and 10 deletions

View File

@ -3,13 +3,19 @@ package main
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"strings"
"time"
"git.rosemyrtle.work/personal-finance/server/internal/db"
"git.rosemyrtle.work/personal-finance/server/internal/model"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/shopspring/decimal"
) )
// Transaction struct represents a transaction // Transaction struct represents a transaction
type Transaction struct { type Transaction struct {
ID int64 `json:"id"` Id int64 `json:"id"`
Date string `json:"date"` Date string `json:"date"`
Description string `json:"description"` Description string `json:"description"`
Value float64 `json:"value"` Value float64 `json:"value"`
@ -37,13 +43,18 @@ func retrieveTransactions(c *gin.Context) {
// bank := c.Query("bank") // bank := c.Query("bank")
// sort := c.Query("sort") // sort := c.Query("sort")
// Placeholder response db := db.Handle{}
transactions := []Transaction{ transactions := db.Transactions()
{ID: 1, Date: "2024-01-24", Description: "Groceries", Value: -50.0, Category: "Food"}, var ret []Transaction
{ID: 2, Date: "2024-01-23", Description: "Salary", Value: 3000.0, Category: "Income"},
for _, t := range transactions {
year, month, day := t.Date.Date()
var b strings.Builder
fmt.Fprintf(&b, "%d-%d-%d", year, month, day)
ret = append(ret, Transaction{t.Id, b.String(), t.Description, t.Value.InexactFloat64(), t.Category})
} }
c.JSON(http.StatusOK, transactions) c.JSON(http.StatusOK, ret)
} }
func retrieveTransactionByID(c *gin.Context) { func retrieveTransactionByID(c *gin.Context) {
@ -51,11 +62,11 @@ func retrieveTransactionByID(c *gin.Context) {
// transactionID := c.Param("transactionId") // transactionID := c.Param("transactionId")
// Placeholder response // Placeholder response
transaction := Transaction{ transaction := model.Transaction{
ID: 1, Id: 1,
Date: "2024-01-24", Date: time.Date(2024, 01, 24, 0, 0, 0, 0, time.UTC),
Description: "Groceries", Description: "Groceries",
Value: -50.0, Value: decimal.NewFromFloat(-50.0),
Category: "Food", Category: "Food",
} }

1
go.mod
View File

@ -20,6 +20,7 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect github.com/ugorji/go/codec v1.2.11 // indirect
golang.org/x/arch v0.3.0 // indirect golang.org/x/arch v0.3.0 // indirect

2
go.sum
View File

@ -40,6 +40,8 @@ github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjY
github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZR9tGQ= github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZR9tGQ=
github.com/pelletier/go-toml/v2 v2.0.8/go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNcZljzZR9VXg+4= github.com/pelletier/go-toml/v2 v2.0.8/go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNcZljzZR9VXg+4=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=

17
internal/db/db.go Normal file
View File

@ -0,0 +1,17 @@
package db
import (
"time"
"git.rosemyrtle.work/personal-finance/server/internal/model"
"github.com/shopspring/decimal"
)
type Handle struct{}
func (*Handle) Transactions() []model.Transaction {
return []model.Transaction{
{Id: 1, Date: time.Date(2024, 01, 24, 0, 0, 0, 0, time.UTC), Description: "Groceries", Value: decimal.NewFromFloat(-50.0), Category: "Food"},
{Id: 2, Date: time.Date(2024, 01, 23, 0, 0, 0, 0, time.UTC), Description: "Salary", Value: decimal.NewFromFloat(3000.0), Category: "Income"},
}
}

View File

@ -0,0 +1,15 @@
package model
import (
"time"
"github.com/shopspring/decimal"
)
type Transaction struct {
Id int64
Date time.Time
Description string
Value decimal.Decimal
Category string
}