public async Task <IEnumerable <TopProductDto> > GetTopSoldProductsFromOrders(IEnumerable <Order> orders, int count = 5) { var query = new GetTopSoldProductsFromOrdersQuery(orders, count); return(await _mediator.Send(query)); }
public async Task QueryHandler_ReturnsDescendingOrderedTopProductsSold( GetTopSoldProductsFromOrdersQuery query, IEnumerable <TopProductDto> expectedProducts) { var queryHandler = new GetTopSoldProductsHandler(_repositoryMock.Object); var result = await queryHandler.Handle(query, new CancellationToken()); result.Should().BeEquivalentTo(expectedProducts, options => options.WithStrictOrdering()); }
public async Task <IEnumerable <TopProductDto> > Handle(GetTopSoldProductsFromOrdersQuery request, CancellationToken cancellationToken) { var productIds = request.Orders.SelectMany(o => o.Lines) .Select(l => l.MerchantProductNo) .Distinct(); var products = await _repository.Products.GetProductsByMerchantNo(productIds); var quantityAggregate = request.Orders.SelectMany(o => o.Lines) .Aggregate(new Dictionary <string, int>(), AggregateQuantityByProduct) .ToArray(); var result = products.Join(quantityAggregate, product => product.MerchantProductNo, qa => qa.Key, (product, qa) => new TopProductDto(product, qa.Value)) .OrderByDescending(p => p.TotalSold) .ThenByDescending(p => p.Name) .Take(request.Count); return(result); }