Luís Murta eb6220fdae
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.
2024-02-05 23:55:40 +00:00

112 lines
2.7 KiB
Go

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"`
Date string `json:"date"`
Description string `json:"description"`
Value float64 `json:"value"`
Category string `json:"category,omitempty"`
}
// Bank struct represents a bank
type Bank struct {
ID string `json:"id"`
Name string `json:"name"`
NordigenID string `json:"nordigenId,omitempty"`
}
// ResponseError struct represents an error response
type ResponseError struct {
Message string `json:"message"`
}
// Handlers
func retrieveTransactions(c *gin.Context) {
// Handle the logic for retrieving transactions here
// limit := c.Query("limit")
// offset := c.Query("offset")
// bank := c.Query("bank")
// sort := c.Query("sort")
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, ret)
}
func retrieveTransactionByID(c *gin.Context) {
// Handle the logic for retrieving a transaction by ID here
// transactionID := c.Param("transactionId")
// Placeholder response
transaction := 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",
}
c.JSON(http.StatusOK, transaction)
}
func retrieveBanks(c *gin.Context) {
// Handle the logic for retrieving banks here
// Placeholder response
banks := []Bank{
{ID: "1", Name: "Bank A", NordigenID: "uuid1"},
{ID: "2", Name: "Bank B", NordigenID: "uuid2"},
}
c.JSON(http.StatusOK, banks)
}
func retrieveBankByID(c *gin.Context) {
// Handle the logic for retrieving a bank by ID here
bankID := c.Param("bankId")
// Placeholder response
bank := Bank{ID: bankID, Name: "Bank A", NordigenID: "uuid1"}
c.JSON(http.StatusOK, bank)
}
func main() {
router := gin.Default()
// Routes
router.GET("/transactions", retrieveTransactions)
router.GET("/transactions/:transactionId", retrieveTransactionByID)
router.GET("/banks", retrieveBanks)
router.GET("/banks/:bankId", retrieveBankByID)
// Start server
port := 8080
fmt.Printf("Server is running on :%d...\n", port)
router.Run(fmt.Sprintf(":%d", port))
}