Beispiel #1
0
        public async Task MapsPagingProperties()
        {
            // given
            var subject = new CustomersClient(_clientConfiguration);

            var firstPageRequest = new GetCustomersRequest
            {
                Limit = 1
            };

            // when
            var firstPageResult = await subject.GetPageAsync(firstPageRequest);

            var secondPageRequest = new GetCustomersRequest
            {
                After = firstPageResult.Meta.Cursors.After,
                Limit = 2
            };

            var secondPageResult = await subject.GetPageAsync(secondPageRequest);

            // then
            Assert.That(firstPageResult.Items.Count(), Is.EqualTo(firstPageRequest.Limit));
            Assert.That(firstPageResult.Meta.Limit, Is.EqualTo(firstPageRequest.Limit));
            Assert.That(firstPageResult.Meta.Cursors.Before, Is.Null);
            Assert.That(firstPageResult.Meta.Cursors.After, Is.Not.Null);

            Assert.That(secondPageResult.Items.Count(), Is.EqualTo(secondPageRequest.Limit));
            Assert.That(secondPageResult.Meta.Limit, Is.EqualTo(secondPageRequest.Limit));
            Assert.That(secondPageResult.Meta.Cursors.Before, Is.Not.Null);
            Assert.That(secondPageResult.Meta.Cursors.After, Is.Not.Null);
        }
Beispiel #2
0
        public GetCustomersResponse GetCustomers(GetCustomersRequest request)
        {
            try
            {
                var response = new GetCustomersResponse();

                // Real code goes here...
                //response.CustomerList.Add(new Customer { Name = "Markus Egger", Company = "CODE" });
                //response.CustomerList.Add(new Customer { Name = "Ellen Whitney", Company = "CODE" });
                //response.CustomerList.Add(new Customer { Name = "Mike Yeager", Company = "CODE" });
                //response.CustomerList.Add(new Customer { Name = "Otto Dobretsberger", Company = "CODE" });

                response.CustomerList = new Customer[] {
                    new Customer {
                        Name = "Markus Egger", Company = "CODE"
                    },
                    new Customer {
                        Name = "Ellen Whitney", Company = "CODE"
                    },
                    new Customer {
                        Name = "Mike Yeager", Company = "CODE"
                    },
                    new Customer {
                        Name = "Otto Dobretsberger", Company = "CODE"
                    }
                };

                response.Success = true;
                return(response);
            }
            catch (Exception ex)
            {
                return(ServiceHelper.GetPopulatedFailureResponse <GetCustomersResponse>(ex));
            }
        }
Beispiel #3
0
        public async Task <IActionResult> Get()
        {
            var request  = new GetCustomersRequest {
            };
            var response = await _mediator.Send(request);

            return(response.ToActionResult(_ => _.Customers));
        }
        public async Task <IEnumerable <GetCustomerResponse> > Handle(GetCustomersRequest request, CancellationToken cancellationToken)
        {
            var customers = await _context.Customers.ToListAsync();

            var response = customers.MapToList <GetCustomerResponse>();

            return(response);
        }
        public override async Task GetCustomers(GetCustomersRequest request, IServerStreamWriter <CustomerResponse> responseStream, ServerCallContext context)
        {
            var customers = _customerManager.GetCustomers(request).ToList();

            foreach (var customer in customers)
            {
                await responseStream.WriteAsync(customer);
            }
        }
Beispiel #6
0
 private void SingleAccount(int id)
 {
     ApiRequest = new GetCustomersRequest()
     {
         CustomerStatuses = new int[] { 1 },
         CustomerID       = id
     };
     ApiResponse = new GetCustomersResponse();
 }
Beispiel #7
0
 private void AccountsCreatedAfter(DateTime createdDate)
 {
     ApiRequest = new GetCustomersRequest()
     {
         BatchSize             = 50000,
         CustomerStatuses      = new int[] { 1 },
         GreaterThanCustomerID = 5
     };
     ApiResponse = new GetCustomersResponse();
 }
