PIbd-21_Markov_D.P._LawFirm/LawFirm/LawFirmBusinessLogic/BusinessLogics/BlankLogic.cs

116 lines
4.1 KiB
C#
Raw Normal View History

2023-02-13 22:22:24 +04:00
using LawFirmContracts.BindingModels;
using LawFirmContracts.BusinessLogicsContracts;
using LawFirmContracts.SearchModels;
using LawFirmContracts.StoragesContracts;
using LawFirmContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace LawFirmBusinessLogic.BusinessLogics
{
public class BlankLogic : IBlankLogic
{
private readonly ILogger _logger;
private readonly IBlankStorage _blankStorage;
public BlankLogic(ILogger<BlankLogic> logger, IBlankStorage blankStorage)
{
_logger = logger;
_blankStorage = blankStorage;
}
public List<BlankViewModel>? ReadList(BlankSearchModel? model)
{
_logger.LogInformation("ReadList. BlankName:{BlankName}.Id:{Id}",
model?.BlankName, model?.Id);
var list = model == null ? _blankStorage.GetFullList() :
_blankStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public BlankViewModel? ReadElement(BlankSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. BlankName:{BlankName}.Id:{Id}",
model.BlankName, model.Id);
var element = _blankStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
public bool Create(BlankBindingModel model)
{
CheckModel(model);
if (_blankStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(BlankBindingModel model)
{
CheckModel(model);
if (_blankStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(BlankBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_blankStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(BlankBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.BlankName))
{
throw new ArgumentNullException("Нет названия компонента",
nameof(model.BlankName));
}
if (model.Price <= 0)
{
throw new ArgumentNullException("Цена компонента должна быть больше 0",
nameof(model.Price));
}
_logger.LogInformation("Blank. BlankName:{BlackName}.Price:{Price}. Id: {Id}",
model.BlankName, model.Price, model.Id);
var element = _blankStorage.GetElement(
new BlankSearchModel
{
BlankName = model.BlankName
}
);
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Компонент с таким названием уже есть");
}
}
}
}