public async Task <IList <ProductDto> > Handle( GetProductListQuery request, CancellationToken cancellationToken) { var collection = await _context.Products .ProjectTo <ProductDto>(_mapper.ConfigurationProvider) .ToListAsync(cancellationToken); return(collection); }
public async Task GetProductListQueryHandler_WhenCalled_ReturnGetCategoryByIdLookupModel() { var returnModel = new GetProductListQueryReturnModel { Products = new List <GetProductListQueryLookupModel> { new GetProductListQueryLookupModel { ProductId = 1, Name = "Product1" }, new GetProductListQueryLookupModel { ProductId = 2, Name = "Product2" } } }; _productServiceMock .Setup(x => x.GetAllProductsAsync(CancellationToken.None)) .ReturnsAsync(returnModel); var query = new GetProductListQuery(); var container = Registrations(); await using var scope = container.BeginLifetimeScope(); var options = scope.Resolve <DbContextOptions <TechnicalTestDbContext> >(); await using var context = new TechnicalTestDbContext(options); await SeedMockDataAsync(context); var handler = new GetProductListQueryHandler(_productServiceMock.Object); var result = await handler.Handle(query, CancellationToken.None); Assert.AreEqual(2, result.Products.Count); Assert.AreEqual("Product1", result.Products[0].Name); Assert.AreEqual("Product2", result.Products[1].Name); }
public GetProductListResponse Handle(GetProductListQuery query) { if (query == null) { return new GetProductListResponse { Success = false, Errors = new[] { "Request is empty!" } } } ; try { var products = _productsRepository.GetAll(); var productsMapped = products.Select(x => new ProductModel { Id = x.Id, Name = x.Name, Code = x.Code, ReleaseDate = x.ReleaseDate, Description = x.Description, Price = x.Price, StarRating = x.StarRating, ImageUrl = x.ImageUrl }); return(new GetProductListResponse { Success = true, Content = productsMapped, }); } catch (Exception ex) { return(new GetProductListResponse { Success = false, Errors = new[] { ex.Message } }); } } }
public async Task <IDataResult <List <Product> > > Handle(GetProductListQuery request, CancellationToken cancellationToken) { var cachedData = _cacheManager.TryGet(CacheKeys.PRODUCT_ALL); List <Product> cachedList; if (cachedData != null) { cachedList = JsonSerializer.Deserialize <List <Product> >(cachedData); } else { cachedList = await _productRepository.GetAllAsync(); Log.Information("{Data} Form DB", "Product List"); _cacheManager.Set(CacheKeys.PRODUCT_ALL, JsonSerializer.Serialize(cachedList)); } return(new DataResult <List <Product> >(cachedList, true)); }
public async Task <List <ProductDto> > Handle(GetProductListQuery request, CancellationToken cancellationToken) { return(await _productRepository.GetProductListAsync()); }
public async Task <List <ProductListVm> > Handle(GetProductListQuery request, CancellationToken cancellationToken) { var allProducts = (await this.ProductRepository.ListAllAsync()).OrderBy(x => x.Name); return(this.mapper.Map <List <ProductListVm> >(allProducts)); }
public async Task <ActionResult <ProductListViewModel> > GetAll([FromQuery] GetProductListQuery query) { return(Ok(await Mediator.Send(query))); }