Beispiel #8
0
 private void TerminatedAccounts()
 {
     ApiRequest = new GetCustomersRequest()
     {
         BatchSize             = 50000,
         CustomerStatuses      = new int[] { 2 },
         GreaterThanCustomerID = 5
     };
     ApiResponse = new GetCustomersResponse();
 }
Beispiel #9
0
 private void AccountsFromLast2Days()
 {
     ApiRequest = new GetCustomersRequest()
     {
         BatchSize        = 500,
         CreatedDateStart = DateTime.Now.Subtract(TimeSpan.FromDays(2)),
         CustomerStatuses = new int[] { 1 }
     };
     ApiResponse = new GetCustomersResponse();
 }
Beispiel #10
0
 private void AccountsGreaterThanID(int id)
 {
     ApiRequest = new GetCustomersRequest()
     {
         BatchSize             = 50000,
         CustomerStatuses      = new int[] { 1 },
         GreaterThanCustomerID = id
     };
     ApiResponse = new GetCustomersResponse();
 }
Beispiel #11
0
 private void AccountsGreaterThanModfiedDate(DateTime modifiedDate)
 {
     ApiRequest = new GetCustomersRequest()
     {
         BatchSize               = 50000,
         CustomerStatuses        = new int[] { 1 },
         GreaterThanModifiedDate = modifiedDate
     };
     ApiResponse = new GetCustomersResponse();
 }
Beispiel #12
0
        private void HandleGetCustomersRequest(GetCustomersRequest message)
        {
            //If we were better citizens we would spawn this to a child actor
            foreach (var customerAccount in _customerAccounts)
            {
                //Pretend that it takes some time to find an account.
                Task.Delay(2000).GetAwaiter().GetResult();

                Sender.Tell(new GetCustomerResponse(customerAccount.Value));
            }
        }
Beispiel #13
0
        public GetCustomersResponse GetCustomersByCriteria(GetCustomersRequest request)
        {
            GetCustomersResponse      response = new GetCustomersResponse();
            CustomerBusinessComponent bc       = DependencyInjectionHelper.GetCustomerBusinessComponent();

            IQueryable <Customer> customers = bc.GetCustomersByCriteria(request.SearchType, request.City, request.CustomerName);

            response.Customers = CustomerAdapter.CustomersToDtos(customers);

            return(response);
        }
Beispiel #14
0
        public IEnumerable <CustomerResponse> GetCustomers(GetCustomersRequest request)
        {
            var customers = _unitOfWork.CustomerRepository.GetActiveCustomers();
            List <CustomerResponse> customersResponse = new List <CustomerResponse>();

            foreach (var customer in customers)
            {
                customersResponse.Add(new CustomerResponse {
                    Id = customer.Id, Name = customer.Name
                });
            }

            return(customersResponse);
        }
Beispiel #15
0
        // 非公開方法,單純示範把全部的查詢處理都寫在 controller 動作方法中(分頁、篩選、排序實際上還是在 DB 端執行)。
        public async Task <GetCustomersResponse> GetCustomersV0([FromUri] GetCustomersRequest request)
        {
            // 明白起見,把建立 DbContext 的程式碼放在這裡。實際上不會這樣做。
            SalesContext salesContext = new SalesContext();

            var customersQuery = salesContext.Customers.AsQueryable();

            // 建立篩選條件
            if (!string.IsNullOrWhiteSpace(request.Name))
            {
                customersQuery = customersQuery.Where(c => c.Name.Contains(request.Name));
            }
            if (!string.IsNullOrWhiteSpace(request.Country))
            {
                customersQuery = customersQuery.Where(c => c.Country.Equals(request.Country));
            }

            // 排序
            switch (request.SortOrder.ToLower())
            {
            case "country":
            case "country asc":
                customersQuery = customersQuery.OrderBy(c => c.Country);
                break;

            case "country desc":
                customersQuery = customersQuery.OrderByDescending(c => c.Country);
                break;

            case "name desc":
                customersQuery = customersQuery.OrderByDescending(c => c.Name);
                break;

            default:
                customersQuery = customersQuery.OrderBy(c => c.Name);
                break;
            }

            // 分頁(使用 PagedList.EntityFramework)
            var pagedResult = await customersQuery.ToPagedListAsync(request.Page, request.PageSize);

            // 轉換成 View Model
            var customerModels = pagedResult.Select(ConvertToCustomerViewModel);

            return(new GetCustomersResponse
            {
                Succeeded = true,
                Customers = customerModels.ToArray()
            });
        }
