114 lines
4.1 KiB
C#
114 lines
4.1 KiB
C#
|
using SecureShopContracts.BindingModels;
|
|||
|
using SecureShopContracts.BusinessLogicsContracts;
|
|||
|
using SecureShopContracts.SearchModels;
|
|||
|
using SecureShopContracts.StoragesContracts;
|
|||
|
using SecureShopContracts.ViewModels;
|
|||
|
using Microsoft.Extensions.Logging;
|
|||
|
|
|||
|
namespace SecureShopBusinessLogic.BusinessLogics
|
|||
|
{
|
|||
|
public class FacilitiesLogic : IFacilitiesLogic
|
|||
|
{
|
|||
|
private readonly ILogger _logger;
|
|||
|
private readonly IFacilitiesStorage _FacilitiesStorage;
|
|||
|
|
|||
|
public FacilitiesLogic(ILogger<FacilitiesLogic> logger, IFacilitiesStorage FacilitiesStorage)
|
|||
|
{
|
|||
|
_logger = logger;
|
|||
|
_FacilitiesStorage = FacilitiesStorage;
|
|||
|
}
|
|||
|
|
|||
|
public List<FacilitiesViewModel>? ReadList(FacilitiesSearchModel? model)
|
|||
|
{
|
|||
|
_logger.LogInformation("ReadList. FacilitiesName:{FacilitiesName}. Id:{Id}", model?.FacilitiesName, model?.Id);
|
|||
|
var list = model == null ? _FacilitiesStorage.GetFullList() : _FacilitiesStorage.GetFilteredList(model);
|
|||
|
if (list == null)
|
|||
|
{
|
|||
|
_logger.LogWarning("ReadList return null list");
|
|||
|
return null;
|
|||
|
}
|
|||
|
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
|
|||
|
return list;
|
|||
|
}
|
|||
|
|
|||
|
public FacilitiesViewModel? ReadElement(FacilitiesSearchModel model)
|
|||
|
{
|
|||
|
if (model == null)
|
|||
|
{
|
|||
|
throw new ArgumentNullException(nameof(model));
|
|||
|
}
|
|||
|
_logger.LogInformation("ReadElement. FacilitiesName:{FacilitiesName}. Id:{Id}", model.FacilitiesName, model.Id);
|
|||
|
var element = _FacilitiesStorage.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(FacilitiesBindingModel model)
|
|||
|
{
|
|||
|
CheckModel(model);
|
|||
|
if (_FacilitiesStorage.Insert(model) == null)
|
|||
|
{
|
|||
|
_logger.LogWarning("Insert operation failed");
|
|||
|
return false;
|
|||
|
}
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
public bool Update(FacilitiesBindingModel model)
|
|||
|
{
|
|||
|
CheckModel(model);
|
|||
|
if (_FacilitiesStorage.Update(model) == null)
|
|||
|
{
|
|||
|
_logger.LogWarning("Update operation failed");
|
|||
|
return false;
|
|||
|
}
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
public bool Delete(FacilitiesBindingModel model)
|
|||
|
{
|
|||
|
CheckModel(model, false);
|
|||
|
_logger.LogInformation("Delete. Id:{Id}", model.Id);
|
|||
|
if (_FacilitiesStorage.Delete(model) == null)
|
|||
|
{
|
|||
|
_logger.LogWarning("Delete operation failed");
|
|||
|
return false;
|
|||
|
}
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
private void CheckModel(FacilitiesBindingModel model, bool withParams = true)
|
|||
|
{
|
|||
|
if (model == null)
|
|||
|
{
|
|||
|
throw new ArgumentNullException(nameof(model));
|
|||
|
}
|
|||
|
if (!withParams)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
if (string.IsNullOrEmpty(model.FacilitiesName))
|
|||
|
{
|
|||
|
throw new ArgumentNullException("Нет названия оборудования", nameof(model.FacilitiesName));
|
|||
|
}
|
|||
|
if (model.Cost <= 0)
|
|||
|
{
|
|||
|
throw new ArgumentNullException("Цена оборудования должна быть больше 0", nameof(model.Cost));
|
|||
|
}
|
|||
|
_logger.LogInformation("Facilities. FacilitiesName:{FacilitiesName}. Cost:{Cost}. Id: {Id}", model.FacilitiesName, model.Cost, model.Id);
|
|||
|
var element = _FacilitiesStorage.GetElement(new FacilitiesSearchModel
|
|||
|
{
|
|||
|
FacilitiesName = model.FacilitiesName
|
|||
|
});
|
|||
|
if (element != null && element.Id != model.Id)
|
|||
|
{
|
|||
|
throw new InvalidOperationException("Оборудование с таким названием уже есть");
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|