Fix Name property in search models (because it's unique)
This commit is contained in:
parent
ce896bda7e
commit
c48da815bb
114
ComputerShopBusinessLogic/BusinessLogics/ComponentLogic.cs
Normal file
114
ComputerShopBusinessLogic/BusinessLogics/ComponentLogic.cs
Normal file
@ -0,0 +1,114 @@
|
|||||||
|
using ComputerShopContracts.BindingModels;
|
||||||
|
using ComputerShopContracts.BusinessLogicContracts;
|
||||||
|
using ComputerShopContracts.SearchModels;
|
||||||
|
using ComputerShopContracts.StorageContracts;
|
||||||
|
using ComputerShopContracts.ViewModels;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
namespace ComputerShopBusinessLogic.BusinessLogics
|
||||||
|
{
|
||||||
|
public class ComponentLogic : IComponentLogic
|
||||||
|
{
|
||||||
|
private readonly ILogger _logger;
|
||||||
|
private readonly IComponentStorage _componentStorage;
|
||||||
|
|
||||||
|
public ComponentLogic(ILogger Logger, IComponentStorage ComponentStorage)
|
||||||
|
{
|
||||||
|
_logger = Logger;
|
||||||
|
_componentStorage = ComponentStorage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<ComponentViewModel>? ReadList(ComponentSearchModel? Model)
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ComponentViewModel? ReadElement(ComponentSearchModel Model)
|
||||||
|
{
|
||||||
|
if (Model == null)
|
||||||
|
throw new ArgumentNullException(nameof(Model));
|
||||||
|
|
||||||
|
var Element = _componentStorage.GetElement(Model);
|
||||||
|
if (Element == null)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("ReadElement component not found");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
_logger.LogInformation("ReadElement component found. Id: {Id}", Element.Id);
|
||||||
|
return Element;
|
||||||
|
}
|
||||||
|
|
||||||
|
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) is null)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("Delete operation failed");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void CheckModel(ComponentBindingModel Model, bool WithParams = true)
|
||||||
|
{
|
||||||
|
if (Model == null)
|
||||||
|
throw new ArgumentNullException(nameof(Model));
|
||||||
|
|
||||||
|
if (!WithParams)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (string.IsNullOrEmpty(Model.ComponentName))
|
||||||
|
throw new ArgumentException($"У комплектующей отсутствует название");
|
||||||
|
|
||||||
|
if (Model.Cost <= 0)
|
||||||
|
throw new ArgumentException("Цена комплектующей должна быть больше 0", nameof(Model.Cost));
|
||||||
|
|
||||||
|
var Element = _componentStorage.GetElement(new ComponentSearchModel
|
||||||
|
{
|
||||||
|
ComponentName = Model.ComponentName
|
||||||
|
});
|
||||||
|
|
||||||
|
if (Element != null && Element.Id != Model.Id)
|
||||||
|
throw new InvalidOperationException("Комплектующая с таким названием уже есть");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -6,6 +6,8 @@
|
|||||||
|
|
||||||
public int? UserId { get; set; }
|
public int? UserId { get; set; }
|
||||||
|
|
||||||
|
public string? AssemblyName { get; set; }
|
||||||
|
|
||||||
public string? Category { get; set; }
|
public string? Category { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,5 +5,7 @@
|
|||||||
public int? Id { get; set; }
|
public int? Id { get; set; }
|
||||||
|
|
||||||
public int? UserId { get; set; }
|
public int? UserId { get; set; }
|
||||||
|
|
||||||
|
public string? ComponentName { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,5 +7,7 @@
|
|||||||
public int? UserId { get; set; }
|
public int? UserId { get; set; }
|
||||||
|
|
||||||
public int? ShipmentId { get; set; }
|
public int? ShipmentId { get; set; }
|
||||||
|
|
||||||
|
public string? ProductName { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -47,6 +47,16 @@ namespace ComputerShopDatabaseImplement.Implements
|
|||||||
{
|
{
|
||||||
using var Context = new ComputerShopDatabase();
|
using var Context = new ComputerShopDatabase();
|
||||||
|
|
||||||
|
// AssemblyName is unique
|
||||||
|
if (!string.IsNullOrEmpty(Model.AssemblyName))
|
||||||
|
{
|
||||||
|
return Context.Assemblies
|
||||||
|
.Include(x => x.Components)
|
||||||
|
.ThenInclude(x => x.Assembly)
|
||||||
|
.FirstOrDefault(x => x.AssemblyName == Model.AssemblyName)?
|
||||||
|
.ViewModel;
|
||||||
|
}
|
||||||
|
|
||||||
return Context.Assemblies
|
return Context.Assemblies
|
||||||
.Include(x => x.Components)
|
.Include(x => x.Components)
|
||||||
.ThenInclude(x => x.Assembly)
|
.ThenInclude(x => x.Assembly)
|
||||||
|
@ -31,6 +31,14 @@ namespace ComputerShopDatabaseImplement.Implements
|
|||||||
{
|
{
|
||||||
using var Context = new ComputerShopDatabase();
|
using var Context = new ComputerShopDatabase();
|
||||||
|
|
||||||
|
// ComponentName is unique
|
||||||
|
if (!string.IsNullOrEmpty(Model.ComponentName))
|
||||||
|
{
|
||||||
|
return Context.Components
|
||||||
|
.FirstOrDefault(x => x.ComponentName == Model.ComponentName)?
|
||||||
|
.ViewModel;
|
||||||
|
}
|
||||||
|
|
||||||
return Context.Components
|
return Context.Components
|
||||||
.FirstOrDefault(x => x.Id == Model.Id)?
|
.FirstOrDefault(x => x.Id == Model.Id)?
|
||||||
.ViewModel;
|
.ViewModel;
|
||||||
|
@ -50,6 +50,17 @@ namespace ComputerShopDatabaseImplement.Implements
|
|||||||
{
|
{
|
||||||
using var Context = new ComputerShopDatabase();
|
using var Context = new ComputerShopDatabase();
|
||||||
|
|
||||||
|
// ProductName is unique
|
||||||
|
if (!string.IsNullOrEmpty(Model.ProductName))
|
||||||
|
{
|
||||||
|
return Context.Products
|
||||||
|
.Include(x => x.Shipment)
|
||||||
|
.Include(x => x.Components)
|
||||||
|
.ThenInclude(x => x.Product)
|
||||||
|
.FirstOrDefault(x => x.ProductName == Model.ProductName)?
|
||||||
|
.ViewModel;
|
||||||
|
}
|
||||||
|
|
||||||
return Context.Products
|
return Context.Products
|
||||||
.Include(x => x.Shipment)
|
.Include(x => x.Shipment)
|
||||||
.Include(x => x.Components)
|
.Include(x => x.Components)
|
||||||
|
Loading…
Reference in New Issue
Block a user