2023-03-04 12:58:49 +04:00
|
|
|
|
using ConfectioneryContracts.BindingModels;
|
|
|
|
|
using ConfectioneryContracts.SearchModels;
|
|
|
|
|
using ConfectioneryContracts.StoragesContract;
|
|
|
|
|
using ConfectioneryContracts.ViewModels;
|
|
|
|
|
|
|
|
|
|
namespace ConfectioneryListImplement
|
|
|
|
|
{
|
|
|
|
|
public class ClientStorage : IClientStorage
|
|
|
|
|
{
|
|
|
|
|
private readonly DataListSingleton _source;
|
|
|
|
|
public ClientStorage()
|
|
|
|
|
{
|
|
|
|
|
_source = DataListSingleton.GetInstance();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ClientViewModel? Delete(ClientBindingModel model)
|
2023-03-13 16:43:23 +04:00
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
2023-03-04 12:58:49 +04:00
|
|
|
|
|
2023-03-13 16:43:23 +04:00
|
|
|
|
public ClientViewModel? GetElement(ClientSearchModel model)
|
2023-03-04 12:58:49 +04:00
|
|
|
|
{
|
2023-03-13 16:43:23 +04:00
|
|
|
|
foreach (var client in _source.Clients)
|
|
|
|
|
{
|
|
|
|
|
if (model.Id.HasValue && model.Id == client.Id)
|
|
|
|
|
return client.GetViewModel;
|
|
|
|
|
if (model.Email != null && model.Password != null &&
|
|
|
|
|
client.Email.Equals(model.Email) && client.Password.Equals(model.Password))
|
|
|
|
|
return client.GetViewModel;
|
|
|
|
|
if (model.Email != null && client.Email.Equals(model.Email))
|
|
|
|
|
return client.GetViewModel;
|
|
|
|
|
}
|
2023-03-04 17:33:38 +04:00
|
|
|
|
return null;
|
2023-03-04 12:58:49 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<ClientViewModel> GetFilteredList(ClientSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
return new();
|
|
|
|
|
}
|
2023-03-13 16:43:23 +04:00
|
|
|
|
// У нас нет каких либо специфических условий для выборки списка клиентов,
|
|
|
|
|
// поэтому получаем лишь один элемент по модели
|
|
|
|
|
var res = GetElement(model);
|
|
|
|
|
return res != null ? new() { res } : new();
|
2023-03-04 12:58:49 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<ClientViewModel> GetFullList()
|
|
|
|
|
{
|
2023-03-13 16:43:23 +04:00
|
|
|
|
var result = new List<ClientViewModel>();
|
|
|
|
|
foreach (var client in _source.Clients)
|
|
|
|
|
{
|
|
|
|
|
result.Add(client.GetViewModel);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2023-03-04 12:58:49 +04:00
|
|
|
|
|
|
|
|
|
public ClientViewModel? Insert(ClientBindingModel model)
|
|
|
|
|
{
|
2023-03-13 23:01:00 +04:00
|
|
|
|
model.Id = 1;
|
2023-03-13 23:12:24 +04:00
|
|
|
|
foreach (var client in _source.Clients)
|
2023-03-13 23:01:00 +04:00
|
|
|
|
{
|
2023-03-13 23:11:36 +04:00
|
|
|
|
if (model.Id <= client.Id)
|
2023-03-13 23:01:00 +04:00
|
|
|
|
{
|
2023-03-13 23:11:36 +04:00
|
|
|
|
model.Id = client.Id + 1;
|
2023-03-13 23:01:00 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
var res = Client.Create(model);
|
2023-03-04 12:58:49 +04:00
|
|
|
|
if (res != null)
|
|
|
|
|
{
|
|
|
|
|
_source.Clients.Add(res);
|
|
|
|
|
}
|
|
|
|
|
return res?.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ClientViewModel? Update(ClientBindingModel model)
|
2023-03-13 16:43:23 +04:00
|
|
|
|
{
|
|
|
|
|
foreach (var client in _source.Clients)
|
|
|
|
|
{
|
|
|
|
|
if (client.Id == model.Id)
|
|
|
|
|
{
|
|
|
|
|
client.Update(model);
|
|
|
|
|
return client.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-04 12:58:49 +04:00
|
|
|
|
}
|