OSDN Git Service

8a23b9f11a6d543109c6be22e60572764c9ac936
[bytom/vapor.git] / toolbar / 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/toolbar/federation/common"
12         "github.com/vapor/toolbar/federation/database/orm"
13         serverCommon "github.com/vapor/toolbar/server"
14 )
15
16 type listCrosschainTxsReq struct{ serverCommon.Display }
17
18 func (s *Server) ListCrosschainTxs(c *gin.Context, listTxsReq *listCrosschainTxsReq, query *serverCommon.PaginationQuery) ([]*orm.CrossTransaction, error) {
19         var ormTxs []*orm.CrossTransaction
20         txFilter := &orm.CrossTransaction{}
21
22         // filter tx status
23         if status, err := listTxsReq.GetFilterString("status"); err == nil && status != "" {
24                 switch strings.ToLower(status) {
25                 case common.CrossTxPendingStatusLabel:
26                         txFilter.Status = common.CrossTxPendingStatus
27                 case common.CrossTxCompletedStatusLabel:
28                         txFilter.Status = common.CrossTxCompletedStatus
29                 }
30         }
31
32         // filter tx hash
33         if txHash, err := listTxsReq.GetFilterString("source_tx_hash"); err == nil && txHash != "" {
34                 txFilter.SourceTxHash = txHash
35         }
36         if txHash, err := listTxsReq.GetFilterString("dest_tx_hash"); err == nil && txHash != "" {
37                 txFilter.DestTxHash = sql.NullString{txHash, true}
38         }
39
40         txQuery := s.db.Preload("Chain").Preload("Reqs").Preload("Reqs.Asset").Where(txFilter)
41         // filter direction
42         if sourceChainName, err := listTxsReq.GetFilterString("source_chain_name"); err == nil && sourceChainName != "" {
43                 txQuery = txQuery.Joins("join chains on chains.id = cross_transactions.chain_id").Where("chains.name = ?", sourceChainName)
44         }
45
46         // filter address
47         if address, err := listTxsReq.GetFilterString("address"); err == nil && address != "" {
48                 txQuery = txQuery.Joins("join cross_transaction_reqs on cross_transaction_reqs.cross_transaction_id = cross_transactions.id").
49                         Where("cross_transaction_reqs.from_address = ? or cross_transaction_reqs.to_address = ?", address, address)
50         }
51
52         // sorter order
53         txQuery = txQuery.Order(fmt.Sprintf("cross_transactions.source_block_height %s", listTxsReq.Sorter.Order))
54         txQuery = txQuery.Order(fmt.Sprintf("cross_transactions.source_tx_index %s", listTxsReq.Sorter.Order))
55         if err := txQuery.Offset(query.Start).Limit(query.Limit).Find(&ormTxs).Error; err != nil {
56                 return nil, errors.Wrap(err, "query txs")
57         }
58
59         return ormTxs, nil
60 }
61
62 func (s *Server) ListChains(c *gin.Context) ([]*orm.Chain, error) {
63         var chains []*orm.Chain
64         if err := s.db.Find(&chains).Error; err != nil {
65                 return nil, err
66         }
67
68         return chains, nil
69 }