Exemplo n.º 1
0
        /// <summary>
        /// Execute the Search Tenancy Use Case
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <SearchTenancyResponse> ExecuteAsync(SearchTenancyRequest request, CancellationToken cancellationToken)
        {
            //validate
            if (request == null)
            {
                //
                throw new BadRequestException();
            }
            //validate
            var validationResponse = request.Validate(request);

            if (!validationResponse.IsValid)
            {
                throw new BadRequestException(validationResponse);
            }

            //Execute Gateway - which will determine how to get the data we requested
            var response = await _searchGateway.SearchTenanciesAsync(request, cancellationToken).ConfigureAwait(false);

            //tenancy could have no attached contacts
            if (response == null)
            {
                return(new SearchTenancyResponse());
            }

            //Create real response and map to response object
            var useCaseResponse = new SearchTenancyResponse
            {
                Tenancies = response.Results.ConvertAll(tenancy => new SearchTenancySummary
                {
                    TenancyRef     = tenancy.TenancyRef,
                    PropertyRef    = tenancy.PropertyRef,
                    Tenure         = tenancy.Tenure,
                    CurrentBalance = new Currency
                    {
                        Value        = tenancy.CurrentBalance,
                        CurrencyCode = "GBP",
                    },
                    PrimaryContact = new PrimaryContact
                    {
                        Name         = tenancy.PrimaryContactName,
                        ShortAddress = tenancy.PrimaryContactShortAddress,
                        Postcode     = tenancy.PrimaryContactPostcode
                    }
                }),
                TotalCount = response.TotalResultsCount,
                PageCount  = response.CalculatePageCount(request.PageSize, response.TotalResultsCount)
            };

            return(useCaseResponse);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Search for Tenants attached to tenancies
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <PagedResults <TenancyListItem> > SearchTenanciesAsync(SearchTenancyRequest request, CancellationToken cancellationToken)
        {
            var results = new PagedResults <TenancyListItem>();

            var validate = request.Validate(request);

            if (!validate.IsValid)
            {
                return(results);
            }

            string whiteSpace = "                    ";
            //Build actual query
            var queryStringBuilder = BuildQuery(request, whiteSpace);
            //Build query to find out total results count
            var countStringBuilder = BuildCountQuery(request, whiteSpace);

            using (var conn = new SqlConnection(_connectionString))
            {
                //open connection explicity
                conn.Open();
                //get paged results
                var all = await conn.QueryAsync <TenancyListItem>(queryStringBuilder.ToString(),
                                                                  new { tenancyRef = request.TenancyRef, firstName = request.FirstName, lastName = request.LastName, address = request.Address, postcode = request.PostCode, page = request.Page > 0 ? request.Page - 1: 0, pageSize = request.PageSize }
                                                                  ).ConfigureAwait(false);

                //add to paged results
                results.Results = all?.ToList();

                //get results total count
                var totalCount = await conn.QueryAsync <int>(countStringBuilder.ToString(),
                                                             new { tenancyRef = request.TenancyRef, firstName = request.FirstName, lastName = request.LastName, address = request.Address, postcode = request.PostCode }
                                                             ).ConfigureAwait(false);

                //add to pages results
                results.TotalResultsCount = totalCount.Sum();
                //close connection explicitly - do not pool connections
                //experienced sql connection issues with connection pooling due to UH database
                conn.Close();
            }

            return(results);
        }