-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into workflow-setup
- Loading branch information
Showing
11 changed files
with
355 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package data | ||
|
||
import ( | ||
"context" | ||
"wallet-backend/internal/db" | ||
|
||
"github.com/stellar/go/support/errors" | ||
) | ||
|
||
type PaymentModel struct { | ||
db db.ConnectionPool | ||
} | ||
|
||
func (m *PaymentModel) SubscribeAddress(ctx context.Context, address string) error { | ||
const query = `INSERT INTO accounts (stellar_address) VALUES ($1) ON CONFLICT DO NOTHING` | ||
_, err := m.db.ExecContext(ctx, query, address) | ||
if err != nil { | ||
return errors.Wrapf(err, "subscribing address %s to payments tracking", address) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (m *PaymentModel) UnsubscribeAddress(ctx context.Context, address string) error { | ||
const query = `DELETE FROM accounts WHERE stellar_address = $1` | ||
_, err := m.db.ExecContext(ctx, query, address) | ||
if err != nil { | ||
return errors.Wrapf(err, "unsubscribing address %s to payments tracking", address) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package data | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"testing" | ||
"wallet-backend/internal/db" | ||
"wallet-backend/internal/db/dbtest" | ||
|
||
"github.com/stellar/go/keypair" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestSubscribeAddress(t *testing.T) { | ||
dbtest := dbtest.Open(t) | ||
defer dbtest.Close() | ||
|
||
dbConnectionPool, err := db.OpenDBConnectionPool(dbtest.DSN) | ||
require.NoError(t, err) | ||
defer dbConnectionPool.Close() | ||
|
||
m := &PaymentModel{ | ||
db: dbConnectionPool, | ||
} | ||
|
||
ctx := context.Background() | ||
address := keypair.MustRandom().Address() | ||
err = m.SubscribeAddress(ctx, address) | ||
require.NoError(t, err) | ||
|
||
var dbAddress sql.NullString | ||
err = m.db.GetContext(ctx, &dbAddress, "SELECT stellar_address FROM accounts LIMIT 1") | ||
require.NoError(t, err) | ||
|
||
assert.True(t, dbAddress.Valid) | ||
assert.Equal(t, address, dbAddress.String) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
-- +migrate Up | ||
|
||
CREATE TABLE accounts ( | ||
stellar_address text NOT NULL, | ||
created_at timestamp with time zone NOT NULL DEFAULT NOW(), | ||
PRIMARY KEY (stellar_address) | ||
); | ||
|
||
-- +migrate Down | ||
|
||
DROP TABLE accounts; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package httphandler | ||
|
||
import ( | ||
"net/http" | ||
"wallet-backend/internal/data" | ||
"wallet-backend/internal/serve/httperror" | ||
|
||
"github.com/stellar/go/support/http/httpdecode" | ||
) | ||
|
||
type PaymentsHandler struct { | ||
*data.PaymentModel | ||
} | ||
|
||
type PaymentsSubscribeRequest struct { | ||
Address string `json:"address"` | ||
} | ||
|
||
func (h PaymentsHandler) SubscribeAddress(w http.ResponseWriter, r *http.Request) { | ||
ctx := r.Context() | ||
|
||
var reqBody PaymentsSubscribeRequest | ||
err := httpdecode.DecodeJSON(r, &reqBody) | ||
if err != nil { | ||
httperror.BadRequest("Invalid request body").Render(w) | ||
return | ||
} | ||
|
||
err = h.PaymentModel.SubscribeAddress(ctx, reqBody.Address) | ||
if err != nil { | ||
httperror.InternalServerError.Render(w) | ||
// TODO: track in Sentry | ||
return | ||
} | ||
} | ||
|
||
func (h PaymentsHandler) UnsubscribeAddress(w http.ResponseWriter, r *http.Request) { | ||
ctx := r.Context() | ||
|
||
var reqBody PaymentsSubscribeRequest | ||
err := httpdecode.DecodeJSON(r, &reqBody) | ||
if err != nil { | ||
httperror.BadRequest("Invalid request body").Render(w) | ||
return | ||
} | ||
|
||
err = h.PaymentModel.UnsubscribeAddress(ctx, reqBody.Address) | ||
if err != nil { | ||
httperror.InternalServerError.Render(w) | ||
// TODO: track in Sentry | ||
return | ||
} | ||
} |
Oops, something went wrong.