-
Notifications
You must be signed in to change notification settings - Fork 178
/
collections.go
42 lines (35 loc) · 1.09 KB
/
collections.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
package rest
import (
"github.com/onflow/flow-go/access"
"github.com/onflow/flow-go/engine/access/rest/models"
"github.com/onflow/flow-go/engine/access/rest/request"
"github.com/onflow/flow-go/model/flow"
)
// GetCollectionByID retrieves a collection by ID and builds a response
func GetCollectionByID(r *request.Request, backend access.API, link models.LinkGenerator) (interface{}, error) {
req, err := r.GetCollectionRequest()
if err != nil {
return nil, NewBadRequestError(err)
}
collection, err := backend.GetCollectionByID(r.Context(), req.ID)
if err != nil {
return nil, err
}
// if we expand transactions in the query retrieve each transaction data
transactions := make([]*flow.TransactionBody, 0)
if req.ExpandsTransactions {
for _, tid := range collection.Transactions {
tx, err := backend.GetTransaction(r.Context(), tid)
if err != nil {
return nil, err
}
transactions = append(transactions, tx)
}
}
var response models.Collection
err = response.Build(collection, transactions, link, r.ExpandFields)
if err != nil {
return nil, err
}
return response, nil
}