138 lines
4.3 KiB
C#
138 lines
4.3 KiB
C#
using PlumbingRepairContracts.BindingModels;
|
||
using PlumbingRepairContracts.BusinessLogicsContracts;
|
||
using PlumbingRepairContracts.SearchModels;
|
||
using PlumbingRepairContracts.StoragesContracts;
|
||
using PlumbingRepairContracts.ViewModels;
|
||
using Microsoft.Extensions.Logging;
|
||
|
||
namespace PlumbingRepairBusinessLogic.BusinessLogic
|
||
{
|
||
public class WorkLogic : IWorkLogic
|
||
{
|
||
private readonly ILogger _logger;
|
||
|
||
private readonly IWorkStorage _workStorage;
|
||
|
||
public WorkLogic(ILogger<WorkLogic> logger, IWorkStorage workStorage)
|
||
{
|
||
_logger = logger;
|
||
_workStorage = workStorage;
|
||
}
|
||
|
||
public bool Create(WorkBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
|
||
if (_workStorage.Insert(model) == null)
|
||
{
|
||
_logger.LogWarning("Insert operation failed");
|
||
return false;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
public bool Delete(WorkBindingModel model)
|
||
{
|
||
CheckModel(model, false);
|
||
|
||
_logger.LogInformation("Delete. Id:{Id}", model.Id);
|
||
|
||
if (_workStorage.Delete(model) == null)
|
||
{
|
||
_logger.LogWarning("Delete operation failed");
|
||
return false;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
public WorkViewModel? ReadElement(WorkSearchModel model)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
|
||
_logger.LogInformation("ReadElement. WorkName:{WorkName}.Id:{ Id}", model.WorkName, model.Id);
|
||
|
||
var element = _workStorage.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<WorkViewModel>? ReadList(WorkSearchModel? model)
|
||
{
|
||
_logger.LogInformation("ReadList. WorkName:{WorkName}.Id:{ Id}", model?.WorkName, model?.Id);
|
||
|
||
var list = model == null ? _workStorage.GetFullList() : _workStorage.GetFilteredList(model);
|
||
|
||
if (list == null)
|
||
{
|
||
_logger.LogWarning("ReadList return null list");
|
||
return null;
|
||
}
|
||
|
||
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
|
||
|
||
return list;
|
||
}
|
||
|
||
public bool Update(WorkBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
|
||
if (_workStorage.Update(model) == null)
|
||
{
|
||
_logger.LogWarning("Update operation failed");
|
||
return false;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
private void CheckModel(WorkBindingModel model, bool withParams = true)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
|
||
if (!withParams)
|
||
{
|
||
return;
|
||
}
|
||
|
||
if (string.IsNullOrEmpty(model.WorkName))
|
||
{
|
||
throw new ArgumentNullException("Нет названия изделия", nameof(model.WorkName));
|
||
}
|
||
|
||
if (model.Price <= 0)
|
||
{
|
||
throw new ArgumentNullException("Цена изделия должна быть больше 0", nameof(model.Price));
|
||
}
|
||
|
||
_logger.LogInformation("Work. WorkName:{WorkName}.Price:{ Cost}. Id: { Id}", model.WorkName, model.Price, model.Id);
|
||
|
||
var element = _workStorage.GetElement(new WorkSearchModel
|
||
{
|
||
WorkName = model.WorkName
|
||
});
|
||
|
||
if (element != null && element.Id != model.Id)
|
||
{
|
||
throw new InvalidOperationException("Изделие с таким названием уже есть");
|
||
}
|
||
}
|
||
}
|
||
}
|