ComputerHardwareStore_YouAr.../ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/BusinessLogic/VendorLogic.cs

108 lines
3.6 KiB
C#
Raw Permalink 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 ComputerHardwareStoreContracts.BindingModels;
using ComputerHardwareStoreContracts.BusinessLogicsContracts;
using ComputerHardwareStoreContracts.SearchModels;
using ComputerHardwareStoreContracts.StorageContracts;
using ComputerHardwareStoreContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace ComputerHardwareStoreBusinessLogic.BusinessLogic
{
public class VendorLogic : IVendorLogic
{
private readonly ILogger _logger;
private readonly IVendorStorage _vendorStorage;
public VendorLogic(ILogger<VendorLogic> logger, IVendorStorage vendorStorage)
{
_logger = logger;
_vendorStorage = vendorStorage;
}
public List<VendorViewModel>? ReadList(VendorSearchModel? model)
{
_logger.LogInformation("ReadList. Login:{Login}.Id:{Id}", model?.Login, model?.Id);
var list = model == null ? _vendorStorage.GetFullList() : _vendorStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public VendorViewModel? ReadElement(VendorSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Login:{Login}.Id:{Id}", model.Login, model.Id);
var element = _vendorStorage.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(VendorBindingModel model)
{
CheckModel(model);
if (_vendorStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(VendorBindingModel model)
{
CheckModel(model);
if (_vendorStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(VendorBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_vendorStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(VendorBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Login))
{
throw new ArgumentNullException("Нет логина работника", nameof(model.Login));
}
var element = _vendorStorage.GetElement(new VendorSearchModel
{
Login = model.Login
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Работник с таким логином уже есть");
}
}
}
}