public async Task <ActionResult <Result <ProductVm> > > GetProductById(Guid id) { var query = new GetProductQuery(id); var product = await _mediatR.Send(query); return(Ok(product)); }
public async Task Handle_ProductExists_ReturnProduct( Entities.Product product, [Frozen] Mock <IRepository <Entities.Product> > productRepoMock, GetProductQueryHandler sut, GetProductQuery query ) { // Arrange productRepoMock.Setup(x => x.GetBySpecAsync( It.IsAny <GetProductSpecification>(), It.IsAny <CancellationToken>() )) .ReturnsAsync(product); //Act var result = await sut.Handle(query, CancellationToken.None); //Assert result.Should().NotBeNull(); productRepoMock.Verify(x => x.GetBySpecAsync( It.IsAny <GetProductSpecification>(), It.IsAny <CancellationToken>() )); result.ProductNumber.Should().Be(product.ProductNumber); }
public async Task <ActionResult <ProductDTO> > Get(string id) { if (string.IsNullOrEmpty(id)) { return(BadRequest("invalid Id provided")); } var query = new GetProductQuery() { ProductId = id }; try { var result = await _mediator.Send(query); if (result != null) { return(Ok(result)); } else { return(BadRequest("Product could not be found")); } } catch { return(BadRequest("Error Retrieving Product")); } }
public async Task <IEnumerable <ProductResponseDto> > Handle(GetProductQuery request, CancellationToken cancellationToken) { using var dbcontext = _dataContextFactory.SpawnDbContext(); var products = await dbcontext.Products.Include(c => c.Category).Include(b => b.Brand).ToListAsync(); return(_mapper.Map <IEnumerable <ProductResponseDto> >(products)); }
public async Task <IActionResult> Get(int ID) { var result = new GetProductQuery(ID); var wait = await meciater.Send(result); return(wait != null ? (IActionResult)Ok(wait) : NotFound()); }
public async Task <ActionResult <BaseDto <Products_> > > Get(int id) { var command = new GetProductQuery(id); var result = await _mediatr.Send(command); return(result != null ? (ActionResult)Ok(new { Message = "success", data = result }) : NotFound(new { Message = "not found" })); }
public async Task <IActionResult> Put(int id, [FromBody] Contract.CreateProduct product) { var request = new GetProductQuery { ProductId = id }; bool doesProductExist = await _queryDispatcher.Execute <GetProductQuery, bool>(request).ConfigureAwait(false); if (doesProductExist) { var updateCmd = _mapper.Map <UpdateProductCommand>(product); updateCmd.ProductId = id; await _commandDispacher.Execute(updateCmd).ConfigureAwait(false); return(Ok()); } var createCmd = _mapper.Map <CreateProductCommand>(product); await _commandDispacher.Execute(createCmd).ConfigureAwait(false); var query = new GetProductQuery { ProductId = createCmd.ProductId }; var response = await _queryDispatcher.Execute <GetProductQuery, ProductDetailsResponse>(query).ConfigureAwait(false); return(CreatedAtRoute("Get", new { id = createCmd.ProductId }, response.Product)); }
public ProductTests() { _getCategoryBaseQuery = new GetCategoryBaseQuery(DbContext, Cache); _getProductQuery = new GetProductQuery(DbContext, _getCategoryBaseQuery); _listProductsQuery = new ListProductsQuery(DbContext); _getProductQueryHandler = new GetProductQueryHandler(_getProductQuery); _listProductsQueryHandler = new ListProductsQueryHandler(_listProductsQuery); }
public async Task <GetProductQuery.QueryResult> HandleAsync(GetProductQuery query) { var productDomainModel = await _productsService.GetProductAsync(query.Id); return(new GetProductQuery.QueryResult { ProductModel = _mapper.Map <ProductViewModel>(productDomainModel) }); }
public async Task <Product> GetProducts(int id) { var query = new GetProductQuery { Id = id }; return(await _mediator.Send(query)); }
public GetProductQueryHandlerTests() { _handler = new GetProductQueryHandler(Context, Mapper); _query = new GetProductQuery { Id = TestContext.TestProductCommon.Id }; }
public async Task <IActionResult> Get(int id) { var query = new GetProductQuery { ProductId = id }; var response = await _queryDispatcher.Execute <GetProductQuery, ProductDetailsResponse>(query).ConfigureAwait(false); return(Ok(response)); }
public void TestValidate_Valid_NoValidationError( GetProductQueryValidator sut, GetProductQuery query ) { query.ProductNumber = query.ProductNumber.Substring(0, 25); var result = sut.TestValidate(query); result.ShouldNotHaveValidationErrorFor(query => query.ProductNumber); }
public async Task <IActionResult> Get(long id) { var request = new GetProductQuery() { Id = id }; var response = await Mediator.Send(request); return(Ok(response)); }
public void TestValidate_QueryWithTooLongProductNumber_ValidationError( GetProductQueryValidator sut, GetProductQuery query ) { var result = sut.TestValidate(query); result.ShouldHaveValidationErrorFor(query => query.ProductNumber) .WithErrorMessage("Product number must not exceed 25 characters"); }
public void TestValidate_QueryWithEmptyProductNumber_ValidationError( GetProductQueryValidator sut, GetProductQuery query ) { query.ProductNumber = ""; var result = sut.TestValidate(query); result.ShouldHaveValidationErrorFor(query => query.ProductNumber) .WithErrorMessage("Product number is required"); }
public async Task <IActionResult> Get([FromRoute] int productId) { var query = new GetProductQuery { ProductId = productId }; var result = await mediator.Send(query).ConfigureAwait(false); return(Ok(new Response <ProductResponse>(result))); }
public void ShouldThrow_WhenProductDoesntExist() { // Arrange var request = new GetProductQuery(); // Act Func <Task <ProductInfoDto> > act = () => SendAsync(new GetProductQuery()); // Assert act.Should().Throw <ValidationException>().And.Error.Message.Should().Be("Product doesn't exist"); }
public async Task Handle_GivenValidId_ReturnsCorrectProduct() { var query = new GetProductQuery { Id = 999 }; var sut = new GetProductQueryHandler(_context); var result = await sut.Handle(query, CancellationToken.None); result.ProductId.ShouldBe(999); }
public async Task <Result <Product> > GetProductAsync(GetProductByCodeRequest request, CancellationToken cancellationToken) { var query = new GetProductQuery(request.ProductCode); var operation = await _mediator.Send(query, cancellationToken); if (!operation.Status) { return(Result <Product> .Failure("", "Error occured when getting the product.")); } return(Result <Product> .Success(operation.Data)); }
public async Task <IActionResult> Post([FromBody] Contract.CreateProduct product) { var createCmd = _mapper.Map <CreateProductCommand>(product); await _commandDispacher.Execute(createCmd); var query = new GetProductQuery { ProductId = createCmd.ProductId }; var response = await _queryDispatcher.Execute <GetProductQuery, ProductDetailsResponse>(query).ConfigureAwait(false); return(CreatedAtRoute("Get", new { id = createCmd.ProductId }, response.Product)); }
public async Task <IActionResult> GetByCode(string code) { var query = new GetProductQuery { Code = code }; var response = await _queryDispatcher.Execute <GetProductQuery, ProductDetailsResponse>(query).ConfigureAwait(false); if (response.Product != null) { return(Ok()); } return(NotFound()); }
public async Task <Result <ProductDto> > Handle(GetProductQuery request, CancellationToken cancellationToken) { var product = await _dataContext.Products.FirstOrDefaultAsync(p => p.Id == request.Id, cancellationToken); if (product == null) { return(Result.Error <ProductDto>(Resource.ProductIdDosentExist)); } var productDto = _mapper.Map <ProductDto>(product); return(Result.Ok(productDto)); }
public GetProductQueryHandlerTest() { mapper = new Mock <IMapper>(); productRepository = new Mock <IProductRepository>(); query = new GetProductQuery(productId); queryHandler = new GetProductQueryHandler(productRepository.Object, mapper.Object); product = new Product { ProductName = productName }; productDto = new GetProductDto { ProductName = productName }; }
public async Task <IActionResult> GetProduct(int productId) { var productQuery = new GetProductQuery { ProductId = productId }; var result = await _mediator.Send(productQuery); if (result == null) { return(NotFound()); } return(Ok(result)); }
public async Task ShouldReturnProduct() { // Arrange var request = new GetProductQuery() { Id = ProductId }; // Act ProductInfoDto productDto = await SendAsync(request); // Assert productDto.Should().NotBeNull(); productDto.Id.Should().Be(ProductId); }
public async Task <ActionResult <EntityResponse <Product> > > GetProduct(string id) { try { var query = new GetProductQuery(id); var result = await _mediator.Send(query); return(Ok(result)); } catch (Exception ex) { var err = new EntityResponse <Product> (); err.ReponseName = nameof(GetProduct); err.Status = ResponseType.Error; err.Message = ex.Message; err.Content = null; return(Ok(err)); } }
public void TestValidate_ProductNumberDoesNotExist_ValidationError( [Frozen] Mock <IRepository <Entities.Product> > productRepoMock, GetProductQueryValidator sut, GetProductQuery query ) { productRepoMock.Setup(x => x.GetBySpecAsync( It.IsAny <GetProductSpecification>(), It.IsAny <CancellationToken>() )) .ReturnsAsync((Entities.Product)null); var result = sut.TestValidate(query); result.ShouldHaveValidationErrorFor(query => query.ProductNumber) .WithErrorMessage("Product does not exist"); }
public async Task GetProduct_ShouldReturnNotFound_WhenProductNotFound( [Frozen] Mock <IMediator> mockMediator, [Greedy] ProductController sut, GetProductQuery query ) { //Arrange mockMediator.Setup(x => x.Send(It.IsAny <GetProductQuery>(), It.IsAny <CancellationToken>())) .ReturnsAsync((Core.Handlers.GetProduct.Product)null); //Act var actionResult = await sut.GetProduct(query); //Assert var okObjectResult = actionResult as NotFoundResult; okObjectResult.Should().NotBeNull(); }
public Task <GetProductQuery> GetByIdAsync(Guid id) { GetProductQuery result = null; var product = _products.FirstOrDefault(a => a.Id == id); if (product != null) { result = new GetProductQuery { Id = product.Id, Name = product.Name, Description = product.Description, Price = product.Price }; } return(Task.FromResult(result)); }