Exemplo n.º 1
0
        public async Task CallAction_CreateQuery()
        {
            var hypermediaObjectRegister = CreateHypermediaObjectRegister();

            var sirenClient = new SirenHttpHypermediaClient <EntryPointHco>(ApiEntryPoint, hypermediaObjectRegister);
            var apiRoot     = await sirenClient.EnterAsync();

            var customersRoot = await apiRoot.NavigateAsync(l => l.Customers);

            var query = new CustomersQuery
            {
                Filter = new CustomerFilter {
                    MinAge = 22
                },
                SortBy = new SortOptions {
                    PropertyName = "Age", SortType = "Ascending"
                },
                Pagination = new Pagination {
                    PageOffset = 2, PageSize = 3
                }
            };

            var resultResource = await customersRoot.CreateQuery.ExecuteAsync(query);

            var queryResultPage = await resultResource.ResultLocation.ResolveAsync();
        }
        public async Task CallAction_CreateQuery()
        {
            var apiRoot = await this.Resolver.ResolveLinkAsync <EntryPointHco>(ApiEntryPoint);

            var customersRoot = await apiRoot.NavigateAsync(l => l.Customers);

            var query = new CustomersQuery
            {
                Filter = new CustomerFilter {
                    MinAge = 22
                },
                SortBy = new SortOptions {
                    PropertyName = "Age", SortType = "Ascending"
                },
                Pagination = new Pagination {
                    PageOffset = 2, PageSize = 3
                }
            };

            var resultResource = await customersRoot.ResultObject.CreateQuery.ExecuteAsync(query, this.Resolver);

            var queryResultPage = await resultResource.ResultLocation.ResolveAsync();

            Assert.IsNotNull(queryResultPage);
        }
Exemplo n.º 3
0
        public async Task <IEnumerable <CustomerServiceResult> > GetCustomersAsync(CustomersQuery qp)
        {
            try
            {
                // Here I list the query result from cache if they exist, but now the data can vary according to the category ID, page and amount of
                // items per page. I have to compose a cache to avoid returning wrong data.
                string cacheKey = GetCacheKeyForCustomersQuery(qp);

                var entities = await _cache.GetOrCreateAsync(cacheKey, (entry) => {
                    entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(1);
                    return(_customersRepository.ListPaginationAsync(qp));
                });

                var models = entities != null && entities.Any()
                    ? _mapper.Map(entities, new List <CustomerServiceResult>())
                    : new List <CustomerServiceResult>();

                return(models);
            }
            catch (Exception ex)
            {
                _logger?.LogError(ex.ToString());
                throw;
            }
        }
Exemplo n.º 4
0
        public string PageQuery([FromBody] CustomersQuery query)
        {
            Thread.Sleep(1000);
            var list = CustomersService.PagerQuery(query);

            return(Util.Helpers.Json.ToJson(list));
        }
        public async Task <IEnumerable <Customer> > ListPaginationAsync(CustomersQuery query)
        {
            // AsNoTracking tells EF Core it doesn't need to track changes on listed entities. Disabling entity
            // tracking makes the code a little faster
            IQueryable <Customer> queryable = _customers
                                              .AsNoTracking();


            if (!string.IsNullOrEmpty(query.Search))
            {
                queryable = queryable.Where(p =>
                                            (p.FirstName + " " +
                                             p.LastName + " " +
                                             p.Address + " " +
                                             p.PhoneNumber + " " +
                                             p.Email).ToLower().Contains(query.Search.ToLower()));
            }


            // Here I apply a simple calculation to skip a given number of items, according to the current page and amount of items per page,
            // and them I return only the amount of desired items. The methods "Skip" and "Take" do the trick here.
            if (query.Limit.HasValue && query.Offset.HasValue)
            {
                queryable = queryable.Skip(query.Offset.Value)
                            .Take(query.Limit.Value);
            }

            return(await queryable.ToListAsync());
        }
Exemplo n.º 6
0
        public int Customers_GetCount(string serializedQuery)
        {
            CustomersQuery query = CustomersQuery.SerializeHelper.FromXml(
                serializedQuery, typeof(CustomersQuery), AllKnownTypes) as CustomersQuery;

            return(query.ExecuteScalar <int>());
        }
        public async Task <List <Customer> > Handle(CustomersQuery request, CancellationToken cancellationToken)
        {
            var customers = request.IsActive == null
                ? await _repository.GetCustomersAsync()
                : await _repository.GetCustomersAsync(c => c.IsActive == (bool)request.IsActive);

            return(customers);
        }
