예제 #1
0
        public async Task <Responses.Indicator> AddIndicator(AddIndicator request)
        {
            // Get indicator
            var indicator = await _mainDbContext.Indicators
                            .Include(x => x.Dependencies)
                            .FirstOrDefaultAsync(x => x.IndicatorId == request.IndicatorId);

            // Throw ConflictException if it exists
            if (indicator != null)
            {
                throw new ConflictException(IndicatorMessage.IndicatorWithSameIdAlreadyExists);
            }

            // Check uniqueness
            indicator = await _mainDbContext.Indicators
                        .Include(x => x.Dependencies)
                        .FirstOrDefaultAsync(IndicatorExpression.IndicatorUnique(request.Name));

            // Throw ConflictException if it exists
            if (indicator != null)
            {
                throw new ConflictException(IndicatorMessage.IndicatorWithSameNameAlreadyExists);
            }

            // Get dependencies
            var dependencies = await GetDependencies(request.Dependencies);

            // Build dependency level
            var dependencyLevel = IndicatorBuilder.BuildDependencyLevel(dependencies);

            // Build new indicator dependencies
            var indicatorDependencies = IndicatorDependencyBuilder.BuildIndicatorDependencies(request.IndicatorId, dependencies);

            // Create
            indicator = new Indicator(
                request.IndicatorId,
                request.IndicatorType,
                request.UserId,
                request.Name,
                request.Description,
                request.Formula,
                indicatorDependencies,
                dependencyLevel,
                DateTime.Now);

            // Add
            _mainDbContext.Indicators.Add(indicator);

            // Save
            await _mainDbContext.SaveChangesAsync();

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

            // Response
            var response = _mapper.Map <Responses.Indicator>(indicator);

            // Return
            return(response);
        }
예제 #2
0
        public async Task <List <Responses.Indicator> > GetAllIndicators(string userId, IndicatorType indicatorType)
        {
            // Get user
            var user = await _userRepository.GetSingle(userId);

            // Check if it exists
            if (user == null)
            {
                throw new NotFoundException(UserMessage.UserNotFound);
            }

            // Get all indicators
            var indicators = await _indicatorRepository.GetAll(IndicatorExpression.IndicatorFilter(indicatorType, null, userId));

            // Get all indicator dependencies
            var indicatorDependencies = await _indicatorDependencyRepository.GetAll();

            // Build indicator dependencies
            IndicatorBuilder.BuildDependencies(indicators, indicatorDependencies);

            // Response
            var response = _mapper.Map <List <Responses.Indicator> >(indicators);

            // Return
            return(response);
        }
예제 #3
0
        public async Task <IndicatorResponse> AddIndicator(AddIndicatorRequest request)
        {
            // Get indicator
            var indicator = await _indicatorRepository.GetSingle(IndicatorExpression.Indicator(request.IndicatorId));

            // Throw ConflictException if it exists
            if (indicator != null)
            {
                throw new ConflictException(IndicatorMessage.IndicatorWithSameIdAlreadyExists);
            }

            // Check uniqueness
            indicator = await _indicatorRepository.GetSingle(IndicatorExpression.IndicatorUnique(request.Name));

            // Throw ConflictException if it exists
            if (indicator != null)
            {
                throw new ConflictException(IndicatorMessage.IndicatorWithSameNameAlreadyExists);
            }

            // Build dependencies
            var dependencies = await BuildDependencies(request.IndicatorId, request.Dependencies);

            // Create
            indicator = new Indicator(
                request.IndicatorId,
                request.IndicatorType,
                request.UserId,
                request.Name,
                request.Description,
                request.Formula,
                dependencies);

            // Add
            _indicatorRepository.Add(indicator);

            // Save
            await _mainDbContext.SaveChangesAsync();

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

            // Response
            var response = _mapper.Map <IndicatorResponse>(indicator);

            // Return
            return(response);
        }
예제 #4
0
        public async Task <List <Responses.Chart> > GetCharts(Period period = Period.ONE_MINUTE, List <string> currencyIds = null, List <string> userIds = null, List <string> indicatorIds = null)
        {
            // Get all currencies
            var currencies = await _mainDbContext.Currencies.Where(CurrencyExpression.Filter(currencyIds)).ToListAsync();

            // Get all indicators
            var indicators = await _mainDbContext.Indicators.Where(IndicatorExpression.Filter(indicatorIds)).ToListAsync();

            // Get all lines
            var lines = await _mainDbContext.Lines.Where(LineExpression.Filter(period, currencyIds, userIds, indicatorIds)).ToListAsync();

            // Build charts
            var charts = ChartBuilder.BuildCharts(currencies, indicators, lines);

            // Response
            var response = _mapper.Map <List <Responses.Chart> >(charts);

            // Return
            return(response);
        }
예제 #5
0
        public async Task <List <Responses.LineChart> > GetAllLineCharts(string currencyId = null, IndicatorType?indicatorType = null, string indicatorId = null, string userId = null)
        {
            // Get all currencies
            var currencies = await _currencyRepository.GetAll(CurrencyExpression.CurrencyFilter(currencyId));

            // Get all indicators
            var indicators = await _indicatorRepository.GetAll(IndicatorExpression.IndicatorFilter(indicatorType, indicatorId, userId));

            // Get all lines
            var lines = await _lineRepository.GetAll(LineExpression.LineFilter(currencyId, indicatorType, indicatorId, userId));

            // Build
            var lineCharts = LineChartBuilder.BuildLineCharts(currencies, indicators, lines);

            // Response
            var response = _mapper.Map <List <Responses.LineChart> >(lineCharts);

            // Return
            return(response);
        }
