174 lines
6.5 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 BankContracts.BindingModels.Client;
using BankContracts.BusinessLogicsContracts.Client;
using BankContracts.SearchModels.Client;
using BankContracts.StoragesModels.Client;
using BankContracts.ViewModels.Client.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BankBusinessLogic.BusinessLogic.Client
{
// Класс, реализующий бизнес-логику для клиентов
public class ClientLogic : IClientLogic
{
private readonly ILogger _logger;
private readonly IClientStorage _clientStorage;
// Конструктор
public ClientLogic(ILogger<ClientLogic> logger, IClientStorage clientStorage)
{
_logger = logger;
_clientStorage = clientStorage;
}
// Вывод конкретного клиента
public ClientViewModel? ReadElement(ClientSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Name:{Name}.Surname:{Surname}.Patronymic:{Patronymic}.Id:{Id}", model.Name, model.Surname, model.Patronymic, model.Id);
var element = _clientStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("Read element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
// Вывод отфильтрованного списка
public List<ClientViewModel>? ReadList(ClientSearchModel model)
{
_logger.LogInformation("ReadList. ClientId:{Id}", model?.Id);
// list хранит весь список в случае, если model пришло со значением null на вход метода
var list = model == null ? _clientStorage.GetFullList() : _clientStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
// Создание клиента
public bool Create(ClientBindingModel model)
{
CheckModel(model);
if (_clientStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
// Обновление клиента
public bool Update(ClientBindingModel model)
{
CheckModel(model);
if (_clientStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
// Удаление клиента
public bool Delete(ClientBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_clientStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
// Проверка входного аргумента для методов Insert, Update и Delete
public void CheckModel(ClientBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
// При удалении параметру withParams передаём false
if (!withParams)
{
return;
}
// Проверка на наличие имени клиента
if (string.IsNullOrEmpty(model.Name))
{
throw new ArgumentNullException("Нет имени пользователя", nameof(model.Name));
}
// Проверка на наличие фамилии клиента
if (string.IsNullOrEmpty(model.Surname))
{
throw new ArgumentNullException("Нет фамилии пользователя", nameof(model.Surname));
}
// Проверка на наличие отчества клиента
if (string.IsNullOrEmpty(model.Patronymic))
{
throw new ArgumentNullException("Нет отчества пользователя", nameof(model.Patronymic));
}
// Проверка на наличие эл. почты
if (string.IsNullOrEmpty(model.Email))
{
throw new ArgumentNullException("Нет почты пользователя", nameof(model.Email));
}
// Проверка на наличие пароля
if (string.IsNullOrEmpty(model.Password))
{
throw new ArgumentNullException("Нет пароля пользователя", nameof(model.Password));
}
// Проверка на наличие мобильного телефона
if (string.IsNullOrEmpty(model.MobilePhone))
{
throw new ArgumentNullException("Нет моб.телефона пользователя", nameof(model.MobilePhone));
}
_logger.LogInformation("Client. Name:{Name}.Surname:{Surname}.Patronymic:{Patronymic}.Email:{Email}.Password:{Password}.Mobeliphone:{MobilePhone}.Id:{Id}",
model.Name, model.Surname, model.Patronymic, model.Email, model.Password, model.MobilePhone, model.Id);
// Для проверка на наличие такого же аккаунта
var element = _clientStorage.GetElement(new ClientSearchModel
{
Email = model.Email,
});
// Если элемент найден и его Id не совпадает с Id переданного объекта
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Клиент с такой почтой уже есть");
}
}
}
}