PIbd-22_Smirnov_A.A._Securi.../SecuritySystem/SecuritySystemFileImplement/Implements/SecureStorage.cs
2024-05-07 16:18:43 +04:00

98 lines
2.7 KiB
C#

using SecuritySystemContracts.BindingModels;
using SecuritySystemContracts.SearchModels;
using SecuritySystemContracts.StoragesContracts;
using SecuritySystemContracts.ViewModels;
using SecuritySystemFileImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SecuritySystemFileImplement.Implements
{
//реализация интерфейса хранилища изделий
public class SecureStorage : ISecureStorage
{
private readonly DataFileSingleton source;
public SecureStorage()
{
source = DataFileSingleton.GetInstance();
}
public List<SecureViewModel> GetFullList()
{
return source.Secures.Select(x => x.GetViewModel).ToList();
}
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 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 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 element = source.Secures.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
source.Secures.Remove(element);
source.SaveSecures();
return element.GetViewModel;
}
return null;
}
}
}