2024-04-28 21:20:10 +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 21:20:10 +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
|
|
|
|
|
{
|
2024-05-27 14:57:41 +04:00
|
|
|
|
public class ClientLogic : IClientLogic
|
2024-04-28 21:20:10 +04:00
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
2024-05-19 16:35:35 +04:00
|
|
|
|
private readonly IClientStorage _storage;
|
2024-04-28 21:20:10 +04:00
|
|
|
|
|
2024-05-25 15:04:18 +04:00
|
|
|
|
public ClientLogic(ILogger<ClientLogic> logger, IClientStorage storage)
|
2024-04-28 23:36:49 +04:00
|
|
|
|
{
|
2024-04-28 21:20:10 +04:00
|
|
|
|
_logger = logger;
|
2024-04-29 01:28:41 +04:00
|
|
|
|
_storage = storage;
|
2024-04-28 21:20:10 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-25 17:47:14 +04:00
|
|
|
|
public bool Create(ClientBindingModel model)
|
2024-04-28 21:20:10 +04:00
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
2024-04-29 01:28:41 +04:00
|
|
|
|
if (_storage.Insert(model) == null)
|
2024-04-28 23:36:49 +04:00
|
|
|
|
{
|
2024-05-26 17:47:24 +04:00
|
|
|
|
_logger.LogWarning("Create operation failed");
|
2024-04-28 21:20:10 +04:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-25 15:04:18 +04:00
|
|
|
|
public bool Update(ClientBindingModel model)
|
2024-04-28 21:20:10 +04:00
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
2024-04-30 20:15:35 +04:00
|
|
|
|
if (_storage.Update(model) == null)
|
2024-04-28 21:20:10 +04:00
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Update operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-25 15:04:18 +04:00
|
|
|
|
public bool Delete(ClientBindingModel model)
|
2024-04-28 21:20:10 +04:00
|
|
|
|
{
|
|
|
|
|
CheckModel(model, false);
|
2024-04-28 23:36:49 +04:00
|
|
|
|
_logger.LogInformation($"Delete.ID:{model.ID}");
|
2024-04-30 20:15:35 +04:00
|
|
|
|
if (_storage.Delete(model) == null)
|
2024-04-28 23:36:49 +04:00
|
|
|
|
{
|
2024-04-30 21:31:36 +04:00
|
|
|
|
_logger.LogWarning("Delete operation failed");
|
2024-04-28 21:20:10 +04:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-19 16:35:35 +04:00
|
|
|
|
public ClientViewModel? ReadElemet(ClientSearchModel? model)
|
2024-04-28 21:20:10 +04:00
|
|
|
|
{
|
2024-04-28 23:36:49 +04:00
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
2024-04-28 21:20:10 +04:00
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
2024-05-25 15:04:18 +04:00
|
|
|
|
_logger.LogInformation($"ReadElement.Email:{model.Email}.ID:{model.ID}");
|
2024-04-29 01:28:41 +04:00
|
|
|
|
var element = _storage.GetElement(model);
|
2024-04-28 23:36:49 +04:00
|
|
|
|
if (element == null)
|
|
|
|
|
{
|
2024-04-30 21:31:36 +04:00
|
|
|
|
_logger.LogWarning("ReadElement. element not fount");
|
2024-04-28 21:20:10 +04:00
|
|
|
|
return null;
|
|
|
|
|
}
|
2024-04-30 21:31:36 +04:00
|
|
|
|
_logger.LogInformation($"ReadElement.find.ID:{element.ID}");
|
2024-04-29 01:28:41 +04:00
|
|
|
|
return element;
|
2024-04-28 21:20:10 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-19 16:35:35 +04:00
|
|
|
|
public List<ClientViewModel>? ReadList(ClientSearchModel? model)
|
2024-04-28 21:20:10 +04:00
|
|
|
|
{
|
2024-04-28 23:36:49 +04:00
|
|
|
|
|
2024-04-30 21:31:36 +04:00
|
|
|
|
_logger.LogInformation($"ReadList. ClientID:{model?.ID}");
|
2024-04-29 01:28:41 +04:00
|
|
|
|
var list = model == null ? _storage.GetFullList() : _storage.GetFilteredList(model); ;
|
2024-04-28 23:36:49 +04:00
|
|
|
|
if (list == null)
|
|
|
|
|
{
|
2024-04-30 21:31:36 +04:00
|
|
|
|
_logger.LogWarning("ReadList. return null list");
|
2024-04-28 21:20:10 +04:00
|
|
|
|
return null;
|
|
|
|
|
}
|
2024-04-30 21:31:36 +04:00
|
|
|
|
_logger.LogInformation($"ReadList.Count:{list.Count}");
|
2024-04-29 01:28:41 +04:00
|
|
|
|
return list;
|
2024-04-28 21:20:10 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-25 15:04:18 +04:00
|
|
|
|
private void CheckModel(ClientBindingModel model, bool withParams = true)
|
2024-04-28 23:36:49 +04:00
|
|
|
|
{
|
|
|
|
|
if (!withParams)
|
|
|
|
|
{
|
2024-04-28 21:20:10 +04:00
|
|
|
|
return;
|
|
|
|
|
}
|
2024-05-25 15:04:18 +04:00
|
|
|
|
if (string.IsNullOrEmpty(model.Email))
|
2024-04-28 23:36:49 +04:00
|
|
|
|
{
|
2024-05-25 15:04:18 +04:00
|
|
|
|
throw new ArgumentNullException("Нет почты пользователя", nameof(model.Email));
|
2024-04-28 21:20:10 +04:00
|
|
|
|
}
|
2024-05-25 15:04:18 +04:00
|
|
|
|
if (string.IsNullOrEmpty(model.ClientFIO))
|
2024-04-28 23:36:49 +04:00
|
|
|
|
{
|
2024-05-25 15:04:18 +04:00
|
|
|
|
throw new ArgumentNullException("Нет имени пользователя", nameof(model.ClientFIO));
|
2024-04-28 21:20:10 +04:00
|
|
|
|
}
|
2024-04-28 23:36:49 +04:00
|
|
|
|
if (string.IsNullOrEmpty(model.Password))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Нет пароля пользователя", nameof(model.Password));
|
2024-04-28 21:20:10 +04:00
|
|
|
|
}
|
2024-05-25 15:04:18 +04:00
|
|
|
|
_logger.LogInformation($"Client. ID:{model.ID}.ClientFIO:{model.ClientFIO}.Password:{model.Password}.Email:{model.Email}.");
|
2024-04-29 01:28:41 +04:00
|
|
|
|
|
2024-05-19 16:35:35 +04:00
|
|
|
|
var element = _storage.GetElement(new ClientSearchModel
|
2024-04-28 23:09:52 +04:00
|
|
|
|
{
|
2024-05-25 15:04:18 +04:00
|
|
|
|
Email= model.Email
|
2024-04-28 23:09:52 +04:00
|
|
|
|
});
|
2024-05-27 14:57:41 +04:00
|
|
|
|
if (element != null && element.Email == model.Email)
|
2024-04-28 23:09:52 +04:00
|
|
|
|
{
|
2024-05-25 15:04:18 +04:00
|
|
|
|
throw new InvalidOperationException("Клиент с такой почтой уже есть");
|
2024-05-25 17:47:14 +04:00
|
|
|
|
}
|
2024-04-28 21:20:10 +04:00
|
|
|
|
}
|
2024-05-25 17:47:14 +04:00
|
|
|
|
}
|
2024-04-28 21:20:10 +04:00
|
|
|
|
}
|