116 lines
4.1 KiB
C#
116 lines
4.1 KiB
C#
using SushiContracts.BindingModels;
|
||
using SushiContracts.BusinessLogicContracts;
|
||
using SushiContracts.SearchModels;
|
||
using SushiContracts.StorageContracts;
|
||
using SushiContracts.ViewModels;
|
||
using Microsoft.Extensions.Logging;
|
||
|
||
namespace SushiBusinessLogic
|
||
{
|
||
public class EmployeeLogic : IEmployeeLogic
|
||
{
|
||
public readonly ILogger _logger;
|
||
public readonly IEmployeeStorage _employeeStorage;
|
||
|
||
public EmployeeLogic(ILogger<EmployeeLogic> logger, IEmployeeStorage employeeStorage)
|
||
{
|
||
_logger = logger;
|
||
_employeeStorage = employeeStorage;
|
||
}
|
||
|
||
public List<EmployeeViewModel>? ReadList(EmployeeSearchModel? model)
|
||
{
|
||
_logger.LogInformation("ReadList. EmployeeName:{EmployeeName}.Id:{ Id}", model?.EmployeeName, model?.Id);
|
||
var list = model == null ? _employeeStorage.GetFullList() : _employeeStorage.GetFilteredList(model);
|
||
if (list == null)
|
||
{
|
||
_logger.LogWarning("ReadList return null list");
|
||
return null;
|
||
}
|
||
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
|
||
return list;
|
||
}
|
||
|
||
public EmployeeViewModel? ReadElement(EmployeeSearchModel model)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
_logger.LogInformation("ReadElement. EmployeeName:{EmployeeName}.Id:{ Id}", model.EmployeeName, model.Id);
|
||
var element = _employeeStorage.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(EmployeeBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
if (_employeeStorage.Insert(model) == null)
|
||
{
|
||
_logger.LogWarning("Insert operation failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public bool Delete(EmployeeBindingModel model)
|
||
{
|
||
CheckModel(model, false);
|
||
_logger.LogInformation("Delete. Id:{Id}", model.Id);
|
||
if (_employeeStorage.Delete(model) == null)
|
||
{
|
||
_logger.LogWarning("Delete operation failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public bool Update(EmployeeBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
if (_employeeStorage.Update(model) == null)
|
||
{
|
||
_logger.LogWarning("Update operation failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
private void CheckModel(EmployeeBindingModel model, bool withParams = true)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
if (!withParams)
|
||
{
|
||
return;
|
||
}
|
||
if (string.IsNullOrEmpty(model.EmployeeName))
|
||
{
|
||
throw new ArgumentNullException("Нет имени сотрудника",
|
||
nameof(model.EmployeeName));
|
||
}
|
||
if (model.EmployeePhone < 80000000000)
|
||
{
|
||
throw new ArgumentNullException("Введен некорректный номер телефона", nameof(model.EmployeePhone));
|
||
}
|
||
_logger.LogInformation("employee. EmployeeName:{EmployeeName}.EmployeePhone:{ EmployeePhone}. Id: { Id}", model.EmployeeName, model.EmployeePhone, model.Id);
|
||
var element = _employeeStorage.GetElement(new EmployeeSearchModel
|
||
{
|
||
EmployeeName = model.EmployeeName
|
||
});
|
||
if (element != null && element.Id != model.Id)
|
||
{
|
||
throw new InvalidOperationException("Сотрудник с таким названием уже есть");
|
||
}
|
||
}
|
||
}
|
||
}
|