public async Task <Responses.Watcher> AddWatcher(AddWatcher request) { // Get user var user = await _userRepository.GetSingle(request.UserId); // Throw NotFound if the currency does not exist if (user == null) { throw new NotFoundException(UserMessage.UserNotFound); } // Get indicator var indicator = await _indicatorRepository.GetSingle(request.IndicatorId); // Throw NotFound if the currency does not exist if (indicator == null) { throw new NotFoundException(IndicatorMessage.IndicatorNotFound); } // Check if it exists var watcher = await _watcherRepository.GetSingle(WatcherExpression.Watcher(request.UserId, request.CurrencyId, request.IndicatorId)); // Throw ConflictException if it exists if (watcher != null) { throw new ConflictException(WatcherMessage.WatcherAlreadyExists); } // Get default watcher var defaultWatcher = await _watcherRepository.GetSingle(WatcherExpression.DefaultWatcher(request.CurrencyId, request.IndicatorId)); // Add watcher = new Watcher( request.UserId, request.CurrencyId, request.IndicatorId, request.IndicatorType, defaultWatcher?.Value, request.Buy, request.Sell, defaultWatcher?.AverageBuy, defaultWatcher?.AverageSell, request.Enabled, DateTime.Now); _watcherRepository.Add(watcher); // Save await _dbContext.SaveChangesAsync(); // Log into Splunk _logger.LogSplunkInformation(request); // Response var response = _mapper.Map <Responses.Watcher>(watcher); // Return return(response); }
public async Task <IActionResult> AddWatcher([FromBody] AddWatcher request) { // Reponse var response = await _watcherService.AddWatcher(request); // Return return(CreatedAtRoute("Watchers_GetWatcher", new { response.WatcherId }, response)); }
public async Task <Responses.Watcher> AddWatcher(AddWatcher request) { // Get currency var currency = await _mainDbContext.Currencies.FindAsync(request.CurrencyId); // Watcher not found if (currency == null) { throw new NotFoundException(CurrencyMessage.CurrencyNotFound); } // Get user var user = await _mainDbContext.Users.FindAsync(request.UserId); // User not found if (user == null) { throw new NotFoundException(UserMessage.UserNotFound); } // Get indicator var indicator = await _mainDbContext.Indicators.FindAsync(request.IndicatorId); // Indicator not found if (indicator == null) { throw new NotFoundException(IndicatorMessage.IndicatorNotFound); } // Get watcher var watcher = await _mainDbContext.Watchers.FirstOrDefaultAsync(WatcherExpression.Unique(request.UserId, request.CurrencyId, indicator.UserId, indicator.IndicatorId)); // Watcher already exists if (watcher != null) { throw new ConflictException(new Conflict <AddWatcherConflictReason>(AddWatcherConflictReason.WATCHER_ALREADY_EXISTS, WatcherMessage.WatcherAlreadyExists)); } // Get default watcher var defaultWatcher = await _mainDbContext.Watchers.FirstOrDefaultAsync(WatcherExpression.DefaultWatcher(request.CurrencyId, request.IndicatorId)); // Add watcher watcher = new Watcher( request.UserId, request.CurrencyId, indicator.IndicatorId, defaultWatcher?.Value, null, null, null, defaultWatcher?.AverageBuy, defaultWatcher?.AverageSell, defaultWatcher?.Price, request.Enabled, DateTime.UtcNow.StripSeconds()); // Add _mainDbContext.Watchers.Add(watcher); // Save await _mainDbContext.SaveChangesAsync(); // Response var response = _mapper.Map <Responses.Watcher>(watcher); // Log _logger.LogInformation("{@Event}, {@UserId}, {@Request}, {@Response}", "WatcherAdded", request.UserId, request, response); // Return return(response); }