108 lines
3.8 KiB
C#
108 lines
3.8 KiB
C#
|
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("Кладовщик с таким логином уже есть");
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|