CourseWork_Factory/FactoryView/FactoryBusinessLogic/BusinessLogics/LatheLogic.cs
2023-04-08 18:52:45 +04:00

104 lines
3.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using FactoryContracts.BusinessLogicsContracts;
using FactoryContracts.StoragesContracts;
using Microsoft.Extensions.Logging;
using FactoryContracts.BindingModels;
using FactoryContracts.ViewModels;
using FactoryContracts.SearchModels;
namespace FactoryBusinessLogic.BusinessLogics
{
public class LatheLogic : ILatheLogic
{
private readonly ILogger _logger;
private readonly ILatheStorage _latheStorage;
public LatheLogic(ILogger<ComponentLogic> logger, ILatheStorage latheStorage)
{
_logger = logger;
_latheStorage = latheStorage;
}
public List<LatheViewModel>? ReadList(LatheSearchModel? model)
{
_logger.LogInformation("ReadList. LatheName:{LatheName}.Id:{ Id}", model?.LatheName, model?.Id);
var list = model == null ? _latheStorage.GetFullList() : _latheStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public LatheViewModel? ReadElement(LatheSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. ComponentName:{LatheName}.Id:{ Id}", model.LatheName, model.Id);
var element = _latheStorage.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(LatheBindingModel model)
{
CheckModel(model);
if (_latheStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(LatheBindingModel model)
{
CheckModel(model);
if (_latheStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(LatheBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_latheStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(LatheBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.LatheName))
{
throw new ArgumentNullException("Нет названия cтанка", nameof(model.LatheName));
}
_logger.LogInformation("Component. ComponentName:{LatheName}.Id: { Id}", model.LatheName, model.Id);
var element = _latheStorage.GetElement(new LatheSearchModel
{
LatheName = model.LatheName
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Компонент с таким названием уже есть");
}
}
}
}