EL_Singature/FileImplements/Implements/CertStorage.cs

89 lines
1.9 KiB
C#

using Contracts.BindingModels;
using Contracts.SearchModels;
using Contracts.ViewModels;
using Contracts.StoragesContracts;
using FileImplements.Models;
namespace FileImplements.Implements
{
public class CertStorage : ICertStorage
{
private readonly DataFileSingleton source;
public CertStorage()
{
source = DataFileSingleton.GetInstance();
}
public List<CertViewModel> GetFullList()
{
return source.Certs.Select(x => x.GetViewModel).ToList();
}
public List<CertViewModel> GetFilteredList(CertSearchModel model)
{
if (string.IsNullOrEmpty(model.Name))
{
return new();
}
return source.Certs.Where(x => x.Name.Contains(model.Name)).Select(x => x.GetViewModel).ToList();
}
public CertViewModel? GetElement(CertSearchModel model)
{
if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue)
{
return null;
}
return source.Certs.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public CertViewModel? Insert(CertBindingModel model)
{
model.Id = source.Certs.Count > 0 ? source.Certs.Max(x => x.Id) + 1 : 1;
var newCert = Cert.Create(model);
if (newCert == null)
{
return null;
}
source.Certs.Add(newCert);
source.SaveCerts();
return newCert.GetViewModel;
}
public CertViewModel? Update(CertBindingModel model)
{
var Cert = source.Certs.FirstOrDefault(x => x.Id == model.Id);
if (Cert == null)
{
return null;
}
Cert.Update(model);
source.SaveCerts();
return Cert.GetViewModel;
}
public CertViewModel? Delete(CertBindingModel model)
{
var element = source.Certs.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
source.Certs.Remove(element);
source.SaveCerts();
return element.GetViewModel;
}
return null;
}
}
}