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-30 20:15:35 +04:00
|
|
|
|
if (_storage.Delete(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-30 20:15:35 +04:00
|
|
|
|
if (_storage.Update(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-30 22:48:51 +04:00
|
|
|
|
if (element != null && element.Name != model.Name)
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|