10Г Егор Романов bb722556eb final 5
2023-05-01 14:00:18 +04:00

85 lines
2.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
using SecuritySystemContracts.BindingModels;
using SecuritySystemContracts.SearchModels;
using SecuritySystemContracts.StoragesContracts;
using SecuritySystemContracts.ViewModels;
using SecuritySystemFileImplement.Models;
using SecuritySystemFileImplement;
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;
}
Secure.Update(model);
source.SaveSecures();
return Secure.GetViewModel;
}
}
}