PIbd-23_Leonteva_V._A._LawFirm/LawFirm/LawFirmFileImplement/Implements/BlankStorage.cs

79 lines
2.5 KiB
C#
Raw Normal View History

2024-04-22 22:48:19 +04:00
using LawFirmContracts.BindingModels;
using LawFirmContracts.SearchModels;
using LawFirmContracts.StoragesContracts;
using LawFirmContracts.ViewModels;
using LawFirmFileImplement.Models;
namespace LawFirmFileImplement.Implements
{
public class BlankStorage : IBlankStorage
{
private readonly DataFileSingleton source;
public BlankStorage()
{
source = DataFileSingleton.GetInstance();
}
public List<BlankViewModel> GetFullList()
{
return source.Blanks.Select(x => x.GetViewModel).ToList();
}
public List<BlankViewModel> GetFilteredList(BlankSearchModel model)
{
if (string.IsNullOrEmpty(model.BlankName))
{
return new();
}
return source.Blanks
.Where(x => x.BlankName.Contains(model.BlankName))
.Select(x => x.GetViewModel)
.ToList();
}
public BlankViewModel? GetElement(BlankSearchModel model)
{
if (string.IsNullOrEmpty(model.BlankName) && !model.Id.HasValue)
{
return null;
}
return source.Blanks
.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.BlankName) &&
x.BlankName == model.BlankName) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public BlankViewModel? Insert(BlankBindingModel model)
{
model.Id = source.Blanks.Count > 0 ? source.Blanks.Max(x => x.Id) + 1 : 1;
var newBlank = Blank.Create(model);
if (newBlank == null)
{
return null;
}
source.Blanks.Add(newBlank);
source.SaveBlanks();
return newBlank.GetViewModel;
}
public BlankViewModel? Update(BlankBindingModel model)
{
var component = source.Blanks.FirstOrDefault(x => x.Id == model.Id);
if (component == null)
{
return null;
}
component.Update(model);
source.SaveBlanks();
return component.GetViewModel;
}
public BlankViewModel? Delete(BlankBindingModel model)
{
var element = source.Blanks.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
source.Blanks.Remove(element);
source.SaveBlanks();
return element.GetViewModel;
}
return null;
}
}
}