2023-04-05 21:47:56 +04:00
|
|
|
|
using ComputerStoreContracts.BindingModels;
|
|
|
|
|
using ComputerStoreContracts.BusinessLogicContracts;
|
|
|
|
|
using ComputerStoreContracts.SearchModels;
|
|
|
|
|
using ComputerStoreContracts.StorageContracts;
|
|
|
|
|
using ComputerStoreContracts.ViewModels;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace ComputerStoreBusinessLogic.BusinessLogic
|
|
|
|
|
{
|
|
|
|
|
public class EmployeeLogic : IEmployeeLogic
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly IEmployeeStorage _employeeStorage;
|
|
|
|
|
public EmployeeLogic(ILogger<EmployeeLogic> logger, IEmployeeStorage employeeStorage)
|
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_employeeStorage = employeeStorage;
|
|
|
|
|
}
|
|
|
|
|
public bool Create(EmployeeBindingModel model)
|
|
|
|
|
{
|
2023-04-06 17:00:40 +04:00
|
|
|
|
CheckEmployee(model);
|
2023-04-05 21:47:56 +04:00
|
|
|
|
if (_employeeStorage.Insert(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Insert operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Update(EmployeeBindingModel model)
|
|
|
|
|
{
|
2023-04-06 17:00:40 +04:00
|
|
|
|
CheckEmployee(model);
|
2023-04-05 21:47:56 +04:00
|
|
|
|
if (_employeeStorage.Update(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Update operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Delete(EmployeeBindingModel model)
|
|
|
|
|
{
|
2023-04-06 17:00:40 +04:00
|
|
|
|
CheckEmployee(model, false);
|
2023-04-05 21:47:56 +04:00
|
|
|
|
_logger.LogInformation("Delete. ID:{ID}", model.ID);
|
|
|
|
|
if (_employeeStorage.Delete(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Delete operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public EmployeeViewModel? ReadElement(EmployeeSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("ReadElement. Username:{Username}.ID:{ ID}", model.Username, 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 List<EmployeeViewModel>? ReadList(EmployeeSearchModel? model)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("ReadList. Username:{Username}. ID:{ ID}", model?.Username, 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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-06 17:00:40 +04:00
|
|
|
|
public void CheckEmployee(EmployeeBindingModel model, bool withParams = true)
|
2023-04-05 21:47:56 +04:00
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
if (!withParams)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (string.IsNullOrEmpty(model.Username))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Invalid username of user", nameof(model.Username));
|
|
|
|
|
}
|
|
|
|
|
if (string.IsNullOrEmpty(model.Password))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Invalid password of user", nameof(model.Password));
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-06 17:00:40 +04:00
|
|
|
|
_logger.LogInformation("Client. Username:{ Username}. ID: { ID} ", model.Username, model.ID);
|
2023-04-05 21:47:56 +04:00
|
|
|
|
|
|
|
|
|
var element = _employeeStorage.GetElement(new EmployeeSearchModel
|
|
|
|
|
{
|
|
|
|
|
Username = model.Username
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (element != null && element.ID != model.ID)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Employee with such username already exists.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|