예제 #1
0
        public async Task <JsonResult> ExecuteCoinChanges(CoinManagementVM vm)
        {
            ResultsItem validationResult = CoinManagementValidation(vm, isSoldMode: false);

            if (!validationResult.IsSuccess)
            {
                return(Json(validationResult));
            }

            vm.Coin.OrderType   = Types.OrderType.Buy;
            vm.Coin.Exchange    = Types.Exchanges.Custom;
            vm.Coin.PortfolioId = vm.SelectedPortfolioID;

            if (vm.Coin.TotalPricePaidUSD.GetValueOrDefault() == 0)
            {
                vm.Coin.TotalPricePaidUSD = vm.Coin.OrderDate.Date == DateTime.Now.Date ? vm.Coin.Shares * CryptoLogic.GetLatestPriceOfSymbol(vm.Coin.Symbol, await GetAllCoinsMarketDetailsAPI())
                                                                                        : CryptoLogic.GenerateTotalPricePaidUSD(vm.Coin, await GetAllHistoricCoinPrices());
            }

            var coinResult = vm.IsCreateMode ? await CryptoLogic.InsertCoinsToUserPortfolioAsync(new List <CryptoCoin> {
                vm.Coin
            }, CurrentUser, vm.SelectedPortfolioID)
                                             : await CryptoLogic.UpdateUserCoinAsync(vm.Coin, CurrentUser);

            return(Json(coinResult));
        }
예제 #2
0
        private ResultsItem CoinManagementValidation(CoinManagementVM vm, bool isSoldMode = false)
        {
            if (!ModelState.IsValid)
            {
                return(ResultsItem.Error(ModelState.GetAllErrorsString()));
            }

            if (!isSoldMode)
            {
                vm.Coin.Symbol = vm.Coin.Symbol.ToUpperInvariant();
                if (!vm.Coin.Symbol.StartsWith("BTC-") && !vm.Coin.Symbol.StartsWith("ETH-") && !vm.Coin.Symbol.StartsWith("USD-") && !vm.Coin.Symbol.StartsWith("USDT-"))
                {
                    return(ResultsItem.Error("Error: The coin symbol must start with BTC-, ETH-, USD-, or USDT-"));
                }

                if (vm.Coin.OrderDate > DateTime.Now)
                {
                    return(ResultsItem.Error("Error: Order date must be today or the past."));
                }

                vm.Coin.CoinCurrency = CryptoLogic.GenerateCoinCurrencyFromSymbol(vm.Coin.Symbol);
                if (vm.Coin.CoinCurrency == Types.CoinCurrency.Unknown || vm.Coin.CoinCurrency == Types.CoinCurrency.EUR)
                {
                    return(ResultsItem.Error("Error: only BTC, ETH, and USD currency is currently supported."));
                }

                if (vm.Coin.PricePerUnit <= 0)
                {
                    return(ResultsItem.Error("Please enter Price Per Unit."));
                }
            }
            else
            {
                if (!CurrentUser.HasPortfolio(vm.Coin.PortfolioId))
                {
                    return(ResultsItem.Error(Lang.PortfolioNotFound));
                }
                if (vm.Coin.SoldCoinCurrency == Types.CoinCurrency.Unknown || vm.Coin.SoldPricePerUnit.GetValueOrDefault() <= 0)
                {
                    return(ResultsItem.Error("Please enter all sold details. TotalSoldPrice is optional."));
                }

                if (vm.SoldDate < vm.Coin.OrderDate)
                {
                    return(ResultsItem.Error("Sold date cannot be lower than Buy date. Unless you're a time traveler."));
                }
            }

            return(ResultsItem.Success());
        }
예제 #3
0
        public async Task <PartialViewResult> UpdateCoin(long coinId)
        {
            CryptoCoin coin = await CryptoLogic.GetSingleCoinByUser(coinId, CurrentUser);

            if (coin == null)
            {
                return(GeneratePartialViewError(Lang.PortfolioNotFound));
            }

            coin.SoldCoinCurrency = coin.CoinCurrency;
            CoinManagementVM vm = new CoinManagementVM
            {
                Coin                = coin,
                IsCreateMode        = false,
                SoldDate            = DateTime.Today,
                SelectedPortfolioID = coin.PortfolioId
            };

            return(PartialView("Crud/_ManageCoin", vm));
        }
예제 #4
0
        public async Task <PartialViewResult> AddNewCoins(int portfolioId, bool isUpdated = false)
        {
            if (CurrentUser.ExchangeApiList.IsNullOrEmpty() || isUpdated)
            {
                CurrentUser.ExchangeApiList = await CryptoLogic.GetAllUserExchangeAPIAsync(CurrentUser.UserId);

                SubmitCurrentUserUpdate();
            }

            CoinManagementVM vm = new CoinManagementVM
            {
                PortfolioList              = CurrentUser.Portfolios.ToSelectList(x => x.Name, x => x.PortfolioId.ToString()),
                IsCreateMode               = true,
                SelectedPortfolioID        = portfolioId,
                ExistingSavedImportAPIList = CurrentUser.ExchangeApiList.ToSelectList(x => (string.IsNullOrEmpty(x.Name) ? x.Exchange.ToString() : x.Name), x => x.Id.ToString()),
                Coin = new CryptoCoin
                {
                    OrderDate = DateTime.Today
                }
            };

            return(PartialView("_AddNewCoins", vm));
        }
예제 #5
0
        public async Task <JsonResult> MarkCoinSold(CoinManagementVM vm)
        {
            ResultsItem validationResult = CoinManagementValidation(vm, isSoldMode: true);

            if (!validationResult.IsSuccess)
            {
                return(Json(validationResult));
            }

            CryptoCoin fetchedCoin = await CryptoLogic.GetSingleCoinByUser(vm.Coin.CoinId, CurrentUser);

            if (fetchedCoin == null)
            {
                return(Json(ResultsItem.Error("This coin cannot be found or belogns to someone else.")));
            }

            fetchedCoin.OrderType             = Types.OrderType.Sell;
            fetchedCoin.OrderDate             = vm.SoldDate;
            fetchedCoin.SoldCoinCurrency      = vm.Coin.SoldCoinCurrency;
            fetchedCoin.SoldPricePerUnit      = vm.Coin.SoldPricePerUnit;
            fetchedCoin.TotalSoldPricePaidUSD = vm.Coin.TotalSoldPricePaidUSD;
            if (fetchedCoin.TotalSoldPricePaidUSD.GetValueOrDefault() <= 0)
            {
                decimal totalPricePaidToUSD = CryptoLogic.GenerateTotalPricePaidUSD(fetchedCoin, await GetAllHistoricCoinPrices(), fetchedCoin.SoldCoinCurrency);
                if (totalPricePaidToUSD == 0)
                {
                    // Getting historic priced failed. Their sold date is probably from another dimension. Screw the user, just get latest from the market.
                    totalPricePaidToUSD = CryptoLogic.GetLatestPriceOfCurrency(vm.Coin.SoldCoinCurrency, await GetAllCoinsMarketDetailsAPI());
                }
                fetchedCoin.TotalSoldPricePaidUSD = totalPricePaidToUSD;
            }

            ResultsItem result = await CryptoLogic.UpdateUserCoinAsync(fetchedCoin, CurrentUser);

            return(Json(result));
        }