public async void Test1()
        {
            PropertyListingRequest propertyListingRequest = new PropertyListingRequest();
            var response = await _propertyListingBusiness.GetListing(propertyListingRequest);

            Assert.True(response.Items.Count() > 0 && response.Items.Count() == propertyListingRequest.Take.GetValueOrDefault());
        }
        public async Task <long> GetListingTotal(PropertyListingRequest request)
        {
            _logger.LogDebug("[GetListing] -> Getting Property list Count from DB");

            return(await _context.PropertyListing
                   .Where(x => String.IsNullOrEmpty(request.Suburb) || x.Suburb == request.Suburb)
                   .Where(x => request.CategoryType == null || x.CategoryType == (int)request.CategoryType.Value)
                   .Where(x => request.StatusType == null || x.StatusType == (int)request.StatusType.Value)
                   .LongCountAsync());
        }
        public async Task <IEnumerable <PropertyListing> > GetListing(PropertyListingRequest request)
        {
            _logger.LogDebug("[GetListing] -> Getting Property list from DB");

            return(await _context.PropertyListing
                   .Where(x => String.IsNullOrEmpty(request.Suburb) || x.Suburb == request.Suburb)
                   .Where(x => request.CategoryType == null || x.CategoryType == (int)request.CategoryType.Value)
                   .Where(x => request.StatusType == null || x.StatusType == (int)request.StatusType.Value)
                   .OrderBy(x => x.ListingId) //Default Ordering
                   .Skip(request.Skip.Value).Take(request.Take.Value).ToListAsync());
        }
        public async void Test2()
        {
            PropertyListingRequest propertyListingRequest = new PropertyListingRequest();

            propertyListingRequest.Suburb       = "SouthBank";
            propertyListingRequest.CategoryType = CategoryType.Rental;
            propertyListingRequest.StatusType   = StatusType.Current;
            propertyListingRequest.Take         = 20;
            var response = await _propertyListingBusiness.GetListing(propertyListingRequest);

            Assert.True(response.Items.Count() > 0 && response.Items.Count() == propertyListingRequest.Take.GetValueOrDefault());
        }
        public async Task Test5()
        {
            PropertyListingRequest propertyListingRequest = new PropertyListingRequest();

            propertyListingRequest.Skip = -5;
            propertyListingRequest.Take = 0;
            Assert.Contains(ValidateModel(propertyListingRequest), v => v.MemberNames.Contains("Skip") && v.ErrorMessage.Contains("Value must be greater than 0"));
            Assert.Contains(ValidateModel(propertyListingRequest), v => v.MemberNames.Contains("Take") && v.ErrorMessage.Contains("Value must be between 0 and 500"));

            var nullEx = await Assert.ThrowsAsync <ArgumentNullException>(() => _propertyListingBusiness.GetListing(null));

            Assert.Equal("Value cannot be null. (Parameter 'request')", nullEx.Message);
        }
Exemplo n.º 6
0
        public async Task <PropertyListingResponse> GetListing(PropertyListingRequest request)
        {
            //Validate Request Parameters first
            if (request == null || ValidateRequestParameters(request) != "")
            {
                throw new ArgumentNullException(nameof(request));
            }
            try
            {
                //Generate a key from the request parameters
                var cacheKey = new StringBuilder().GenerateKey(request).ToString();

                //Get Listing from cache
                var cacheResponse = GetListingFromCache(cacheKey);

                //If listing is found in the cache, return the listing. No need to get from db
                if (cacheResponse != null && cacheResponse.Items != null)
                {
                    cacheResponse.Message = $"Cache HIT -- Found Data in Cache";
                    return(cacheResponse);
                }

                //Get Listing from DB
                _logger.LogInformation($"Cache Miss -- Getting data from DB");
                _listingResponse = await GetListingFromDB(request);

                //Set Listing in the cache only if there is some result in the DB, otherwise there is no use
                if (_listingResponse != null && _listingResponse.Items != null && _listingResponse.Items.Count() > 0)
                {
                    SetListingCache(_listingResponse, cacheKey);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"Error in [GetListing] -> {ex.StackTrace}");
            }
            return(_listingResponse);
        }
Exemplo n.º 7
0
        public async Task <ActionResult <PropertyListingResponse> > GetListings([FromQuery] PropertyListingRequest request)
        {
            //Put Validation here (if required) and Send a Bad Request

            //Create logs here
            try
            {
                var result = await _listingBL.GetListing(request);

                if (result.Items != null)
                {
                    return(Ok(result));
                }
                else
                {
                    return(NotFound("Some Error Occured, Please contact Admin for more information"));
                }
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
Exemplo n.º 8
0
        private string ValidateRequestParameters(PropertyListingRequest request)
        {
            List <string> validationError = new List <string>();

            if (!request.Skip.HasValue)
            {
                validationError.Add("Specify the values you want to skip");
            }

            if (!request.Take.HasValue)
            {
                validationError.Add("Specify the values you want to Select");
            }

            if (validationError.Count > 0)
            {
                return(JsonSerializer.Serialize(validationError));
            }
            else
            {
                return("");
            }
        }
Exemplo n.º 9
0
        private async Task <PropertyListingResponse> GetListingFromDB(PropertyListingRequest request)
        {
            try
            {
                var listing = await _listingRepository.GetListing(request);

                long total = 0;

                if (listing != null && listing.Any())
                {
                    total = await _listingRepository.GetListingTotal(request);

                    var result = _mapper.Map <IEnumerable <PropertyListing> >(listing);
                    _listingResponse.Items   = result;
                    _listingResponse.Total   = total;
                    _listingResponse.Message = $"Cache Miss -- Fetched Data From DB";
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("[GetListing] -> {ErrorMessage}", ex.StackTrace);
            }
            return(_listingResponse);
        }
        public async void Test3(PropertyListingRequest propertyListingRequest)
        {
            var response = await _propertyListingBusiness.GetListing(propertyListingRequest);

            Assert.True(response.Items == null);
        }