Compare commits

..

3 Commits

Author SHA1 Message Date
0397253ba7
Implement basic /transactions method 2024-03-09 17:45:17 +00:00
606f44e683
OpenAPI RESTful API code generator
This patch includes and uses the oapi-codegen tool to auto-generate go
boilerplate code, based on the OpenAPI spec.
2024-03-09 17:40:45 +00:00
eddb62b512
OpenAPI spec including transactions and banks 2024-02-25 11:08:30 +00:00
12 changed files with 531 additions and 371 deletions

View File

@ -1,130 +0,0 @@
package main
import (
"fmt"
"log"
"net/http"
"os"
"strings"
"time"
"git.rosemyrtle.work/personal-finance/server/internal/dal"
"git.rosemyrtle.work/personal-finance/server/internal/entities"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"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"`
}
type Env struct {
db *dal.Connection
}
func (e *Env) 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")
transactions := e.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 (e *Env) retrieveTransactionByID(c *gin.Context) {
// Handle the logic for retrieving a transaction by ID here
// transactionID := c.Param("transactionId")
// Placeholder response
transaction := entities.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 (e *Env) 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 (e *Env) 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() {
if err := godotenv.Load(); err != nil {
log.Fatal("Error loading .env file")
return
}
user := os.Getenv("PG_USER")
pass := os.Getenv("PG_PASSWORD")
location := os.Getenv("PG_URL")
database := os.Getenv("PG_DB")
credentials := entities.Credentials{Database: "postgres://" + user + ":" + pass + "@" + location + "/" + database}
db, err := dal.Open(&credentials)
if err != nil {
log.Fatal(err)
return
}
defer db.Close()
router := gin.Default()
env := Env{db: db}
// Routes
router.GET("/transactions", env.retrieveTransactions)
router.GET("/transactions/:transactionId", env.retrieveTransactionByID)
router.GET("/banks", env.retrieveBanks)
router.GET("/banks/:bankId", env.retrieveBankByID)
// Start server
port := 8080
fmt.Printf("Server is running on :%d...\n", port)
router.Run(fmt.Sprintf(":%d", port))
}

View File

@ -14,8 +14,14 @@ paths:
/transactions:
get:
summary: Retrieve existing transactions
operationId: retrieveTransactions
operationId: getTransactions
parameters:
- name: category
in: query
description: filter by transaction category
schema:
type: string
nullable: true
- name: limit
in: query
description: number of transactions to return
@ -54,7 +60,7 @@ paths:
/transactions/{transactionId}:
get:
summary: Find transaction by ID
operationId: retrieveTransactions
operationId: getTransactionsById
parameters:
- name: transactionId
in: path
@ -78,7 +84,7 @@ paths:
/banks:
get:
summary: Retrieve existing banks
operationId: retrieveBanks
operationId: getBanks
responses:
"200":
description: Successful operation
@ -92,7 +98,7 @@ paths:
/banks/{bankId}:
get:
summary: Find bank by ID
operationId: retrieveBank
operationId: getBanksById
parameters:
- name: bankId
in: path

53
go.mod
View File

@ -2,39 +2,32 @@ module git.rosemyrtle.work/personal-finance/server
go 1.21.1
require github.com/gin-gonic/gin v1.9.1
require (
github.com/DATA-DOG/go-sqlmock v1.5.2 // indirect
github.com/bytedance/sonic v1.9.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.14.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.5.3 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/joho/godotenv v1.5.1 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
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
github.com/DATA-DOG/go-sqlmock v1.5.2
github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect
github.com/deepmap/oapi-codegen/v2 v2.1.0 // indirect
github.com/getkin/kin-openapi v0.123.0 // indirect
github.com/go-openapi/jsonpointer v0.20.2 // indirect
github.com/go-openapi/swag v0.22.8 // indirect
github.com/google/uuid v1.5.0 // indirect
github.com/invopop/yaml v0.2.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/labstack/echo/v4 v4.11.4 // indirect
github.com/labstack/gommon v0.4.2 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
github.com/oapi-codegen/runtime v1.1.1 // indirect
github.com/perimeterx/marshmallow v1.1.5 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
golang.org/x/tools v0.12.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

135
go.sum
View File

@ -1,101 +1,68 @@
github.com/DATA-DOG/go-sqlmock v1.5.2 h1:OcvFkGmslmlZibjAjaHm3L//6LiuBgolP7OputlJIzU=
github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU=
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
github.com/bytedance/sonic v1.9.1 h1:6iJ6NqdoxCDr6mbY8h18oSO+cShGSMRGCEo7F2h0x8s=
github.com/bytedance/sonic v1.9.1/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U=
github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY=
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 h1:qSGYFH7+jGhDF8vLC+iwCD4WpbV1EBDSzWkJODFLams=
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk=
github.com/RaveNoX/go-jsoncommentstrip v1.0.0/go.mod h1:78ihd09MekBnJnxpICcwzCMzGrKSKYe4AqU6PDYYpjk=
github.com/apapsch/go-jsonmerge/v2 v2.0.0 h1:axGnT1gRIfimI7gJifB699GoE/oq+F2MU7Dml6nw9rQ=
github.com/apapsch/go-jsonmerge/v2 v2.0.0/go.mod h1:lvDnEdqiQrp0O42VQGgmlKpxL1AP2+08jFMw88y4klk=
github.com/bmatcuk/doublestar v1.1.1/go.mod h1:UD6OnuiIn0yFxxA2le/rnRU1G4RaI4UvFv1sNto9p6w=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU=
github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg=
github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.14.0 h1:vgvQWe3XCz3gIeFDm/HnTIbj6UGmg/+t63MyGU2n5js=
github.com/go-playground/validator/v10 v10.14.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.5.3 h1:Ces6/M3wbDXYpM8JyyPD57ivTtJACFZJd885pdIaV2s=
github.com/jackc/pgx/v5 v5.5.3/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A=
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/deepmap/oapi-codegen/v2 v2.1.0 h1:I/NMVhJCtuvL9x+S2QzZKpSjGi33oDZwPRdemvOZWyQ=
github.com/deepmap/oapi-codegen/v2 v2.1.0/go.mod h1:R1wL226vc5VmCNJUvMyYr3hJMm5reyv25j952zAVXZ8=
github.com/getkin/kin-openapi v0.123.0 h1:zIik0mRwFNLyvtXK274Q6ut+dPh6nlxBp0x7mNrPhs8=
github.com/getkin/kin-openapi v0.123.0/go.mod h1:wb1aSZA/iWmorQP9KTAS/phLj/t17B5jT7+fS8ed9NM=
github.com/go-openapi/jsonpointer v0.20.2 h1:mQc3nmndL8ZBzStEo3JYF8wzmeWffDH4VbXz58sAx6Q=
github.com/go-openapi/jsonpointer v0.20.2/go.mod h1:bHen+N0u1KEO3YlmqOjTT9Adn1RfD91Ar825/PuiRVs=
github.com/go-openapi/swag v0.22.8 h1:/9RjDSQ0vbFR+NyjGMkFTsA1IA0fmhKSThmfGZjicbw=
github.com/go-openapi/swag v0.22.8/go.mod h1:6QT22icPLEqAM/z/TChgb4WAveCHF92+2gF0CNjHpPI=
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/invopop/yaml v0.2.0 h1:7zky/qH+O0DwAyoobXUqvVBwgBFRxKoQ/3FjcVpjTMY=
github.com/invopop/yaml v0.2.0/go.mod h1:2XuRLgs/ouIrW3XNzuNj7J3Nvu/Dig5MXvbCEdiBN3Q=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/juju/gnuflag v0.0.0-20171113085948-2ce1bb71843d/go.mod h1:2PavIy+JPciBPrBUjwbNvtwB6RQlve+hkpll6QSNmOE=
github.com/kisielk/sqlstruct v0.0.0-20201105191214-5f3e10d3ab46/go.mod h1:yyMNCyc/Ib3bDTKd379tNMpB/7/H5TjM2Y9QJ5THLbE=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.2.4 h1:acbojRNwl3o09bUq+yDCtZFc1aiwaAAxtcn8YkZXnvk=
github.com/klauspost/cpuid/v2 v2.2.4/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY=
github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
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/labstack/echo/v4 v4.11.4 h1:vDZmA+qNeh1pd/cCkEicDMrjtrnMGQ1QFI9gWN1zGq8=
github.com/labstack/echo/v4 v4.11.4/go.mod h1:noh7EvLwqDsmh/X/HWKPUl1AjzJrhyptRyEbQJfxen8=
github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0=
github.com/labstack/gommon v0.4.2/go.mod h1:QlUFxVM+SNXhDL/Z7YhocGIBYOiwB0mXm1+1bAPHPyU=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9M+97sNutRR1RKhG96O6jWumTTnw=
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8=
github.com/oapi-codegen/runtime v1.1.1 h1:EXLHh0DXIJnWhdRPN2w4MXAzFyE4CskzhNLUmtpMYro=
github.com/oapi-codegen/runtime v1.1.1/go.mod h1:SK9X900oXmPWilYR5/WKPzt3Kqxn/uS/+lbpREv+eCg=
github.com/perimeterx/marshmallow v1.1.5 h1:a2LALqQ1BlHM8PZblsDdidgv1mWi1DgC2UmX50IvK2s=
github.com/perimeterx/marshmallow v1.1.5/go.mod h1:dsXbUu8CRzfYP5a87xpp0xq9S3u0Vchtcl8we9tYaXw=
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/spkg/bom v0.0.0-20160624110644-59b7046e48ad/go.mod h1:qLr4V1qq6nMqFKkMo8ZTx3f+BZEkzsRUY10Xsm2mwU0=
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=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU=
github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/arch v0.3.0 h1:02VY4/ZcO/gBOH6PUaoiptASxtXU10jazRCP865E97k=
golang.org/x/arch v0.3.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/crypto v0.9.0 h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g=
golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=
github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng=
google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
golang.org/x/tools v0.12.0 h1:YW6HUoUmYBpwSgyaGaZq1fHjrBjX1rlpZ54T6mu2kss=
golang.org/x/tools v0.12.0/go.mod h1:Sc0INKfu04TlqNoRA1hgpFZbhYXHPr4V5DzpSBTPqQM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=

34
internal/api/converter.go Normal file
View File

@ -0,0 +1,34 @@
package api
import (
"database/sql"
"log"
"reflect"
)
func convert[T any](rows *sql.Rows) []T {
var ans []T
for rows.Next() {
var r T
s := reflect.ValueOf(&r).Elem()
log.Println(s)
numCols := s.NumField()
columns := make([]interface{}, numCols)
log.Println(columns)
for i := 0; i < numCols; i++ {
field := s.Field(i)
log.Println(field)
columns[i] = field.Addr().Interface()
}
log.Println(columns)
if err := rows.Scan(columns...); err != nil {
log.Fatal(err)
}
ans = append(ans, r)
}
return ans
}

36
internal/api/impl.go Normal file
View File

@ -0,0 +1,36 @@
package api
//go:generate go run github.com/deepmap/oapi-codegen/v2/cmd/oapi-codegen --config=api.cfg.yaml ../../docs/openapi.yaml
import (
"database/sql"
"log"
"net/http"
"github.com/labstack/echo/v4"
)
type ServerImpl struct {
db *sql.DB
}
func (*ServerImpl) GetBanks(ctx echo.Context) error {
return echo.NewHTTPError(http.StatusNotImplemented)
}
func (*ServerImpl) GetBanksById(ctx echo.Context, bankId int64) error {
return echo.NewHTTPError(http.StatusNotImplemented)
}
func (pf *ServerImpl) GetTransactions(ctx echo.Context, params GetTransactionsParams) error {
rows, err := pf.db.Query("SELECT t.category, t.date, t.description, t.id, tc.amount FROM pfbudget.transactions t LEFT JOIN pfbudget.transactions_categorized tc ON t.id = tc.id")
if err != nil {
log.Fatal(err)
}
return ctx.JSON(http.StatusOK, convert[Transaction](rows))
}
func (*ServerImpl) GetTransactionsById(ctx echo.Context, transactionId int64) error {
return echo.NewHTTPError(http.StatusNotImplemented)
}

65
internal/api/impl_test.go Normal file
View File

@ -0,0 +1,65 @@
package api
import (
"log"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/DATA-DOG/go-sqlmock"
"github.com/labstack/echo/v4"
)
func TestGetTransactions(t *testing.T) {
db, mock, err := sqlmock.New()
if err != nil {
t.Fatal(err)
}
e := echo.New()
handlers := ServerImpl{db}
RegisterHandlers(e, &handlers)
t.Run("when successful", func(t *testing.T) {
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/transactions", nil)
date := time.Now()
rows := mock.NewRows([]string{"category", "date", "description", "id", "amount"}).
AddRow(nil, date, "#1", 1, 1000).
AddRow("expense", date, "#2", 2, -1000)
mock.ExpectQuery("SELECT t.category, t.date, t.description, t.id, tc.amount FROM pfbudget.transactions t LEFT JOIN pfbudget.transactions_categorized tc ON t.id = tc.id").WillReturnRows(rows)
ctx := e.NewContext(req, rec)
err := handlers.GetTransactions(ctx, GetTransactionsParams{})
if err != nil {
t.Error(err)
}
if rec.Code != http.StatusOK {
t.Error(rec.Code)
}
expected := `[
{
"date":"` + date.Format(time.DateOnly) + `",
"description": "#1",
"id":1,
"value":1000
},
{
"category": "expense",
"date":"` + date.Format(time.DateOnly) + `",
"description": "#2",
"id":2,
"value":-1000
}
]`
expected = strings.Join(strings.Fields(expected), "")
log.Println(expected)
if ret := strings.TrimRight(rec.Body.String(), "\n"); ret != expected {
t.Error(ret, expected)
}
})
}

312
internal/api/server.gen.go Normal file
View File

@ -0,0 +1,312 @@
// Package api provides primitives to interact with the openapi HTTP API.
//
// Code generated by github.com/deepmap/oapi-codegen/v2 version v2.1.0 DO NOT EDIT.
package api
import (
"bytes"
"compress/gzip"
"encoding/base64"
"errors"
"fmt"
"net/http"
"net/url"
"path"
"strings"
"time"
"github.com/getkin/kin-openapi/openapi3"
"github.com/labstack/echo/v4"
"github.com/oapi-codegen/runtime"
openapi_types "github.com/oapi-codegen/runtime/types"
)
// Bank defines model for Bank.
type Bank struct {
Id string `json:"id"`
Name string `json:"name"`
NordigenId *openapi_types.UUID `json:"nordigenId,omitempty"`
}
// Banks defines model for Banks.
type Banks = []Bank
// Manually added date time that implements the Scanner interface
type ScannableDate struct {
openapi_types.Date
}
func (d *ScannableDate) Scan(src any) error {
switch s := src.(type) {
case time.Time:
d.Time = s
return nil
}
return errors.New("column is not convertible to date")
}
// Transaction defines model for Transaction.
type Transaction struct {
Category *string `json:"category,omitempty"`
Date ScannableDate `json:"date"`
Description string `json:"description"`
Id int64 `json:"id"`
Value float32 `json:"value"`
}
// Transactions defines model for Transactions.
type Transactions = []Transaction
// GetTransactionsParams defines parameters for GetTransactions.
type GetTransactionsParams struct {
// Category filter by transaction category
Category *string `form:"category,omitempty" json:"category,omitempty"`
// Limit number of transactions to return
Limit *int32 `form:"limit,omitempty" json:"limit,omitempty"`
// Offset offset from where to retrieve transactions
Offset *int32 `form:"offset,omitempty" json:"offset,omitempty"`
// Bank ID of the bank
Bank *string `form:"bank,omitempty" json:"bank,omitempty"`
// Sort field name by which to sort
Sort *string `form:"sort,omitempty" json:"sort,omitempty"`
}
// ServerInterface represents all server handlers.
type ServerInterface interface {
// Retrieve existing banks
// (GET /banks)
GetBanks(ctx echo.Context) error
// Find bank by ID
// (GET /banks/{bankId})
GetBanksById(ctx echo.Context, bankId int64) error
// Retrieve existing transactions
// (GET /transactions)
GetTransactions(ctx echo.Context, params GetTransactionsParams) error
// Find transaction by ID
// (GET /transactions/{transactionId})
GetTransactionsById(ctx echo.Context, transactionId int64) error
}
// ServerInterfaceWrapper converts echo contexts to parameters.
type ServerInterfaceWrapper struct {
Handler ServerInterface
}
// GetBanks converts echo context to params.
func (w *ServerInterfaceWrapper) GetBanks(ctx echo.Context) error {
var err error
// Invoke the callback with all the unmarshaled arguments
err = w.Handler.GetBanks(ctx)
return err
}
// GetBanksById converts echo context to params.
func (w *ServerInterfaceWrapper) GetBanksById(ctx echo.Context) error {
var err error
// ------------- Path parameter "bankId" -------------
var bankId int64
err = runtime.BindStyledParameterWithOptions("simple", "bankId", ctx.Param("bankId"), &bankId, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationPath, Explode: false, Required: true})
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter bankId: %s", err))
}
// Invoke the callback with all the unmarshaled arguments
err = w.Handler.GetBanksById(ctx, bankId)
return err
}
// GetTransactions converts echo context to params.
func (w *ServerInterfaceWrapper) GetTransactions(ctx echo.Context) error {
var err error
// Parameter object where we will unmarshal all parameters from the context
var params GetTransactionsParams
// ------------- Optional query parameter "category" -------------
err = runtime.BindQueryParameter("form", true, false, "category", ctx.QueryParams(), &params.Category)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter category: %s", err))
}
// ------------- Optional query parameter "limit" -------------
err = runtime.BindQueryParameter("form", true, false, "limit", ctx.QueryParams(), &params.Limit)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter limit: %s", err))
}
// ------------- Optional query parameter "offset" -------------
err = runtime.BindQueryParameter("form", true, false, "offset", ctx.QueryParams(), &params.Offset)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter offset: %s", err))
}
// ------------- Optional query parameter "bank" -------------
err = runtime.BindQueryParameter("form", true, false, "bank", ctx.QueryParams(), &params.Bank)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter bank: %s", err))
}
// ------------- Optional query parameter "sort" -------------
err = runtime.BindQueryParameter("form", true, false, "sort", ctx.QueryParams(), &params.Sort)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter sort: %s", err))
}
// Invoke the callback with all the unmarshaled arguments
err = w.Handler.GetTransactions(ctx, params)
return err
}
// GetTransactionsById converts echo context to params.
func (w *ServerInterfaceWrapper) GetTransactionsById(ctx echo.Context) error {
var err error
// ------------- Path parameter "transactionId" -------------
var transactionId int64
err = runtime.BindStyledParameterWithOptions("simple", "transactionId", ctx.Param("transactionId"), &transactionId, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationPath, Explode: false, Required: true})
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter transactionId: %s", err))
}
// Invoke the callback with all the unmarshaled arguments
err = w.Handler.GetTransactionsById(ctx, transactionId)
return err
}
// This is a simple interface which specifies echo.Route addition functions which
// are present on both echo.Echo and echo.Group, since we want to allow using
// either of them for path registration
type EchoRouter interface {
CONNECT(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
DELETE(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
GET(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
HEAD(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
OPTIONS(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
PATCH(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
POST(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
PUT(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
TRACE(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
}
// RegisterHandlers adds each server route to the EchoRouter.
func RegisterHandlers(router EchoRouter, si ServerInterface) {
RegisterHandlersWithBaseURL(router, si, "")
}
// Registers handlers, and prepends BaseURL to the paths, so that the paths
// can be served under a prefix.
func RegisterHandlersWithBaseURL(router EchoRouter, si ServerInterface, baseURL string) {
wrapper := ServerInterfaceWrapper{
Handler: si,
}
router.GET(baseURL+"/banks", wrapper.GetBanks)
router.GET(baseURL+"/banks/:bankId", wrapper.GetBanksById)
router.GET(baseURL+"/transactions", wrapper.GetTransactions)
router.GET(baseURL+"/transactions/:transactionId", wrapper.GetTransactionsById)
}
// Base64 encoded, gzipped, json marshaled Swagger object
var swaggerSpec = []string{
"H4sIAAAAAAAC/8xWX2/bNhD/KgS3R0VS6mAPetqKoIGBoQvW7anrAy2dZHb8o5JHZ0bg7z4cKTuSJdcZ",
"sA57iSXzePf7c3fOM6+t7q0Bg55Xz9zXW9AiPr4V5k/67J3twaGE+K1s6C/ue+AV9+ik6fgh40ZoWD6w",
"rpEdmHW811qnBfKKhyAbnp2HHzLu4EuQDhpefeQxJGb+dAq1m89QI2UmfAkSgo4P3ztoecW/K144FQOh",
"IrI5nNII58Se3n9zwnhRo7RmTrYWCJ11+0VmjUCYcIpfZAuB4Gsn+2OJ2bmcSiMN/nDHM66lkTpoXpWn",
"nNIgdODo0k6oMC3fKivwpb4JekOhS5oOSMfAjhmXlB5p9HrBx8LOdCdU0rQ2imwNihrpEbSQiqhII0wN",
"P+rgUOQN7GYy8kdw3hqh2LsUyz6A24H7g5goWYPxUZ3Ul/zh/e/sAQw4odhj2ChZs59TENut8pJZx5RA",
"cDzjwRGCLWLvq6J4enrKOxNy67piSOuLrlc3q7y88ShMI5Q1kG9Rq0hTooIleDfslx7MT49rtspLUhuc",
"T0zKvMxv6a7twYhe8oqv8jJf8Yz3ArdR6GJzbPYOolLUpYKkoLniD4BpGshr31tCSVFvyvKoMJh4T/S9",
"knW8WXz2qSGTY68ZIJ+cmzrxIdQ1eN8GxU6oiM5deUc5p8HvLdsMiTLug9aCpov/Cugk7IDBX9KjNN0o",
"KnEvnulj3RyuivB2v26idk5oQHCeVx/PYazvmW1jDYaWOcDgqHEknZHox71T8VSVj4cIXYBspNpscs+n",
"9fDpG/vyz2wp57aszU4o2bD1PfOBoEBz0UIqyIxF1tpgmjMj30nTJF03e7a+Twbi2f64ZN9kz1xxsJUK",
"wVGVUXZ2WtiDl18CxJfBzNHxi7gmKCU2NLXJ2Nlv0nnptFmpgcbE5o10VlxJLXFSuYFWBIW8ui3LbNJG",
"qzdXfgDmqGzbekDWOqvZ0xYcDIDSXOFU2iV8KcEywJvbBXzXMaUxwy3ElrhQdzh6qXrVgFaCahhdJ/+f",
"trLeEllvHV6oMRxdrvEtJ3TS1v/CAsVpvq/v0VnwZBqL59HbleU6pvH6HTueziurdgLlf7xxJ//Z/IeL",
"d1T36/t3rPlxDR8OfwcAAP//XTQsSusLAAA=",
}
// GetSwagger returns the content of the embedded swagger specification file
// or error if failed to decode
func decodeSpec() ([]byte, error) {
zipped, err := base64.StdEncoding.DecodeString(strings.Join(swaggerSpec, ""))
if err != nil {
return nil, fmt.Errorf("error base64 decoding spec: %w", err)
}
zr, err := gzip.NewReader(bytes.NewReader(zipped))
if err != nil {
return nil, fmt.Errorf("error decompressing spec: %w", err)
}
var buf bytes.Buffer
_, err = buf.ReadFrom(zr)
if err != nil {
return nil, fmt.Errorf("error decompressing spec: %w", err)
}
return buf.Bytes(), nil
}
var rawSpec = decodeSpecCached()
// a naive cached of a decoded swagger spec
func decodeSpecCached() func() ([]byte, error) {
data, err := decodeSpec()
return func() ([]byte, error) {
return data, err
}
}
// Constructs a synthetic filesystem for resolving external references when loading openapi specifications.
func PathToRawSpec(pathToFile string) map[string]func() ([]byte, error) {
res := make(map[string]func() ([]byte, error))
if len(pathToFile) > 0 {
res[pathToFile] = rawSpec
}
return res
}
// GetSwagger returns the Swagger specification corresponding to the generated code
// in this file. The external references of Swagger specification are resolved.
// The logic of resolving external references is tightly connected to "import-mapping" feature.
// Externally referenced files must be embedded in the corresponding golang packages.
// Urls can be supported but this task was out of the scope.
func GetSwagger() (swagger *openapi3.T, err error) {
resolvePath := PathToRawSpec("")
loader := openapi3.NewLoader()
loader.IsExternalRefsAllowed = true
loader.ReadFromURIFunc = func(loader *openapi3.Loader, url *url.URL) ([]byte, error) {
pathToFile := url.String()
pathToFile = path.Clean(pathToFile)
getSpec, ok := resolvePath[pathToFile]
if !ok {
err1 := fmt.Errorf("path not found: %s", pathToFile)
return nil, err1
}
return getSpec()
}
var specData []byte
specData, err = rawSpec()
if err != nil {
return
}
swagger, err = loader.LoadFromData(specData)
if err != nil {
return
}
return
}

View File

@ -1,60 +0,0 @@
package dal
import (
"database/sql"
"errors"
"fmt"
"log"
"os"
"git.rosemyrtle.work/personal-finance/server/internal/entities"
_ "github.com/jackc/pgx/v5/stdlib"
)
type Connection struct {
db *sql.DB
}
func Open(credentials *entities.Credentials) (*Connection, error) {
if credentials == nil {
return nil, errors.New("missing credentials")
}
db, err := sql.Open("pgx", credentials.Database)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
return nil, err
}
if err := db.Ping(); err != nil {
log.Fatal(err)
}
return &Connection{db}, nil
}
func (c *Connection) Close() {
c.db.Close()
}
func (conn *Connection) Transactions() []entities.Transaction {
q := "SELECT t.date, t.description, t.amount FROM transactions t;"
rows, err := conn.db.Query(q)
if err != nil {
log.Fatal(err)
}
var result []entities.Transaction
for rows.Next() {
var t entities.Transaction
if err := rows.Scan(&t.Date, &t.Description, &t.Value); err != nil {
log.Fatal(err)
}
result = append(result, t)
}
return result
}

View File

@ -1,43 +0,0 @@
package dal
import (
"database/sql/driver"
"testing"
"time"
"github.com/DATA-DOG/go-sqlmock"
)
type AnyTime struct{}
func (t AnyTime) Match(v driver.Value) bool {
_, ok := v.(time.Time)
return ok
}
// type AnyDecimal struct{}
func TestTransactions(t *testing.T) {
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("failed to create a mock DB: %s", err)
}
defer db.Close()
conn := Connection{db}
rows := sqlmock.NewRows([]string{"date", "description", "amount"}).
AddRow(time.Now(), "income", 1000).
AddRow(time.Now(), "expense", -500)
mock.ExpectQuery("SELECT t.date, t.description, t.amount").WithoutArgs().WillReturnRows(rows)
transactions := conn.Transactions()
if len(transactions) <= 0 {
t.Error("No transactions returned")
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("unfulfilled expectations: %s", err)
}
}

View File

@ -1,5 +0,0 @@
package entities
type Credentials struct {
Database string
}

View File

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