PIbd-21_Raspaev_N.I._FoodOr.../FoodOrders/FoodOrdersBusinessLogic/BusinessLogics/ComponentLogic.cs

109 lines
4.1 KiB
C#
Raw Normal View History

2023-02-07 13:00:02 +04:00
using FoodOrdersContracts.BindingModels;
using FoodOrdersContracts.BusinessLogicsContracts;
using FoodOrdersContracts.SearchModels;
using FoodOrdersContracts.StoragesContracts;
using FoodOrdersContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace FoodOrdersBusinessLogic.BusinessLogics
{
2023-02-12 10:11:34 +04:00
public class ComponentLogic : IComponentLogic
2023-02-07 13:00:02 +04:00
{
private readonly ILogger _logger;
2023-02-12 10:11:34 +04:00
private readonly IComponentStorage _componentStorage;
public ComponentLogic(ILogger<ComponentLogic> logger, IComponentStorage
ComponentStorage)
2023-02-07 13:00:02 +04:00
{
_logger = logger;
2023-02-12 10:11:34 +04:00
_componentStorage = ComponentStorage;
2023-02-07 13:00:02 +04:00
}
2023-02-12 10:11:34 +04:00
public List<ComponentViewModel>? ReadList(ComponentSearchModel? model)
2023-02-07 13:00:02 +04:00
{
2023-03-11 19:13:24 +04:00
_logger.LogInformation("ReadList. ComponentName:{ComponentName}. Id:{Id}", model?.ComponentName, model?.Id);
var list = model == null ? _componentStorage.GetFullList() : _componentStorage.GetFilteredList(model);
2023-02-07 13:00:02 +04:00
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
2023-02-12 10:11:34 +04:00
public ComponentViewModel? ReadElement(ComponentSearchModel model)
2023-02-07 13:00:02 +04:00
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
2023-02-12 10:11:34 +04:00
_logger.LogInformation("ReadElement. ComponentName:{ComponentName}. Id:{Id}", model.ComponentName, model.Id);
2023-03-11 19:13:24 +04:00
var element = _componentStorage.GetElement(model);
2023-02-07 13:00:02 +04:00
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
2023-02-12 10:11:34 +04:00
public bool Create(ComponentBindingModel model)
2023-02-07 13:00:02 +04:00
{
CheckModel(model);
2023-02-12 10:11:34 +04:00
if (_componentStorage.Insert(model) == null)
2023-02-07 13:00:02 +04:00
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
2023-02-12 10:11:34 +04:00
public bool Update(ComponentBindingModel model)
2023-02-07 13:00:02 +04:00
{
CheckModel(model);
2023-02-12 10:11:34 +04:00
if (_componentStorage.Update(model) == null)
2023-02-07 13:00:02 +04:00
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
2023-02-12 10:11:34 +04:00
public bool Delete(ComponentBindingModel model)
2023-02-07 13:00:02 +04:00
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
2023-02-12 10:11:34 +04:00
if (_componentStorage.Delete(model) == null)
2023-02-07 13:00:02 +04:00
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
2023-03-11 19:13:24 +04:00
private void CheckModel(ComponentBindingModel model, bool withParams = true)
2023-02-07 13:00:02 +04:00
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
2023-02-12 10:11:34 +04:00
if (string.IsNullOrEmpty(model.ComponentName))
2023-02-07 13:00:02 +04:00
{
2023-02-12 10:11:34 +04:00
throw new ArgumentNullException("Нет названия компонента",
nameof(model.ComponentName));
2023-02-07 13:00:02 +04:00
}
2023-02-12 10:11:34 +04:00
if (model.Cost <= 0)
2023-02-07 13:00:02 +04:00
{
2023-02-12 10:11:34 +04:00
throw new ArgumentNullException("Цена компонента должна быть больше 0", nameof(model.Cost));
2023-02-07 13:00:02 +04:00
}
2023-02-12 10:11:34 +04:00
_logger.LogInformation("Component. ComponentName:{ComponentName}. Cost:{ Cost}. Id:{Id}", model.ComponentName, model.Cost, model.Id);
2023-03-11 19:13:24 +04:00
var element = _componentStorage.GetElement(new ComponentSearchModel
{
ComponentName = model.ComponentName
});
2023-02-07 13:00:02 +04:00
if (element != null && element.Id != model.Id)
{
2023-02-12 10:11:34 +04:00
throw new InvalidOperationException("Компонент с таким названием уже есть");
2023-02-07 13:00:02 +04:00
}
}
}
2023-02-12 10:11:34 +04:00
}