/// <inheritdoc /> public async Task <int> CreateStrategy(StrategyDto strategy) { if (await _strategiesRepository.StrategyExists(strategy.UserId, strategy.Name)) { throw new BusinessException(nameof(strategy.Name), "Strategy with this name already exists"); } if (!strategy.Indicators.Any()) { throw new BusinessException(nameof(strategy.Indicators), "At least one indicator has to be chosen"); } var investmentStrategy = new InvestmentStrategy { UserId = strategy.UserId, Name = strategy.Name, SignalDaysPeriod = strategy.SignalDaysPeriod, Indicators = new List <StrategyIndicator>() }; foreach (var indicator in strategy.Indicators) { AddIndicator(indicator, investmentStrategy); } _strategiesRepository.Insert(investmentStrategy); await _strategiesRepository.Save(); return(investmentStrategy.Id); }
public void Start() { const string queryString = "SELECT [ID],[Name],[Description] FROM [dbo].[Strategies] ORDER BY [ID] ASC"; const string countString = "SELECT COUNT([ID]) AS COUNT FROM [dbo].[Strategies]"; const string connectionString = "Server=.;Database=TransactionManagement;User Id=stocktrading;Password=stocktrading;"; //Load from db using (var connection = new SqlConnection(connectionString)) { connection.Open(); using (var command = new SqlCommand(countString, connection)) { var count = (int)command.ExecuteScalar(); LoggingService.Info($" ({count})"); } using (var command = new SqlCommand(queryString, connection)) { using (var reader = command.ExecuteReader()) { while (reader.Read()) { var item = new StrategyDto(); item.OldId = int.Parse(reader["ID"].ToString()); item.Name = reader["Name"].ToString(); item.Description = reader["Description"].ToString(); Items.Add(item.OldId, item); } } } } //Import foreach (var item in Items) { var cmd = new StrategyAddCommand( item.Value.Id, -1, item.Value.Name, item.Value.Description, null); CommandDispatcher.Execute(cmd); //Image if (ImportImage(cmd, item.Value.OldId)) { LoggingService.Info($"Stock {item.Value.Name} ({item.Value.OldId}) + IMAGE"); } else { LoggingService.Info($"Stock {item.Value.Name} ({item.Value.OldId})"); } } }
public void When_user_already_has_strategy_with_given_name_should_throw() { var newStrategy = new StrategyDto { Name = "Strategy1", UserId = 1 }; _strategyRepository.Setup(s => s.StrategyExists(1, "Strategy1")).Returns(System.Threading.Tasks.Task.FromResult(true)); Func <System.Threading.Tasks.Task> act = async() => await _service.CreateStrategy(newStrategy); act.ShouldThrow <BusinessException>(); }
public async Task <ActionResult> UpdateStrategy([FromBody] StrategyDto strategyDto) { try { _logger.LogInformation($"Market update request {strategyDto}"); await _client.UpdateStrategy(strategyDto); return(Ok()); } catch (Exception e) { _logger.LogError(e.StackTrace); } return(BadRequest()); }
/// <inheritdoc /> public async Task UpdateStrategy(StrategyDto dto) { var strategy = await _strategiesRepository.GetStrategy(dto.UserId, dto.Id); if (strategy == null) { throw new BusinessException("Strategy not found", ErrorStatus.DataNotFound); } strategy.Name = dto.Name; strategy.SignalDaysPeriod = dto.IsConjunctiveStrategy ? dto.SignalDaysPeriod : null; var toDelete = strategy.Indicators .Where(i => dto.Indicators .All(im => im.IndicatorType != (IndicatorType)i.IndicatorType)) .ToList(); foreach (var strategyIndicator in toDelete) { _strategiesRepository.DeleteIndicator(strategyIndicator); } foreach (var indicatorDto in dto.Indicators) { var indicator = strategy.Indicators.FirstOrDefault(i => (IndicatorType)i.IndicatorType == indicatorDto.IndicatorType); if (indicator == null) { AddIndicator(indicatorDto, strategy); } else { foreach (var property in indicatorDto.Properties) { var indicatorProperty = indicator.Properties.FirstOrDefault(p => p.Name == property.Name); if (indicatorProperty == null) { continue; } indicatorProperty.Value = property.Value; } } } await _strategiesRepository.Save(); }
public List <StrategyDto> GetItems() { var items = new List <StrategyDto>(); const string queryString = "SELECT [ID],[Name],[Description] FROM [dbo].[Strategies] ORDER BY [ID] ASC"; const string countString = "SELECT COUNT([ID]) AS COUNT FROM [dbo].[Strategies]"; //Load from db using (var connection = new SqlConnection(SourceConnectionString)) { connection.Open(); using (var command = new SqlCommand(countString, connection)) { var count = (int)command.ExecuteScalar(); LoggingService.Info($" ({count})"); } using (var command = new SqlCommand(queryString, connection)) { using (var reader = command.ExecuteReader()) { while (reader.Read()) { var item = new StrategyDto(); item.OldId = int.Parse(reader["ID"].ToString()); item.Name = reader["Name"].ToString(); item.Description = reader["Description"].ToString(); items.Add(item); } } } } return(items); }
private StrategyDto BuildCreateStrategyDto(EditStrategyViewModel model) { var dto = new StrategyDto { Name = model.Name, UserId = CurrentUserId, Id = model.Id ?? 0, Indicators = model.Indicators.Select(i => new ParameterizedIndicator { IndicatorType = i.Type, Properties = i.Properties .Where(p => !string.IsNullOrWhiteSpace(p.Name)) .Select(p => new IndicatorProperty { Name = p.Name, Value = p.Value }).ToList() }).ToList(), SignalDaysPeriod = model.IsConjunctiveStrategy ? (int?)model.SignalDaysPeriod : null }; return(dto); }
public Task Update(StrategyDto strategy) { //_logger.LogInformation($"Update received from one of the strategy apis {strategy}"); _context.Clients.All.SendAsync("Update", strategy); return(Task.CompletedTask); }
public Task Delete(StrategyDto strategy) { _context.Clients.All.SendAsync("Delete", strategy); return(Task.CompletedTask); }
public async Task Add(StrategyDto value) { await Clients.All.Add(value); }
public async Task Update(StrategyDto value) { await Clients.All.Update(value); }
public async Task Delete(StrategyDto value) { await Clients.All.Delete(value); }