10Г Егор Романов beeb41756a fix
2023-03-07 14:10:53 +04:00

116 lines
4.0 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 SecuritySystemContracts.BindingModels;
using SecuritySystemContracts.BusinessLogicsContracts;
using SecuritySystemContracts.SearchModels;
using SecuritySystemContracts.StoragesContracts;
using SecuritySystemContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace SecuritySystemBusinessLogic
{
public class SecureLogic : ISecureLogic
{
private readonly ILogger _logger;
private readonly ISecureStorage _SecureStorage;
public SecureLogic(ILogger<SecureLogic> logger, ISecureStorage SecureStorage)
{
_logger = logger;
_SecureStorage = SecureStorage;
}
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 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 bool Create(SecureBindingModel model)
{
CheckModel(model);
if (_SecureStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(SecureBindingModel model)
{
CheckModel(model);
if (_SecureStorage.Update(model) == null)
{
_logger.LogWarning("Update 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;
}
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("Нет названия камеры", nameof(model.SecureName));
}
if (model.Price <= 0)
{
throw new ArgumentNullException("Цена камеры должна быть больше 0", nameof(model.Price));
}
_logger.LogInformation("Secure. SecureName:{SecureName}.Cost:{ Cost}. 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("Камера с таким названием уже есть");
}
}
}
}