through the PUT /transactions/{transactionId} method.
The category is also returned on a /transaction(s) call.
Restrict the PUT to changing only the category. The other existing
attributes should remain immutable.
Remove the body of the PUT response, it isn't required, and it was
returning a 204, which shouldn't have it.
This patch also extracts the CategoryName as a separate component on the
OpenAPI spec, so that it can be reused on the Transaction.
Issues #26 and #23
68 lines
1.5 KiB
Go
68 lines
1.5 KiB
Go
package api
|
|
|
|
import (
|
|
"git.rosemyrtle.work/personal-finance/server/internal/entity"
|
|
"git.rosemyrtle.work/personal-finance/server/internal/golang"
|
|
openapi_types "github.com/oapi-codegen/runtime/types"
|
|
"github.com/shopspring/decimal"
|
|
)
|
|
|
|
func entity2transaction(t entity.Transaction) Transaction {
|
|
return Transaction{
|
|
Id: golang.Ptr(int64(t.Id)),
|
|
Date: openapi_types.Date{Time: t.Date},
|
|
Description: t.Description,
|
|
Value: float32(t.Value.InexactFloat64()),
|
|
Category: t.Category,
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|