116 lines
4.0 KiB
C#
Raw Permalink Normal View History

2023-03-06 17:15:03 +04:00

using SecuritySystemContracts.BindingModels;
using SecuritySystemContracts.BusinessLogicsContracts;
using SecuritySystemContracts.SearchModels;
using SecuritySystemContracts.StoragesContracts;
using SecuritySystemContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace SecuritySystemBusinessLogic
{
2023-03-07 12:47:29 +04:00
public class SecureLogic : ISecureLogic
2023-03-06 17:15:03 +04:00
{
private readonly ILogger _logger;
2023-03-07 12:47:29 +04:00
private readonly ISecureStorage _SecureStorage;
2023-03-06 17:15:03 +04:00
2023-03-07 12:47:29 +04:00
public SecureLogic(ILogger<SecureLogic> logger, ISecureStorage SecureStorage)
2023-03-06 17:15:03 +04:00
{
_logger = logger;
2023-03-07 12:47:29 +04:00
_SecureStorage = SecureStorage;
2023-03-06 17:15:03 +04:00
}
2023-03-07 12:47:29 +04:00
public List<SecureViewModel>? ReadList(SecureSearchModel? model)
2023-03-06 17:15:03 +04:00
{
2023-03-07 12:47:29 +04:00
_logger.LogInformation("ReadList. SecureName:{SecureName}.Id:{ Id}", model?.SecureName, model?.Id);
var list = model == null ? _SecureStorage.GetFullList() : _SecureStorage.GetFilteredList(model);
2023-03-06 17:15:03 +04:00
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
2023-03-07 12:47:29 +04:00
public SecureViewModel? ReadElement(SecureSearchModel model)
2023-03-06 17:15:03 +04:00
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
2023-03-07 12:47:29 +04:00
_logger.LogInformation("ReadElement. SecureName:{SecureName}.Id:{ Id}", model.SecureName, model.Id);
var element = _SecureStorage.GetElement(model);
2023-03-06 17:15:03 +04:00
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
2023-03-07 12:47:29 +04:00
public bool Create(SecureBindingModel model)
2023-03-06 17:15:03 +04:00
{
CheckModel(model);
2023-03-07 12:47:29 +04:00
if (_SecureStorage.Insert(model) == null)
2023-03-06 17:15:03 +04:00
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
2023-03-07 12:47:29 +04:00
public bool Update(SecureBindingModel model)
2023-03-06 17:15:03 +04:00
{
CheckModel(model);
2023-03-07 12:47:29 +04:00
if (_SecureStorage.Update(model) == null)
2023-03-06 17:15:03 +04:00
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
2023-03-07 12:47:29 +04:00
public bool Delete(SecureBindingModel model)
2023-03-06 17:15:03 +04:00
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
2023-03-07 12:47:29 +04:00
if (_SecureStorage.Delete(model) == null)
2023-03-06 17:15:03 +04:00
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
2023-03-07 12:47:29 +04:00
private void CheckModel(SecureBindingModel model, bool withParams = true)
2023-03-06 17:15:03 +04:00
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
2023-03-07 12:47:29 +04:00
if (string.IsNullOrEmpty(model.SecureName))
2023-03-06 17:15:03 +04:00
{
2023-03-07 12:47:29 +04:00
throw new ArgumentNullException("Нет названия камеры", nameof(model.SecureName));
2023-03-06 17:15:03 +04:00
}
if (model.Price <= 0)
{
throw new ArgumentNullException("Цена камеры должна быть больше 0", nameof(model.Price));
}
2023-03-07 12:47:29 +04:00
_logger.LogInformation("Secure. SecureName:{SecureName}.Cost:{ Cost}. Id: { Id}", model.SecureName, model.Price, model.Id);
var element = _SecureStorage.GetElement(new SecureSearchModel
2023-03-06 17:15:03 +04:00
{
2023-03-07 12:47:29 +04:00
SecureName = model.SecureName
2023-03-06 17:15:03 +04:00
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Камера с таким названием уже есть");
}
}
}
}