feat: add expense repo type conversion
This commit is contained in:
parent
716a58d44c
commit
58cff774e6
85
internal/howmuch/adapter/repo/expense.go
Normal file
85
internal/howmuch/adapter/repo/expense.go
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
package repo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
|
||||||
|
"git.vinchent.xyz/vinchent/howmuch/internal/howmuch/adapter/repo/sqlc"
|
||||||
|
"git.vinchent.xyz/vinchent/howmuch/internal/howmuch/model"
|
||||||
|
"git.vinchent.xyz/vinchent/howmuch/internal/pkg/log"
|
||||||
|
)
|
||||||
|
|
||||||
|
func convToPayments(raw json.RawMessage) ([]model.Payment, error) {
|
||||||
|
var paymentsRetrieved []model.PaymentRetrieved
|
||||||
|
err := json.Unmarshal(raw, &paymentsRetrieved)
|
||||||
|
if err != nil {
|
||||||
|
// Unexpected
|
||||||
|
log.ErrorLog("json unmarshal error", "err", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var payments []model.Payment
|
||||||
|
for _, p := range paymentsRetrieved {
|
||||||
|
payment := model.Payment{
|
||||||
|
PayerID: p.PayerID,
|
||||||
|
PayerFirstName: p.PayerFirstName,
|
||||||
|
PayerLastName: p.PayerLastName,
|
||||||
|
Amount: model.MakeMoney(p.Amount, model.Currency(p.Currency)),
|
||||||
|
}
|
||||||
|
payments = append(payments, payment)
|
||||||
|
}
|
||||||
|
|
||||||
|
return payments, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func convToBenefits(raw json.RawMessage) ([]model.Benefit, error) {
|
||||||
|
var benefitsRetrieved []model.BenefitRetrieved
|
||||||
|
err := json.Unmarshal(raw, &benefitsRetrieved)
|
||||||
|
if err != nil {
|
||||||
|
// Unexpected
|
||||||
|
log.ErrorLog("json unmarshal error", "err", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var benefits []model.Benefit
|
||||||
|
for _, b := range benefitsRetrieved {
|
||||||
|
benefit := model.Benefit{
|
||||||
|
RecipientID: b.RecipientID,
|
||||||
|
RecipientFirstName: b.RecipientFirstName,
|
||||||
|
RecipientLastName: b.RecipientLastName,
|
||||||
|
Amount: model.MakeMoney(b.Amount, model.Currency(b.Currency)),
|
||||||
|
}
|
||||||
|
benefits = append(benefits, benefit)
|
||||||
|
}
|
||||||
|
|
||||||
|
return benefits, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func convToExpenseRetrieved(expenseDTO *sqlc.GetExpenseByIDRow) (*model.ExpenseRetrieved, error) {
|
||||||
|
payments, err := convToPayments(expenseDTO.Payments)
|
||||||
|
if err != nil {
|
||||||
|
// Unexpected
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
benefits, err := convToBenefits(expenseDTO.Benefits)
|
||||||
|
if err != nil {
|
||||||
|
// Unexpected
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
expenseRetrieved := &model.ExpenseRetrieved{
|
||||||
|
ID: int(expenseDTO.ID),
|
||||||
|
CreatedAt: expenseDTO.CreatedAt,
|
||||||
|
UpdatedAt: expenseDTO.UpdatedAt,
|
||||||
|
Amount: model.MakeMoney(int(expenseDTO.Amount), model.Currency(expenseDTO.Currency)),
|
||||||
|
EventID: int(expenseDTO.EventID),
|
||||||
|
Detail: model.ExpenseDetail{
|
||||||
|
Name: expenseDTO.Name.String,
|
||||||
|
Place: expenseDTO.Place.String,
|
||||||
|
},
|
||||||
|
Payments: payments,
|
||||||
|
Benefits: benefits,
|
||||||
|
}
|
||||||
|
|
||||||
|
return expenseRetrieved, nil
|
||||||
|
}
|
74
internal/howmuch/adapter/repo/expense_test.go
Normal file
74
internal/howmuch/adapter/repo/expense_test.go
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
package repo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.vinchent.xyz/vinchent/howmuch/internal/howmuch/adapter/repo/sqlc"
|
||||||
|
"git.vinchent.xyz/vinchent/howmuch/internal/howmuch/model"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestConvToExpenseRetrieved(t *testing.T) {
|
||||||
|
input := &sqlc.GetExpenseByIDRow{
|
||||||
|
ID: 123,
|
||||||
|
CreatedAt: time.Date(2000, time.April, 11, 0, 0, 0, 0, time.UTC),
|
||||||
|
UpdatedAt: time.Date(2000, time.April, 11, 0, 0, 0, 0, time.UTC),
|
||||||
|
Amount: 123,
|
||||||
|
Currency: "EUR",
|
||||||
|
EventID: 123,
|
||||||
|
Name: sql.NullString{Valid: false},
|
||||||
|
Place: sql.NullString{Valid: false},
|
||||||
|
Payments: json.RawMessage(
|
||||||
|
`[{"payer_id": 1, "payer_first_name": "toto", "payer_last_name": "titi", "amount": 10, "currency": "EUR"},
|
||||||
|
{"payer_id": 2, "payer_first_name": "tata", "payer_last_name": "titi", "amount": 10, "currency": "EUR"}]`,
|
||||||
|
),
|
||||||
|
Benefits: json.RawMessage(
|
||||||
|
`[{"recipient_id": 1, "recipient_first_name": "toto", "recipient_last_name": "titi", "amount": 10, "currency": "EUR"},
|
||||||
|
{"recipient_id": 2, "recipient_first_name": "tata", "recipient_last_name": "titi", "amount": 10, "currency": "EUR"}]`,
|
||||||
|
),
|
||||||
|
}
|
||||||
|
|
||||||
|
want := &model.ExpenseRetrieved{
|
||||||
|
ID: 123,
|
||||||
|
CreatedAt: time.Date(2000, time.April, 11, 0, 0, 0, 0, time.UTC),
|
||||||
|
UpdatedAt: time.Date(2000, time.April, 11, 0, 0, 0, 0, time.UTC),
|
||||||
|
Amount: model.Money{Amount: 123, Currency: model.Currency("EUR")},
|
||||||
|
EventID: 123,
|
||||||
|
Detail: model.ExpenseDetail{},
|
||||||
|
Payments: []model.Payment{
|
||||||
|
{
|
||||||
|
PayerID: 1,
|
||||||
|
PayerFirstName: "toto",
|
||||||
|
PayerLastName: "titi",
|
||||||
|
Amount: model.Money{Amount: 10, Currency: model.Currency("EUR")},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
PayerID: 2,
|
||||||
|
PayerFirstName: "tata",
|
||||||
|
PayerLastName: "titi",
|
||||||
|
Amount: model.Money{Amount: 10, Currency: model.Currency("EUR")},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Benefits: []model.Benefit{
|
||||||
|
{
|
||||||
|
RecipientID: 1,
|
||||||
|
RecipientFirstName: "toto",
|
||||||
|
RecipientLastName: "titi",
|
||||||
|
Amount: model.Money{Amount: 10, Currency: model.Currency("EUR")},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
RecipientID: 2,
|
||||||
|
RecipientFirstName: "tata",
|
||||||
|
RecipientLastName: "titi",
|
||||||
|
Amount: model.Money{Amount: 10, Currency: model.Currency("EUR")},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
got, err := convToExpenseRetrieved(input)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, want, got)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user