CourseWork_Factory/FactoryView/FactoryBusinessLogic/BusinessLogics/PlanLogic.cs

160 lines
5.6 KiB
C#
Raw Normal View History

2023-04-08 18:52:45 +04:00
using FactoryContracts.BusinessLogicsContracts;
using FactoryContracts.StoragesContracts;
using Microsoft.Extensions.Logging;
using FactoryContracts.BindingModels;
using FactoryContracts.ViewModels;
using FactoryContracts.SearchModels;
namespace FactoryBusinessLogic.BusinessLogics
{
public class PlanLogic : IPlanLogic
{
private readonly ILogger _logger;
private readonly IPlanStorage _planStorage;
2023-05-19 21:35:04 +04:00
private readonly IComponentStorage componentStorage;
private readonly ILatheStorage latheStorage;
public PlanLogic(ILogger<PlanLogic> logger, IPlanStorage planStorage, IComponentStorage componentStorage, ILatheStorage latheStorage)
2023-04-08 18:52:45 +04:00
{
_logger = logger;
_planStorage = planStorage;
2023-05-19 21:35:04 +04:00
this.componentStorage = componentStorage;
this.latheStorage = latheStorage;
2023-04-08 18:52:45 +04:00
}
public List<PlanViewModel>? ReadList(PlanSearchModel? model)
{
_logger.LogInformation("ReadList. PlanName:{PlanName}.Id:{ Id}", model?.PlanName, model?.Id);
var list = model == null ? _planStorage.GetFullList() : _planStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public PlanViewModel? ReadElement(PlanSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
2023-05-19 21:35:04 +04:00
_logger.LogInformation("ReadElement. ComponentName:{PlanName}.Id:{ Id}", model?.PlanName, model.Id);
2023-04-08 18:52:45 +04:00
var element = _planStorage.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(PlanBindingModel model)
{
CheckModel(model);
if (_planStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(PlanBindingModel model)
{
CheckModel(model);
if (_planStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(PlanBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_planStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(PlanBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.PlanName))
{
throw new ArgumentNullException("Нет названия плана", nameof(model.PlanName));
}
_logger.LogInformation("Component. PlanName:{PlanName}.Id: { Id}", model.PlanName, model.Id);
var element = _planStorage.GetElement(new PlanSearchModel
{
PlanName = model.PlanName
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Компонент с таким названием уже есть");
}
}
2023-05-19 21:35:04 +04:00
public bool addComponent(int planId, int ComponentId)
{
try
{
var plan = _planStorage.GetElement(new PlanSearchModel() { Id = planId });
var model = new PlanBindingModel()
{
PlanName = plan.PlanName,
Id = plan.Id,
PlanLathes = plan.PlanLathes,
date = plan.date,
PlanComponents = plan.PlanComponents
};
var component = componentStorage.GetElement(new ComponentSearchModel() { Id = ComponentId });
model.PlanComponents.Add(component.Id, component);
Update(model);
return true;
}
catch (Exception ex)
{
return false;
}
}
public bool addLathe(int planId, int latheId)
{
try
{
var plan = _planStorage.GetElement(new PlanSearchModel() { Id = planId });
var model = new PlanBindingModel()
{
PlanName = plan.PlanName,
Id = plan.Id,
PlanLathes = plan.PlanLathes,
date = plan.date,
PlanComponents = plan.PlanComponents
};
var component = latheStorage.GetElement(new LatheSearchModel() { Id = latheId });
model.PlanLathes.Add(component.Id, component);
Update(model);
return true;
}
catch (Exception ex)
{
return false;
}
}
2023-04-08 18:52:45 +04:00
}
}