public static bool validateUpdate(string name, int stock, int price, out string errorMsg)
 {
     errorMsg = "";
     if (name == "")
     {
         errorMsg = "Data must be filled!";
         return(false);
     }
     else if (stock <= 1)
     {
         errorMsg = "Input must be 1 or more";
         return(false);
     }
     else
     {
         var product = UpdateProductHandler.validateInsert(stock, price);
         if (product != null && !name.Equals(""))
         {
             return(true);
         }
         else
         {
             errorMsg = "Input must be above 1000 and multiply of 1000";
             return(false);
         }
     }
 }
        public async Task UpdateProductWhichIsNotInDb()
        {
            var id = Guid.NewGuid();
            var contextProvider = new ProductContextTestProvider();

            using var context = contextProvider.GetContext();
            contextProvider.AddProduct(new Database.Entities.Product(id, "Name1", "Number1", 11, 22));
            var handler      = new UpdateProductHandler(context, new ProductMapper());
            var productCount = context.Products.Count();

            var result = await handler.Handle(_request);

            var productCountAfterOperation = context.Products.Count();

            productCount.ShouldBe(1);
            productCountAfterOperation.ShouldBe(1);
            result.ShouldBeNull();
            var productDb = context.Products.SingleOrDefault(x => x.Id == id);

            productDb.ShouldNotBeNull();
            productDb.Number.ShouldBe("Number1");
            productDb.Name.ShouldBe("Name1");
            productDb.Quantity.ShouldBe(11);
            productDb.Id.ShouldBe(id);
        }
示例#3
0
        public UpdateProductTests()
        {
            _context = ProductCatalogContextFactory.Create();
            var mapper = MapperFactory.Create();

            _handler = new UpdateProductHandler(_context, mapper);
        }
        public async Task UpdateProductWhenDbIsEmpty()
        {
            using var context = new ProductContextTestProvider().GetContext();
            var handler      = new UpdateProductHandler(context, new ProductMapper());
            var productCount = context.Products.Count();

            var result = await handler.Handle(_request);

            var productCountAfterOperation = context.Products.Count();

            productCount.ShouldBe(0);
            productCountAfterOperation.ShouldBe(0);
            result.ShouldBeNull();
        }
        public async void UpdateProductHandlerShouldReturnId()
        {
            var sut      = new UpdateProductHandler(_context);
            var category = await _context.Categories.FirstOrDefaultAsync();

            var result = await sut.Handle(new UpdateProductCommand {
                Description = "Zmieniono",
                Name        = "Ryzen 4600fx",
                Category    = category.Adapt <CategoryViewModel>(),
                Price       = 1200
            },
                                          CancellationToken.None);

            var changed = await _context.Products.FirstAsync(x => x.Id == result);

            result.Should().BeOfType(typeof(int));
            changed.Description.Should().Be("Zmieniono");
        }
示例#6
0
        public async Task <ActionResult> Update([FromServices] UpdateProductHandler handler,
                                                [FromBody] UpdateProductCommand command)
        {
            try
            {
                var result = (CommandResult)handler.handle(command);

                if (!result.Success)
                {
                    return(BadRequest(result));
                }

                return(Ok(result));
            }
            catch (Exception e)
            {
                return(BadRequest(e));
            }
        }
