Пример #1
0
        public async void ShouldCallQueryWithInvalidIdAndReturnNull()
        {
            var query = new FindProductByIdQuery(Guid.NewGuid());
            IQueryHandler <FindProductByIdQuery, ProductInfo> handler = new FindProductByIdQueryHandler(productRepository);
            var productInfo = await handler.ExecuteAsync(query);

            Assert.Null(productInfo);
        }
        public async Task <Result <ProductDto> > Handle(FindProductByIdQuery request, CancellationToken cancellationToken)
        {
            var result = await
                         Result.Try(async() => await _context.Products.FindAsync(new object[] { request.Id }, cancellationToken))
                         .TapIf(product => product is null, () => throw new NotFoundException(nameof(Product), request.Id))
                         .Map(_mapper.Map <ProductDto>);

            return(result);
        }
Пример #3
0
        public async void ShouldCreateProductCallQueryAndReturnProductInfo()
        {
            var product = new Product(Guid.NewGuid(), "Code", "Name");
            await productRepository.Save(product);

            var query = new FindProductByIdQuery(product.Id);
            IQueryHandler <FindProductByIdQuery, ProductInfo> handler = new FindProductByIdQueryHandler(productRepository);
            var productInfo = await handler.ExecuteAsync(query);

            Assert.NotNull(productInfo);
            Assert.Equal(productInfo.Id, product.Id);
            Assert.Equal(productInfo.Code, product.Code);
            Assert.Equal(productInfo.Name, product.Name);
            Assert.Contains(product.Code, productInfo.FullDescription);
            Assert.Contains(product.Name, productInfo.FullDescription);
        }
Пример #4
0
        public async Task <IActionResult> Get(Guid id)
        {
            try
            {
                var query = new FindProductByIdQuery(id);
                var info  = await findProductByIdQuery.ExecuteAsync(query);

                if (info == null)
                {
                    return(new NotFoundResult());
                }

                return(new OkObjectResult(info));
            }
            catch (Exception ex)
            {
                return(new BadRequestObjectResult(ex));
            }
        }