Beispiel #16
0
        public SocketMessage HadleMessage(GetCustomersRequest msg)
        {
            var response = new GetCustomersResponse();

            if (msg.CustomerIDs == null)
            {
                response.Customers = _context.Customers.ToList();
            }
            else
            {
                response.Customers = _context.Customers.Where(c => msg.CustomerIDs.Contains(c.Id)).ToList();
            }
            return(response);
        }
Beispiel #17
0
        private Expression <Func <Customer, bool> > CreateFilterExpression(GetCustomersRequest request)
        {
            var expr = PredicateBuilder.True <Customer>();

            if (!string.IsNullOrWhiteSpace(request.Name))
            {
                expr = expr.And(c => c.Name.Contains(request.Name));
            }
            if (!string.IsNullOrWhiteSpace(request.Country))
            {
                expr = expr.And(c => c.Country.Equals(request.Country));
            }
            return(expr);
        }
Beispiel #18
0
        private static async Task GetCustomers(CustomerServiceDefinition.CustomerServiceDefinitionClient customerClient)
        {
            var input = new GetCustomersRequest();

            using (var call = customerClient.GetCustomers(input))
            {
                while (await call.ResponseStream.MoveNext())
                {
                    var currentCustomer = call.ResponseStream.Current;
                    Console.WriteLine($"{currentCustomer.Id} {currentCustomer.Name}");
                }
                Console.WriteLine($"Kilép");
            }
        }
Beispiel #19
0
        public void GetCustomersRequestIsNullThrows()
        {
            // given
            var subject = new CustomersClient(_clientConfiguration);

            GetCustomersRequest request = null;

            // when
            AsyncTestDelegate test = () => subject.GetPageAsync(request);

            // then
            var ex = Assert.ThrowsAsync <ArgumentNullException>(test);

            Assert.That(ex.ParamName, Is.EqualTo(nameof(request)));
        }
Beispiel #20
0
        public async Task <GetCustomersResponse> GetCustomersV1([FromUri] GetCustomersRequest request)
        {
            // 建立篩選條件
            var filterExpr = CreateFilterExpression(request);

            // 取得分頁結果
            var customers = await _customerQueries.GetCustomersAsync(request.Page, request.PageSize, request.SortOrder, filterExpr);

            // 轉換成 View Model
            var customerModels = customers.Select(ConvertToCustomerViewModel);

            return(new GetCustomersResponse
            {
                Succeeded = true,
                Customers = customerModels.ToArray()
            });
        }
Beispiel #21
0
        public async Task <GetCustomersResponse> GetCustomersV2([FromUri] GetCustomersRequest request)
        {
            var customersQuery = _customerQueries.GetCustomersQuery();

            // 建立篩選條件
            var filterExpr = CreateFilterExpression(request);

            customersQuery = customersQuery
                             .AsExpandable() // 必須先呼叫此擴充方法(來自 LinqKit)。
                             .Where(filterExpr);

            // 排序
            switch (request.SortOrder.ToLower())
            {
            case "country":
            case "country asc":
                customersQuery = customersQuery.OrderBy(c => c.Country);
                break;

            case "country desc":
                customersQuery = customersQuery.OrderByDescending(c => c.Country);
                break;

            case "name desc":
                customersQuery = customersQuery.OrderByDescending(c => c.Name);
                break;

            default:
                customersQuery = customersQuery.OrderBy(c => c.Name);
                break;
            }

            // 分頁
            var pagedResult = await customersQuery.ToPagedListAsync(request.Page, request.PageSize);

            // 轉換成 View Model
            var customerModels = pagedResult.Select(ConvertToCustomerViewModel);

            return(new GetCustomersResponse
            {
                Succeeded = true,
                Customers = customerModels.ToArray()
            });
        }
