OSDN Git Service

doc(federation): add /list-crosschain-txs docs (#222)
[bytom/vapor.git] / federation / api / handler.go
1 package api
2
3 import (
4         "database/sql"
5         "fmt"
6         "strings"
7
8         "github.com/gin-gonic/gin"
9
10         "github.com/vapor/errors"
11         "github.com/vapor/federation/common"
12         "github.com/vapor/federation/database/orm"
13 )
14
15 type listCrosschainTxsReq struct{ Display }
16
17 func (s *Server) ListCrosschainTxs(c *gin.Context, listTxsReq *listCrosschainTxsReq, query *PaginationQuery) ([]*orm.CrossTransaction, error) {
18         var ormTxs []*orm.CrossTransaction
19         txFilter := &orm.CrossTransaction{}
20
21         // filter tx status
22         if status, err := listTxsReq.GetFilterString("status"); err == nil && status != "" {
23                 switch strings.ToLower(status) {
24                 case common.CrossTxPendingStatusLabel:
25                         txFilter.Status = common.CrossTxPendingStatus
26                 case common.CrossTxCompletedStatusLabel:
27                         txFilter.Status = common.CrossTxCompletedStatus
28                 }
29         }
30
31         // filter tx hash
32         if txHash, err := listTxsReq.GetFilterString("source_tx_hash"); err == nil && txHash != "" {
33                 txFilter.SourceTxHash = txHash
34         }
35         if txHash, err := listTxsReq.GetFilterString("dest_tx_hash"); err == nil && txHash != "" {
36                 txFilter.DestTxHash = sql.NullString{txHash, true}
37         }
38
39         txQuery := s.db.Preload("Chain").Preload("Reqs").Preload("Reqs.Asset").Where(txFilter)
40         // filter direction
41         if fromChainName, err := listTxsReq.GetFilterString("from_chain"); err == nil && fromChainName != "" {
42                 txQuery = txQuery.Joins("join chains on chains.id = cross_transactions.chain_id").Where("chains.name = ?", fromChainName)
43         }
44         txQuery = txQuery.Order(fmt.Sprintf("cross_transactions.source_block_height %s", listTxsReq.Sorter.Order))
45         txQuery = txQuery.Order(fmt.Sprintf("cross_transactions.source_tx_index %s", listTxsReq.Sorter.Order))
46         if err := txQuery.Offset(query.Start).Limit(query.Limit).Find(&ormTxs).Error; err != nil {
47                 return nil, errors.Wrap(err, "query txs")
48         }
49
50         return ormTxs, nil
51 }