Skip to content

Commit

Permalink
Migrates supplier service into two seperate services - product servic…
Browse files Browse the repository at this point in the history
…e and product category service

#12 #13 #14 #16
  • Loading branch information
Nathan Parnell committed Oct 23, 2023
1 parent 96086ce commit 0c5f8b9
Show file tree
Hide file tree
Showing 11 changed files with 446 additions and 23 deletions.
12 changes: 12 additions & 0 deletions StockTrackerApp/Components/CustomerComponents/Home.razor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StockTrackerApp.Components.CustomerComponents
{
public partial class Home
{
}
}
Empty file.
15 changes: 8 additions & 7 deletions StockTrackerApp/Components/SupplierComponents/Home.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ public partial class Home
{
//Inject Services
[Inject]
private ISupplierService _supplierService { get; set; }
private IUserService _userService { get; set; }

[Inject]
private IUserService _userService { get; set; }
private IProductCategoryService _productCategoryService { get; set; }

[Inject]
private IProductService _productService { get; set; }

[Inject]
private IJSRuntime _jSRuntime { get; set; }
Expand All @@ -30,8 +33,6 @@ public partial class Home
private ISessionHistoryService _sessionHistoryService { get; set; }


private Popup popupRef = new();

//Define Variables
private List<Product> _products = new List<Product>();
private List<ProductCategory> _productCategories = new List<ProductCategory>();
Expand All @@ -50,11 +51,11 @@ protected override async Task OnInitializedAsync()
private async Task GetProductsInformation()
{
//Get all of the products which belong to the current user (the supplier)
_products = _supplierService.GetProductBySupplierId(_userService.CurrentUser.UserId);
_products = _productService.GetProductBySupplierId(_userService.CurrentUser.UserId);

//Get a list of the product category ids
List<string> productCategoryIds = _products.Select(cat => cat.ProductCategoryId.ToString()).Distinct().ToList();
_productCategories = _supplierService.GetProductCategoriesByProductCategoryIds(productCategoryIds);
_productCategories = _productCategoryService.GetProductCategoriesByProductCategoryIds(productCategoryIds);
}

private void RowSelected(Product product)
Expand All @@ -72,7 +73,7 @@ private async void DeleteProduct()
bool confirmed = await _jSRuntime.InvokeAsync<bool>("confirm", "Are you sure you would like to delete this Product?");
if (confirmed)
{
if (_supplierService.DeleteProductByProductID(_selectedProduct.ProductId) == true)
if (_productService.DeleteProductByProductID(_selectedProduct.ProductId) == true)
{
await _jSRuntime.InvokeAsync<object>("alert", "Product successfully deleted");
_navManager.NavigateTo("Home", true);
Expand Down
4 changes: 3 additions & 1 deletion StockTrackerApp/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ public static MauiApp CreateMauiApp()

builder.Services.AddSingleton<WeatherForecastService>();
builder.Services.AddSingleton<IUserService, UserService>();
builder.Services.AddSingleton<ISupplierService, SupplierService>();
builder.Services.AddSingleton<IProductCategoryService, ProductCategoryService>();
builder.Services.AddSingleton<IProductService, ProductService>();
//builder.Services.AddSingleton<ISupplierService, SupplierService>();
builder.Services.AddSingleton<IClientTransportService, NetmqClientTransportService>();
builder.Services.AddSingleton<ISessionHistoryService, SessionHistoryService>();

Expand Down
6 changes: 3 additions & 3 deletions StockTrackerApp/Pages/AddProductCategory.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public partial class AddProductCategory
{
//Inject Services
[Inject]
private ISupplierService _supplierService { get; set; }
private IProductCategoryService _productCategoryService { get; set; }

[Inject]
private IJSRuntime _jSRuntime { get; set; }
Expand Down Expand Up @@ -44,12 +44,12 @@ protected override async Task OnInitializedAsync()

private async Task GetProductsCategoryInformation()
{
_existingProductCategories = _supplierService.GetAllProductCategories();
_existingProductCategories = _productCategoryService.GetAllProductCategories();
}

private async Task AddCategory()
{
string prompt = _supplierService.ValidateAndAddProductCategory(_newProductCategory, _existingProductCategories);
string prompt = _productCategoryService.ValidateAndAddProductCategory(_newProductCategory, _existingProductCategories);

await _jSRuntime.InvokeAsync<object>("alert", prompt);

Expand Down
20 changes: 12 additions & 8 deletions StockTrackerApp/Pages/ManageProduct.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ namespace StockTrackerApp.Pages
public partial class ManageProduct
{
//Inject Services
[Inject]
private ISupplierService _supplierService { get; set; }

[Inject]
private IJSRuntime _jSRuntime { get; set; }

Expand All @@ -27,9 +24,16 @@ public partial class ManageProduct
[Inject]
private IUserService _userService { get; set; }

[Inject]
private IProductCategoryService _productCategoryService { get; set; }

[Inject]
private IProductService _productService { get; set; }

[Inject]
private ISessionHistoryService _sessionHistoryService { get; set; }


//Define Parameters
[Parameter]
public string ProductId { get; set; }
Expand Down Expand Up @@ -84,12 +88,12 @@ private async Task SetPageState(ManageProductPageState state = ManageProductPage

private async Task GetExistingProductsInformation()
{
_productCategories = _supplierService.GetAllProductCategories();
_existingProducts = _supplierService.GetAllProducts();
_productCategories = _productCategoryService.GetAllProductCategories();
_existingProducts = _productService.GetAllProducts();

if (_pageState == ManageProductPageState.ViewProductMode)
{
_product = _supplierService.GetProductByProductId(ProductId);
_product = _productService.GetProductByProductId(ProductId);
_measurementUnit = _product.ProductMeasurementUnit.ToString();
}

Expand Down Expand Up @@ -143,12 +147,12 @@ private async Task ProductAction()
//set the supplier id to be the id of the current user (who is a suppier in this case)
_product.SupplierId = _userService.CurrentUser.UserId;

prompt = _supplierService.ValidateAndAddProduct(_product, _existingProducts, _productCategories, ref actionSuccess);
prompt = _productService.ValidateAndAddProduct(_product, _existingProducts, _productCategories, ref actionSuccess);
}
//if the user is updating an existing product, then we attempt to validate and update the product
else if (_pageState == ManageProductPageState.EditProductMode)
{
prompt = _supplierService.ValidateAndUpdateProduct(_product, _existingProducts, _productCategories, ref actionSuccess);
prompt = _productService.ValidateAndUpdateProduct(_product, _existingProducts, _productCategories, ref actionSuccess);
}

await _jSRuntime.InvokeAsync<object>("alert", prompt);
Expand Down
25 changes: 25 additions & 0 deletions StockTrackerApp/Services/Infrastructure/IProductCategoryService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using StockTrackerCommon.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StockTrackerApp.Services.Infrastructure
{
public interface IProductCategoryService
{
#region "Get Methods"
List<ProductCategory> GetProductCategoriesByProductCategoryIds(List<string> productCategoryIds);
List<ProductCategory> GetAllProductCategories();
#endregion

#region "Add Methods"
bool AddProductCategory(ProductCategory productCategory);
#endregion

#region "Validation Methods"
string ValidateAndAddProductCategory(ProductCategory productCategory, List<ProductCategory> existingProductCategories);
#endregion
}
}
36 changes: 36 additions & 0 deletions StockTrackerApp/Services/Infrastructure/IProductService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using StockTrackerCommon.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StockTrackerApp.Services.Infrastructure
{
public interface IProductService
{
#region "Get Methods"
List<Product> GetProductBySupplierId(string supplierId);
Product GetProductByProductId(string productId);
List<Product> GetProductsByProductIds(List<string> productIds);
List<Product> GetAllProducts();
#endregion

#region "Add Methods"
bool AddProduct(Product product);
#endregion

#region "Update Methods"
bool UpdateProduct(Product updatedProduct);
#endregion

#region "Delete Methods"
bool DeleteProductByProductID(string productId);
#endregion

#region "Validation Methods"
string ValidateAndAddProduct(Product newProduct, List<Product> existingProducts, List<ProductCategory> productCategories, ref bool success);
string ValidateAndUpdateProduct(Product updatedProduct, List<Product> suppliersExistingProducts, List<ProductCategory> productCategories, ref bool success);
#endregion
}
}
92 changes: 92 additions & 0 deletions StockTrackerApp/Services/ProductCategoryService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using StockTrackerCommon.Helpers;
using StockTrackerCommon.Models;
using StockTrackerApp.Services.Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StockTrackerApp.Services
{
public class ProductCategoryService : IProductCategoryService
{
//setup logger
private static readonly log4net.ILog Logger = log4net.LogManager.GetLogger(typeof(ProductService));

//define services
private readonly IClientTransportService _clientTransportService;

public ProductCategoryService(IClientTransportService clientTransportService)
{
_clientTransportService = clientTransportService;
}

#region "Get Methods"
public List<ProductCategory> GetProductCategoriesByProductCategoryIds(List<string> productCategoryIds)
{
string jsonResponse = _clientTransportService.TcpHandler(RequestSerializingHelper.CreateGetProductCategoriesByProductCategoryIDsRequest(productCategoryIds));

//if the method we tried to call did not exist
if (String.IsNullOrEmpty(jsonResponse))
return null;

List<ProductCategory> productCategories = ResponseDeserializingHelper.DeserializeResponse<List<ProductCategory>>(jsonResponse).First().ToList();
return productCategories;
}

public List<ProductCategory> GetAllProductCategories()
{
string jsonResponse = _clientTransportService.TcpHandler(RequestSerializingHelper.CreateGetAllProductCategoriesRequest());

//if the method we tried to call did not exist
if (String.IsNullOrEmpty(jsonResponse))
return null;

List<ProductCategory> productCategories = ResponseDeserializingHelper.DeserializeResponse<List<ProductCategory>>(jsonResponse).First().ToList();
return productCategories;
}
#endregion

#region "Add Methods"
public bool AddProductCategory(ProductCategory productCategory)
{
string jsonResponse = _clientTransportService.TcpHandler(RequestSerializingHelper.CreateAddProductCategoryRequest(productCategory));

//if the method we tried to call did not exist
if (String.IsNullOrEmpty(jsonResponse))
return false;

bool addConfirmation = ResponseDeserializingHelper.DeserializeResponse<bool>(jsonResponse).First();
return addConfirmation;
}
#endregion

#region "Validation Methods"
public string ValidateAndAddProductCategory(ProductCategory productCategory, List<ProductCategory> existingProductCategories)
{
bool isProductCategoryValid = true;
string prompt = "Product Category invalid, please enter:\n";

if (String.IsNullOrEmpty(productCategory.ProductCategoryName) || existingProductCategories.Any(cat => cat.ProductCategoryName == productCategory.ProductCategoryName))
{
prompt += "A Unique Product Category Name";
isProductCategoryValid = false;
}

if (isProductCategoryValid == true)
{
productCategory.ProductCategoryId = Taikandi.SequentialGuid.NewGuid().ToString();
if (AddProductCategory(productCategory))
return "Product Category Added Successfully";
else
return "Error Adding Product Category";
}
else
{
return prompt;
}
}
#endregion
}
}
Loading

0 comments on commit 0c5f8b9

Please sign in to comment.