diff --git a/cmd/api-server/main.go b/cmd/api-server/main.go index a0be3fd..db210cf 100644 --- a/cmd/api-server/main.go +++ b/cmd/api-server/main.go @@ -3,13 +3,19 @@ package main import ( "fmt" "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/shopspring/decimal" ) // Transaction struct represents a transaction type Transaction struct { - ID int64 `json:"id"` + Id int64 `json:"id"` Date string `json:"date"` Description string `json:"description"` Value float64 `json:"value"` @@ -37,13 +43,18 @@ func retrieveTransactions(c *gin.Context) { // bank := c.Query("bank") // sort := c.Query("sort") - // Placeholder response - transactions := []Transaction{ - {ID: 1, Date: "2024-01-24", Description: "Groceries", Value: -50.0, Category: "Food"}, - {ID: 2, Date: "2024-01-23", Description: "Salary", Value: 3000.0, Category: "Income"}, + db := db.Handle{} + transactions := db.Transactions() + var ret []Transaction + + 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) { @@ -51,11 +62,11 @@ func retrieveTransactionByID(c *gin.Context) { // transactionID := c.Param("transactionId") // Placeholder response - transaction := Transaction{ - ID: 1, - Date: "2024-01-24", + transaction := model.Transaction{ + Id: 1, + Date: time.Date(2024, 01, 24, 0, 0, 0, 0, time.UTC), Description: "Groceries", - Value: -50.0, + Value: decimal.NewFromFloat(-50.0), Category: "Food", } diff --git a/go.mod b/go.mod index d38314d..330aeb1 100644 --- a/go.mod +++ b/go.mod @@ -20,6 +20,7 @@ require ( github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // 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/ugorji/go/codec v1.2.11 // indirect golang.org/x/arch v0.3.0 // indirect diff --git a/go.sum b/go.sum index e968ca4..8d35c7c 100644 --- a/go.sum +++ b/go.sum @@ -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/go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNcZljzZR9VXg+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.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= diff --git a/internal/db/db.go b/internal/db/db.go new file mode 100644 index 0000000..64da321 --- /dev/null +++ b/internal/db/db.go @@ -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"}, + } +} diff --git a/internal/model/entities.go b/internal/model/entities.go new file mode 100644 index 0000000..5f7ad13 --- /dev/null +++ b/internal/model/entities.go @@ -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 +}