Advertisement
klinki

Invsetment Updater

Apr 4th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using Inviser.GeneratedModels;
  5. using Inviser.Services.DataDownloader;
  6. using Microsoft.EntityFrameworkCore;
  7. using Microsoft.Extensions.Logging;
  8.  
  9. namespace Inviser.Services
  10. {
  11.     public class InvestmentRecordsUpdater
  12.     {
  13.         private readonly DataDownloaderRepository _dataDownloaderRepository;
  14.         private readonly GeneratedInviserContext _context;
  15.         private readonly ILogger<InvestmentRecordsUpdater> _logger;
  16.  
  17.         public InvestmentRecordsUpdater(
  18.             DataDownloaderRepository dataDownloaderRepository,
  19.             GeneratedInviserContext context,
  20.             ILogger<InvestmentRecordsUpdater> logger
  21.         ) {
  22.             _dataDownloaderRepository = dataDownloaderRepository;
  23.             _context = context;
  24.             _logger = logger;
  25.         }
  26.  
  27.         public async Task UpdateRecordsAsync()
  28.         {
  29.             var investments = await _context.Investment.ToListAsync();
  30.  
  31.             foreach (var investment in investments)
  32.             {
  33.                 IDataDownloader dataDownloader = _dataDownloaderRepository.GetBySource(investment.Source);
  34.  
  35.                 if (dataDownloader == null)
  36.                 {
  37.                     _logger.LogError("Missing {Downloader} for {Source}", nameof(IDataDownloader), investment.Source);
  38.                     continue;
  39.                 }
  40.  
  41.                 _logger.LogInformation("Updating data for {Investment}", investment.Symbol);
  42.                
  43.                 IEnumerable<InvestmentRecord> data = await dataDownloader.GetDataAsync(investment.Symbol);
  44.  
  45.                 using (var transaction = await _context.Database.BeginTransactionAsync())
  46.                 {
  47.                     try
  48.                     {
  49.                         foreach (InvestmentRecord record in data)
  50.                         {
  51.                             record.Investment = investment;
  52.                             record.InvestmentId = investment.Id;
  53.                             await _context.InvestmentRecord.AddAsync(record);
  54.                             _context.Entry(record.Investment).State = EntityState.Unchanged;
  55.                         }
  56.  
  57.                         await _context.SaveChangesAsync();
  58.                         transaction.Commit();
  59.                     }
  60.                     catch (Exception e)
  61.                     {                      
  62.                         transaction.Rollback();
  63.                         _context.ResetContextState();
  64.                         _logger.LogError(e, "Could not add records for {Investment}", investment.Name);
  65.                     }
  66.                 }
  67.             }
  68.         }
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement