CourseWorkElectronicsShop/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/RoleLogic.cs
Илья Федотов 134f7d6cef Business logic 0.2
2024-04-29 01:28:41 +04:00

100 lines
3.0 KiB
C#

using ElectronicsShopContracts.BindingModels;
using ElectronicsShopContracts.BusinessLogicContracts;
using ElectronicsShopContracts.SearchModels;
using ElectronicsShopContracts.StorageContracts;
using ElectronicsShopContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ElectronicsShopBusinessLogic.BusinessLogic
{
public class RoleLogic : IRoleLogic
{
private readonly ILogger _logger;
private readonly IRoleStorage _storage;
public RoleLogic(ILogger<RoleLogic> logger, IRoleStorage storage)
{
_logger = logger;
_storage = storage;
}
public bool Create(RoleBindingModel model)
{
CheckModel(model);
if (_storage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(RoleBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation($"Delete. ID:{model.ID}");
if (_storage.Insert(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public bool Update(RoleBindingModel model)
{
CheckModel(model);
if (_storage.Insert(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public List<RoleViewModel>? ReadList(RoleSearchModel? model)
{
_logger.LogInformation($"ReadList. ID:{model?.ID}");
var list = model == null ? _storage.GetFullList() : _storage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation($"ReadList. Count:{list.Count}");
return list;
}
private void CheckModel(RoleBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Name))
{
throw new ArgumentNullException("Нет названия категории продукта", nameof(model.Name));
}
_logger.LogInformation($"CategoryProduct. ID:{model.ID}.Name:{model.Name}");
var element = _storage.GetElement(new RoleSearchModel
{
Name = model.Name
});
if (element != null && element.ID != model.ID)
{
throw new InvalidOperationException("Такая роль уже есть");
}
}
}
}