public void Create_WhenValidProductVersionProvided_CallsCreateProductAndProductVersion() { //Assign var productVersion = new FullProductDto { Description = "Product 6", Name = "Product 6", Cost = 2, ProductId = Guid.NewGuid(), SoftDelete = false, UrlImg = "", ProductVersionId = Guid.NewGuid(), //guid specifinis IsInStore = true, IsOrderable = false, Quantity = 10, ProductCreated = DateTime.Now, ProductVersionCreated = DateTime.Now, }; //Act _productVersionLogic.Create(productVersion); //Assert _productRepository.Verify(x => x.Create(It.IsAny <Product>()), Times.Once); _productVersionRepository.Verify(x => x.Create(It.IsAny <ProductVersion>()), Times.Once); }
async public Task <ActionResult> Post([FromBody] FullProductDto fullproductDto) { fullproductDto.ProductCreated = DateTime.Now; fullproductDto.ProductVersionCreated = DateTime.Now; _productLogic.Create(fullproductDto); return(Ok()); }
public void Create_WhenProductNameAlreadyExists_ThrowsDuplicateProductNameBusinessException() { //Assign var productVersion = new FullProductDto { Description = "Product 5", Name = "Product 5", Cost = 2, ProductId = Guid.NewGuid(), SoftDelete = false, UrlImg = "", ProductVersionId = Guid.NewGuid(), IsInStore = true, IsOrderable = false, Quantity = 0, ProductCreated = DateTime.Now, ProductVersionCreated = DateTime.Now, }; //Act var ex = (BusinessException)Record.Exception(() => _productVersionLogic.Create(productVersion)); //Assert Assert.Equal(ExceptionCode.DuplicateProductNames, ex.Code); }
public void Create(FullProductDto combined) { Product product; ProductVersion productVersion; var sameNameProductVersion = _productVersionRepository.GetMany(x => x.Name.Equals(combined.Name) && !x.SoftDelete && !Guid.Equals(x.Id, combined.ProductVersionId)); if (sameNameProductVersion.Count() != 0) { throw new BusinessException(ExceptionCode.DuplicateProductNames); } combined.SplitCombinedProduct(out productVersion, out product); _productRepository.Create(product); _productVersionRepository.Create(productVersion); }
public ICollection <FullProductDto> GetAllCombined() { var versions = GetExistingProductVersions(); var products = GetProducts(); List <FullProductDto> listOfActiveProducts = new List <FullProductDto>(); foreach (var version in versions) { FullProductDto temp = new FullProductDto(); var product = products.Where(x => x.Id == version.ProductId).FirstOrDefault(); temp = version.ToCombinedProduct(product); listOfActiveProducts.Add(temp); } return(listOfActiveProducts); }
public bool UpdateVersion(FullProductDto combined) { var success = false; using (var scope = _productVersionRepository.DatabaseFacade.BeginTransaction()) { var sameNameProductVersion = _productVersionRepository.GetMany(x => x.Name.Equals(combined.Name) && x.SoftDelete == false && x.Id != combined.ProductVersionId); if (sameNameProductVersion.Count() != 0) { throw new BusinessException(ExceptionCode.DuplicateProductNames); } var oldProductVersion = _productVersionRepository.GetByID(combined.ProductVersionId); if (oldProductVersion != null) { var oldProduct = _productRepository.Get(x => x.Id == oldProductVersion.ProductId); oldProduct.IsInStore = combined.IsInStore; oldProduct.Quantity = combined.Quantity; oldProduct.IsOrderable = combined.IsOrderable; _productRepository.Update(oldProduct); _productRepository.SaveChanges(); if (IsNewProductVersionNeeded(combined, oldProductVersion)) { oldProductVersion.SoftDelete = true; ProductVersion newProductVersion = new ProductVersion(); newProductVersion.Description = combined.Description; newProductVersion.Cost = combined.Cost; newProductVersion.Created = DateTime.Now; newProductVersion.Name = combined.Name; newProductVersion.ProductId = oldProduct.Id; newProductVersion.UrlImg = combined.UrlImg; newProductVersion.SoftDelete = false; _productVersionRepository.Update(oldProductVersion); _productVersionRepository.Create(newProductVersion); _productVersionRepository.SaveChanges(); } scope.Commit(); success = true; } return(success); } }
public static void SplitCombinedProduct(this FullProductDto combined, out ProductVersion version, out Product product) { version = new ProductVersion(); product = new Product(); version.Cost = combined.Cost; version.Created = DateTime.Now; version.Description = combined.Description; version.Name = combined.Name; version.SoftDelete = combined.SoftDelete; version.UrlImg = combined.UrlImg; version.Product = product; product.IsOrderable = combined.IsOrderable; product.Created = DateTime.Now; product.IsInStore = combined.IsInStore; product.Quantity = combined.Quantity; }
public static FullProductDto ToCombinedProduct(this ProductVersionDto version, ProductDto product) { FullProductDto newProduct = new FullProductDto(); newProduct.ProductVersionId = version.Id; newProduct.Cost = version.Cost; newProduct.ProductVersionCreated = version.Created; newProduct.Description = version.Description; newProduct.IsInStore = product.IsInStore; newProduct.ProductCreated = product.Created; newProduct.Name = version.Name; newProduct.UrlImg = version.UrlImg; newProduct.IsOrderable = product.IsOrderable; newProduct.Quantity = product.Quantity; newProduct.SoftDelete = version.SoftDelete; newProduct.ProductId = version.ProductId; return(newProduct); }
//Returns true only if name, category or cost changes. Changes to quantity, active or orderable fields do not require new version creation. private bool IsNewProductVersionNeeded(FullProductDto newProductVersion, ProductVersion currentProductVersion) { if (!currentProductVersion.Name.Equals(newProductVersion.Name)) { return(true); } if (currentProductVersion.Cost != newProductVersion.Cost) { return(true); } if (currentProductVersion.Description != newProductVersion.Description) { return(true); } if (currentProductVersion.UrlImg != newProductVersion.UrlImg) { return(true); } return(false); }
public void Create(FullProductDto combined) { Product product; ProductVersion productVersion; if (_productVersionRepository.Count() > 0) { var sameNameProductVersion = _productVersionRepository.GetMany(x => x.Name.Equals(combined.Name) && !x.SoftDelete && !Guid.Equals(x.Id, combined.ProductVersionId)); if (sameNameProductVersion.Count() != 0) { throw new BusinessException(ExceptionCode.DuplicateProductNames); } } combined.SplitCombinedProduct(out productVersion, out product); if (product != null && productVersion != null) { using (var scope = _productVersionRepository.DatabaseFacade.BeginTransaction()) { try { _productRepository.Create(product); _productRepository.SaveChanges(); _productVersionRepository.Create(productVersion); _productVersionRepository.SaveChanges(); scope.Commit(); } catch (Exception e) { throw new BusinessException(ExceptionCode.Unhandled); } } } else { throw new BusinessException(ExceptionCode.Unhandled); } }
public void UpdateVersion(FullProductDto combined) { var productId = _productVersionRepository.GetAll().ToList(); var sameNameProductVersion = _productVersionRepository.GetMany(x => x.Name.Equals(combined.Name) && x.SoftDelete == false && !Guid.Equals(x.Id, combined.ProductVersionId)); if (sameNameProductVersion.Count() != 0) { throw new BusinessException(ExceptionCode.DuplicateProductNames); } var oldProductVersion = _productVersionRepository.GetByID(combined.ProductVersionId); if (oldProductVersion != null) { var oldProduct = _productRepository.GetByID(oldProductVersion.ProductId); oldProduct.IsInStore = combined.IsInStore; oldProduct.Quantity = combined.Quantity; oldProduct.IsOrderable = combined.IsOrderable; _productRepository.Update(oldProduct); if (IsNewProductVersionNeeded(combined, oldProductVersion)) { oldProductVersion.SoftDelete = true; ProductVersion newProductVersion = new ProductVersion(); newProductVersion.Description = combined.Description; newProductVersion.Cost = combined.Cost; newProductVersion.Created = DateTime.Now; newProductVersion.Name = combined.Name; newProductVersion.ProductId = oldProduct.Id; newProductVersion.UrlImg = combined.UrlImg; newProductVersion.SoftDelete = false; _productVersionRepository.Update(oldProductVersion); _productVersionRepository.Create(newProductVersion); } } }
public void UpdateVersion_WhenProductChangesAndNewVersionIsNeeded_CallsUpdateAndCreateProductVersion(FullProductDto productVersion) { //Assign //Act _productVersionLogic.UpdateVersion(productVersion); //Assert _productVersionRepository.Verify(x => x.Update(It.IsAny <ProductVersion>()), Times.Once); _productVersionRepository.Verify(x => x.Create(It.IsAny <ProductVersion>()), Times.Once); }
public ActionResult Update([FromBody] FullProductDto fullproductDto) { _productLogic.UpdateVersion(fullproductDto); return(Ok()); }