Coursework_ComputerStore_Li.../ComputerStoreBusinessLogic/BusinessLogic/EmployeeLogic.cs

123 lines
4.0 KiB
C#

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)
{
CheckEmployee(model);
if (_employeeStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(EmployeeBindingModel model)
{
CheckEmployee(model);
if (_employeeStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(EmployeeBindingModel model)
{
CheckEmployee(model, false);
_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;
}
public void CheckEmployee(EmployeeBindingModel model, bool withParams = true)
{
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));
}
_logger.LogInformation("Client. Username:{ Username}. ID: { ID} ", model.Username, model.ID);
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.");
}
}
}
}