Example #1
0
        public CryptocurrencyDTO UpdatePrice(CryptocurrencyDTO crypto, User user)
        {
            var GeeckoCrypto = GeeckoApiConsumer.UpdatePrice(crypto.Name, user.Currency.Symbol);

            crypto.Price      = GeeckoCrypto.Price;
            crypto.LastUpdate = GeeckoCrypto.LastUpdate;
            return(crypto);
        }
Example #2
0
        public static CryptocurrencyDTO UpdatePrice(string cryptoName, string currencySymbol)
        {
            WebClient         client = new WebClient();
            string            url    = "https://api.coingecko.com/api/v3/simple/price?ids=" + cryptoName.ToLower() + "&vs_currencies=" + currencySymbol.ToLower() + "&include_last_updated_at=true";
            string            data   = client.DownloadString(url);
            dynamic           Info   = JsonConvert.DeserializeObject(data);
            CryptocurrencyDTO crypto = new CryptocurrencyDTO();

            crypto.Price      = Info[cryptoName.ToLower()][currencySymbol.ToLower()];
            crypto.LastUpdate = DateTime.Now;
            return(crypto);
        }
Example #3
0
        public async Task <IActionResult> RemoveCryptoFromDbAsync([FromBody] CryptocurrencyDTO crypto)
        {
            var result = await _mediator.Send(new RemoveCryptoFromDbAsyncCommand
            {
                cryptocurrencyDTO = crypto
            });

            if (result.StatusCode == (int)HttpStatusCode.OK)
            {
                return(StatusCode(result.StatusCode));
            }

            return(StatusCode(result.StatusCode, result.Message));
        }
Example #4
0
        public async Task <Response> AddCryptoToDbAsync(CryptocurrencyDTO crypto)
        {
            var fnResult = new Response
            {
                StatusCode = 200,
                Error      = false
            };

            var cryptoObj = _mapper.Map <Cryptocurrency>(crypto);

            await _unitOfWork.Cryptocurrencies.Insert(cryptoObj);

            await _unitOfWork.Save();

            return(fnResult);
        }
Example #5
0
        public async Task <IActionResult> AddCryptoToDbAsync([FromBody] CryptocurrencyDTO crypto)
        {
            try
            {
                var result = await _mediator.Send(new AddCryptoToDbAsyncCommand
                {
                    crypto = crypto
                });

                if (result.StatusCode == (int)HttpStatusCode.OK)
                {
                    return(Ok());
                }

                return(BadRequest("The crpytocurrency you added had errors in the data."));
            }
            catch (Exception e)
            {
                Log.Error("---Error adding crypto to do --- \n" + e.Message);
                return(Problem());
            }
        }
Example #6
0
        public async Task <Response> RemoveCryptoFromDbAsync(CryptocurrencyDTO crypto)
        {
            var result = await _cryptoRepository.RemoveCryptoFromDbAsync(crypto);

            return(result);
        }
Example #7
0
        public async Task <Response> AddCryptoToDbAsync(CryptocurrencyDTO crypto)
        {
            var result = await _cryptoRepository.AddCryptoToDbAsync(crypto);

            return(result);
        }
Example #8
0
        public async Task <Response> RemoveCryptoFromDbAsync(CryptocurrencyDTO crypto)
        {
            var fnResult = new Response
            {
                StatusCode = (int)HttpStatusCode.BadRequest
            };

            try
            {
                var cryptoObject = await _unitOfWork.Cryptocurrencies.GetAll(q => q.Name == crypto.Name);

                if (!cryptoObject.Any())
                {
                    fnResult.Message = $"No matching cryptocurrency found for {crypto.Name}.";
                    return(fnResult);
                }

                var currentStockTotal = 0.0;

                foreach (var foundStock in cryptoObject)
                {
                    currentStockTotal += foundStock.Amount;
                }

                if (crypto.Amount > currentStockTotal)
                {
                    fnResult.Message = $"{crypto.Name} sell amount is too high than current total.";
                    return(fnResult);
                }

                if (crypto.Amount < cryptoObject.First().Amount)
                {
                    cryptoObject.First().Amount = cryptoObject.First().Amount - crypto.Amount;
                    _unitOfWork.Cryptocurrencies.Update(cryptoObject.First());
                    await _unitOfWork.Save();
                }
                else
                {
                    foreach (var currentCrypto in cryptoObject)
                    {
                        if (crypto.Amount >= currentCrypto.Amount)
                        {
                            crypto.Amount = crypto.Amount - currentCrypto.Amount;
                            await _unitOfWork.Stocks.Delete(currentCrypto.Id);
                        }
                        else
                        {
                            currentCrypto.Amount = currentCrypto.Amount - crypto.Amount;
                            _unitOfWork.Cryptocurrencies.Update(currentCrypto);
                        }
                    }

                    await _unitOfWork.Save();
                }

                fnResult.StatusCode = (int)HttpStatusCode.OK;
                return(fnResult);
            }
            catch (Exception e)
            {
                fnResult.StatusCode = (int)HttpStatusCode.InternalServerError;
                fnResult.Message    = e.Message;
                return(fnResult);
            }
        }