예제 #6
0
        public async Task <List <Responses.Chart> > GetAllCharts(string currencyId = null, IndicatorType?indicatorType = null, string indicatorId = null, string userId = null)
        {
            // Get all currencies
            var currencies = await _mainDbContext.Currencies.Where(CurrencyExpression.CurrencyFilter(currencyId)).ToListAsync();

            // Get all indicators
            var indicators = await _mainDbContext.Indicators.Where(IndicatorExpression.IndicatorFilter(indicatorType, indicatorId, userId)).ToListAsync();

            // Get all lines
            var lines = await _mainDbContext.Lines.Where(LineExpression.LineFilter(currencyId, indicatorType, indicatorId, userId)).ToListAsync();

            // Build
            var charts = ChartBuilder.BuildCharts(currencies, indicators, lines);

            // Response
            var response = _mapper.Map <List <Responses.Chart> >(charts);

            // Return
            return(response);
        }
예제 #7
0
        public async Task <List <Responses.Indicator> > GetAllIndicators(string userId, IndicatorType indicatorType)
        {
            // Get user
            var user = await _mainDbContext.Users.FindAsync(userId);

            // Check if it exists
            if (user == null)
            {
                throw new NotFoundException(UserMessage.UserNotFound);
            }

            // Get all indicators
            var indicators = await _mainDbContext.Indicators
                             .Include(x => x.Dependencies)
                             .Where(IndicatorExpression.IndicatorFilter(indicatorType, null, userId)).ToListAsync();

            // Response
            var response = _mapper.Map <List <Responses.Indicator> >(indicators);

            // Return
            return(response);
        }
예제 #8
0
        private async Task <List <Indicator> > GetDependencies(string[] dependencyIds)
        {
            var dependencies = new List <Indicator>();

            foreach (var dependencyId in dependencyIds)
            {
                // Get indicator
                var dependency = await _mainDbContext.Indicators.FirstOrDefaultAsync(IndicatorExpression.Indicator(dependencyId));

                // Throw ValidationException if it does not exist
                if (dependency == null)
                {
                    throw new ValidationException(string.Format(IndicatorMessage.DependencyNotFound, dependencyId));
                }

                // Add
                dependencies.Add(dependency);
            }

            // Return
            return(dependencies);
        }
예제 #9
0
        private async Task <List <IndicatorDependency> > BuildDependencies(string indicatorId, string[] dependencies)
        {
            var indicatorDependencies = new List <IndicatorDependency>();

            foreach (var dependencyId in dependencies)
            {
                // Get indicator
                var dependency = await _indicatorRepository.GetSingle(IndicatorExpression.Indicator(dependencyId));

                // Throw ValidationException if it does not exist
                if (dependency == null)
                {
                    throw new ValidationException(string.Format(IndicatorMessage.DepenedencyNotFound, dependencyId));
                }

                // Add
                var indicatorDependency = new IndicatorDependency(indicatorId, dependency.IndicatorId, dependency.DependencyLevel);
                indicatorDependencies.Add(indicatorDependency);
            }

            // Return
            return(indicatorDependencies);
        }
예제 #10
0
        public async Task <Responses.Indicator> AddIndicator(AddIndicator request)
        {
            // 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
                            .Include(x => x.Dependencies)
                            .FirstOrDefaultAsync(IndicatorExpression.Unique(request.UserId, request.Abbreviation));

            // Throw ConflictException if it exists
            if (indicator != null)
            {
                throw new ConflictException(new Conflict <AddIndicatorConflictReason>(AddIndicatorConflictReason.INDICATOR_ALREADY_EXISTS, IndicatorMessage.IndicatorWithSameIdAlreadyExists));
            }

            // Get dependencies
            var dependencies = await GetIndicators(request.Dependencies);

            // Build dependency level
            var dependencyLevel = IndicatorBuilder.BuildDependencyLevel(dependencies);

            // Build indicator dependencies
            var indicatorDependencies = IndicatorDependencyBuilder.BuildIndicatorDependencies(request.IndicatorId, dependencies);

            // Create
            indicator = new Indicator(
                request.UserId,
                request.Abbreviation,
                request.Name,
                request.Description,
                request.Formula,
                indicatorDependencies,
                dependencyLevel,
                DateTime.UtcNow.StripSeconds());

            // Add
            _mainDbContext.Indicators.Add(indicator);

            // Save
            await _mainDbContext.SaveChangesAsync();

            // Get indicator
            indicator = await _mainDbContext.Indicators
                        .Include(x => x.Dependencies)
                        .ThenInclude(x => x.Dependency)
                        .FirstOrDefaultAsync(x => x.IndicatorId == indicator.IndicatorId);

            // Response
            var response = _mapper.Map <Responses.Indicator>(indicator);

            // Log
            _logger.LogInformation("{@Event}, {@UserId}, {@Request}, {@Response}", "IndicatorAdded", request.UserId, request, response);

            // Return
            return(response);
        }