117 lines
3.1 KiB
C#
117 lines
3.1 KiB
C#
using MotorPlantContracts.StoragesContracts;
|
|
using MotorPlantContracts.BindingModels;
|
|
using MotorPlantContracts.SearchModels;
|
|
using MotorPlantContracts.VeiwModels;
|
|
using MotorPlantListImplement.Models;
|
|
|
|
|
|
namespace MotorPlantListImplement.Implements
|
|
{
|
|
public class EngineStorage : IEngineStorage
|
|
{
|
|
private readonly DataListSingleton _source;
|
|
|
|
public EngineStorage()
|
|
{
|
|
_source = DataListSingleton.GetInstance();
|
|
}
|
|
public EngineViewModel? Delete(EngineBindingModel model)
|
|
{
|
|
for (int i = 0; i < _source.Engines.Count; ++i)
|
|
{
|
|
if (_source.Engines[i].Id == model.Id)
|
|
{
|
|
var element = _source.Engines[i];
|
|
_source.Engines.RemoveAt(i);
|
|
return element.GetViewModel;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
public EngineViewModel? GetElement(EngineSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.EngineName) && !model.Id.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
foreach (var package in _source.Engines)
|
|
{
|
|
if ((!string.IsNullOrEmpty(model.EngineName) && package.EngineName == model.EngineName) || (model.Id.HasValue && package.Id == model.Id))
|
|
{
|
|
return package.GetViewModel;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
public List<EngineViewModel> GetFilteredList(EngineSearchModel model)
|
|
{
|
|
var result = new List<EngineViewModel>();
|
|
|
|
if (string.IsNullOrEmpty(model.EngineName))
|
|
{
|
|
return result;
|
|
}
|
|
|
|
foreach (var package in _source.Engines)
|
|
{
|
|
if (package.EngineName.Contains(model.EngineName))
|
|
{
|
|
result.Add(package.GetViewModel);
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
public List<EngineViewModel> GetFullList()
|
|
{
|
|
var result = new List<EngineViewModel>();
|
|
|
|
foreach (var package in _source.Engines)
|
|
{
|
|
result.Add(package.GetViewModel);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
public EngineViewModel? Insert(EngineBindingModel model)
|
|
{
|
|
model.Id = 1;
|
|
|
|
foreach (var package in _source.Engines)
|
|
{
|
|
if (model.Id <= package.Id)
|
|
{
|
|
model.Id = package.Id + 1;
|
|
}
|
|
}
|
|
|
|
var newEngine = Engine.Create(model);
|
|
|
|
if (newEngine == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
_source.Engines.Add(newEngine);
|
|
|
|
return newEngine.GetViewModel;
|
|
}
|
|
public EngineViewModel? Update(EngineBindingModel model)
|
|
{
|
|
foreach (var package in _source.Engines)
|
|
{
|
|
if (package.Id == model.Id)
|
|
{
|
|
package.Update(model);
|
|
return package.GetViewModel;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|
|
}
|