88 lines
2.1 KiB
C#
88 lines
2.1 KiB
C#
|
using Contracts.BindingModels;
|
|||
|
using Contracts.SearchModels;
|
|||
|
using Contracts.StoragesContracts;
|
|||
|
using Contracts.ViewModels;
|
|||
|
using FileImplements.Models;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
|
|||
|
namespace FileImplements.Implements
|
|||
|
{
|
|||
|
public class KeyStorage : IKeyStorage
|
|||
|
{
|
|||
|
private readonly DataFileSingleton source;
|
|||
|
public KeyStorage()
|
|||
|
{
|
|||
|
source = DataFileSingleton.GetInstance();
|
|||
|
}
|
|||
|
public List<KeyViewModel> GetFullList()
|
|||
|
{
|
|||
|
return source.Keys
|
|||
|
.Select(x => x.GetViewModel)
|
|||
|
.ToList();
|
|||
|
}
|
|||
|
public List<KeyViewModel> GetFilteredList(KeySearchModel model)
|
|||
|
{
|
|||
|
if (!string.IsNullOrEmpty(model.Name))
|
|||
|
{
|
|||
|
return source.Keys
|
|||
|
.Where(x => x.Name.Contains(model.Name))
|
|||
|
.Select(x => x.GetViewModel)
|
|||
|
.ToList();
|
|||
|
}
|
|||
|
return new();
|
|||
|
}
|
|||
|
public KeyViewModel? GetElement(KeySearchModel model)
|
|||
|
{
|
|||
|
if (model.Id.HasValue)
|
|||
|
{
|
|||
|
return source.Keys
|
|||
|
.FirstOrDefault(x => (x.Id == model.Id))?.GetViewModel;
|
|||
|
}
|
|||
|
else if (!string.IsNullOrEmpty(model.Name) && !string.IsNullOrEmpty(model.Path))
|
|||
|
{
|
|||
|
return source.Keys
|
|||
|
.FirstOrDefault(x => (x.Name == model.Name && x.Path == model.Path))?.GetViewModel;
|
|||
|
}
|
|||
|
return new();
|
|||
|
}
|
|||
|
public KeyViewModel? Insert(KeyBindingModel model)
|
|||
|
{
|
|||
|
model.Id = source.Keys.Count > 0 ? source.Keys.Max(x => x.Id) + 1 : 1;
|
|||
|
var newClient = Key.Create(model);
|
|||
|
if (newClient == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
source.Keys.Add(newClient);
|
|||
|
source.SaveKeys();
|
|||
|
return newClient.GetViewModel;
|
|||
|
}
|
|||
|
public KeyViewModel? Update(KeyBindingModel model)
|
|||
|
{
|
|||
|
var client = source.Keys.FirstOrDefault(x => x.Id == model.Id);
|
|||
|
if (client == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
client.Update(model);
|
|||
|
source.SaveKeys();
|
|||
|
return client.GetViewModel;
|
|||
|
}
|
|||
|
public KeyViewModel? Delete(KeyBindingModel model)
|
|||
|
{
|
|||
|
var client = source.Keys.FirstOrDefault(x => x.Id == model.Id);
|
|||
|
if (client != null)
|
|||
|
{
|
|||
|
source.Keys.Remove(client);
|
|||
|
source.SaveKeys();
|
|||
|
return client.GetViewModel;
|
|||
|
}
|
|||
|
return null;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|