public async Task <IActionResult> GetProduct(Guid productId, CancellationToken token)
        {
            var query  = new GetProductByIdQuery(productId);
            var result = await _mediator.Send(query, token);

            return(result != null ? (IActionResult)Ok(result) : NotFound());
        }
Пример #2
0
        public async Task GetProductByIdQueryHandler_WhenCalled_ReturnGetCategoryByIdLookupModel()
        {
            var returnModel = new GetProductByIdQueryLookupModel
            {
                ProductId = 1,
                Name      = "Product1"
            };

            _productServiceMock
            .Setup(x => x.GetProductByIdAsync(It.IsAny <int>(), CancellationToken.None))
            .ReturnsAsync(returnModel);

            var query = new GetProductByIdQuery {
                ProductId = 1
            };

            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 GetProductByIdQueryHandler(_productServiceMock.Object);

            var result = await handler.Handle(query, CancellationToken.None);

            Assert.AreEqual("Product1", result.Name);
        }
Пример #3
0
        public async Task <IActionResult> Delete(Guid id)
        {
            var query  = new GetProductByIdQuery(id);
            var result = await _mediator.Send(query);

            return(View(new UpdateProductCommand(result)));
        }
Пример #4
0
        public ActionResult Update(int id)
        {
            var query   = new GetProductByIdQuery(id);
            var product = query.Execute();

            return(View(product));
        }
Пример #5
0
        public async Task <IActionResult> ProductById(Guid id)
        {
            var query    = new GetProductByIdQuery(id);
            var response = await _mediator.Send(query);

            return(response != null?Ok(response) : NotFound());
        }
Пример #6
0
        public async Task <IActionResult> GetProductById(int productId)
        {
            var query    = new GetProductByIdQuery(productId);
            var response = await _mediator.Send(query);

            return(Ok(response));
        }
Пример #7
0
        public async Task <ApiResponse <ProductDto> > Handle(GetProductByIdQuery request, CancellationToken cancellationToken)
        {
            var product = await _productService.GetProductByIdAsync(request.Id);

            var productDto = _mapper.Map <ProductDto>(product);

            return(new ApiResponse <ProductDto>(productDto));
        }
        public async Task <IActionResult> GetById(Guid id)
        {
            var query = new GetProductByIdQuery()
            {
                Id = id
            };

            return(Ok(await _mediator.Send(query)));
        }
        public async Task Handle(GetProductByIdQuery command)
        {
            var product = await _repository.GetProductByIdAsync(command.Id);

            var responseCommand = new GetProductByIdResponseCommand(product, command.CommandId);
            var gatewayMessage  = new GatewayMessage(responseCommand);

            await _producer.Produce(gatewayMessage);
        }
        public void Handle_GivenInvalidId_ThrowsException()
        {
            var query = new GetProductByIdQuery {
                Id = 99
            };

            Should.ThrowAsync <NotFoundException>(() =>
                                                  _handler.Handle(query, CancellationToken.None));
        }
Пример #11
0
        public void ShouldRequireValidProductId()
        {
            var command = new GetProductByIdQuery {
                Id = 99
            };

            FluentActions.Invoking(() =>
                                   SendAsync(command)).Should().Throw <NotFoundException>();
        }
Пример #12
0
        public async Task Returns_null_ifProductNotFound()
        {
            var query = new GetProductByIdQuery()
            {
                ProductId = context.Products.Count() + 1
            };
            var act = await new GetProductByIdHandler(context, mapper).Handle(query);

            Assert.IsNull(act);
        }
Пример #13
0
        public async Task CanGetProduct()
        {
            var query = new GetProductByIdQuery()
            {
                ProductId = 1
            };
            var act = await new GetProductByIdHandler(context, mapper).Handle(query);

            Assert.AreEqual(1, act.Id);
        }
Пример #14
0
        public async Task <IActionResult> Get(int id)
        {
            var query  = new GetProductByIdQuery(id);
            var result = await _mediator.Send(query);

            if (!result.Success)
            {
                return(BadRequest());
            }
            return(Ok(result));
        }
Пример #15
0
        // GET: ProductsController/Details/5
        public async Task <ActionResult> Details(int id)
        {
            var query  = new GetProductByIdQuery(id);
            var result = await _mediator.Send(query);

            if (result == null)
            {
                return(NotFound());
            }
            return(View(result.Products.FirstOrDefault()));
        }
Пример #16
0
        public async Task <ProductQuery> Handle(GetProductByIdQuery request, CancellationToken cancellationToken)
        {
            #region Persistence

            var productDomain = await _ProductRepository.GetById(request.Id);

            var response = productDomain.ToQueryModel <ProductQuery>(_Mapper);

            #endregion

            return(response);
        }
Пример #17
0
        public async Task GetProductWhichIsNotInDb()
        {
            var query = new GetProductByIdQuery(Guid.NewGuid());

            using var context = new ProductContextTestProvider().GetContext();
            var handler      = new GetProductByIdHandler(context, new ProductMapper());
            var productCount = context.Products.Count();

            var result = await handler.Handle(query);

            productCount.ShouldBe(0);
            result.ShouldBeNull();
        }
        public async Task <Response <ProductVm> > Handle(GetProductByIdQuery request, CancellationToken cancellationToken)
        {
            var response = await _productService.GetProductById(request.Id);

            if (response == null)
            {
                return(Response.Fail404NotFound <ProductVm>("Product Not Found"));
            }

            return(Response.Ok200(new ProductVm()
            {
                Id = response.Id, Name = response.Name, Price = response.Price
            }));
        }
        public async Task Handle_GivenValidId_ReturnsCorrectVmAndObject()
        {
            var query = new GetProductByIdQuery {
                Id = 1
            };

            var result = await _handler.Handle(query, CancellationToken.None);

            result.ShouldNotBeNull();
            result.ShouldBeOfType <ProductByIdVm>();

            var product = result.Product;

            product.ShouldBeOfType <ProductByIdDto>();
        }
