github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/pkg/mw/coin/fiat/currency/history/query.go (about)

     1  package currencyhistory
     2  
     3  import (
     4  	"context"
     5  
     6  	basetypes "github.com/NpoolPlatform/message/npool/basetypes/v1"
     7  	npool "github.com/NpoolPlatform/message/npool/chain/mw/v1/coin/fiat/currency"
     8  
     9  	"github.com/NpoolPlatform/chain-middleware/pkg/db"
    10  	"github.com/NpoolPlatform/chain-middleware/pkg/db/ent"
    11  
    12  	historycrud "github.com/NpoolPlatform/chain-middleware/pkg/crud/coin/fiat/currency/history"
    13  	entcoinbase "github.com/NpoolPlatform/chain-middleware/pkg/db/ent/coinbase"
    14  	entcurrencyhis "github.com/NpoolPlatform/chain-middleware/pkg/db/ent/coinfiatcurrencyhistory"
    15  	entfiat "github.com/NpoolPlatform/chain-middleware/pkg/db/ent/fiat"
    16  
    17  	"entgo.io/ent/dialect/sql"
    18  )
    19  
    20  type queryHandler struct {
    21  	*Handler
    22  	stm   *ent.CoinFiatCurrencyHistorySelect
    23  	infos []*npool.Currency
    24  	total uint32
    25  }
    26  
    27  func (h *queryHandler) selectCurrencyHistory(stm *ent.CoinFiatCurrencyHistoryQuery) {
    28  	h.stm = stm.Select(
    29  		entcurrencyhis.FieldID,
    30  		entcurrencyhis.FieldEntID,
    31  		entcurrencyhis.FieldCoinTypeID,
    32  		entcurrencyhis.FieldFeedType,
    33  		entcurrencyhis.FieldMarketValueHigh,
    34  		entcurrencyhis.FieldMarketValueLow,
    35  		entcurrencyhis.FieldCreatedAt,
    36  		entcurrencyhis.FieldUpdatedAt,
    37  	)
    38  }
    39  
    40  func (h *queryHandler) queryCurrencyHistories(ctx context.Context, cli *ent.Client) error {
    41  	stm, err := historycrud.SetQueryConds(cli.CoinFiatCurrencyHistory.Query(), h.Conds)
    42  	if err != nil {
    43  		return err
    44  	}
    45  
    46  	_total, err := stm.Count(ctx)
    47  	if err != nil {
    48  		return err
    49  	}
    50  
    51  	h.total = uint32(_total)
    52  	h.selectCurrencyHistory(stm)
    53  	return nil
    54  }
    55  
    56  func (h *queryHandler) queryJoinCoin(s *sql.Selector) {
    57  	t1 := sql.Table(entcoinbase.Table)
    58  	s.LeftJoin(t1).
    59  		On(
    60  			s.C(entcurrencyhis.FieldCoinTypeID),
    61  			t1.C(entcoinbase.FieldEntID),
    62  		).
    63  		AppendSelect(
    64  			sql.As(t1.C(entcoinbase.FieldName), "coin_name"),
    65  			sql.As(t1.C(entcoinbase.FieldLogo), "coin_logo"),
    66  			sql.As(t1.C(entcoinbase.FieldUnit), "coin_unit"),
    67  			sql.As(t1.C(entcoinbase.FieldEnv), "coin_env"),
    68  		)
    69  }
    70  
    71  func (h *queryHandler) queryJoinFiat(s *sql.Selector) {
    72  	t := sql.Table(entfiat.Table)
    73  	s.
    74  		LeftJoin(t).
    75  		On(
    76  			s.C(entcurrencyhis.FieldFiatID),
    77  			t.C(entfiat.FieldEntID),
    78  		).
    79  		AppendSelect(
    80  			sql.As(t.C(entfiat.FieldName), "fiat_name"),
    81  			sql.As(t.C(entfiat.FieldLogo), "fiat_logo"),
    82  			sql.As(t.C(entfiat.FieldUnit), "fiat_unit"),
    83  		)
    84  }
    85  
    86  func (h *queryHandler) queryJoin() {
    87  	h.stm.Modify(func(s *sql.Selector) {
    88  		h.queryJoinCoin(s)
    89  		h.queryJoinFiat(s)
    90  	})
    91  }
    92  
    93  func (h *queryHandler) scan(ctx context.Context) error {
    94  	return h.stm.Scan(ctx, &h.infos)
    95  }
    96  
    97  func (h *queryHandler) formalize() {
    98  	for _, info := range h.infos {
    99  		info.FeedType = basetypes.CurrencyFeedType(basetypes.CurrencyFeedType_value[info.FeedTypeStr])
   100  	}
   101  }
   102  
   103  func (h *Handler) GetCurrencies(ctx context.Context) ([]*npool.Currency, uint32, error) {
   104  	handler := &queryHandler{
   105  		Handler: h,
   106  	}
   107  
   108  	err := db.WithClient(ctx, func(_ctx context.Context, cli *ent.Client) error {
   109  		if err := handler.queryCurrencyHistories(_ctx, cli); err != nil {
   110  			return err
   111  		}
   112  		handler.queryJoin()
   113  		handler.stm.
   114  			Order(ent.Desc(entcurrencyhis.FieldCreatedAt)).
   115  			Offset(int(h.Offset)).
   116  			Limit(int(h.Limit))
   117  		return handler.scan(_ctx)
   118  	})
   119  	if err != nil {
   120  		return nil, 0, err
   121  	}
   122  
   123  	handler.formalize()
   124  	return handler.infos, handler.total, nil
   125  }