示例#7
0
 public static Response UpdateItem(String ID, String Name, int Price, int Stock)
 {
     if (Name == "")
     {
         return(new Response(false, "Product Name Cannot Be Empty"));
     }
     if (Stock <= 0)
     {
         return(new Response(false, "Product Stock Must Be 1 Or More"));
     }
     if (Price <= 1000)
     {
         return(new Response(false, "Product Price Must Be Above 1000 and Multiply of 1000"));
     }
     if (Price % 1000 != 0)
     {
         return(new Response(false, "Product Price Must Be Above 1000 and Multiply of 1000"));
     }
     return(UpdateProductHandler.UpdateItem(ID, Name, Price, Stock));
 }
        public async Task UpdateProduct()
        {
            var contextProvider = new ProductContextTestProvider();

            using var context = contextProvider.GetContext();
            contextProvider.AddProduct(new Database.Entities.Product(_request.Id, "Name1", "Number1", 11, 22));
            var handler      = new UpdateProductHandler(context, new ProductMapper());
            var productCount = context.Products.Count();

            var result = await handler.Handle(_request);

            var productCountAfterOperation = context.Products.Count();

            productCount.ShouldBe(1);
            productCountAfterOperation.ShouldBe(1);
            result.ShouldNotBeNull();
            result.Number.ShouldBe("Number");
            result.Name.ShouldBe("Name");
            result.Quantity.ShouldBe(17);
            result.Id.ShouldBe(_request.Id);
        }
        public void AddsProductToRepository_RaisesIntegrationEvent()
        {
            var product    = new Domain.Product(Id, "Product Name", null);
            var repository = new Mock <IProductRepository>(MockBehavior.Strict);

            repository.Setup(r => r.GetAsync(product.Id)).Returns(Task.FromResult(product)).Verifiable();
            repository.Setup(r => r.UpdateAsync(product)).Returns(Task.CompletedTask).Verifiable();
            repository.Setup(r => r.SaveChanges()).Returns(Task.CompletedTask).Verifiable();

            var busPublisher = new Mock <IBusPublisher>(MockBehavior.Strict);

            busPublisher.Setup(p => p.Publish <IProductUpdated>(It.Is <IProductUpdated>(e => ValidateEquality(e)))).Returns(Task.CompletedTask).Verifiable();

            var handler = new UpdateProductHandler(repository.Object, busPublisher.Object);
            var result  = handler.HandleAsync(Cmd, new Mock <ICorrelationContext>().Object).GetAwaiter().GetResult();

            Assert.IsTrue(result.Successful);
            repository.Verify();
            busPublisher.Verify();
            ValidateEquality(product);
        }
示例#10
0
 public UpdateProductHandlerTests()
 {
     repository = Substitute.For <IProductsRepository>();
     handler    = new UpdateProductHandler(repository);
 }
示例#11
0
 public static MsProduct GetProductByID(String ID)
 {
     return(UpdateProductHandler.GetProductByID(ID));
 }
 public static void updateProduct(int id, string name, int price, int stock)
 {
     UpdateProductHandler.updateProduct(id, name, price, stock);
 }
        public async Task UpdateProduct_Success_ReturnUnit()
        {
            // Arrange
            // Mock data for product
            var category = new Category()
            {
                CategoryId = Constants.CategoryId,
                Name       = "Phone",
                Thumbnail  = "no-image.jpg"
            };

            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 = new List <ProductOption>()
                    {
                        new ProductOption()
                        {
                            ProductOptionId = Guid.NewGuid(),
                            OptionKey       = "Color",
                            OptionValues    = "Black, Product Red, White"
                        }
                    },
                    Images     = "no-images",
                    CreateDate = DateTime.Now,
                    CreatedBy  = Constants.UserId.ToString()
                }
            };

            _fuhoDbContext.Products.AddRange(products);
            _fuhoDbContext.SaveChanges();

            // Mock signle upload file
            var fileMock = new Mock <IFormFile>();
            //Setup mock file using a memory stream
            var content  = "Hello World from a Fake File";
            var fileName = "test.png";
            var ms       = new MemoryStream();
            var writer   = new StreamWriter(ms);

            writer.Write(content);
            writer.Flush();
            ms.Position = 0;
            fileMock.Setup(_ => _.OpenReadStream()).Returns(ms);
            fileMock.Setup(_ => _.FileName).Returns(fileName);
            fileMock.Setup(_ => _.Length).Returns(ms.Length);

            var mockImage = _fileSystemService.Setup(x => x.SingleUpdate(fileMock.Object, fileName)).ReturnsAsync(fileName);


            var updateProductCommand = new UpdateProductCommand()
            {
                BrandName      = "Test",
                ProductName    = "Test",
                CategoryId     = Constants.CategoryId,
                ProductId      = Constants.ProductId,
                Price          = 1000,
                Stock          = 10,
                Sku            = "123123",
                ProductOptions = new List <ProductOption>
                {
                    new ProductOption
                    {
                        ProductOptionId = Guid.NewGuid(),
                        OptionKey       = "Color",
                        OptionValues    = "Black, Product Red, White"
                    }
                },
                File   = fileMock.Object,
                UserId = Constants.UserId.ToString()
            };

            // Act
            var sut    = new UpdateProductHandler(_fuhoDbContext, _fileSystemService.Object, _logger.Object);
            var result = await sut.Handle(updateProductCommand, CancellationToken.None);

            // Result
            Assert.IsType <Unit>(result);
        }
 public static void GetUpdateProductController(string name, int stock, int price, int id)
 {
     UpdateProductHandler.GetUpdateProductHandler(name, stock, price, id);
 }
 public static Product GetProductController(int id)
 {
     return(UpdateProductHandler.GetProductHandler(id));
 }