логика работника (+ подкорректировала vendorSearchModel) и логика кладовщика (+ подкорректировала storekeeperSearchModel)

This commit is contained in:
dex_moth 2024-04-29 13:17:43 +04:00
parent fe14f02d22
commit 06ec3952d3
4 changed files with 216 additions and 2 deletions

View File

@ -0,0 +1,107 @@
using ComputerHardwareStoreContracts.BindingModels;
using ComputerHardwareStoreContracts.BusinessLogicsContracts;
using ComputerHardwareStoreContracts.SearchModels;
using ComputerHardwareStoreContracts.StorageContracts;
using ComputerHardwareStoreContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace ComputerHardwareStoreBusinessLogic.BusinessLogic
{
public class StoreKeeperLogic : IStoreKeeperLogic
{
private readonly ILogger _logger;
private readonly IStoreKeeperStorage _storeKeeperStorage;
public StoreKeeperLogic(ILogger<StoreKeeperLogic> logger, IStoreKeeperStorage storeKeeperStorage)
{
_logger = logger;
_storeKeeperStorage = storeKeeperStorage;
}
public List<StoreKeeperViewModel>? ReadList(StoreKeeperSearchModel? model)
{
_logger.LogInformation("ReadList. Login:{Login}.Id:{Id}", model?.Login, model?.Id);
var list = model == null ? _storeKeeperStorage.GetFullList() : _storeKeeperStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public StoreKeeperViewModel? ReadElement(StoreKeeperSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Login:{Login}.Id:{Id}", model.Login, model.Id);
var element = _storeKeeperStorage.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(StoreKeeperBindingModel model)
{
CheckModel(model);
if (_storeKeeperStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(StoreKeeperBindingModel model)
{
CheckModel(model);
if (_storeKeeperStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(StoreKeeperBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_storeKeeperStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(StoreKeeperBindingModel 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 = _storeKeeperStorage.GetElement(new StoreKeeperSearchModel
{
Login = model.Login
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Кладовщик с таким логином уже есть");
}
}
}
}

View File

@ -0,0 +1,107 @@
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("Работник с таким логином уже есть");
}
}
}
}

View File

@ -3,7 +3,7 @@
public class StoreKeeperSearchModel
{
public int? Id { get; set; }
public string? Email { get; set; }
public string? Login { get; set; }
public string? Password { get; set; }
}
}

View File

@ -3,7 +3,7 @@
public class VendorSearchModel
{
public int? Id { get; set; }
public string? Email { get; set; }
public string? Login { get; set; }
public string? Password { get; set; }
}
}