Exemple #1
0
        public async Task <IActionResult> AddWatcher([FromBody] AddWatcherRequest request)
        {
            // Reponse
            var response = await _watcherService.AddWatcher(request);

            // Return
            return(CreatedAtRoute("Watchers_GetWatcher", new { response.WatcherId }, response));
        }
        public async Task <WatcherResponse> AddWatcher(AddWatcherRequest request)
        {
            // Get user
            var user = await _userRepository.GetSingle(request.UserId);

            // Throw NotFoundException if the currency does not exist
            if (user == null)
            {
                throw new NotFoundException(UserMessage.UserNotFound);
            }

            // Get indicator
            var indicator = await _indicatorRepository.GetSingle(request.IndicatorId);

            // Throw NotFoundException 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.TargetId, 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.TargetId, request.IndicatorId));

            // Add
            watcher = new Watcher(
                request.UserId,
                request.TargetId,
                request.IndicatorId,
                request.IndicatorType,
                defaultWatcher?.Value,
                request.Buy,
                request.Sell,
                defaultWatcher?.AverageBuy,
                defaultWatcher?.AverageSell,
                request.Enabled);
            _watcherRepository.Add(watcher);

            // Save
            await _mainDbContext.SaveChangesAsync();

            // Log into Splunk
            _logger.LogSplunkRequest(request);

            // Response
            var response = _mapper.Map <WatcherResponse>(watcher);

            // Return
            return(response);
        }