|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "connectrpc.com/connect" |
| 5 | + "context" |
| 6 | + accountsv1 "github.com/ft-t/go-money-pb/gen/gomoneypb/accounts/v1" |
| 7 | + "github.com/ft-t/go-money-pb/gen/gomoneypb/accounts/v1/accountsv1connect" |
| 8 | + "github.com/ft-t/go-money/pkg/boilerplate" |
| 9 | +) |
| 10 | + |
| 11 | +type AccountsApi struct { |
| 12 | + accSvc AccountSvc |
| 13 | +} |
| 14 | + |
| 15 | +func (a *AccountsApi) CreateAccount( |
| 16 | + ctx context.Context, |
| 17 | + c *connect.Request[accountsv1.CreateAccountRequest], |
| 18 | +) (*connect.Response[accountsv1.CreateAccountResponse], error) { |
| 19 | + // todo auth |
| 20 | + |
| 21 | + resp, err := a.accSvc.Create(ctx, c.Msg) |
| 22 | + if err != nil { |
| 23 | + return nil, err |
| 24 | + } |
| 25 | + |
| 26 | + return connect.NewResponse(resp), nil |
| 27 | +} |
| 28 | + |
| 29 | +func (a *AccountsApi) UpdateAccount( |
| 30 | + ctx context.Context, |
| 31 | + c *connect.Request[accountsv1.UpdateAccountRequest], |
| 32 | +) (*connect.Response[accountsv1.UpdateAccountResponse], error) { |
| 33 | + // todo auth |
| 34 | + |
| 35 | + resp, err := a.accSvc.Update(ctx, c.Msg) |
| 36 | + if err != nil { |
| 37 | + return nil, err |
| 38 | + } |
| 39 | + |
| 40 | + return connect.NewResponse(resp), nil |
| 41 | +} |
| 42 | + |
| 43 | +func (a *AccountsApi) ListAccounts( |
| 44 | + ctx context.Context, |
| 45 | + c *connect.Request[accountsv1.ListAccountsRequest], |
| 46 | +) (*connect.Response[accountsv1.ListAccountsResponse], error) { |
| 47 | + // todo auth |
| 48 | + resp, err := a.accSvc.List(ctx, c.Msg) |
| 49 | + if err != nil { |
| 50 | + return nil, err |
| 51 | + } |
| 52 | + |
| 53 | + return connect.NewResponse(resp), nil |
| 54 | +} |
| 55 | + |
| 56 | +func NewAccountsApi( |
| 57 | + mux *boilerplate.DefaultGrpcServer, |
| 58 | + accSvc AccountSvc, |
| 59 | +) (*AccountsApi, error) { |
| 60 | + res := &AccountsApi{ |
| 61 | + accSvc: accSvc, |
| 62 | + } |
| 63 | + |
| 64 | + mux.GetMux().Handle( |
| 65 | + accountsv1connect.NewAccountsServiceHandler(res, mux.GetDefaultHandlerOptions()...), |
| 66 | + ) |
| 67 | + |
| 68 | + return res, nil |
| 69 | +} |
0 commit comments