Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transactions endpoint #12

Merged
merged 4 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
31 changes: 31 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,34 @@ Response:
"seller_name": "string"
}
```

## `/transactions`

List all transactions

Request:

```bash
curl -i -X 'GET' \
'http://localhost:8080/transactions' \
-H 'accept: application/json'
```

Response:

```JSON
{
"transactions": [
{
"type": "string",
"date": "2023-09-27T17:34:41.455Z",
"product_description": "string",
"product_price": "string",
"seller_name": "string",
"seller_type": "affiliate",
"action": "venda produtor"
}
],
"total": 0
}
```
8 changes: 4 additions & 4 deletions docs/api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,16 @@ components:
type: string
format: date-time
description: Transaction date
productDescription:
product_description:
type: string
description: Production description
productPrice:
product_price:
type: string
description: Production price
sellerName:
seller_name:
type: string
description: Seller name
sellerType:
seller_type:
type: string
description: Seller type
enum:
Expand Down