Adds type to OpenAPI. Feature done and tested E2E. Adds common utilities package named "golang" to overcome language deficiencies. Issue #27
71 lines
1.4 KiB
Go
71 lines
1.4 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
|
|
}
|
|
|
|
func entity2category(cat entity.Category) Category {
|
|
if cat.Group != nil {
|
|
return Category{Name: cat.Name, Group: &CategoryGroup{*cat.Group}}
|
|
}
|
|
return Category{Name: cat.Name}
|
|
}
|
|
|
|
func entities2categories(cats entity.Categories) []Category {
|
|
var ans []Category
|
|
for _, cat := range cats {
|
|
ans = append(ans, entity2category(cat))
|
|
}
|
|
return ans
|
|
}
|