datastore/internal/api/converter.go
Luís Murta b506a575fa
Implements /transactions POST method
It adds the method to the OpenAPI spec and generates a new server
config. The requirement for the ID on the Transaction component is
removed, so that it can be reused for insertions.
It also adds two new middlewares, a logging and a spec validator. If a
request does not follow the spec, a 400 is returned immediately.

Issue: #18
2024-05-27 22:17:06 +01:00

56 lines
1.1 KiB
Go

package api
import (
"git.rosemyrtle.work/personal-finance/server/internal/entity"
openapi_types "github.com/oapi-codegen/runtime/types"
"github.com/shopspring/decimal"
)
func ptr[T any](v T) *T {
return &v
}
func entity2transaction(t entity.Transaction) Transaction {
return Transaction{
nil,
openapi_types.Date{Time: t.Date},
t.Description,
ptr(int64(t.Id)),
float32(t.Value.InexactFloat64()),
}
}
func transaction2entity(t Transaction) entity.Transaction {
var id uint64 = entity.InvalidId
if t.Id != nil {
id = uint64(*t.Id)
}
return entity.Transaction{
Id: id,
Date: t.Date.Time,
Description: t.Description,
Value: decimal.NewFromFloat32(t.Value),
}
}
func convertTransactions(ts entity.Transactions) Transactions {
var ans Transactions
for _, t := range ts {
ans = append(ans, entity2transaction(t))
}
return ans
}
func convertBank(b entity.Bank) Bank {
return Bank{b.Id, b.Name, &b.NordigenId}
}
func convertBanks(bs entity.Banks) Banks {
var ans Banks
for _, b := range bs {
ans = append(ans, convertBank(b))
}
return ans
}