PIbd-21_Zhirnova_A.E._SUBD/TransportCompany/TransportCompanyBusinessLogic/BusinessLogics/DriverLogic.cs
2024-04-15 11:23:34 +04:00

115 lines
4.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Microsoft.Extensions.Logging;
using TransportCompanyContracts.BindingModels;
using TransportCompanyContracts.BusinessLogicsContracts;
using TransportCompanyContracts.SearchModels;
using TransportCompanyContracts.StoragesContracts;
using TransportCompanyContracts.ViewModels;
namespace TransportCompanyBusinessLogic.BusinessLogics
{
public class DriverLogic : IDriverLogic
{
private readonly ILogger _logger;
private readonly IDriverStorage _driverStorage;
public DriverLogic(ILogger<DriverLogic> logger, IDriverStorage driverStorage)
{
_logger = logger;
_driverStorage = driverStorage;
}
public List<DriverViewModel>? ReadList(DriverSearchModel? model)
{
_logger.LogInformation("ReadList. DriverFio:{DriverFio}.Id:{ Id}", model?.DriverFio, model?.Id);
var list = model == null ? _driverStorage.GetFullList() : _driverStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public DriverViewModel? ReadElement(DriverSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. DriverFio:{DriverFio}.Id:{ Id}", model.DriverFio, model.Id);
var element = _driverStorage.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(DriverBindingModel model)
{
CheckModel(model);
if (_driverStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(DriverBindingModel model)
{
CheckModel(model);
if (_driverStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(DriverBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_driverStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(DriverBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.DriverFio))
{
throw new ArgumentNullException("Нет ФИО водителя", nameof(model.DriverFio));
}
if (string.IsNullOrEmpty(model.PhoneNumber))
{
throw new ArgumentNullException("Нет номера телефона водителя", nameof(model.PhoneNumber));
}
_logger.LogInformation("Driver. DriverFio:{DriverFio}.PhoneNumber:{ PhoneNumber}.Id: { Id}", model.DriverFio, model.PhoneNumber, model.Id);
var element = _driverStorage.GetElement(new DriverSearchModel
{
PhoneNumber = model.PhoneNumber
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Водитель с таким номером телефона уже есть");
}
}
}
}