github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/pkg/mw/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/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/fiat/currency/history"
    13  	entfiat "github.com/NpoolPlatform/chain-middleware/pkg/db/ent/fiat"
    14  	entcurrencyhis "github.com/NpoolPlatform/chain-middleware/pkg/db/ent/fiatcurrencyhistory"
    15  
    16  	"entgo.io/ent/dialect/sql"
    17  )
    18  
    19  type queryHandler struct {
    20  	*Handler
    21  	stm   *ent.FiatCurrencyHistorySelect
    22  	infos []*npool.Currency
    23  	total uint32
    24  }
    25  
    26  func (h *queryHandler) selectCurrencyHistory(stm *ent.FiatCurrencyHistoryQuery) {
    27  	h.stm = stm.Select(
    28  		entcurrencyhis.FieldID,
    29  		entcurrencyhis.FieldEntID,
    30  		entcurrencyhis.FieldFiatID,
    31  		entcurrencyhis.FieldFeedType,
    32  		entcurrencyhis.FieldMarketValueHigh,
    33  		entcurrencyhis.FieldMarketValueLow,
    34  		entcurrencyhis.FieldCreatedAt,
    35  		entcurrencyhis.FieldUpdatedAt,
    36  	)
    37  }
    38  
    39  func (h *queryHandler) queryCurrencyHistories(ctx context.Context, cli *ent.Client) error {
    40  	stm, err := historycrud.SetQueryConds(cli.FiatCurrencyHistory.Query(), h.Conds)
    41  	if err != nil {
    42  		return err
    43  	}
    44  
    45  	_total, err := stm.Count(ctx)
    46  	if err != nil {
    47  		return err
    48  	}
    49  
    50  	h.total = uint32(_total)
    51  	h.selectCurrencyHistory(stm)
    52  	return nil
    53  }
    54  
    55  func (h *queryHandler) queryJoinCoin(s *sql.Selector) {
    56  	t1 := sql.Table(entfiat.Table)
    57  	s.LeftJoin(t1).
    58  		On(
    59  			s.C(entcurrencyhis.FieldFiatID),
    60  			t1.C(entfiat.FieldEntID),
    61  		).
    62  		AppendSelect(
    63  			sql.As(t1.C(entfiat.FieldName), "fiat_name"),
    64  			sql.As(t1.C(entfiat.FieldLogo), "fiat_logo"),
    65  			sql.As(t1.C(entfiat.FieldUnit), "fiat_unit"),
    66  		)
    67  }
    68  
    69  func (h *queryHandler) queryJoin() {
    70  	h.stm.Modify(func(s *sql.Selector) {
    71  		h.queryJoinCoin(s)
    72  	})
    73  }
    74  
    75  func (h *queryHandler) scan(ctx context.Context) error {
    76  	return h.stm.Scan(ctx, &h.infos)
    77  }
    78  
    79  func (h *queryHandler) formalize() {
    80  	for _, info := range h.infos {
    81  		info.FeedType = basetypes.CurrencyFeedType(basetypes.CurrencyFeedType_value[info.FeedTypeStr])
    82  	}
    83  }
    84  
    85  func (h *Handler) GetCurrencies(ctx context.Context) ([]*npool.Currency, uint32, error) {
    86  	handler := &queryHandler{
    87  		Handler: h,
    88  	}
    89  
    90  	err := db.WithClient(ctx, func(_ctx context.Context, cli *ent.Client) error {
    91  		if err := handler.queryCurrencyHistories(_ctx, cli); err != nil {
    92  			return err
    93  		}
    94  		handler.stm.
    95  			Order(ent.Asc(entcurrencyhis.FieldCreatedAt)).
    96  			Offset(int(h.Offset)).
    97  			Limit(int(h.Limit))
    98  		handler.queryJoin()
    99  		return handler.scan(_ctx)
   100  	})
   101  	if err != nil {
   102  		return nil, 0, err
   103  	}
   104  
   105  	handler.formalize()
   106  	return handler.infos, handler.total, nil
   107  }