Exemplo n.º 8
0
        public async Task <IEnumerable <Contact> > GetContactsAsync(Guid id)
        {
            var query = new CustomersQuery {
                FilterIds = id.Collect()
            };

            return((await _queryInvoker.Execute <CustomersQuery, Customer>(query)).SingleOrDefault()?.Contacts);
        }
Exemplo n.º 9
0
        public async Task <Customer> GetAsync(Guid id)
        {
            var query = new CustomersQuery {
                FilterIds = id.Collect()
            };

            return((await _queryInvoker.Execute <CustomersQuery, Customer>(query)).SingleOrDefault());
        }
        public async Task ShouldReturnCustomerListForValidCountry(IQueryHandler <CustomersQuery, Customer[]> queryHandler)
        {
            var query = new CustomersQuery {
                Country = "Germany"
            };
            var result = await queryHandler.HandleAsync(query);

            result.ShouldNotBeEmpty();
        }
        public async Task <IActionResult> All([FromQuery] CustomersQuery query)
        {
            var model = await _mediator.Send(query);

            return(new ObjectResult(model)
            {
                StatusCode = (int)HttpStatusCode.OK
            });
        }
        public async Task ShouldReturnEmptyListForUnknownCountry(IQueryHandler <CustomersQuery, Customer[]> queryHandler)
        {
            var query = new CustomersQuery {
                Country = "Fantasyland"
            };
            var result = await queryHandler.HandleAsync(query);

            result.ShouldBeEmpty();
        }
Exemplo n.º 13
0
        public async Task ShouldGetCustomersFromGermany(IQueryExecutor queryExecutor)
        {
            var query = new CustomersQuery {
                Country = "Germany"
            };
            var result = await queryExecutor.ExecuteAsync(query);

            Assert.Equal(11, result.Length);
        }
Exemplo n.º 14
0
        public CustomersCollection Customers_LoadByDynamic(string serializedQuery)
        {
            CustomersQuery query = CustomersQuery.SerializeHelper.FromXml(
                serializedQuery, typeof(CustomersQuery), AllKnownTypes) as CustomersQuery;

            CustomersCollection coll = new CustomersCollection();

            coll.es.IsLazyLoadDisabled = true;
            coll.Load(query);
            return(coll);
        }
Exemplo n.º 15
0
        private string GetCacheKeyForCustomersQuery(CustomersQuery query)
        {
            string key = CacheKeys.CustomersList.ToString();

            if (!string.IsNullOrEmpty(query.Search))
            {
                key = string.Concat(key, "_", query.Search);
            }

            key = string.Concat(key, "_", query.Limit, "_", query.Offset);
            return(key);
        }
 public Task <PaginatedList <CustomersQueryDto> > Handle(CustomersQuery request, CancellationToken cancellationToken)
 {
     return(_unitOfWork.CustomerRepository.GetAll()
            .Select(x => new CustomersQueryDto()
     {
         Id = x.Id,
         Name = x.PersonalInfo.Name,
         Surname = x.PersonalInfo.Surname,
         Email = x.PersonalInfo.Email,
         Nationality = x.PersonalInfo.Nationality,
         PhoneNumber = x.PersonalInfo.PhoneNumber
     })
            .AsPaginatedList(request.GetPagingElements()));
 }
        public CustomersProxyStub Customers_QueryForEntity(string serializedQuery)
        {
            CustomersQuery query = CustomersQuery.SerializeHelper.FromXml(
                serializedQuery, typeof(CustomersQuery), AllKnownTypes) as CustomersQuery;

            Customers obj = new Customers();

            if (obj.Load(query))
            {
                return(obj);
            }

            return(null);
        }
        public CustomersCollectionProxyStub Customers_QueryForCollection(string serializedQuery)
        {
            CustomersQuery query = CustomersQuery.SerializeHelper.FromXml(
                serializedQuery, typeof(CustomersQuery), AllKnownTypes) as CustomersQuery;

            CustomersCollection coll = new CustomersCollection();

            if (coll.Load(query))
            {
                return(coll);
            }

            return(null);
        }
Exemplo n.º 19
0
        public async Task <IActionResult> GetCustomers([FromQuery] CustomersQuery customersQuery)
        {
            if (customersQuery == null)
            {
                return(BadRequest());
            }

            var result = await _mediator.Send(customersQuery);

            if (customersQuery.Invalid)
            {
                return(new BadRequestObjectResult(result));
            }

            return(new OkObjectResult(result));
        }
