PIbd-21_Zhirnova_A.E._SUBD/TransportCompany/TransportCompanyBusinessLogic/BusinessLogics/PointLogic.cs

115 lines
3.9 KiB
C#
Raw Normal View History

2024-04-15 11:23:34 +04:00
using Microsoft.Extensions.Logging;
using TransportCompanyContracts.BindingModels;
using TransportCompanyContracts.BusinessLogicsContracts;
using TransportCompanyContracts.SearchModels;
using TransportCompanyContracts.StoragesContracts;
using TransportCompanyContracts.ViewModels;
namespace TransportCompanyBusinessLogic.BusinessLogics
{
public class PointLogic : IPointLogic
{
private readonly ILogger _logger;
private readonly IPointStorage _pointStorage;
public PointLogic(ILogger<PointLogic> logger, IPointStorage pointStorage)
{
_logger = logger;
_pointStorage = pointStorage;
}
public List<PointViewModel>? ReadList(PointSearchModel? model)
{
_logger.LogInformation("ReadList. PointName:{PointName}.Id:{ Id}", model?.PointName, model?.Id);
var list = model == null ? _pointStorage.GetFullList() : _pointStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public PointViewModel? ReadElement(PointSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. PointName:{PointName}.Id:{ Id}", model.PointName, model.Id);
var element = _pointStorage.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(PointBindingModel model)
{
CheckModel(model);
if (_pointStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(PointBindingModel model)
{
CheckModel(model);
if (_pointStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(PointBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_pointStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(PointBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.PointName))
{
throw new ArgumentNullException("Нет наименования пункта", nameof(model.PointName));
}
if (string.IsNullOrEmpty(model.Address))
{
throw new ArgumentNullException("Нет адреса пункта", nameof(model.Address));
}
_logger.LogInformation("Point. PointName:{PointName}.Address:{ Address}.Id: { Id}", model.PointName, model.Address, model.Id);
var element = _pointStorage.GetElement(new PointSearchModel
{
Address = model.Address
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Пункт с таким адресом уже есть");
}
}
}
}