Skip to content

Commit

Permalink
✨ feat: /transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
perebaj committed Sep 27, 2023
1 parent 15b7c6d commit f4c301f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
14 changes: 14 additions & 0 deletions api/transactionhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
type transactionStorage interface {
SaveTransaction(ctx context.Context, t []contractus.Transaction) error
Balance(ctx context.Context, sellerType, sellerName string) (*contractus.BalanceResponse, error)
Transactions(ctx context.Context) (contractus.TransactionResponse, error)
}

type transactionHandler struct {
Expand All @@ -38,6 +39,9 @@ func RegisterHandler(r chi.Router, storage transactionStorage) {

const upload = "/upload"
r.Method(http.MethodPost, upload, http.HandlerFunc(h.upload))

const transactions = "/transactions"
r.Method(http.MethodGet, transactions, http.HandlerFunc(h.transactions))
}

func (s transactionHandler) producerBalance(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -106,6 +110,16 @@ func (s transactionHandler) upload(w http.ResponseWriter, r *http.Request) {
send(w, http.StatusOK, nil)
}

func (s transactionHandler) transactions(w http.ResponseWriter, r *http.Request) {
tResponse, err := s.storage.Transactions(r.Context())
if err != nil {
sendErr(w, http.StatusInternalServerError, err)
return
}

send(w, http.StatusOK, tResponse)
}

// Transaction represents the raw transaction from the file.
type Transaction struct {
Type string `json:"type"`
Expand Down
19 changes: 19 additions & 0 deletions api/transactionhandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ func (m *mockTransactionStorage) Balance(_ context.Context, _ string, _ string)
return nil, nil
}

func (m *mockTransactionStorage) Transactions(_ context.Context) (contractus.TransactionResponse, error) {
return contractus.TransactionResponse{}, nil
}

func TestTransactionHandlerUpload(t *testing.T) {
var b bytes.Buffer
w := multipart.NewWriter(&b)
Expand Down Expand Up @@ -86,3 +90,18 @@ func TestTransactionHandlerBalanceAffiliate(t *testing.T) {
t.Fatalf("expected status code %d, got %d", http.StatusOK, resp.Code)
}
}

func TestTransactionHandlerTransactions(t *testing.T) {
m := &mockTransactionStorage{}
r := chi.NewRouter()
RegisterHandler(r, m)

req := httptest.NewRequest(http.MethodGet, "/transactions", nil)
resp := httptest.NewRecorder()

r.ServeHTTP(resp, req)

if resp.Code != http.StatusOK {
t.Fatalf("expected status code %d, got %d", http.StatusOK, resp.Code)
}
}

0 comments on commit f4c301f

Please sign in to comment.