コード例 #1
0
 public async Task AddCryptoValue(CryptoValue cryptoValue)
 {
     try
     {
         await _context.CryptoValues.InsertOneAsync(cryptoValue);
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
コード例 #2
0
        public async Task <bool> DeleteCryptoValue(CryptoValue cryptoValue)
        {
            try
            {
                DeleteResult actionResult = await _context.ApiInformation.DeleteOneAsync(nameof => nameof.Id.Equals(cryptoValue.Id));

                return(actionResult.IsAcknowledged && actionResult.DeletedCount > 0);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
コード例 #3
0
        public async Task <Response <IEnumerable <CryptoValue> > > GetCryptoValuesAsync(string ids)
        {
            var fnResult = new Response <IEnumerable <CryptoValue> >
            {
                StatusCode = (int)HttpStatusCode.BadRequest
            };

            try
            {
                var cryptoTickers = ids.Split(",");

                var uri = _coinMarketcapApiSettings.BaseClient + string.Format(_coinMarketcapApiSettings.GetCoinQuoteDataUri, ids);

                var response = await _coinMarketCapClient.SendAsync(new HttpRequestMessage(HttpMethod.Get, uri));

                if (response.StatusCode != HttpStatusCode.OK)
                {
                    return(fnResult);
                }

                var content = await response.Content.ReadAsStringAsync();

                var requestBody = JObject.Parse(content);

                var cryptoValueList = new List <CryptoValue>();

                foreach (var ticker in cryptoTickers)
                {
                    if (requestBody.SelectToken($"data.{ticker}.quote.AUD.price") != null)
                    {
                        var cryptoTicker = new CryptoValue
                        {
                            Name     = ticker,
                            FullName = (string)requestBody.SelectToken($"data.{ticker}.name"),
                            Price    = (double)requestBody.SelectToken($"data.{ticker}.quote.AUD.price")
                        };
                        cryptoValueList.Add(cryptoTicker);
                    }
                }
                fnResult.StatusCode = (int)HttpStatusCode.OK;
                fnResult.Data       = cryptoValueList;

                return(fnResult);
            }
            catch (Exception e)
            {
                fnResult.StatusCode = (int)HttpStatusCode.InternalServerError;
                fnResult.Message    = e.Message;
                return(fnResult);
            }
        }
コード例 #4
0
        public async Task <bool> UpdateCryptoValue(CryptoValue cryptoValue)
        {
            try
            {
                ReplaceOneResult actionResult
                    = await _context.CryptoValues
                      .ReplaceOneAsync(n => n.Id.Equals(cryptoValue.Id)
                                       , cryptoValue
                                       , new UpdateOptions { IsUpsert = true });

                return(actionResult.IsAcknowledged &&
                       actionResult.ModifiedCount > 0);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }