PIbd-23-Lisov-N.A-SewingDre.../DressAtelierListImplement/Implements/ClientStorage.cs

114 lines
3.2 KiB
C#

using DressAtelierContracts.BindingModels;
using DressAtelierContracts.SearchModels;
using DressAtelierContracts.StorageContracts;
using DressAtelierContracts.ViewModels;
using DressAtelierListImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace DressAtelierListImplement.Implements
{
public class ClientStorage : IClientStorage
{
private readonly DataListSingleton _source;
public ClientStorage()
{
_source = DataListSingleton.GetInstance();
}
public ClientViewModel? Insert(ClientBindingModel model)
{
model.ID = 1;
foreach(var client in _source.Clients)
{
if(model.ID <= client.ID)
{
model.ID = client.ID + 1;
}
}
var newClient = Client.Create(model);
if(newClient == null) { return null; }
_source.Clients.Add(newClient);
return newClient.GetViewModel;
}
public ClientViewModel? Update(ClientBindingModel model)
{
foreach(var client in _source.Clients)
{
if(client.ID == model.ID)
{
client.Update(model);
return client.GetViewModel;
}
}
return null;
}
public ClientViewModel? Delete(ClientBindingModel model)
{
for(int i = 0 ; i< _source.Clients.Count;i++)
{
if(model.ID == _source.Clients[i].ID)
{
var element = _source.Clients[i];
_source.Clients.RemoveAt(i);
return element.GetViewModel;
}
}
return null;
}
public ClientViewModel? GetElement(ClientSearchModel model)
{
if(string.IsNullOrEmpty(model.Email) && !model.ID.HasValue)
{
return null;
}
foreach(var client in _source.Clients)
{
if ((!string.IsNullOrEmpty(model.Email) && model.Email == client.Email) || (model.ID.HasValue && model.ID.Value == client.ID))
{
return client.GetViewModel;
}
}
return null;
}
public List<ClientViewModel> GetFilteredList(ClientSearchModel model)
{
var result = new List<ClientViewModel>();
if (string.IsNullOrEmpty(model.Email))
{
return result;
}
foreach (var client in _source.Clients)
{
if (client.Email.Contains(model.Email))
{
result.Add(client.GetViewModel);
}
}
return result;
}
public List<ClientViewModel> GetFullList()
{
var result = new List<ClientViewModel>();
foreach (var client in _source.Clients)
{
result.Add(client.GetViewModel);
}
return result;
}
}
}