Business Logic

This commit is contained in:
Daniel 2023-04-08 18:52:45 +04:00
parent efe1193cc6
commit 341de52e3d
9 changed files with 956 additions and 0 deletions

View File

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

View File

@ -0,0 +1,123 @@
using Microsoft.Extensions.Logging;
using FactoryContracts.BindingModels;
using FactoryContracts.BusinessLogicsContracts;
using FactoryContracts.SearchModels;
using FactoryContracts.StoragesContracts;
using FactoryContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FactoryBusinessLogic.BusinessLogics
{
public class EngenierLogic : IEngenierLogic
{
private readonly ILogger _logger;
private readonly IEngenierStorage _engenierStorage;
public EngenierLogic(ILogger<EngenierLogic> logger, IEngenierStorage engenierStorage) {
_logger = logger;
_engenierStorage = engenierStorage;
}
public bool Create(EngenierBindingModel model)
{
CheckModel(model);
if (_engenierStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(EngenierBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_engenierStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public EngenierViewModel? ReadElement(EngenierSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Name:{Name}.Surname:{Surname}.Patronymic:{Patronymic}.Id:{ Id}", model.Name, model.Surname, model.Patronymic, model.Id);
var element = _engenierStorage.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<EngenierViewModel>? ReadList(EngenierSearchModel? model)
{
_logger.LogInformation("ReadList. EngenierId:{Id}", model?.Id);
var list = model == null ? _engenierStorage.GetFullList() : _engenierStorage.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(EngenierBindingModel model)
{
CheckModel(model);
if (_engenierStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(EngenierBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Fio))
{
throw new ArgumentNullException("Нет имени пользователя", nameof(model.Fio));
}
if (string.IsNullOrEmpty(model.Email))
{
throw new ArgumentNullException("Нет почты пользователя", nameof(model.Email));
}
if (string.IsNullOrEmpty(model.Password))
{
throw new ArgumentNullException("Нет пароля пользователя", nameof(model.Password));
}
_logger.LogInformation("Engenier. Name:{Fio}.Email:{Email}.Password:{Password}.Id:{Id}",
model.Fio, model.Email, model.Password, model.Id);
var element = _engenierStorage.GetElement(new EngenierSearchModel
{
Name = model.Fio,
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Клиент с таким именем уже есть");
}
}
}
}

View File

@ -0,0 +1,99 @@
using FactoryContracts.BusinessLogicsContracts;
using FactoryContracts.StoragesContracts;
using Microsoft.Extensions.Logging;
using FactoryContracts.BindingModels;
using FactoryContracts.ViewModels;
using FactoryContracts.SearchModels;
namespace FactoryBusinessLogic.BusinessLogics
{
public class LatheBusyLogic : ILatheBusyLogic
{
private readonly ILogger _logger;
private readonly ILatheBusyStorage _latheBusyStorage;
public LatheBusyLogic(ILogger<ComponentLogic> logger, ILatheBusyStorage latheBusyStorage)
{
_logger = logger;
_latheBusyStorage = latheBusyStorage;
}
public List<LatheBusyViewModel>? ReadList(LatheBusySearchModel? model)
{
_logger.LogInformation("ReadList. Percent:{ComponentName}.Id:{ Id}", model?.Percent, model?.Id);
var list = model == null ? _latheBusyStorage.GetFullList() : _latheBusyStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public LatheBusyViewModel? ReadElement(LatheBusySearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. ComponentName:{Percent}.Id:{ Id}", model.Percent, model.Id);
var element = _latheBusyStorage.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(LatheBusyBindingModel model)
{
CheckModel(model);
if (_latheBusyStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(LatheBusyBindingModel model)
{
CheckModel(model);
if (_latheBusyStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(LatheBusyBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_latheBusyStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(LatheBusyBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
_logger.LogInformation("Component. ComponentName:{ComponentName}.Id: { Id}", model.Percent, model.Id);
var element = _latheBusyStorage.GetElement(new LatheBusySearchModel
{
Percent = model.Percent
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Компонент с таким названием уже есть");
}
}
}
}

View File

@ -0,0 +1,103 @@
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("Компонент с таким названием уже есть");
}
}
}
}

View File

@ -0,0 +1,150 @@
using FactoryContracts.BindingModels;
using FactoryContracts.BusinessLogicsContracts;
using FactoryContracts.SearchModels;
using FactoryContracts.StoragesContracts;
using FactoryContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FactoryBusinessLogic.BusinessLogics
{
public class MasterLogic : IMasterLogic
{
private readonly ILogger _logger;
private readonly IMasterStorage _masterStorage;
public MasterLogic(ILogger<MasterLogic> logger, IMasterStorage masterStorage)
{
_logger = logger;
_masterStorage = masterStorage;
}
public MasterViewModel? ReadElement(MasterSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. MasterName:{Name}. MasterSurname:{Surname}. MasterPatronymic:{Patronymic}. Id:{Id}",
model.Name, model.Surname, model.Patronymic, model?.Id);
var element = _masterStorage.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<MasterViewModel>? ReadList(MasterSearchModel? model)
{
_logger.LogInformation("ReadList. MasterName:{Name}. MasterSurname:{Surname}. MasterPatronymic:{Patronymic}. Id:{Id}",
model.Name, model.Surname, model.Patronymic, model?.Id);
var list = model == null ? _masterStorage.GetFullList() : _masterStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public bool Create(MasterBindingModel model)
{
CheckModel(model);
if (_masterStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(MasterBindingModel model)
{
CheckModel(model);
if (_masterStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(MasterBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_masterStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(MasterBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Fio))
{
throw new ArgumentNullException("Отсутствие имени в учётной записи", nameof(model.Fio));
}
if (string.IsNullOrEmpty(model.Email))
{
throw new ArgumentNullException("Отсутствие почты в учётной записи (логина)", nameof(model.Email));
}
if (string.IsNullOrEmpty(model.Password))
{
throw new ArgumentNullException("Отсутствие пароля в учётной записи", nameof(model.Password));
}
_logger.LogInformation("Master. MasterName:{Fio}.Email:{Email}.Password:{Password}.Id:{Id}",
model.Fio, model.Email, model.Password, model.Id);
var element = _masterStorage.GetElement(new MasterSearchModel
{
Email = model.Email,
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Аккаунт с таким логином уже есть");
}
}
}
}

View File

@ -0,0 +1,103 @@
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;
public PlanLogic(ILogger<PlanLogic> logger, IPlanStorage planStorage)
{
_logger = logger;
_planStorage = planStorage;
}
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));
}
_logger.LogInformation("ReadElement. ComponentName:{PlanName}.Id:{ Id}", model.PlanName, model.Id);
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("Компонент с таким названием уже есть");
}
}
}
}

View File

@ -0,0 +1,107 @@
using Microsoft.Extensions.Logging;
using FactoryContracts.BindingModels;
using FactoryContracts.BusinessLogicsContracts;
using FactoryContracts.SearchModels;
using FactoryContracts.StoragesContracts;
using FactoryContracts.ViewModels;
namespace FactoryBusinessLogic.BusinessLogics
{
public class ReinforcedLogic : IReinforcedLogic
{
private readonly ILogger _logger;
private readonly IReinforcedStorage _reinforcedStorage;
public ReinforcedLogic(ILogger<ReinforcedLogic> logger, IReinforcedStorage reinforcedStorage)
{
_logger = logger;
_reinforcedStorage= reinforcedStorage;
}
public List<ReinforcedViewModel>? ReadList(ReinforcedSearchModel? model)
{
_logger.LogInformation("ReadList. ReinforcedName:{ReinforcedName}.Id:{ Id}", model?.ReinforcedName, model?.Id);
var list = model == null ? _reinforcedStorage.GetFullList() : _reinforcedStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public ReinforcedViewModel? ReadElement(ReinforcedSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. ReinforcedName:{ReinforcedName}.Id:{ Id}", model.ReinforcedName, model.Id);
var element = _reinforcedStorage.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(ReinforcedBindingModel model)
{
CheckModel(model);
if (_reinforcedStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(ReinforcedBindingModel model)
{
CheckModel(model);
if (_reinforcedStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(ReinforcedBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_reinforcedStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(ReinforcedBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.ReinforcedName))
{
throw new ArgumentNullException("Нет названия изделия", nameof(model.ReinforcedName));
}
if(model.ReinforcedComponents==null || model.ReinforcedComponents.Count == 0)
{
throw new ArgumentNullException("Перечень компонентов не может быть пустым", nameof(model.ReinforcedComponents));
}
_logger.LogInformation("Reinforced. ReinforcedName:{ReinforcedName}.Id: { Id}", model.ReinforcedName, model.Id);
var element = _reinforcedStorage.GetElement(new ReinforcedSearchModel
{
ReinforcedName = model.ReinforcedName
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Изделие с таким названием уже есть");
}
}
}
}

View File

@ -0,0 +1,69 @@

using FactoryContracts.BindingModels;
using FactoryContracts.BusinessLogicsContracts;
using FactoryContracts.SearchModels;
using FactoryContracts.StoragesContracts;
using FactoryContracts.ViewModels;
using PrecastConcretePlantContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace FactoryBusinessLogic.BusinessLogics
{
public class ReportLogic : IReportLogic
{
private readonly IComponentStorage _componentStorage;
private readonly IPlanStorage _planStorage;
private readonly ILatheStorage _latheStorage;
private readonly ILatheBusyStorage _latheBusyStorage;
public ReportLogic(IComponentStorage componentStorage, IPlanStorage planStorage, ILatheStorage latheStorage, ILatheBusyStorage latheBusyStorage)
{
_planStorage = planStorage;
_componentStorage = componentStorage;
_latheStorage = latheStorage;
_latheBusyStorage = latheBusyStorage;
}
public List<ReportLatheComponentViewModel> GetPlanLathesAndComponents(ReportBindingModel model)
{
return _planStorage.GetFilteredList(new PlanSearchModel { DateFrom = model.DateFrom, DateTo = model.DateTo})
.Select(x => new ReportLatheComponentViewModel {
PlanName = x.PlanName,
Components = x.PlanComponents.Select(x => (x.Value.Item1.ComponentName, x.Value.Item2)).ToList(),
Lathes = x.PlanLathes.Select(x => (x.Value.Item1.LatheName, x.Value.Item2)).ToList()
})
.ToList();
}
public List<ReportLatheViewModel> GetLatheByBusy(ReportBindingModel model)
{
return _latheBusyStorage.GetFilteredList(new LatheBusySearchModel { DateFrom = model.DateFrom, DateTo = model.DateTo })
.Select(x => new ReportLatheViewModel
{
Lathes = _latheStorage.GetFilteredList(new LatheSearchModel { BusyId = x.Id}).Select(y => y.LatheName).ToList(),
})
.ToList();
}
public void SaveOrdersToPdfFile(ReportBindingModel model)
{
throw new NotImplementedException();
}
public void SaveReinforcedComponentToExcelFile(ReportBindingModel model)
{
throw new NotImplementedException();
}
public void SaveReinforcedesToWordFile(ReportBindingModel model)
{
throw new NotImplementedException();
}
}
}

View File

@ -0,0 +1,99 @@
using FactoryContracts.BusinessLogicsContracts;
using FactoryContracts.StoragesContracts;
using Microsoft.Extensions.Logging;
using FactoryContracts.BindingModels;
using FactoryContracts.ViewModels;
using FactoryContracts.SearchModels;
namespace FactoryBusinessLogic.BusinessLogics
{
public class StageLogic : IStageLogic
{
private readonly ILogger _logger;
private readonly IStageStorage _stageStorage;
public StageLogic(ILogger<StageLogic> logger, IStageStorage stageStorage)
{
_logger = logger;
_stageStorage = stageStorage;
}
public List<StageViewModel>? ReadList(StageSearchModel? model)
{
_logger.LogInformation("ReadList. StageId:{Id}", model?.Id);
var list = model == null ? _stageStorage.GetFullList() : _stageStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public StageViewModel? ReadElement(StageSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. StageId:{Id}", model?.Id);
var element = _stageStorage.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(StageBindingModel model)
{
CheckModel(model);
if (_stageStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(StageBindingModel model)
{
CheckModel(model);
if (_stageStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(StageBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_stageStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(StageBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
_logger.LogInformation("Stage.Id:{Id}", model?.Id);
var element = _stageStorage.GetElement(new StageSearchModel
{
Id = model.Id
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Компонент с таким названием уже есть");
}
}
}
}