CourseWorkElectronicsShop/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/RoleLogic.cs

100 lines
3.0 KiB
C#
Raw Normal View History

2024-04-28 23:09:52 +04:00
using ElectronicsShopContracts.BindingModels;
using ElectronicsShopContracts.BusinessLogicContracts;
using ElectronicsShopContracts.SearchModels;
2024-04-29 01:28:41 +04:00
using ElectronicsShopContracts.StorageContracts;
2024-04-28 23:09:52 +04:00
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;
2024-04-29 01:28:41 +04:00
private readonly IRoleStorage _storage;
2024-04-28 23:09:52 +04:00
2024-04-29 01:28:41 +04:00
public RoleLogic(ILogger<RoleLogic> logger, IRoleStorage storage)
2024-04-28 23:09:52 +04:00
{
_logger = logger;
2024-04-29 01:28:41 +04:00
_storage = storage;
2024-04-28 23:09:52 +04:00
}
public bool Create(RoleBindingModel model)
{
CheckModel(model);
2024-04-29 01:28:41 +04:00
if (_storage.Insert(model) == null)
2024-04-28 23:09:52 +04:00
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(RoleBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation($"Delete. ID:{model.ID}");
2024-04-29 01:28:41 +04:00
if (_storage.Insert(model) == null)
2024-04-28 23:09:52 +04:00
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public bool Update(RoleBindingModel model)
{
CheckModel(model);
2024-04-29 01:28:41 +04:00
if (_storage.Insert(model) == null)
2024-04-28 23:09:52 +04:00
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public List<RoleViewModel>? ReadList(RoleSearchModel? model)
{
_logger.LogInformation($"ReadList. ID:{model?.ID}");
2024-04-29 01:28:41 +04:00
var list = model == null ? _storage.GetFullList() : _storage.GetFilteredList(model);
2024-04-28 23:09:52 +04:00
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
2024-04-29 01:28:41 +04:00
_logger.LogInformation($"ReadList. Count:{list.Count}");
return list;
2024-04-28 23:09:52 +04:00
}
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));
}
2024-04-29 01:28:41 +04:00
_logger.LogInformation($"CategoryProduct. ID:{model.ID}.Name:{model.Name}");
var element = _storage.GetElement(new RoleSearchModel
2024-04-28 23:09:52 +04:00
{
Name = model.Name
});
2024-04-29 01:28:41 +04:00
if (element != null && element.ID != model.ID)
2024-04-28 23:09:52 +04:00
{
throw new InvalidOperationException("Такая роль уже есть");
}
2024-04-29 01:28:41 +04:00
2024-04-28 23:09:52 +04:00
}
}
}