Beispiel #22
0
        public async Task <GetCustomersResponse> GetCustomersV3([FromUri] GetCustomersRequest request)
        {
            var customersQuery = _customerQueries.GetCustomersQuery();

            // 建立篩選條件(使用 SalesApp.Core.Extensions.QueryableWhereExtension 裡面的擴充方法)
            customersQuery = customersQuery
                             .Where(request.Name, c => c.Name.Contains(request.Name))
                             .Where(request.Country, c => c.Country.Equals(request.Country));

            // 排序
            switch (request.SortOrder.ToLower())
            {
            case "country":
            case "country asc":
                customersQuery = customersQuery.OrderBy(c => c.Country);
                break;

            case "country desc":
                customersQuery = customersQuery.OrderByDescending(c => c.Country);
                break;

            case "name desc":
                customersQuery = customersQuery.OrderByDescending(c => c.Name);
                break;

            default:
                customersQuery = customersQuery.OrderBy(c => c.Name);
                break;
            }

            // 分頁
            var pagedResult = await customersQuery.ToPagedListAsync(request.Page, request.PageSize);

            // 轉換成 View Model
            var customerModels = pagedResult.Select(ConvertToCustomerViewModel);

            return(new GetCustomersResponse
            {
                Succeeded = true,
                Customers = customerModels.ToArray()
            });
        }
Beispiel #23
0
        public async Task CallsGetCustomersEndpointUsingRequest()
        {
            // given
            var subject = new CustomersClient(_clientConfiguration);

            var request = new GetCustomersRequest
            {
                Before = "before test",
                After  = "after test",
                Limit  = 5
            };

            // when
            await subject.GetPageAsync(request);

            // then
            _httpTest
            .ShouldHaveCalled("https://api.gocardless.com/customers?before=before%20test&after=after%20test&limit=5")
            .WithVerb(HttpMethod.Get);
        }
 private IList <CustomerListDTO> getCustomers(CustomerSearchType searchType, string name, string city)
 {
     try
     {
         GetCustomersRequest request = new GetCustomersRequest();
         request.SearchType   = searchType;
         request.CustomerName = name;
         request.City         = city;
         GetCustomersResponse response = Service.GetCustomersByCriteria(request);
         return(response.Customers);
     }
     catch (Exception ex)
     {
         if (ExceptionPolicy.HandleException(ex, "PL Policy"))
         {
             throw;
         }
         return(new List <CustomerListDTO>());
     }
 }
