public override async Task <PortalCentralBankExchangeRateSearchGetResponse> ExecuteAsync(PortalCentralBankExchangeRateSearchGetRequest request)
        {
            var searchQuery = _dbContext.CentralBankExchangeRates
                              .AsQueryable();

            if (request.IsAdvancedSearch)
            {
                searchQuery = searchQuery
                              .Where(x => (string.IsNullOrEmpty(request.SearchStruct.LocalCurrencyCode) || x.LocalCurrencyCode == request.SearchStruct.LocalCurrencyCode) &&
                                     (string.IsNullOrEmpty(request.SearchStruct.ForeignCurrencyCode) || x.ForeignCurrencyCode == request.SearchStruct.ForeignCurrencyCode));
            }
            else
            {
                searchQuery = searchQuery.Where(x => string.IsNullOrEmpty(request.SearchTerm) ||
                                                x.LocalCurrencyCode.Contains(request.SearchTerm) ||
                                                x.ForeignCurrencyCode.Contains(request.SearchTerm));
            }

            var totalCount = await searchQuery.CountAsync();

            var records = await searchQuery
                          .OrderBy(x => x.LocalCurrencyCode)
                          .ThenBy(x => x.DefaultOrder)
                          .ApplyMetadata(request.Metadata, ignoreOrdering: true)
                          .Select(x => new PortalCentralBankExchangeRateSearchRecord()
            {
                Id = x.Id,
                LocalCurrencyCode   = x.LocalCurrencyCode,
                ForeignCurrencyCode = x.ForeignCurrencyCode,
                DefaultOrder        = x.DefaultOrder,
            })
                          .ToArrayAsync();

            // calculate current rates
            var currentUserTime = TimeZones.GetCustomerNow(_currentUser);

            foreach (var record in records)
            {
                record.Rate = await _centralBankExchangeRateManager.GetCurrentRateAsync(record.Id, currentUserTime) ?? 0;
            }

            return(new PortalCentralBankExchangeRateSearchGetResponse
            {
                Total = totalCount,
                Records = records,
                Currencies = CurrencyHelper.GetCurrencyListOptions(),
                IsNewAllowed = _currentUser.Role == UserRoleEnum.GlobalAdmin,
            });
        }
Ejemplo n.º 2
0
        public override async Task <PortalCentralBankExchangeRateUpdateDetailsGetResponse> ExecuteAsync(PortalCentralBankExchangeRateUpdateDetailsGetRequest request)
        {
            var entityId = request.Id;
            PortalCentralBankExchangeRateUpdateDetailsGetResponse response;

            if (entityId == null)
            {
                response = new PortalCentralBankExchangeRateUpdateDetailsGetResponse
                {
                    Form = new PortalCentralBankExchangeRateUpdateDetailsForm
                    {
                        CentralBankExchangeRateId = request.CentralBankExchangeRateId,
                        // start of tomorrow by default
                        StartDate = TimeZones.GetCustomerNow(_currentUser).Date.AddDays(1),
                        StartTime = "00:00:00",
                    },
                };
            }
            else
            {
                response = await _dbContext.CentralBankExchangeRateUpdates
                           .Where(x => x.Id == entityId.Value)
                           .Select(x => new PortalCentralBankExchangeRateUpdateDetailsGetResponse
                {
                    Form = new PortalCentralBankExchangeRateUpdateDetailsForm
                    {
                        Id = x.Id,
                        CentralBankExchangeRateId = x.CentralBankExchangeRateId,
                        StartDate = x.StartTime.Date,
                        StartTime = x.StartTime.ToString("HH:mm:ss"),
                        Rate      = x.Rate,
                    }
                })
                           .FirstOrDefaultAsync();

                if (response == null)
                {
                    throw EntityNotFoundException.Create <CentralBankExchangeRate>(entityId.Value);
                }
            }

            return(response);
        }