127 lines
4.4 KiB
C#
127 lines
4.4 KiB
C#
|
using BankContracts.BindingModels;
|
|||
|
using BankContracts.BusinessLogicsContracts;
|
|||
|
using BankContracts.SearchModels;
|
|||
|
using BankContracts.StoragesContracts;
|
|||
|
using BankContracts.ViewModels;
|
|||
|
using Microsoft.Extensions.Logging;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
|
|||
|
namespace BankBusinessLogic.BusinessLogics
|
|||
|
{
|
|||
|
public class OperatorLogic : IOperatorLogic
|
|||
|
{
|
|||
|
private readonly ILogger _logger;
|
|||
|
private readonly IOperatorStorage _operatorStorage;
|
|||
|
|
|||
|
public OperatorLogic(ILogger<OperatorLogic> logger, IOperatorStorage operatorStorage)
|
|||
|
{
|
|||
|
_logger = logger;
|
|||
|
_operatorStorage = operatorStorage;
|
|||
|
}
|
|||
|
|
|||
|
public List<OperatorViewModel>? ReadList(OperatorSearchModel? model)
|
|||
|
{
|
|||
|
_logger.LogInformation("ReadList. Id:{Id}", model?.Id);
|
|||
|
var list = model == null ? _operatorStorage.GetFullList() : _operatorStorage.GetFilteredList(model);
|
|||
|
if (list == null)
|
|||
|
{
|
|||
|
_logger.LogWarning("ReadList return null list");
|
|||
|
return null;
|
|||
|
}
|
|||
|
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
|
|||
|
return list;
|
|||
|
}
|
|||
|
|
|||
|
public OperatorViewModel? ReadElement(OperatorSearchModel model)
|
|||
|
{
|
|||
|
if (model == null)
|
|||
|
{
|
|||
|
throw new ArgumentNullException(nameof(model));
|
|||
|
}
|
|||
|
_logger.LogInformation("ReadElement. Id:{Id}", model.Id);
|
|||
|
var element = _operatorStorage.GetElement(model);
|
|||
|
if (element == null)
|
|||
|
{
|
|||
|
_logger.LogWarning("ReadElement element not found");
|
|||
|
return null;
|
|||
|
}
|
|||
|
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
|
|||
|
return element;
|
|||
|
}
|
|||
|
|
|||
|
public bool Create(OperatorBindingModel model)
|
|||
|
{
|
|||
|
CheckModel(model);
|
|||
|
if (_operatorStorage.Insert(model) == null)
|
|||
|
{
|
|||
|
_logger.LogWarning("Insert operation failed");
|
|||
|
return false;
|
|||
|
}
|
|||
|
return true;
|
|||
|
}
|
|||
|
public bool Update(OperatorBindingModel model)
|
|||
|
{
|
|||
|
CheckModel(model);
|
|||
|
if (_operatorStorage.Update(model) == null)
|
|||
|
{
|
|||
|
_logger.LogWarning("Update operation failed");
|
|||
|
return false;
|
|||
|
}
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
public bool Delete(OperatorBindingModel model)
|
|||
|
{
|
|||
|
CheckModel(model, false);
|
|||
|
_logger.LogInformation("Delete. Id:{Id}", model.Id);
|
|||
|
if (_operatorStorage.Delete(model) == null)
|
|||
|
{
|
|||
|
_logger.LogWarning("Delete operation failed");
|
|||
|
return false;
|
|||
|
}
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
private void CheckModel(OperatorBindingModel model, bool withParams = true)
|
|||
|
{
|
|||
|
if (model == null)
|
|||
|
{
|
|||
|
throw new ArgumentNullException(nameof(model));
|
|||
|
}
|
|||
|
if (!withParams)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
if (string.IsNullOrEmpty(model.FirstName))
|
|||
|
{
|
|||
|
throw new ArgumentNullException("Нет имени оператора", nameof(model.FirstName));
|
|||
|
}
|
|||
|
if (string.IsNullOrEmpty(model.LastName))
|
|||
|
{
|
|||
|
throw new ArgumentNullException("Нет фамилии оператора", nameof(model.LastName));
|
|||
|
}
|
|||
|
if (string.IsNullOrEmpty(model.Login))
|
|||
|
{
|
|||
|
throw new ArgumentNullException("Нет логина оператора", nameof(model.Login));
|
|||
|
}
|
|||
|
if (string.IsNullOrEmpty(model.Password))
|
|||
|
{
|
|||
|
throw new ArgumentNullException("Нет пароля оператора", nameof(model.Password));
|
|||
|
}
|
|||
|
_logger.LogInformation("Operator. LastName:{LastName}. FirstName:{FirstName}. Login:{Login}. Password:{Password}. Id:{Id}", model.LastName, model.FirstName, model.Login, model.Password, model.Id);
|
|||
|
var element = _operatorStorage.GetElement(new OperatorSearchModel
|
|||
|
{
|
|||
|
Login = model.Login
|
|||
|
});
|
|||
|
if (element != null && element.Id != model.Id)
|
|||
|
{
|
|||
|
throw new InvalidOperationException("Оператор с таким логином уже есть");
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|