public async Task CreateProductCommandHandlerAsync_WithNullParameter_ThrowsException()
        {
            //Arrange
            CreateProductCommandRequest command = null;

            //Act
            var actualException = await Assert.ThrowsAsync <Exception>(() => _createProductCommandHandler.Handle(command, new CancellationToken()));

            //Assert
            Assert.Equal(MessageConstants.CreateProductCommandRequestNull, actualException.Message);
        }
        public CreateProductCommandResponse CreateProduct(CreateProductCommandRequest createProductCommandRequest)
        {
            var id = Guid.NewGuid();

            ApplicationDbContext.ProductList.Add(new()
            {
                Id         = id,
                Name       = createProductCommandRequest.Name,
                Price      = createProductCommandRequest.Price,
                Quantity   = createProductCommandRequest.Quantity,
                CreateTime = DateTime.Now
            });
            return(new CreateProductCommandResponse
            {
                IsSuccess = true,
                ProductId = id
            });
        }
        public async Task CreateProductCommandHandlerAsync_WithUnexistingCategory_ThrowsException()
        {
            //Arrange
            var createProductRequest = new CreateProductCommandRequest
            {
                Title         = "testTitle",
                Description   = "testDescription",
                StockQuantity = 15,
                CategoryId    = 3
            };

            _mockUnitOfWork.Setup(x => x.CategoryRepository.GetById(createProductRequest.CategoryId)).Returns((Category)(null));


            //Act
            var actualException = await Assert.ThrowsAsync <Exception>(() => _createProductCommandHandler.Handle(createProductRequest, new CancellationToken()));

            //Assert
            Assert.Equal(MessageConstants.CreateProductCommandRequestNull, actualException.Message);;
        }
        public async Task CreateProductCommandHandlerAsync_WithValidParameters_CreatesProduct()
        {
            //Arrange
            var createProductRequest = new CreateProductCommandRequest
            {
                Title         = "testTitle",
                Description   = "testDescription",
                StockQuantity = 15,
                CategoryId    = 1
            };

            _mockUnitOfWork.Setup(x => x.ProductRepository.Add(It.IsAny <Product>())).Verifiable();
            _mockUnitOfWork.Setup(x => x.CategoryRepository.GetById(createProductRequest.CategoryId)).Returns(Category.Create(1, "HouseHoldAppliences", 10));
            _mockUnitOfWork.Setup(x => x.SaveChanges()).Verifiable();
            _mediatoR.Setup(x => x.Publish(new ProductCreatedEvent(Guid.NewGuid(), createProductRequest.Title, createProductRequest.Description, createProductRequest.StockQuantity), new CancellationToken())).Verifiable();

            //Act
            var createProductResponse = await _createProductCommandHandler.Handle(createProductRequest, new CancellationToken());

            //Assert
            Assert.True(createProductResponse.IsSuccess);
        }
        public IActionResult Post([FromBody] CreateProductCommandRequest requestModel)
        {
            CreateProductCommandResponse response = _createProductCommandHandler.CreateProduct(requestModel);

            return(Ok(response));
        }
        public async Task <IActionResult> Post([FromBody] CreateProductCommandRequest requestModel)
        {
            var response = await _mediator.Send(requestModel);

            return(Ok(response));
        }
Beispiel #7
0
        public async Task <ActionResult <CreateProductCommandResponse> > AddProduct(CreateProductCommandRequest requestModel)
        {
            var productInsertResult = await Mediator.Send(requestModel);

            return(productInsertResult);
        }