// .csv files -> Need proper encoding. Open .csv with notepad, and click "Save As". On the dialog, it should show current encoding. public static List <ImportedCoin> ImportCSV_Coins(Types.Exchanges exchange, Stream fileStream) { switch (exchange) { case Types.Exchanges.Custom: return(CSVImport_Custom(fileStream)); case Types.Exchanges.BitTrex: return(CSVImport_BitTrex(fileStream)); case Types.Exchanges.Kraken: return(CSVImport_Kraken(fileStream)); case Types.Exchanges.GDax: return(CSVImport_GDax(fileStream)); case Types.Exchanges.Binance: return(CSVImport_Binance(fileStream)); } return(new List <ImportedCoin>()); }
// Warn, this will delete previous imported Exchange (BitTrex, Kraken, etc) trades public async Task <IActionResult> PostCSVTradeHistoryFile(IFormFile file, Types.Exchanges exchange, int portfolioId) { if (file == null || file.Length > 100000 || (!file.FileName.EndsWith(".csv", StringComparison.OrdinalIgnoreCase) && !file.FileName.EndsWith(".xlsx", StringComparison.OrdinalIgnoreCase))) { return(Json(ResultsItem.Error("Please select a proper .csv/.xlsx file that is less than 100kb."))); } List <Core.Data.ServiceModels.ImportedCoin> importedCoins = new List <Core.Data.ServiceModels.ImportedCoin>(); using (var stream = file.OpenReadStream()) { importedCoins = FetchAPILogic.ImportCSV_Coins(exchange, stream); } if (importedCoins.IsNullOrEmpty()) { return(Json(ResultsItem.Error(Lang.ImportFailedCSV))); } int maxAllowedImport = SubscriptionLogic.GetMaxAllowedTradesImportPerUser(CurrentUser.PTUserInfo.SubscriptionLevel); if (importedCoins.Count > maxAllowedImport) { return(Json(ResultsItem.Error(string.Format(Lang.CSVMaxImportAllowed, maxAllowedImport)))); } List <CryptoCoin> fetchedCoins = FetchAPILogic.FormatCoinsAndGenerateTotalPricePaid(importedCoins, await GetAllHistoricCoinPrices()); if (fetchedCoins.Count > 0) { fetchedCoins = CryptoLogic.FormatCoinsAndBoughtSoldLogicUpdate(fetchedCoins); await CryptoLogic.DeleteAllUserCoinByExchangeAsync(portfolioId, exchange, CurrentUser); ResultsItem insertResults = await CryptoLogic.InsertCoinsToUserPortfolioAsync(fetchedCoins, CurrentUser, portfolioId); if (insertResults.IsSuccess) { return(Json(ResultsItem.Success("Successfully imported coins to your portfolio."))); } } return(Json(ResultsItem.Error("An error occured when trying to import trades. Are you sure the import .csv file has correct format?"))); }
public static async Task <ResultsItem> DeleteAllUserCoinByExchangeAsync(int portfolioId, Types.Exchanges exchange, PegaUser user) { var validateRequest = IsValidDBRequest(user, portfolioId, validatePortfolio: true); if (!validateRequest.IsSuccess) { return(validateRequest); } return(await CryptoRepository.DeleteAllUserCoinByExchangeAsync(portfolioId, exchange)); }
public static async Task <ResultsItem> DeleteAllUserCoinByExchangeAsync(int portfolioId, Types.Exchanges exchange) { try { int exchangeId = (int)exchange; using (PegasunDBContext db = new PegasunDBContext()) { var coins = db.Coins.Where(x => x.PortfolioId == portfolioId && x.Exchange == exchangeId); db.Coins.RemoveRange(coins); await db.SaveChangesAsync(); } return(ResultsItem.Success("Successfully deleted all coins by exchange.")); } catch (Exception ex) { return(ResultsItem.Error($"Unable to delete all coins. {ex.Message}")); } }