Aparyan.ISE-22.MotorPlant/MotorPlantFileImplement/Implements/EngineStorage.cs
2024-03-24 01:07:16 +04:00

70 lines
2.5 KiB
C#

using MotorPlantContracts.BindingModels;
using MotorPlantContracts.SearchModels;
using MotorPlantContracts.StoragesContracts;
using MotorPlantContracts.VeiwModels;
using MotorPlantFileImplement.Models;
namespace MotorPlantFileImplement.Implements
{
public class EngineStorage : IEngineStorage
{
private readonly DataFileSingleton source;
public EngineStorage()
{
source = DataFileSingleton.GetInstance();
}
public List<EngineViewModel> GetFullList()
{
return source.Engines.Select(x => x.GetViewModel).ToList();
}
public List<EngineViewModel> GetFilteredList(EngineSearchModel model)
{
if (string.IsNullOrEmpty(model.EngineName))
{
return new();
}
return source.Engines.Where(x => x.EngineName.Contains(model.EngineName)).Select(x => x.GetViewModel).ToList();
}
public EngineViewModel? GetElement(EngineSearchModel model)
{
if (string.IsNullOrEmpty(model.EngineName) && !model.Id.HasValue)
{
return null;
}
return source.Engines.FirstOrDefault(x => (!string.IsNullOrEmpty(model.EngineName) && x.EngineName == model.EngineName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public EngineViewModel? Insert(EngineBindingModel model)
{
model.Id = source.Engines.Count > 0 ? source.Engines.Max(x => x.Id) + 1 : 1;
var newPizza = Engine.Create(model);
if (newPizza == null)
{
return null;
}
source.Engines.Add(newPizza);
source.SaveEngines();
return newPizza.GetViewModel;
}
public EngineViewModel? Update(EngineBindingModel model)
{
var Pizza = source.Engines.FirstOrDefault(x => x.Id == model.Id);
if (Pizza == null)
{
return null;
}
Pizza.Update(model);
source.SaveEngines();
return Pizza.GetViewModel;
}
public EngineViewModel? Delete(EngineBindingModel model)
{
var Pizza = source.Engines.FirstOrDefault(x => x.Id == model.Id);
if (Pizza != null)
{
source.Engines.Remove(Pizza);
source.SaveEngines();
return Pizza.GetViewModel;
}
return null;
}
}
}