public List <DailyCurrency> GetTodaysRates() { var now = GetLatestDate(); var result = _context.DailyRate.Where(x => x.Date.Equals(now)).Include(x => x.Currencies).ThenInclude(x => x.Currency).ToList(); List <DailyCurrency> rates = new List <DailyCurrency>(); foreach (var item in result) { foreach (var curr in item.Currencies.Select(x => new { Rate = x.Rate, Name = x.Currency.Name })) { DailyCurrency dailyRate = new DailyCurrency() { Name = curr.Name, Rate = curr.Rate }; rates.Add(dailyRate); } } return(rates); }
public async Task <bool> TryCreateOrUpdate(DailyCurrency dailyCurrency) { dailyCurrency.Id = Guid.NewGuid(); try { var currVacancy = await _dailyCurrencies.FindAsync(s => s.Id == dailyCurrency.Id); if (await currVacancy.FirstOrDefaultAsync() == null) { await _dailyCurrencies.InsertOneAsync(dailyCurrency); } else { await _dailyCurrencies.ReplaceOneAsync(s => s.Id == dailyCurrency.Id, dailyCurrency); } } catch (Exception e) { _logger.LogError(e, "Error TryCreateOrUpdate"); return(false); } return(true); }