|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "connectrpc.com/connect" |
| 5 | + "context" |
| 6 | + "github.com/ft-t/go-money-pb/gen/gomoneypb/currency/v1" |
| 7 | + "github.com/ft-t/go-money-pb/gen/gomoneypb/currency/v1/currencyv1connect" |
| 8 | + "github.com/ft-t/go-money/pkg/boilerplate" |
| 9 | + "github.com/shopspring/decimal" |
| 10 | +) |
| 11 | + |
| 12 | +type CurrencyApi struct { |
| 13 | + currencySvc CurrencySvc |
| 14 | + converterSvc ConverterSvc |
| 15 | + decimalSvc DecimalSvc |
| 16 | +} |
| 17 | + |
| 18 | +func NewCurrencyApi( |
| 19 | + mux *boilerplate.DefaultGrpcServer, |
| 20 | + currencySvc CurrencySvc, |
| 21 | + converterSvc ConverterSvc, |
| 22 | + decimalSvc DecimalSvc, |
| 23 | +) (*CurrencyApi, error) { |
| 24 | + res := &CurrencyApi{ |
| 25 | + currencySvc: currencySvc, |
| 26 | + converterSvc: converterSvc, |
| 27 | + decimalSvc: decimalSvc, |
| 28 | + } |
| 29 | + |
| 30 | + mux.GetMux().Handle( |
| 31 | + currencyv1connect.NewCurrencyServiceHandler(res, mux.GetDefaultHandlerOptions()...), |
| 32 | + ) |
| 33 | + |
| 34 | + return res, nil |
| 35 | +} |
| 36 | + |
| 37 | +func (a *CurrencyApi) Exchange( |
| 38 | + ctx context.Context, |
| 39 | + c *connect.Request[currencyv1.ExchangeRequest], |
| 40 | +) (*connect.Response[currencyv1.ExchangeResponse], error) { |
| 41 | + amount, err := decimal.NewFromString(c.Msg.Amount) |
| 42 | + if err != nil { |
| 43 | + return nil, err |
| 44 | + } |
| 45 | + |
| 46 | + resp, err := a.converterSvc.Convert(ctx, c.Msg.FromCurrency, c.Msg.ToCurrency, amount) |
| 47 | + if err != nil { |
| 48 | + return nil, err |
| 49 | + } |
| 50 | + |
| 51 | + return connect.NewResponse(¤cyv1.ExchangeResponse{ |
| 52 | + Amount: a.decimalSvc.ToString(ctx, resp, c.Msg.ToCurrency), |
| 53 | + }), nil |
| 54 | +} |
| 55 | + |
| 56 | +func (a *CurrencyApi) GetCurrencies( |
| 57 | + ctx context.Context, |
| 58 | + c *connect.Request[currencyv1.GetCurrenciesRequest], |
| 59 | +) (*connect.Response[currencyv1.GetCurrenciesResponse], error) { |
| 60 | + resp, err := a.currencySvc.GetCurrencies(ctx, c.Msg) |
| 61 | + if err != nil { |
| 62 | + return nil, err |
| 63 | + } |
| 64 | + |
| 65 | + return connect.NewResponse(resp), nil |
| 66 | +} |
| 67 | + |
| 68 | +func (a *CurrencyApi) CreateCurrency( |
| 69 | + ctx context.Context, |
| 70 | + c *connect.Request[currencyv1.CreateCurrencyRequest], |
| 71 | +) (*connect.Response[currencyv1.CreateCurrencyResponse], error) { |
| 72 | + // todo auth |
| 73 | + |
| 74 | + resp, err := a.currencySvc.CreateCurrency(ctx, c.Msg) |
| 75 | + if err != nil { |
| 76 | + return nil, err |
| 77 | + } |
| 78 | + |
| 79 | + return connect.NewResponse(resp), nil |
| 80 | +} |
| 81 | + |
| 82 | +func (a *CurrencyApi) UpdateCurrency( |
| 83 | + ctx context.Context, |
| 84 | + c *connect.Request[currencyv1.UpdateCurrencyRequest], |
| 85 | +) (*connect.Response[currencyv1.UpdateCurrencyResponse], error) { |
| 86 | + // todo auth |
| 87 | + |
| 88 | + resp, err := a.currencySvc.UpdateCurrency(ctx, c.Msg) |
| 89 | + if err != nil { |
| 90 | + return nil, err |
| 91 | + } |
| 92 | + |
| 93 | + return connect.NewResponse(resp), nil |
| 94 | +} |
0 commit comments