PIbd-21_Potapov_N.S._Securi.../SecuritySystem/SecuritySystemFileImplement/Implements/SecureStorage.cs

82 lines
2.6 KiB
C#

using SecuritySystemContracts.BindingModels;
using SecuritySystemContracts.SearchModels;
using SecuritySystemContracts.StoragesContracts;
using SecuritySystemContracts.ViewModels;
using SecuritySystemFileImplement.Models;
namespace SecuritySystemFileImplement.Implements
{
public class SecureStorage : ISecureStorage
{
private readonly DataFileSingleton source;
public SecureStorage()
{
source = DataFileSingleton.GetInstance();
}
public SecureViewModel? GetElement(SecureSearchModel model)
{
if (string.IsNullOrEmpty(model.SecureName) && !model.Id.HasValue)
{
return null;
}
return source.Secures
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.SecureName) && x.SecureName == model.SecureName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public List<SecureViewModel> GetFilteredList(SecureSearchModel model)
{
if (string.IsNullOrEmpty(model.SecureName))
{
return new();
}
return source.Secures
.Where(x => x.SecureName.Contains(model.SecureName))
.Select(x => x.GetViewModel)
.ToList();
}
public List<SecureViewModel> GetFullList()
{
return source.Secures.Select(x => x.GetViewModel).ToList();
}
public SecureViewModel? Insert(SecureBindingModel model)
{
model.Id = source.Secures.Count > 0 ? source.Secures.Max(x => x.Id) + 1 : 1;
var newSecure = Secure.Create(model);
if (newSecure == null)
{
return null;
}
source.Secures.Add(newSecure);
source.SaveSecures();
return newSecure.GetViewModel;
}
public SecureViewModel? Update(SecureBindingModel model)
{
var secure = source.Secures.FirstOrDefault(x => x.Id == model.Id);
if (secure == null)
{
return null;
}
secure.Update(model);
source.SaveSecures();
return secure.GetViewModel;
}
public SecureViewModel? Delete(SecureBindingModel model)
{
var secure = source.Secures.FirstOrDefault(x => x.Id == model.Id);
if (secure == null)
{
return null;
}
source.Secures.Remove(secure);
source.SaveSecures();
return secure.GetViewModel;
}
}
}