PIbd-21_Ihonkina_E.S._Preca.../PrecastConcreteFileImplement/Implements/ReinforcedStorage.cs

95 lines
2.8 KiB
C#
Raw Normal View History

2023-03-18 17:55:02 +04:00
using PrecastConcretePlantContracts.BindingModels;
using PrecastConcretePlantContracts.SearchModels;
using PrecastConcretePlantContracts.StoragesContracts;
using PrecastConcretePlantContracts.ViewModels;
using PrecastConcretePlantFileImplement.Models;
using System;
2023-03-18 17:47:29 +04:00
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PrecastConcretePlantFileImplement.Implements
{
2023-03-18 17:55:02 +04:00
public class ReinforcedStorage : IReinforcedStorage
2023-03-18 17:47:29 +04:00
{
2023-03-18 17:55:02 +04:00
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;
}
2023-03-18 17:47:29 +04:00
}
}