PIbd-21_Ihonkina_E.S._Preca.../PrecastConcreteFileImplement/Implements/ReinforcedStorage.cs
Катя Ихонкина 9583e8dbee 2лр коммит3
2023-03-18 17:55:02 +04:00

95 lines
2.8 KiB
C#

using PrecastConcretePlantContracts.BindingModels;
using PrecastConcretePlantContracts.SearchModels;
using PrecastConcretePlantContracts.StoragesContracts;
using PrecastConcretePlantContracts.ViewModels;
using PrecastConcretePlantFileImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PrecastConcretePlantFileImplement.Implements
{
public class ReinforcedStorage : IReinforcedStorage
{
private readonly DataFileSingleton source;
public ReinforcedStorage()
{
source = DataFileSingleton.GetInstance();
}
public ReinforcedViewModel? Delete(ReinforcedBindingModel model)
{
var element = source.Reinforceds.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
source.Reinforceds.Remove(element);
source.SaveReinforceds();
return element.GetViewModel;
}
return null;
}
public ReinforcedViewModel? GetElement(ReinforcedSearchModel model)
{
if (string.IsNullOrEmpty(model.ReinforcedName) && !model.Id.HasValue)
{
return null;
}
return source.Reinforceds.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ReinforcedName) && x.ReinforcedName == model.ReinforcedName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public List<ReinforcedViewModel> GetFilteredList(ReinforcedSearchModel model)
{
if (string.IsNullOrEmpty(model.ReinforcedName))
{
return new();
}
return source.Reinforceds.Where(x => x.ReinforcedName.Contains(model.ReinforcedName)).Select(x => x.GetViewModel).ToList();
}
public List<ReinforcedViewModel> GetFullList()
{
return source.Reinforceds.Select(x => x.GetViewModel).ToList();
}
public ReinforcedViewModel? Insert(ReinforcedBindingModel model)
{
model.Id = source.Reinforceds.Count > 0 ? source.Reinforceds.Max(x => x.Id) + 1 : 1;
var newReinforced = Reinforced.Create(model);
if (newReinforced == null)
{
return null;
}
source.Reinforceds.Add(newReinforced);
source.SaveReinforceds();
return newReinforced.GetViewModel;
}
public ReinforcedViewModel? Update(ReinforcedBindingModel model)
{
var reinforceds = source.Reinforceds.FirstOrDefault(x => x.Id == model.Id);
if (reinforceds == null)
{
return null;
}
reinforceds.Update(model);
source.SaveReinforceds();
return reinforceds.GetViewModel;
}
}
}