Exemplo n.º 20
0
        public async Task QueryInvokerShouldResolveQueryHandler()
        {
            var customers = Bogus.CustomerFaker.Generate(3);

            _dbContext.AddRange(customers);
            _dbContext.SaveChanges();

            var queryInvoker = Root.Container.GetInstance <IQueryInvoker>();

            var query = new CustomersQuery
            {
                FilterIds = customers[0].Id.Collect()
            };
            var response = await queryInvoker.Execute <CustomersQuery, Customer>(query);

            Assert.That(response.Count, Is.EqualTo(1));
            Assert.That(response[0], Is.EqualTo(customers[0]));

            query = new CustomersQuery
            {
                FilterNameLike = customers[0].Name
            };
            response = await queryInvoker.Execute <CustomersQuery, Customer>(query);

            Assert.That(response.Count, Is.EqualTo(1));
            Assert.That(response[0], Is.EqualTo(customers[0]));

            query = new CustomersQuery
            {
                FilterIds = new[] { customers[0].Id, customers[1].Id }
            };
            response = await queryInvoker.Execute <CustomersQuery, Customer>(query);

            Assert.That(response.Count, Is.EqualTo(2));
            Assert.That(response, Is.EquivalentTo(new[] { customers[0], customers[1] }));

            response = await queryInvoker.Execute <CustomersQuery, Customer>(new CustomersQuery());

            Assert.That(response.Count, Is.EqualTo(customers.Count));
            Assert.That(response, Is.EquivalentTo(customers));
        }
Exemplo n.º 21
0
        public async Task <IEnumerable <Customer> > GetAsync([FromQuery] string nameLike,
                                                             [FromQuery] CustomerType?typeExact, [FromQuery] string countryExact,
                                                             [FromQuery] string provinceExact, [FromQuery] string cityExact, int?take, int?skip,
                                                             string orderBy, string orderMode)
        //ToDo:4 optionally include archived?
        {
            var query = new CustomersQuery
            {
                Take                    = take,
                Skip                    = skip,
                OrderBy                 = orderBy,
                OrderMode               = orderMode,
                FilterNameLike          = nameLike,
                FilterCustomerTypeExact = typeExact,
                FilterCountryExact      = countryExact,
                FilterProvinceExact     = provinceExact,
                FilterCityExact         = cityExact
            };

            //ToDo:2 query.AsLazy(); ??
            return(await _queryInvoker.Execute <CustomersQuery, Customer>(query));
        }
Exemplo n.º 22
0
 public async Task <PaginatedList <CustomersQueryDto> > GetCustomers([FromQuery] CustomersQuery query)
 => await Mediator.Send(query);
        public IActionResult GetCustomers(CustomersQuery customersQuery)
        {
            var result = _processor.Get(customersQuery);

            return(Ok(new { result.Items, result.Total }));
        }
Exemplo n.º 24
0
 //[Authorize(Roles = "user")]
 public async Task <IEnumerable <CustomerDto> > Customers([FromQuery] CustomersQuery customersQuery)
 {
     return(await _mediator.Send(customersQuery));
 }
 public async Task ShouldReturnEmptyListForUnknownCountry(IQueryHandler<CustomersQuery, Customer[]> queryHandler)
 {
     var query = new CustomersQuery { Country = "Fantasyland" };
     var result = await queryHandler.HandleAsync(query);
     result.ShouldBeEmpty();
 }       
 public async Task ShouldReturnCustomerListForValidCountry(IQueryHandler<CustomersQuery, Customer[]> queryHandler)
 {
     var query = new CustomersQuery { Country = "Germany" };
     var result = await queryHandler.HandleAsync(query);
     result.ShouldNotBeEmpty();
 }
 public PagedResult <CustomerItem> Handle(CustomersQuery query)
 {
     return(_archContext.Customers.GetPagedResult <Domain.Entities.Customer, CustomerItem>(_mapper, query.Paging));
 }
Exemplo n.º 28
0
        public Task <IEnumerable <CustomerDto> > Handle(CustomersQuery request, CancellationToken cancellationToken)
        {
            var customers = _customerDomain.GetAll();

            return(Task.FromResult(customers.MapTo <IEnumerable <CustomerDto> >()));
        }