2023-04-01 22:41:11 +04:00
|
|
|
|
using CarServiceContracts.BindingModels;
|
|
|
|
|
using CarServiceContracts.BusinessLogicsContracts;
|
|
|
|
|
using CarServiceContracts.SearchModels;
|
|
|
|
|
using CarServiceContracts.StorageContracts;
|
|
|
|
|
using CarServiceContracts.ViewModels;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
|
|
|
|
namespace CarServiceBusinessLogic.BusinessLogics
|
|
|
|
|
{
|
|
|
|
|
public class WorkerLogic : IWorkerLogic
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
2023-04-04 22:42:28 +04:00
|
|
|
|
private readonly IWorkerStorage _workerStorage;
|
|
|
|
|
public WorkerLogic(ILogger<WorkerLogic> logger, IWorkerStorage workerStorage)
|
2023-04-01 22:41:11 +04:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
2023-04-04 22:42:28 +04:00
|
|
|
|
_workerStorage = workerStorage;
|
2023-04-01 22:41:11 +04:00
|
|
|
|
}
|
|
|
|
|
public List<WorkerViewModel>? ReadList(WorkerSearchModel? model)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("ReadList. Id: {Id}", model?.Id);
|
2023-04-04 22:42:28 +04:00
|
|
|
|
var list = model == null ? _workerStorage.GetFullList() : _workerStorage.GetFilteredList(model);
|
2023-04-01 22:41:11 +04:00
|
|
|
|
if (list == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("ReadList return null list");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
public WorkerViewModel? ReadElement(WorkerSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("ReadElement. Id: {Id}", model.Id);
|
2023-04-04 22:42:28 +04:00
|
|
|
|
var element = _workerStorage.GetElement(model);
|
2023-04-01 22:41:11 +04:00
|
|
|
|
if (element == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("ReadElement element not found");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("ReadElement found. Id: {Id}", element.Id);
|
|
|
|
|
return element;
|
|
|
|
|
}
|
|
|
|
|
public bool Create(WorkerBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
2023-04-04 22:42:28 +04:00
|
|
|
|
if (_workerStorage.Insert(model) == null)
|
2023-04-01 22:41:11 +04:00
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Insert operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
public bool Update(WorkerBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
2023-04-04 22:42:28 +04:00
|
|
|
|
if (_workerStorage.Update(model) == null)
|
2023-04-01 22:41:11 +04:00
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Update operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
public bool Delete(WorkerBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model, false);
|
|
|
|
|
_logger.LogInformation("Delete. Id:{Id}", model.Id);
|
2023-04-04 22:42:28 +04:00
|
|
|
|
if (_workerStorage.Delete(model) == null)
|
2023-04-01 22:41:11 +04:00
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Delete operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
private void CheckModel(WorkerBindingModel model, bool withParams = true)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
if (!withParams)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("Worker. Id: {Id}", model.Id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|