108 lines
3.9 KiB
C#
108 lines
3.9 KiB
C#
using Microsoft.Extensions.Logging;
|
||
using SecuritySystemContracts.BindingModels;
|
||
using SecuritySystemContracts.BusinessLogicsContracts;
|
||
using SecuritySystemContracts.SearchModels;
|
||
using SecuritySystemContracts.StoragesContracts;
|
||
using SecuritySystemContracts.ViewModels;
|
||
|
||
namespace SecuritySystemBusinessLogic.BusinessLogics
|
||
{
|
||
public class SecureLogic : ISecureLogic
|
||
{
|
||
private readonly ILogger _logger;
|
||
private readonly ISecureStorage _secureStorage;
|
||
public SecureLogic(ILogger<ComponentLogic> logger, ISecureStorage secureStorage)
|
||
{
|
||
_logger = logger;
|
||
_secureStorage = secureStorage;
|
||
}
|
||
public bool Create(SecureBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
if (_secureStorage.Insert(model) == null)
|
||
{
|
||
_logger.LogWarning("Insert operation failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
public bool Delete(SecureBindingModel model)
|
||
{
|
||
CheckModel(model, false);
|
||
_logger.LogInformation("Delete. Id:{Id}", model.Id);
|
||
if (_secureStorage.Delete(model) == null)
|
||
{
|
||
_logger.LogWarning("Delete operation failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
public SecureViewModel? ReadElement(SecureSearchModel model)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
_logger.LogInformation("ReadElement. SecureName:{SecureName}. Id:{Id}", model.SecureName, model.Id);
|
||
var element = _secureStorage.GetElement(model);
|
||
if (element == null)
|
||
{
|
||
_logger.LogWarning("ReadElement element not found");
|
||
return null;
|
||
}
|
||
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
|
||
return element;
|
||
}
|
||
public List<SecureViewModel>? ReadList(SecureSearchModel? model)
|
||
{
|
||
_logger.LogInformation("ReadList. SecureName:{SecureName}. Id:{Id}", model?.SecureName, model?.Id);
|
||
var list = model == null ? _secureStorage.GetFullList() : _secureStorage.GetFilteredList(model);
|
||
if (list == null)
|
||
{
|
||
_logger.LogWarning("ReadList return null list");
|
||
return null;
|
||
}
|
||
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
|
||
return list;
|
||
}
|
||
public bool Update(SecureBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
if (_secureStorage.Update(model) == null)
|
||
{
|
||
_logger.LogWarning("Update operation failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
private void CheckModel(SecureBindingModel model, bool withParams = true)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
if (!withParams)
|
||
{
|
||
return;
|
||
}
|
||
if (string.IsNullOrEmpty(model.SecureName))
|
||
{
|
||
throw new ArgumentNullException("Нет названия secure", nameof(model.SecureName));
|
||
}
|
||
if (model.Price <= 0)
|
||
{
|
||
throw new ArgumentNullException("Цена secure должна быть больше 0", nameof(model.Price));
|
||
}
|
||
_logger.LogInformation("Secure. SecureName:{SecureName}. Price:{Price}. Id:{Id}", model.SecureName, model.Price, model.Id);
|
||
var element = _secureStorage.GetElement(new SecureSearchModel
|
||
{
|
||
SecureName = model.SecureName
|
||
});
|
||
if (element != null && element.Id != model.Id)
|
||
{
|
||
throw new InvalidOperationException("Secure с таким названием уже есть");
|
||
}
|
||
}
|
||
}
|
||
}
|