Guarantor: DataModels fixes and BusinessLogic.

This commit is contained in:
Yuee Shiness 2023-04-05 21:23:13 +04:00
parent e7ec00c14e
commit 7d2196d4e8
11 changed files with 428 additions and 5 deletions

View File

@ -0,0 +1,116 @@
using ComputerStoreContracts.BindingModels;
using ComputerStoreContracts.BusinessLogicContracts;
using ComputerStoreContracts.SearchModels;
using ComputerStoreContracts.StorageContracts;
using ComputerStoreContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputerStoreBusinessLogic.BusinessLogic
{
public class ComponentLogic : IComponentLogic
{
private readonly ILogger _logger;
private readonly IComponentStorage _componentStorage;
public ComponentLogic(ILogger<ComponentLogic> logger, IComponentStorage componentStorage)
{
_logger = logger;
_componentStorage = componentStorage;
}
public bool Create(ComponentBindingModel model)
{
CheckModel(model);
if(_componentStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(ComponentBindingModel model)
{
CheckModel(model);
if (_componentStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(ComponentBindingModel model)
{
CheckModel(model,false);
_logger.LogInformation("Delete. ID:{ID}", model.ID);
if (_componentStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public ComponentViewModel? ReadElement(ComponentSearchModel model)
{
if(model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. ComponentName:{ComponentName}.ID:{ ID}", model.Name, model.ID);
var element = _componentStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. ID:{ID}", element.ID);
return element;
}
public List<ComponentViewModel>? ReadList(ComponentSearchModel? model)
{
_logger.LogInformation("ReadList. ComponentName:{ComponentName}. ID:{ ID}", model?.Name, model?.ID);
var list = model == null ? _componentStorage.GetFullList() : _componentStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
private void CheckModel(ComponentBindingModel model, bool withParams = true)
{
if (model == null) { return; }
if (!withParams) { return; }
if(string.IsNullOrEmpty(model.Name)) { throw new ArgumentNullException("Invalid component's name", nameof(model)); }
if(model.Price <= 0) { throw new ArgumentNullException("Invalid component's price", nameof(model)); }
_logger.LogInformation("Component. ComponentName:{ComponentName}. Cost:{ Cost}. ID: { ID} ", model.Name, model.Price, model.ID);
var element = _componentStorage.GetElement(new ComponentSearchModel
{
Name = model.Name
});
if (element != null && element.ID != model.ID)
{
throw new InvalidOperationException("Component with such name already exists.");
}
}
}
}

View File

@ -0,0 +1,115 @@
using ComputerStoreContracts.BindingModels;
using ComputerStoreContracts.BusinessLogicContracts;
using ComputerStoreContracts.SearchModels;
using ComputerStoreContracts.StorageContracts;
using ComputerStoreContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputerStoreBusinessLogic.BusinessLogic
{
public class PCLogic : IPCLogic
{
private readonly ILogger _logger;
private readonly IPCStorage _PCStorage;
public PCLogic(ILogger<PCLogic> logger, IPCStorage PCStorage)
{
_logger = logger;
_PCStorage = PCStorage;
}
public bool Create(PCBindingModel model)
{
CheckModel(model);
if (_PCStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(PCBindingModel model)
{
CheckModel(model);
if (_PCStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(PCBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. ID:{ID}", model.ID);
if (_PCStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public PCViewModel? ReadElement(PCSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. PCName:{PCName}.ID:{ ID}", model.Name, model.ID);
var element = _PCStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. ID:{ID}", element.ID);
return element;
}
public List<PCViewModel>? ReadList(PCSearchModel? model)
{
_logger.LogInformation("ReadList. PCName:{PCName}. ID:{ ID}", model?.Name, model?.ID);
var list = model == null ? _PCStorage.GetFullList() : _PCStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
private void CheckModel(PCBindingModel model, bool withParams = true)
{
if (model == null) { return; }
if (!withParams) { return; }
if (string.IsNullOrEmpty(model.Name)) { throw new ArgumentNullException("Invalid PC's name", nameof(model)); }
if (model.Price <= 0) { throw new ArgumentNullException("Invalid PC's price", nameof(model)); }
_logger.LogInformation("PC. PCName:{PCName}. Cost:{ Cost}. ID: { ID} ", model.Name, model.Price, model.ID);
var element = _PCStorage.GetElement(new PCSearchModel
{
Name = model.Name
});
if (element != null && element.ID != model.ID)
{
throw new InvalidOperationException("PC with such name already exists.");
}
}
}
}

View File

@ -0,0 +1,115 @@
using ComputerStoreContracts.BindingModels;
using ComputerStoreContracts.BusinessLogicContracts;
using ComputerStoreContracts.SearchModels;
using ComputerStoreContracts.StorageContracts;
using ComputerStoreContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputerStoreBusinessLogic.BusinessLogic
{
public class ProductLogic : IProductLogic
{
private readonly ILogger _logger;
private readonly IProductStorage _productStorage;
public ProductLogic(ILogger<ProductLogic> logger, IProductStorage productStorage)
{
_logger = logger;
_productStorage = productStorage;
}
public bool Create(ProductBindingModel model)
{
CheckModel(model);
if (_productStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(ProductBindingModel model)
{
CheckModel(model);
if (_productStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(ProductBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. ID:{ID}", model.ID);
if (_productStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public ProductViewModel? ReadElement(ProductSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. ProductName:{ProductName}.ID:{ ID}", model.Name, model.ID);
var element = _productStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. ID:{ID}", element.ID);
return element;
}
public List<ProductViewModel>? ReadList(ProductSearchModel? model)
{
_logger.LogInformation("ReadList. ProductName:{ProductName}. ID:{ ID}", model?.Name, model?.ID);
var list = model == null ? _productStorage.GetFullList() : _productStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
private void CheckModel(ProductBindingModel model, bool withParams = true)
{
if (model == null) { return; }
if (!withParams) { return; }
if (string.IsNullOrEmpty(model.Name)) { throw new ArgumentNullException("Invalid product's name", nameof(model)); }
if (model.Price <= 0) { throw new ArgumentNullException("Invalid product's price", nameof(model)); }
_logger.LogInformation("Product. product:{product}. Cost:{ Cost}. ID: { ID} ", model.Name, model.Price, model.ID);
var element = _productStorage.GetElement(new ProductSearchModel
{
Name = model.Name
});
if (element != null && element.ID != model.ID)
{
throw new InvalidOperationException("Product with such name already exists.");
}
}
}
}

View File

@ -6,4 +6,13 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ComputerStoreContracts\ComputerStoreContracts.csproj" />
<ProjectReference Include="..\ComputerStoreDataModels\ComputerStoreDataModels.csproj" />
</ItemGroup>
</Project>

View File

@ -14,8 +14,10 @@ namespace ComputerStoreContracts.BindingModels
public double Price { get; set; }
public int RequestID { get; set; }
public Dictionary<int, (IComponentModel, int)> PCComponents { get; set; } = new();
}
}

View File

@ -0,0 +1,22 @@
using ComputerStoreContracts.BindingModels;
using ComputerStoreContracts.SearchModels;
using ComputerStoreContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputerStoreContracts.BusinessLogicContracts
{
public interface IComponentLogic
{
List<ComponentViewModel>? ReadList(ComponentSearchModel? model);
ComponentViewModel? ReadElement(ComponentSearchModel model);
bool Create(ComponentBindingModel model);
bool Update(ComponentBindingModel model);
bool Delete(ComponentBindingModel model);
}
}

View File

@ -0,0 +1,22 @@
using ComputerStoreContracts.BindingModels;
using ComputerStoreContracts.SearchModels;
using ComputerStoreContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputerStoreContracts.BusinessLogicContracts
{
public interface IPCLogic
{
List<PCViewModel>? ReadList(PCSearchModel? model);
PCViewModel? ReadElement(PCSearchModel model);
bool Create(PCBindingModel model);
bool Update(PCBindingModel model);
bool Delete(PCBindingModel model);
}
}

View File

@ -0,0 +1,22 @@
using ComputerStoreContracts.BindingModels;
using ComputerStoreContracts.SearchModels;
using ComputerStoreContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputerStoreContracts.BusinessLogicContracts
{
public interface IProductLogic
{
List<ProductViewModel>? ReadList(ProductSearchModel? model);
ProductViewModel? ReadElement(ProductSearchModel model);
bool Create(ProductBindingModel model);
bool Update(ProductBindingModel model);
bool Delete(ProductBindingModel model);
}
}

View File

@ -6,10 +6,6 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Folder Include="BusinessLogicContracts\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ComputerStoreDataModels\ComputerStoreDataModels.csproj" />
</ItemGroup>

View File

@ -18,6 +18,9 @@ namespace ComputerStoreContracts.ViewModels
[DisplayName("PC's price")]
public double Price { get; set; }
[DisplayName("Request ID")]
public int RequestID { get; set; }
public Dictionary<int, (IComponentModel, int)> PCComponents { get; set; } = new();

View File

@ -10,6 +10,7 @@ namespace ComputerStoreDataModels.Models
{
string Name { get; }
double Price { get; }
public int RequestID { get; }
Dictionary<int,(IComponentModel,int)> PCComponents { get; }
}
}