Beispiel #25
0
        public async Task <ActionResult <PagedResult <Customer> > > Get([FromQuery] GetCustomersRequest request)
        {
            var validator        = new GetCustomersRequestValidator();
            var validationResult = validator.Validate(request);

            if (!validationResult.IsValid)
            {
                return(BadRequest(new { ErrorMessage = validationResult.ErrorMessage }));
            }

            var filter = _mapper.Map <GetCustomersFilter>(request);
            var result = await _customerService.GetCustomersAsync(filter);

            var customers = _mapper.Map <IEnumerable <CustomerEntity>, IEnumerable <Customer> >(result.Result.Items);

            //A pagination result will give frontend flexibility to display data in pages.
            return(Ok(new PagedResult <Customer>()
            {
                Page = result.Result.Page, TotalCount = result.Result.TotalCount, RecordsPerPage = result.Result.RecordsPerPage, Items = customers
            }));
        }
        public async Task <IActionResult> Get(GetCustomersRequest request)
        {
            var requestStatus = request.Validate();

            if (!requestStatus.Status)
            {
                _logger.LogError($"Error: Invalid request. {requestStatus.Message}");

                return(BadRequest("Invalid Request"));
            }

            var getCustomersStatus = await _customerService.GetCustomersAsync(request);

            if (!getCustomersStatus.Status)
            {
                _logger.LogError($"Error: Cannot retrieve customers");

                return(StatusCode((int)HttpStatusCode.InternalServerError));
            }

            return(Ok(getCustomersStatus.Data));
        }
        public async Task <OperationResult <GetCustomersResponse> > HandleAsync(GetCustomersRequest input)
        {
            var result = input.Validate();

            if (!result.Status)
            {
                _logger.LogError($"Error: {result.Message}");
            }

            var getCustomersOperation = await _customerRepository.GetCustomersAsync(new PageModel()).ConfigureAwait(false);

            if (!getCustomersOperation.Status)
            {
                var message = $"Error: Cannot retrieve customers {getCustomersOperation.Message}";
                _logger.LogError(message);

                return(OperationResult <GetCustomersResponse> .Failure(message));
            }

            var displayCustomers = getCustomersOperation.Data.Select(x => new DisplayCustomer(x.Id, x.FullName));

            return(OperationResult <GetCustomersResponse> .Success(new GetCustomersResponse(displayCustomers)));
        }
Beispiel #28
0
        public async Task PagesThroughCustomers()
        {
            // given
            var subject = new CustomersClient(_clientConfiguration);
            var firstId = (await subject.GetPageAsync()).Items.First().Id;

            var initialRequest = new GetCustomersRequest
            {
                After = firstId,
                CreatedGreaterThan = new DateTimeOffset(DateTime.Now.AddDays(-1)),
                Limit = 1,
            };

            // when
            var result = await subject
                         .BuildPager()
                         .StartFrom(initialRequest)
                         .AndGetAllAfterAsync();

            // then
            Assert.That(result.Count, Is.GreaterThan(1));
            Assert.That(result[0].Id, Is.Not.Null.And.Not.EqualTo(result[1].Id));
            Assert.That(result[1].Id, Is.Not.Null.And.Not.EqualTo(result[0].Id));
        }
Beispiel #29
0
 /// <remarks/>
 public void GetCustomersAsync(GetCustomersRequest GetCustomersRequest, object userState) {
     if ((this.GetCustomersOperationCompleted == null)) {
         this.GetCustomersOperationCompleted = new System.Threading.SendOrPostCallback(this.OnGetCustomersOperationCompleted);
     }
     this.InvokeAsync("GetCustomers", new object[] {
                 GetCustomersRequest}, this.GetCustomersOperationCompleted, userState);
 }
        private IEnumerable<Customer> GetCustomers(string username, GetCustomersRequest getCustomersRequest)
        {
            if (!_authorizationService.CanExecute(username, "ViewCustomers"))
                throw new SecurityException("User is not authorized to View Customers");

            return _customerRepository.GetAllShallow();
        }
Beispiel #31
0
        public async Task <OperationResult <GetCustomersResponse> > GetCustomersAsync(GetCustomersRequest request)
        {
            var requestResult = request.Validate();

            if (!requestResult.Status)
            {
                var message = $"Error: Invalid request: {requestResult.Message}";
                _logger.LogError(message);

                return(OperationResult <GetCustomersResponse> .Failure(message));
            }

            var getCustomersOperationResult = await _getCustomersHandler.HandleAsync(request).ConfigureAwait(false);

            return(getCustomersOperationResult);
        }
Beispiel #32
0
 /// <remarks/>
 public void GetCustomersAsync(GetCustomersRequest GetCustomersRequest) {
     this.GetCustomersAsync(GetCustomersRequest, null);
 }
Beispiel #33
0
 public async Task <IEnumerable <GetCustomerResponse> > Get(GetCustomersRequest request) => await _mediator.Send(request);