Skip to content

Commit

Permalink
Refactoring of AddOrUpdateProduct method and its reference.
Browse files Browse the repository at this point in the history
  • Loading branch information
clodoalves committed Jul 25, 2022
1 parent 4a30865 commit 707d3f1
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -117,7 +118,9 @@ private void ExecuteUpdateOrAddProduct()
try
{
Product product = BindToProduct();
_productService.AddOrUpdateProduct(product, Images);
AddImagesToProduct(product, Images);

_productService.AddOrUpdateProduct(product);

RegionManager.RequestNavigate("MainRegion", "ProductsWindow");
}
Expand Down Expand Up @@ -153,6 +156,14 @@ private void BindToViewModel(Product product)
Quantity = product.Quantity;
}

private void AddImagesToProduct(Product product, IList<FileStream> filesWindow)
{
product.ProductImages = filesWindow.Select(f => new ProductImage()
{
Path = Path.GetFileName(f.Name)

}).ToList();
}
#endregion

#region Navigation
Expand Down
2 changes: 1 addition & 1 deletion WPFSample/WPFSample.Service/Contract/IProductService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace WPFSample.Service.Contract
{
public interface IProductService
{
void AddOrUpdateProduct(Product product, IList<FileStream> filesWindow);
void AddOrUpdateProduct(Product product);

IList<Product> GetAllProducts();

Expand Down
27 changes: 9 additions & 18 deletions WPFSample/WPFSample.Service/Implementation/ProductService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,12 @@ public ProductService(IProductRepository productRepository, IProductImageReposit
_productImageRepository = productImageRepository;
}

public void AddOrUpdateProduct(Product product, IList<FileStream> filesWindow)
public void AddOrUpdateProduct(Product product)
{
ValidateRequiredFields(product);
ValidateNumericFields(product);
ValidateLimitCharacters(product);

AddImagesToProduct(product, filesWindow);

if (product.Id != 0)
{
Product databaseRegister = GetProductById(product.Id);
Expand All @@ -48,7 +46,7 @@ public void AddOrUpdateProduct(Product product, IList<FileStream> filesWindow)
_productRepository.Add(product);
}

SaveImages(product, filesWindow);
SaveImages(product);
}

private void ValidateRequiredFields(Product product)
Expand Down Expand Up @@ -112,31 +110,24 @@ private void ValidateLimitCharacters(Product product)
throw new FieldExceedCaracterLimitException(sb.ToString());
}

private void AddImagesToProduct(Product product, IList<FileStream> filesWindow)
{
product.ProductImages = filesWindow.Select(f => new ProductImage()
{
Path = Path.GetFileName(f.Name)

}).ToList();
}

private void SaveImages(Product product, IList<FileStream> filesWindow)
private void SaveImages(Product product)
{
string rootPath = AppDomain.CurrentDomain.BaseDirectory;

string pathImagesProduct = $"{rootPath}/{product.Id}";

Directory.CreateDirectory(pathImagesProduct);

foreach (var item in filesWindow)
foreach (var productImage in product.ProductImages)
{
string fileName = Path.GetFileName(item.Name);
FileStream originalFile = new FileStream(productImage.Path, FileMode.Open, FileAccess.Read);

string fileName = Path.GetFileName(productImage.Path);

using (FileStream fs = new FileStream($"{pathImagesProduct}/{fileName}", FileMode.Create))
{
item.Seek(0, SeekOrigin.Begin);
item.CopyTo(fs);
originalFile.Seek(0, SeekOrigin.Begin);
originalFile.CopyTo(fs);
}
}
}
Expand Down
27 changes: 14 additions & 13 deletions WPFSample/WPFSample.Test/Service/ProductServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,70 +33,71 @@ public void ConfigureDependecies()
_mockProduct.Description = "This is such a nice smartphone";
_mockProduct.Price = 1000;
_mockProduct.Quantity = 15;
_mockProduct.ProductImages = new List<ProductImage>();
}

[Test]
public void AddProductWithoutTitleTest()
{
_mockProduct.Title = string.Empty;

Assert.Throws(typeof(RequiredFieldException), () => _productService.AddOrUpdateProduct(_mockProduct, _files));
Assert.Throws(typeof(RequiredFieldException), () => _productService.AddOrUpdateProduct(_mockProduct));
}

[Test]
public void AddProductWithoutPriceTest()
{
_mockProduct.Price = 0;

Assert.Throws(typeof(RequiredFieldException), () => _productService.AddOrUpdateProduct(_mockProduct, _files));
Assert.Throws(typeof(RequiredFieldException), () => _productService.AddOrUpdateProduct(_mockProduct));
}

