-
Notifications
You must be signed in to change notification settings - Fork 2
/
transaction.go
47 lines (41 loc) · 1.45 KB
/
transaction.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package service
import (
"context"
"fmt"
"strings"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
"github.com/spacemeshos/explorer-backend/model"
)
// GetTransaction returns tx by id.
func (e *Service) GetTransaction(ctx context.Context, txID string) (*model.Transaction, error) {
filter := &bson.D{{Key: "id", Value: strings.ToLower(txID)}}
txs, total, err := e.getTransactions(ctx, filter, options.Find().SetLimit(1).SetProjection(bson.D{{Key: "_id", Value: 0}}))
if err != nil {
return nil, fmt.Errorf("error get transaction: %w", err)
}
if total == 0 {
return nil, ErrNotFound
}
return txs[0], nil
}
// GetTransactions returns txs by filter.
func (e *Service) GetTransactions(ctx context.Context, page, perPage int64) (txs []*model.Transaction, total int64, err error) {
return e.getTransactions(ctx, &bson.D{}, e.getFindOptionsSort(bson.D{
{Key: "layer", Value: -1}, {Key: "blockIndex", Value: -1},
}, page, perPage))
}
func (e *Service) getTransactions(ctx context.Context, filter *bson.D, options *options.FindOptions) (txs []*model.Transaction, total int64, err error) {
total, err = e.storage.CountTransactions(ctx, filter)
if err != nil {
return nil, 0, fmt.Errorf("error count txs: %w", err)
}
if total == 0 {
return []*model.Transaction{}, 0, nil
}
txs, err = e.storage.GetTransactions(ctx, filter, options)
if err != nil {
return nil, 0, fmt.Errorf("error get txs: %w", err)
}
return txs, total, nil
}