PIbd-21_KozyrevSS_SewingDre.../SewingDresses/SewingDressesFileImplement/Implements/DressStorage.cs
2024-02-23 20:02:16 +04:00

72 lines
2.4 KiB
C#

using SewingDressesContracts.BindingModels;
using SewingDressesContracts.SearchModels;
using SewingDressesContracts.StoragesContracts;
using SewingDressesContracts.ViewModels;
using SewingDressesDataModels.Models;
using SewingDressesFileImplement.Models;
namespace SewingDressesFileImplement.Implements
{
public class DressStorage : IDressStorage
{
private readonly DataFileSingleton source;
public DressStorage()
{
source = DataFileSingleton.GetInstance();
}
public List<DressViewModel> GetFullList()
{
return source.Dresses.Select(x => x.GetViewModel).ToList();
}
public List<DressViewModel> GetFilteredList(DressSearchModel model)
{
return source.Dresses.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
}
public DressViewModel? GetElement(DressSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
return source.Dresses.FirstOrDefault(x => (!string.IsNullOrEmpty(model.DressName) && x.DressName == model.DressName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public DressViewModel? Insert(DressBindingModel model)
{
model.Id = source.Dresses.Count > 0 ? source.Dresses.Max(x => x.Id) + 1 : 1;
var newDress = Dress.Create(model);
if (newDress == null)
{
return null;
}
source.Dresses.Add(newDress);
source.SaveDresses();
return newDress.GetViewModel;
}
public DressViewModel? Update(DressBindingModel model)
{
var component = source.Dresses.FirstOrDefault(x => x.Id == model.Id);
if (component == null)
{
return null;
}
component.Update(model);
source.SaveDresses();
return component.GetViewModel;
}
public DressViewModel? Delete(DressBindingModel model)
{
var component = source.Dresses.FirstOrDefault(x => x.Id == model.Id);
if (component == null)
{
return null;
}
source.Dresses.Remove(component);
source.SaveDresses();
return component.GetViewModel;
}
}
}