[Test]
public void AddProductWithPriceLessThanZeroTest()
{
_mockProduct.Price = -2;

Assert.Throws(typeof(NumericFieldLessThanZeroException), () => _productService.AddOrUpdateProduct(_mockProduct, _files));
Assert.Throws(typeof(NumericFieldLessThanZeroException), () => _productService.AddOrUpdateProduct(_mockProduct));
}

[Test]
public void AddProductWithQuantityLessThanZeroTest()
{
_mockProduct.Quantity = -5;

Assert.Throws(typeof(NumericFieldLessThanZeroException), () => _productService.AddOrUpdateProduct(_mockProduct, _files));
Assert.Throws(typeof(NumericFieldLessThanZeroException), () => _productService.AddOrUpdateProduct(_mockProduct));
}

[Test]
public void AddProductWithTitleThatExceedsLimitCharactersTest()
{
_mockProduct.Title = "Product with loooooooooooooooooooooooooooooooooooooooooooooooooog title";

Assert.Throws(typeof(FieldExceedCaracterLimitException), () => _productService.AddOrUpdateProduct(_mockProduct, _files));
Assert.Throws(typeof(FieldExceedCaracterLimitException), () => _productService.AddOrUpdateProduct(_mockProduct));
}

[Test]
public void AddProductWithDescriptionThatExceedsLimitCharactersTest()
{
_mockProduct.Description = "Product with loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong description";

Assert.Throws(typeof(FieldExceedCaracterLimitException), () => _productService.AddOrUpdateProduct(_mockProduct, _files));
Assert.Throws(typeof(FieldExceedCaracterLimitException), () => _productService.AddOrUpdateProduct(_mockProduct));
}

[Test]
public void AddProductWithPriceThatExceedsLimitNumbersTest()
{
_mockProduct.Price = 100000000000;

Assert.Throws(typeof(FieldExceedCaracterLimitException), () => _productService.AddOrUpdateProduct(_mockProduct, _files));
Assert.Throws(typeof(FieldExceedCaracterLimitException), () => _productService.AddOrUpdateProduct(_mockProduct));
}

[Test]
public void AddProductWithQuantityThatExceedsLimitNumbersTest()
{
_mockProduct.Quantity = 100000000;

Assert.Throws(typeof(FieldExceedCaracterLimitException), () => _productService.AddOrUpdateProduct(_mockProduct, _files));
Assert.Throws(typeof(FieldExceedCaracterLimitException), () => _productService.AddOrUpdateProduct(_mockProduct));
}

[Test]
Expand All @@ -118,7 +119,7 @@ public void SaveNewProductWithoutImagesTest()
_mockProductRepository.Setup(s => s.GetById(It.IsAny<int>()))
.Returns(newProduct);

_productService.AddOrUpdateProduct(_mockProduct, _files);
_productService.AddOrUpdateProduct(_mockProduct);

Product savedProduct = _productService.GetProductById(newProductId);

Expand All @@ -136,7 +137,7 @@ public void UpdateTitleProductTest()
Product originalProduct = _productService.GetProductById(_mockProduct.Id);
string newTitle = "New title";
originalProduct.Title = newTitle;
_productService.AddOrUpdateProduct(originalProduct, _files);
_productService.AddOrUpdateProduct(originalProduct);

Product updatedProduct = _productService.GetProductById(productId);

Expand All @@ -153,7 +154,7 @@ public void UpdateDescriptionProductTest()
Product originalProduct = _productService.GetProductById(productId);
string newDescription = "This is a pretty nice product";
originalProduct.Description = newDescription;
_productService.AddOrUpdateProduct(originalProduct, _files);
_productService.AddOrUpdateProduct(originalProduct);

Product updatedProduct = _productService.GetProductById(productId);

Expand All @@ -170,7 +171,7 @@ public void UpdatePriceProductTest()
Product originalProduct = _productService.GetProductById(productId);
int newPrice = 3500;
originalProduct.Price = newPrice;
_productService.AddOrUpdateProduct(originalProduct, _files);
_productService.AddOrUpdateProduct(originalProduct);

Product updatedProduct = _productService.GetProductById(productId);

Expand All @@ -188,7 +189,7 @@ public void UpdateQuantityProductTest()
Product originalProduct = _productService.GetProductById(productId);
int newQuantity = 300;
originalProduct.Quantity = newQuantity;
_productService.AddOrUpdateProduct(originalProduct, _files);
_productService.AddOrUpdateProduct(originalProduct);

Product updatedProduct = _productService.GetProductById(productId);

Expand Down

0 comments on commit 707d3f1

Please sign in to comment.