CourseWorkElectronicsShop/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/ClientLogic.cs
2024-05-25 17:47:14 +04:00

127 lines
4.1 KiB
C#
Raw 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 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
{
internal class ClientLogic : IClientLogic
{
private readonly ILogger _logger;
private readonly IClientStorage _storage;
public ClientLogic(ILogger<ClientLogic> logger, IClientStorage storage)
{
_logger = logger;
_storage = storage;
}
public bool Create(ClientBindingModel model)
{
CheckModel(model);
if (_storage.Insert(model) == null)
{
_logger.LogWarning("Add operation failed");
return false;
}
return true;
}
public bool Update(ClientBindingModel model)
{
CheckModel(model);
if (_storage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(ClientBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation($"Delete.ID:{model.ID}");
if (_storage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public ClientViewModel? ReadElemet(ClientSearchModel? model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation($"ReadElement.Email:{model.Email}.ID:{model.ID}");
var element = _storage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement. element not fount");
return null;
}
_logger.LogInformation($"ReadElement.find.ID:{element.ID}");
return element;
}
public List<ClientViewModel>? ReadList(ClientSearchModel? model)
{
_logger.LogInformation($"ReadList. ClientID:{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(ClientBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Email))
{
throw new ArgumentNullException("Нет почты пользователя", nameof(model.Email));
}
if (string.IsNullOrEmpty(model.ClientFIO))
{
throw new ArgumentNullException("Нет имени пользователя", nameof(model.ClientFIO));
}
if (string.IsNullOrEmpty(model.Password))
{
throw new ArgumentNullException("Нет пароля пользователя", nameof(model.Password));
}
_logger.LogInformation($"Client. ID:{model.ID}.ClientFIO:{model.ClientFIO}.Password:{model.Password}.Email:{model.Email}.");
var element = _storage.GetElement(new ClientSearchModel
{
Email= model.Email
});
if (element != null && element.Email != model.Email)
{
throw new InvalidOperationException("Клиент с такой почтой уже есть");
}
}
}
}