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

116 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 CargoLogic : ICargoLogic
{
private readonly ILogger _logger;
private readonly ICargoStorage _cargoStorage;
public CargoLogic(ILogger <CargoLogic> logger, ICargoStorage cargoStorage)
{
_logger = logger;
_cargoStorage = cargoStorage;
}
public List<CargoViewModel>? ReadList(CargoSearchModel? model)
{
_logger.LogInformation("ReadList. CargoName:{CargoName}. Id:{Id}", model?.CargoName, model?.Id);
var list = model == null ? _cargoStorage.GetFullList() : _cargoStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public CargoViewModel? ReadElement(CargoSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. CargoName:{CargoName}. Id:{Id}", model.CargoName, model.Id);
var element = _cargoStorage.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(CargoBindingModel model)
{
CheckModel(model);
if (_cargoStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(CargoBindingModel model)
{
CheckModel(model);
if (_cargoStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(CargoBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_cargoStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(CargoBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.CargoName))
{
throw new ArgumentNullException("Нет названия груза", nameof(model.CargoName));
}
if (model.Weight <= 0)
{
throw new ArgumentNullException("Вес груза должен быть больше 0", nameof(model.Weight));
}
_logger.LogInformation("Cargo. CargoName:{CargoName}. Weight:{Weight}. Id:{Id}", model.CargoName, model.Weight, model.Id);
var element = _cargoStorage.GetElement(new CargoSearchModel
{
CargoName = model.CargoName
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Груз с таким названием уже есть");
}
}
}
}