PIbd22_Kuznetsov_A.V._Sewin.../SewingDresses/SewingDressesFileImplement/Implements/ImplementStorage.cs

78 lines
2.3 KiB
C#

using SewingDressesContracts.StoragesContracts;
using SewingDressesContracts.BindingModels;
using SewingDressesContracts.SearchModels;
using SewingDressesContracts.ViewModels;
using SewingDressesFileImplement.Models;
namespace SewingDressesFileImplement.Implements
{
public class ImplementStorage : IImplementStorage
{
private readonly DataFileSingleton source;
public ImplementStorage()
{
source = DataFileSingleton.GetInstance();
}
public List<ImplementViewModel> GetFullList()
{
return source.Implements
.Select(x => x.GetViewModel)
.ToList();
}
public List<ImplementViewModel> GetFilteredList(ImplementSearchModel model)
{
if (string.IsNullOrEmpty(model.ImplementFIO) && string.IsNullOrEmpty(model.Password))
{
return new();
}
return source.Implements
.Where(x => (string.IsNullOrEmpty(model.ImplementFIO) || x.ImplementFIO.Contains(model.ImplementFIO)) &&
(string.IsNullOrEmpty(model.Password) || x.Password.Contains(model.Password)))
.Select(x => x.GetViewModel)
.ToList();
}
public ImplementViewModel? GetElement(ImplementSearchModel model)
{
return source.Implements
.FirstOrDefault(x => (string.IsNullOrEmpty(model.ImplementFIO) || x.ImplementFIO == model.ImplementFIO) &&
(!model.Id.HasValue || x.Id == model.Id) && (string.IsNullOrEmpty(model.Password) || x.Password == model.Password))
?.GetViewModel;
}
public ImplementViewModel? Insert(ImplementBindingModel model)
{
model.Id = source.Implements.Count > 0 ? source.Implements.Max(x =>
x.Id) + 1 : 1;
var newImplement = Implement.Create(model);
if (newImplement == null)
{
return null;
}
source.Implements.Add(newImplement);
source.SaveImplements();
return newImplement.GetViewModel;
}
public ImplementViewModel? Update(ImplementBindingModel model)
{
var Implement = source.Implements.FirstOrDefault(x => x.Id == model.Id);
if (Implement == null)
{
return null;
}
Implement.Update(model);
source.SaveImplements();
return Implement.GetViewModel;
}
public ImplementViewModel? Delete(ImplementBindingModel model)
{
var element = source.Implements.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
source.Implements.Remove(element);
source.SaveImplements();
return element.GetViewModel;
}
return null;
}
}
}