github.com/diadata-org/diadata@v1.4.593/pkg/model/assetlist.go (about)

     1  package models
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/diadata-org/diadata/pkg/dia"
     8  )
     9  
    10  func (rdb *RelDB) DeleteAssetList(sheetName string) error {
    11  	result, err := rdb.postgresClient.Exec(context.Background(),
    12  		"DELETE FROM asset_list WHERE list_name = $1", sheetName)
    13  	if err != nil {
    14  		return err
    15  	}
    16  	rowsAffected := result.RowsAffected()
    17  
    18  	if rowsAffected == 0 {
    19  		log.Printf("No asset found with the name: %s\n", sheetName)
    20  	} else {
    21  		log.Printf("Deleted asset and its related data with the name: %s\n", sheetName)
    22  	}
    23  	return nil
    24  }
    25  
    26  func (rdb *RelDB) SetAssetList(asset dia.AssetList) error {
    27  	var assetID int
    28  	err := rdb.postgresClient.QueryRow(context.Background(),
    29  		`INSERT INTO asset_list (asset_name, custom_name, symbol, methodology,list_name) 
    30  		VALUES ($1, $2, $3, $4, $5) RETURNING id`,
    31  		asset.AssetName, asset.CustomName, asset.Symbol, asset.Methodology, asset.ListName,
    32  	).Scan(&assetID)
    33  	if err != nil {
    34  		return err
    35  	}
    36  
    37  	for _, exchange := range asset.Exchanges {
    38  		var exchangeID int
    39  		err := rdb.postgresClient.QueryRow(context.Background(),
    40  			`INSERT INTO exchange_list (name, asset_id) VALUES ($1, $2) RETURNING id`,
    41  			exchange.Name, assetID,
    42  		).Scan(&exchangeID)
    43  		if err != nil {
    44  			return err
    45  		}
    46  
    47  		for _, pair := range exchange.Pairs {
    48  			_, err := rdb.postgresClient.Exec(context.Background(),
    49  				"INSERT INTO exchange_pairs (exchange_id, pair) VALUES ($1, $2)",
    50  				exchangeID, pair,
    51  			)
    52  			if err != nil {
    53  				return err
    54  			}
    55  		}
    56  	}
    57  	fmt.Println("Asset and related exchanges and pairs inserted successfully!")
    58  	return nil
    59  }
    60  
    61  func (rdb *RelDB) SearchAssetList(searchTerm string) ([]dia.AssetList, error) {
    62  	var assets []dia.AssetList
    63  
    64  	rows, err := rdb.postgresClient.Query(context.Background(),
    65  		`SELECT id, asset_name, custom_name, symbol, methodology 
    66           FROM asset_list 
    67           WHERE asset_name LIKE '%' || $1 || '%' OR symbol LIKE '%' || $1 || '%'`,
    68  		searchTerm,
    69  	)
    70  	if err != nil {
    71  		return nil, err
    72  	}
    73  	defer rows.Close()
    74  
    75  	for rows.Next() {
    76  		var asset dia.AssetList
    77  		var assetID int
    78  		if err := rows.Scan(&assetID, &asset.AssetName, &asset.CustomName, &asset.Symbol, &asset.Methodology); err != nil {
    79  			return nil, err
    80  		}
    81  
    82  		exchanges, err := rdb.getExchangesByAssetID(assetID)
    83  		if err != nil {
    84  			return nil, err
    85  		}
    86  		asset.Exchanges = exchanges
    87  		assets = append(assets, asset)
    88  	}
    89  
    90  	return assets, nil
    91  }
    92  
    93  func (rdb *RelDB) GetAssetList(listname string) ([]dia.AssetList, error) {
    94  	var assets []dia.AssetList
    95  
    96  	rows, err := rdb.postgresClient.Query(context.Background(),
    97  		`SELECT id, asset_name, custom_name, symbol, methodology 
    98           FROM asset_list 
    99           WHERE  list_name LIKE $1`, listname,
   100  	)
   101  	if err != nil {
   102  		return nil, err
   103  	}
   104  	defer rows.Close()
   105  
   106  	for rows.Next() {
   107  		var asset dia.AssetList
   108  		var assetID int
   109  		if err := rows.Scan(&assetID, &asset.AssetName, &asset.CustomName, &asset.Symbol, &asset.Methodology); err != nil {
   110  			return nil, err
   111  		}
   112  
   113  		exchanges, err := rdb.getExchangesByAssetID(assetID)
   114  		if err != nil {
   115  			return nil, err
   116  		}
   117  		asset.Exchanges = exchanges
   118  		assets = append(assets, asset)
   119  	}
   120  
   121  	return assets, nil
   122  }
   123  func (rdb *RelDB) GetAssetListBySymbol(symbol string, listname string) ([]dia.AssetList, error) {
   124  	var assets []dia.AssetList
   125  
   126  	rows, err := rdb.postgresClient.Query(context.Background(),
   127  		`SELECT id, asset_name, custom_name, symbol, methodology 
   128           FROM asset_list 
   129           WHERE custom_name LIKE   $1  AND list_name LIKE $2`,
   130  		symbol, listname,
   131  	)
   132  	if err != nil {
   133  		return nil, err
   134  	}
   135  	defer rows.Close()
   136  
   137  	for rows.Next() {
   138  		var asset dia.AssetList
   139  		var assetID int
   140  		if err := rows.Scan(&assetID, &asset.AssetName, &asset.CustomName, &asset.Symbol, &asset.Methodology); err != nil {
   141  			return nil, err
   142  		}
   143  
   144  		exchanges, err := rdb.getExchangesByAssetID(assetID)
   145  		if err != nil {
   146  			return nil, err
   147  		}
   148  		asset.Exchanges = exchanges
   149  		assets = append(assets, asset)
   150  	}
   151  
   152  	return assets, nil
   153  }
   154  
   155  func (rdb *RelDB) getExchangesByAssetID(assetID int) ([]dia.ExchangeList, error) {
   156  	rows, err := rdb.postgresClient.Query(context.Background(),
   157  		`SELECT e.name, ep.pair 
   158           FROM exchange_list e
   159           JOIN exchange_pairs ep ON e.id = ep.exchange_id
   160           WHERE e.asset_id = $1`,
   161  		assetID,
   162  	)
   163  	if err != nil {
   164  		return nil, err
   165  	}
   166  	defer rows.Close()
   167  
   168  	exchangeMap := make(map[string]*dia.ExchangeList)
   169  
   170  	for rows.Next() {
   171  		var exchangeName string
   172  		var pair string
   173  		if err := rows.Scan(&exchangeName, &pair); err != nil {
   174  			return nil, err
   175  		}
   176  
   177  		if _, exists := exchangeMap[exchangeName]; !exists {
   178  			exchangeMap[exchangeName] = &dia.ExchangeList{
   179  				Name:  exchangeName,
   180  				Pairs: []string{},
   181  			}
   182  		}
   183  		exchangeMap[exchangeName].Pairs = append(exchangeMap[exchangeName].Pairs, pair)
   184  	}
   185  
   186  	var exchanges []dia.ExchangeList
   187  	for _, exchange := range exchangeMap {
   188  		exchanges = append(exchanges, *exchange)
   189  	}
   190  
   191  	return exchanges, nil
   192  }