PIbd-21_RazubaevSM_SUBD_Labs/SYBDBusinessLogic/BusinessLogics/InsuranceLogic.cs

119 lines
4.2 KiB
C#
Raw Normal View History

using SYBDContracts.BindingModels;
using SYBDContracts.BusinessLogicsContracts;
using SYBDContracts.SearchModels;
using SYBDContracts.StoragesContracts;
using SYBDContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace SYBDBusinessLogic.BusinessLogics
{
public class InsuranceLogic : IInsuranceLogic
{
private readonly ILogger _logger;
private readonly IInsuranceStorage _insuranceStorage;
public InsuranceLogic(ILogger<InsuranceLogic> logger, IInsuranceStorage insuranceStorage)
{
_logger = logger;
_insuranceStorage = insuranceStorage;
}
public List<InsuranceViewModel>? ReadList(InsuranceSearchModel? model)
{
_logger.LogInformation("ReadList. InsuranceName:{Name}. Id:{Id}", model?.Name, model?.Id);
var list = model == null ? _insuranceStorage.GetFullList() : _insuranceStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public InsuranceViewModel? ReadElement(InsuranceSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. InsuranceName:{Name}. Id:{Id}", model.Name, model.Id);
var element = _insuranceStorage.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(InsuranceBindingModel model)
{
CheckModel(model);
if (_insuranceStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(InsuranceBindingModel model)
{
CheckModel(model);
if (_insuranceStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(InsuranceBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_insuranceStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(InsuranceBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Name))
{
throw new ArgumentNullException("Нет названия страховой", nameof(model.Name));
}
if (string.IsNullOrEmpty(model.Address))
{
throw new ArgumentNullException("Нет адреса страховой", nameof(model.Address));
}
if (string.IsNullOrEmpty(model.Phone))
{
throw new ArgumentNullException("Нет телефона страховой", nameof(model.Phone));
}
_logger.LogInformation("Insurance. Name:{Name}. Address:{Address}.Phone:{Phone}. Id:{Id}", model.Name, model.Address, model.Phone, model.Id);
var element = _insuranceStorage.GetElement(new InsuranceSearchModel
{
Name = model.Name
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Страховая с таким названием уже есть");
}
}
}
}