104 lines
3.1 KiB
C#
104 lines
3.1 KiB
C#
|
using AutomobilePlantContracts.BindingModels;
|
|||
|
using AutomobilePlantContracts.SearchModels;
|
|||
|
using AutomobilePlantContracts.StorageContracts;
|
|||
|
using AutomobilePlantContracts.ViewModels;
|
|||
|
using AutomobilePlantListImplements.Models;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
|
|||
|
namespace AutomobilePlantListImplements.Implements
|
|||
|
{
|
|||
|
public class ClientStorage : IClientStorage
|
|||
|
{
|
|||
|
private readonly DataListSingleton _source;
|
|||
|
public ClientStorage()
|
|||
|
{
|
|||
|
_source = DataListSingleton.GetInstance();
|
|||
|
}
|
|||
|
|
|||
|
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) && client.Email == model.Email) || (model.Id.HasValue && client.Id == model.Id))
|
|||
|
{
|
|||
|
return client.GetViewModel;
|
|||
|
}
|
|||
|
}
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
public List<ClientViewModel> GetFilteredList(ClientSearchModel model)
|
|||
|
{
|
|||
|
var result = new List<ClientViewModel>();
|
|||
|
if (string.IsNullOrEmpty(model.Email))
|
|||
|
{
|
|||
|
return result;
|
|||
|
}
|
|||
|
var client = GetElement(model);
|
|||
|
if (client != null) result.Add(client);
|
|||
|
return result;
|
|||
|
}
|
|||
|
|
|||
|
public List<ClientViewModel> GetFullList()
|
|||
|
{
|
|||
|
var result = new List<ClientViewModel>();
|
|||
|
foreach (var client in _source.Clients)
|
|||
|
{
|
|||
|
result.Add(client.GetViewModel);
|
|||
|
}
|
|||
|
return result;
|
|||
|
}
|
|||
|
|
|||
|
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 res = Client.Create(model);
|
|||
|
if (res != null)
|
|||
|
{
|
|||
|
_source.Clients.Add(res);
|
|||
|
}
|
|||
|
return res?.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 (_source.Clients[i].Id == model.Id)
|
|||
|
{
|
|||
|
var element = _source.Clients[i];
|
|||
|
_source.Clients.RemoveAt(i);
|
|||
|
return element.GetViewModel;
|
|||
|
}
|
|||
|
}
|
|||
|
return null;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|