github.com/koko1123/flow-go-1@v0.29.6/engine/access/rest/collections.go (about)

     1  package rest
     2  
     3  import (
     4  	"github.com/koko1123/flow-go-1/access"
     5  	"github.com/koko1123/flow-go-1/engine/access/rest/models"
     6  	"github.com/koko1123/flow-go-1/engine/access/rest/request"
     7  	"github.com/koko1123/flow-go-1/model/flow"
     8  )
     9  
    10  // GetCollectionByID retrieves a collection by ID and builds a response
    11  func GetCollectionByID(r *request.Request, backend access.API, link models.LinkGenerator) (interface{}, error) {
    12  	req, err := r.GetCollectionRequest()
    13  	if err != nil {
    14  		return nil, NewBadRequestError(err)
    15  	}
    16  
    17  	collection, err := backend.GetCollectionByID(r.Context(), req.ID)
    18  	if err != nil {
    19  		return nil, err
    20  	}
    21  
    22  	// if we expand transactions in the query retrieve each transaction data
    23  	transactions := make([]*flow.TransactionBody, 0)
    24  	if req.ExpandsTransactions {
    25  		for _, tid := range collection.Transactions {
    26  			tx, err := backend.GetTransaction(r.Context(), tid)
    27  			if err != nil {
    28  				return nil, err
    29  			}
    30  
    31  			transactions = append(transactions, tx)
    32  		}
    33  	}
    34  
    35  	var response models.Collection
    36  	err = response.Build(collection, transactions, link, r.ExpandFields)
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  
    41  	return response, nil
    42  }