Пример #20
0
        public async Task <Infrastructure.Database.Query.Model.Product> GetProductById(
            [GraphQLType(typeof(IdType))] Guid id,
            [Service] IServiceProvider serviceProvider,
            CancellationToken cancellationToken)
        {
            var getProductByIdQuery = new GetProductByIdQuery
            {
                Id = id
            };

            using (var scope = serviceProvider.CreateScope())
            {
                var mediator = scope.ServiceProvider.GetRequiredService <IMediator>();
                return(await mediator.Send(getProductByIdQuery, cancellationToken));
            }
        }
Пример #21
0
        public async Task <ActionResult <ProductDto> > GetProductAsync(Guid id)
        {
            var query = new GetProductByIdQuery {
                Id = id
            };
            var result = await _mediator.Send(query);

            if (result is null)
            {
                return(NotFound());
            }
            else
            {
                return(Ok(result));
            }
        }
Пример #22
0
        public async Task <IActionResult> GetProduct(Guid id)
        {
            var getProductQuery = new GetProductByIdQuery
            {
                Id = id
            };

            var result = await _mediator.Send(getProductQuery, new CancellationToken()).ConfigureAwait(false);

            if (result == null)
            {
                return(NotFound());
            }

            return(Ok(result));
        }
Пример #23
0
        public async Task <IActionResult> GetProductById(int id)
        {
            var query = new GetProductByIdQuery(id);

            var handler = new GetProductByIdHandler();

            var result = await handler
                         .HandlerAsync(query);

            if (result == null)
            {
                return(NotFound());
            }
            else
            {
                return(Ok(result));
            }
        }
Пример #24
0
        public async Task GetProductById()
        {
            var query           = new GetProductByIdQuery(Guid.NewGuid());
            var contextProvider = new ProductContextTestProvider();

            using var context = contextProvider.GetContext();
            var handler = new GetProductByIdHandler(context, new ProductMapper());

            contextProvider.AddProduct(new Database.Entities.Product(query.Id, "Name1", "Number1", 11, 22));
            contextProvider.AddProduct(new Database.Entities.Product(Guid.NewGuid(), "Name2", "Number2", 12, 23));
            contextProvider.AddProduct(new Database.Entities.Product(Guid.NewGuid(), "Name3", "Number3", 13, 24));
            var productCount = context.Products.Count();

            var result = await handler.Handle(query);

            productCount.ShouldBe(3);
            result.ShouldNotBeNull();
            result.Number.ShouldBe("Number1");
            result.Name.ShouldBe("Name1");
            result.Quantity.ShouldBe(11);
        }
        public async Task GetProductById_Success_ReturnProductDetail()
        {
            //Arrange
            var category = new Category()
            {
                CategoryId = Constants.CategoryId,
                Name       = "Phone",
                Thumbnail  = "no-image.jpg"
            };

            var productOption = new List <ProductOption>()
            {
                new ProductOption()
                {
                    ProductOptionId = Guid.NewGuid(),
                    OptionKey       = "Color",
                    OptionValues    = "Black, Product Red, White"
                },
                new ProductOption()
                {
                    ProductOptionId = Guid.NewGuid(),
                    OptionKey       = "Capacity",
                    OptionValues    = "64GB, 128GB"
                }
            };

            var products = new List <Product>()
            {
                new Product()
                {
                    ProductId      = Constants.ProductId,
                    BrandName      = "Pineapple",
                    ProductName    = "PinePhone X",
                    CategoryId     = category.CategoryId,
                    Price          = 1200,
                    Stock          = 12,
                    Sku            = "12312",
                    Category       = category,
                    ProductOptions = productOption,
                    Images         = "no-images",
                    CreateDate     = DateTime.Now,
                    CreatedBy      = Constants.UserId.ToString()
                }
            };

            await _fuhoDbContext.Products.AddRangeAsync(products);

            await _fuhoDbContext.SaveChangesAsync();

            var getProductbyIdQuery = new GetProductByIdQuery()
            {
                ProductId = Constants.ProductId
            };

            //Act
            var sut    = new GetProductByIdHandler(_fuhoDbContext, _logger.Object);
            var result = await sut.Handle(getProductbyIdQuery, CancellationToken.None);

            //Assert

            Assert.NotNull(sut);
            Assert.IsType <ProductDetailVm>(result);
        }
Пример #26
0
 public ProductDto GetProduct([FromUri] GetProductByIdQuery query)
 {
     return(_getProductByIdQueryHandler.HandleQuery(query));
 }
Пример #27
0
        public async Task <ProductDTO> GetProductAsync(int id)
        {
            var query = new GetProductByIdQuery(id);

            return(await _mediator.Send(query));
        }
Пример #28
0
 public async Task <ProductDto> GetProductById([FromRoute] GetProductByIdQuery query,
                                               CancellationToken cancellationToken)
 {
     return(await _mediator.Send(query, cancellationToken));
 }
Пример #29
0
        public async Task <ActionResult <ProductDto> > GetProduct([FromHybrid] GetProductByIdQuery query)
        {
            var result = await _mediator.Send(query);

            return(Ok(result));
        }
Пример #30
0
 public async Task <ProductVm> Handle(GetProductByIdQuery query, CancellationToken cancellationToken)
 {
     return(_productRepository.FindAsNoTracking(x => x.Id == query.Id).ToVm());
 }