Start implementing BusinessLogic

This commit is contained in:
ShabOl 2024-05-01 13:56:07 +04:00
parent c48da815bb
commit 5f9b1f4a2f
12 changed files with 252 additions and 18 deletions

View File

@ -0,0 +1,117 @@
using ComputerShopContracts.BindingModels;
using ComputerShopContracts.BusinessLogicContracts;
using ComputerShopContracts.SearchModels;
using ComputerShopContracts.StorageContracts;
using ComputerShopContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace ComputerShopBusinessLogic.BusinessLogics
{
public class AssemblyLogic : IAssemblyLogic
{
private readonly ILogger _logger;
private readonly IAssemblyStorage _assemblyStorage;
public AssemblyLogic(ILogger<AssemblyLogic> Logger, IAssemblyStorage AssemblyStorage)
{
_logger = Logger;
_assemblyStorage = AssemblyStorage;
}
public List<AssemblyViewModel>? ReadList(AssemblySearchModel? Model)
{
var List = (Model == null) ? _assemblyStorage.GetFullList() : _assemblyStorage.GetFilteredList(Model);
if (List == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count: {Count}", List.Count);
return List;
}
public AssemblyViewModel? ReadElement(AssemblySearchModel Model)
{
if (Model == null)
throw new ArgumentNullException(nameof(Model));
var Element = _assemblyStorage.GetElement(Model);
if (Element == null)
{
_logger.LogWarning("ReadElement Assembly not found");
return null;
}
_logger.LogInformation("ReadElement Assembly found. Id: {Id}", Element.Id);
return Element;
}
public bool Create(AssemblyBindingModel Model)
{
CheckModel(Model);
if (_assemblyStorage.Insert(Model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(AssemblyBindingModel Model)
{
CheckModel(Model);
if (_assemblyStorage.Update(Model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(AssemblyBindingModel Model)
{
CheckModel(Model, false);
_logger.LogInformation("Delete. Id:{Id}", Model.Id);
if (_assemblyStorage.Delete(Model) is null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(AssemblyBindingModel Model, bool WithParams = true)
{
if (Model == null)
throw new ArgumentNullException(nameof(Model));
if (!WithParams)
return;
if (string.IsNullOrEmpty(Model.AssemblyName))
throw new ArgumentException($"У сборки отсутствует название");
if (string.IsNullOrEmpty(Model.Category))
throw new ArgumentException($"У сборки отсутствует категория");
if (Model.Price <= 0)
throw new ArgumentException("Цена сборки должна быть больше 0", nameof(Model.Price));
var Element = _assemblyStorage.GetElement(new AssemblySearchModel
{
AssemblyName = Model.AssemblyName
});
if (Element != null && Element.Id != Model.Id)
throw new InvalidOperationException("Товар с таким названием уже есть");
}
}
}

View File

@ -12,7 +12,7 @@ namespace ComputerShopBusinessLogic.BusinessLogics
private readonly ILogger _logger;
private readonly IComponentStorage _componentStorage;
public ComponentLogic(ILogger Logger, IComponentStorage ComponentStorage)
public ComponentLogic(ILogger<ComponentLogic> Logger, IComponentStorage ComponentStorage)
{
_logger = Logger;
_componentStorage = ComponentStorage;

View File

@ -0,0 +1,117 @@
using ComputerShopContracts.BindingModels;
using ComputerShopContracts.BusinessLogicContracts;
using ComputerShopContracts.SearchModels;
using ComputerShopContracts.StorageContracts;
using ComputerShopContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace ComputerShopBusinessLogic.BusinessLogics
{
public class ProductLogic : IProductLogic
{
private readonly ILogger _logger;
private readonly IProductStorage _productStorage;
public ProductLogic(ILogger<ProductLogic> Logger, IProductStorage ProductStorage)
{
_logger = Logger;
_productStorage = ProductStorage;
}
public List<ProductViewModel>? ReadList(ProductSearchModel? Model)
{
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;
}
public ProductViewModel? ReadElement(ProductSearchModel Model)
{
if (Model == null)
throw new ArgumentNullException(nameof(Model));
var Element = _productStorage.GetElement(Model);
if (Element == null)
{
_logger.LogWarning("ReadElement Product not found");
return null;
}
_logger.LogInformation("ReadElement Product found. Id: {Id}", Element.Id);
return Element;
}
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) is null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(ProductBindingModel Model, bool WithParams = true)
{
if (Model == null)
throw new ArgumentNullException(nameof(Model));
if (!WithParams)
return;
if (string.IsNullOrEmpty(Model.ProductName))
throw new ArgumentException($"У товара отсутствует название");
if (Model.Price <= 0)
throw new ArgumentException("Цена товара должна быть больше 0", nameof(Model.Price));
if (Model.Warranty <= 0)
throw new ArgumentException("Гарантия на товар должна быть больше 0", nameof(Model.Warranty));
var Element = _productStorage.GetElement(new ProductSearchModel
{
ProductName = Model.ProductName
});
if (Element != null && Element.Id != Model.Id)
throw new InvalidOperationException("Товар с таким названием уже есть");
}
}
}

View File

@ -10,7 +10,7 @@ namespace ComputerShopContracts.BindingModels
public string AssemblyName { get; set; } = string.Empty;
public double Cost { get; set; }
public double Price { get; set; }
public string Category { get; set; } = string.Empty;

View File

@ -12,7 +12,7 @@ namespace ComputerShopContracts.BindingModels
public string ProductName { get; set; } = string.Empty;
public double Cost { get; set; }
public double Price { get; set; }
public int Warranty { get; set; }

View File

@ -13,7 +13,7 @@ namespace ComputerShopContracts.ViewModels
public string AssemblyName { get; set; } = string.Empty;
[DisplayName("Стоимость")]
public double Cost { get; set; }
public double Price { get; set; }
[DisplayName("Категория")]
public string Category { get; set; } = string.Empty;

View File

@ -18,7 +18,7 @@ namespace ComputerShopContracts.ViewModels
public string ProductName { get; set; } = string.Empty;
[DisplayName("Стоимость")]
public double Cost { get; set; }
public double Price { get; set; }
[DisplayName("Гарантия (мес.)")]
public int Warranty { get; set; }

View File

@ -18,7 +18,7 @@
/// <summary>
/// Стоимость
/// </summary>
double Cost { get; }
double Price { get; }
/// <summary>
/// Категория

View File

@ -18,7 +18,7 @@
/// <summary>
/// Стоимость товара
/// </summary>
double Cost { get; }
double Price { get; }
/// <summary>
/// Гарантия

View File

@ -17,7 +17,7 @@ namespace ComputerShopDatabaseImplement.Models
public string AssemblyName { get; private set; } = string.Empty;
[Required]
public double Cost { get; private set; }
public double Price { get; private set; }
[Required]
public string Category { get; private set; } = string.Empty;
@ -54,7 +54,7 @@ namespace ComputerShopDatabaseImplement.Models
Id = Model.Id,
UserId = Model.UserId,
AssemblyName = Model.AssemblyName,
Cost = Model.Cost,
Price = Model.Price,
Category = Model.Category,
Components = Model.AssemblyComponents.Select(x => new AssemblyComponent
{
@ -76,7 +76,7 @@ namespace ComputerShopDatabaseImplement.Models
Category = Model.Category;
}
Cost = Model.Cost;
Price = Model.Price;
}
public AssemblyViewModel ViewModel => new()
@ -84,7 +84,7 @@ namespace ComputerShopDatabaseImplement.Models
Id = Id,
UserId = UserId,
AssemblyName = AssemblyName,
Cost = Cost,
Price = Price,
Category = Category,
AssemblyComponents = AssemblyComponents,
};

View File

@ -21,7 +21,7 @@ namespace ComputerShopDatabaseImplement.Models
public string ProductName { get; set; } = string.Empty;
[Required]
public double Cost { get; set; }
public double Price { get; set; }
[Required]
public int Warranty { get; set; }
@ -56,7 +56,7 @@ namespace ComputerShopDatabaseImplement.Models
UserId = Model.UserId,
ShipmentId = Model.ShipmentId,
ProductName = Model.ProductName,
Cost = Model.Cost,
Price = Model.Price,
Warranty = Model.Warranty,
Components = Model.ProductComponents.Select(x => new ProductComponent
{
@ -74,7 +74,7 @@ namespace ComputerShopDatabaseImplement.Models
}
ShipmentId = Model.ShipmentId;
Cost = Model.Cost;
Price = Model.Price;
Warranty = Model.Warranty;
}
@ -84,7 +84,7 @@ namespace ComputerShopDatabaseImplement.Models
UserId = UserId,
ShipmentId = ShipmentId,
ProviderName = Shipment?.ProviderName,
Cost = Cost,
Price = Price,
Warranty = Warranty,
ProductComponents = ProductComponents,
};

View File

@ -95,7 +95,7 @@ namespace ComputerShopDatabaseImplement.Models
{
var currentRequest = context.Requests.First(x => x.Id == Id);
//стоимость сборки, связанной с заявкой (или 0, если заявка не связана со сборкой)
double cost_of_assembly = (currentRequest.Assembly.Cost != null) ? currentRequest.Assembly.Cost : 0;
double cost_of_assembly = (currentRequest.Assembly.Price != null) ? currentRequest.Assembly.Price : 0;
var requestOrders = context.RequestOrders.Where(x => x.RequestId == model.Id).ToList();
@ -134,7 +134,7 @@ namespace ComputerShopDatabaseImplement.Models
public void ConnectAssembly(ComputerShopDatabase context, RequestBindingModel model)
{
//стоимость старой сборки (или 0, если её не было)
double cost_of_old_assembly = (Assembly.Cost != null) ? Assembly.Cost : 0;
double cost_of_old_assembly = (Assembly.Price != null) ? Assembly.Price : 0;
AssemblyId = model.AssemblyId;
Assembly = context.Assemblies.First(x => x.Id == model.AssemblyId);
@ -145,7 +145,7 @@ namespace ComputerShopDatabaseImplement.Models
//вычитание из стоимости заказа старой сборки
connectedOrder.ChangeSum(-cost_of_old_assembly);
//прибавление стоимости новой сборки
connectedOrder.ChangeSum(Assembly.Cost);
connectedOrder.ChangeSum(Assembly.Price);
context.SaveChanges();
}
}