Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
Merge pull request #61 from BuxOrg/siggi/paymail-get-actions
Browse files Browse the repository at this point in the history
Added paymail get actions
  • Loading branch information
mrz1836 authored Apr 14, 2022
2 parents 4675856 + d4fed25 commit 7509ded
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
46 changes: 46 additions & 0 deletions action_paymails.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,52 @@ func (c *Client) GetPaymailAddress(ctx context.Context, address string, opts ...
return paymailAddress, nil
}

// GetPaymailAddresses will get all the paymails from the Datastore
func (c *Client) GetPaymailAddresses(ctx context.Context, metadataConditions *Metadata,
conditions *map[string]interface{}, pageSize, page int) ([]*PaymailAddress, error) {

// Check for existing NewRelic transaction
ctx = c.GetOrStartTxn(ctx, "get_transaction")

// Get the transaction by ID
// todo: add params for: page size and page (right now it is unlimited)
paymailAddresses, err := getPaymails(
ctx, metadataConditions, conditions, pageSize, page,
c.DefaultModelOptions()...,
)
if err != nil {
return nil, err
}

return paymailAddresses, nil
}

// GetPaymailAddressesByXPubID will get all the paymails for xPubID from the Datastore
func (c *Client) GetPaymailAddressesByXPubID(ctx context.Context, xPubID string,
metadataConditions *Metadata, conditions *map[string]interface{}, pageSize, page int) ([]*PaymailAddress, error) {

// Check for existing NewRelic transaction
ctx = c.GetOrStartTxn(ctx, "get_transaction")

if conditions == nil {
*conditions = make(map[string]interface{})
}
// add the xpub_id to the conditions
(*conditions)["xpub_id"] = xPubID

// Get the transaction by ID
// todo: add params for: page size and page (right now it is unlimited)
paymailAddresses, err := getPaymails(
ctx, metadataConditions, conditions, pageSize, page,
c.DefaultModelOptions()...,
)
if err != nil {
return nil, err
}

return paymailAddresses, nil
}

// NewPaymailAddress will create a new paymail address
func (c *Client) NewPaymailAddress(ctx context.Context, xPubKey, address, publicName, avatar string,
opts ...ModelOps) (*PaymailAddress, error) {
Expand Down
2 changes: 2 additions & 0 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ type XPubService interface {
// PaymailService is the paymail actions
type PaymailService interface {
GetPaymailAddress(ctx context.Context, address string, opts ...ModelOps) (*PaymailAddress, error)
GetPaymailAddresses(ctx context.Context, metadataConditions *Metadata, conditions *map[string]interface{}, pageSize, page int) ([]*PaymailAddress, error)
GetPaymailAddressesByXPubID(ctx context.Context, xPubID string, metadataConditions *Metadata, conditions *map[string]interface{}, pageSize, page int) ([]*PaymailAddress, error)
NewPaymailAddress(ctx context.Context, key, address, publicName, avatar string, opts ...ModelOps) (*PaymailAddress, error)
DeletePaymailAddress(ctx context.Context, address string, opts ...ModelOps) error
UpdatePaymailAddress(ctx context.Context, address, publicName, avatar string,
Expand Down
41 changes: 41 additions & 0 deletions model_paymails.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,47 @@ func getPaymail(ctx context.Context, address string, opts ...ModelOps) (*Paymail
return paymailAddress, nil
}

// getPaymails will get all the paymails with the given conditions
func getPaymails(ctx context.Context, metadata *Metadata, conditions *map[string]interface{},
pageSize, page int, opts ...ModelOps) ([]*PaymailAddress, error) {

var models []PaymailAddress
dbConditions := map[string]interface{}{}

if metadata != nil {
dbConditions[metadataField] = metadata
}

if conditions != nil && len(*conditions) > 0 {
and := make([]map[string]interface{}, 0)
if _, ok := dbConditions["$and"]; ok {
and = dbConditions["$and"].([]map[string]interface{})
}
and = append(and, *conditions)
dbConditions["$and"] = and
}

// Get the records
if err := getModels(
ctx, NewBaseModel(ModelNameEmpty, opts...).Client().Datastore(),
&models, dbConditions, pageSize, page, "", "", defaultDatabaseReadTimeout,
); err != nil {
if errors.Is(err, datastore.ErrNoResults) {
return nil, nil
}
return nil, err
}

// Loop and enrich
destinations := make([]*PaymailAddress, 0)
for index := range models {
models[index].enrich(ModelDestination, opts...)
destinations = append(destinations, &models[index])
}

return destinations, nil
}

// getPaymailByID will get the paymail with the given ID
func getPaymailByID(ctx context.Context, id string, opts ...ModelOps) (*PaymailAddress, error) {

Expand Down

0 comments on commit 7509ded

Please sign in to comment.