80 lines
2.3 KiB
C#
80 lines
2.3 KiB
C#
using ConfectioneryContracts.BindingModels;
|
|
using ConfectioneryContracts.SearchModels;
|
|
using ConfectioneryContracts.StoragesContract;
|
|
using ConfectioneryContracts.ViewModels;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace ConfectioneryListImplement
|
|
{
|
|
public class ClientStorage : IClientStorage
|
|
{
|
|
private readonly DataListSingleton _source;
|
|
public ClientStorage()
|
|
{
|
|
_source = DataListSingleton.GetInstance();
|
|
}
|
|
|
|
public ClientViewModel? Delete(ClientBindingModel model)
|
|
{
|
|
var res = _source.Clients.FirstOrDefault(x => x.Id == model.Id);
|
|
if (res != null)
|
|
{
|
|
_source.Clients.Remove(res);
|
|
}
|
|
return res?.GetViewModel;
|
|
}
|
|
|
|
public ClientViewModel? GetElement(ClientSearchModel model)
|
|
{
|
|
return _source.Clients.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
|
|
}
|
|
|
|
public List<ClientViewModel> GetFilteredList(ClientSearchModel model)
|
|
{
|
|
if (model == null)
|
|
{
|
|
return new();
|
|
}
|
|
if (model.Id.HasValue)
|
|
{
|
|
var res = GetElement(model);
|
|
return res != null ? new() { res } : new();
|
|
}
|
|
if (model.Email != null)
|
|
{
|
|
return _source.Clients
|
|
.Where(x => x.Email.Contains(model.Email))
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
return new();
|
|
}
|
|
|
|
public List<ClientViewModel> GetFullList()
|
|
{
|
|
return _source.Clients.Select(x => x.GetViewModel).ToList();
|
|
}
|
|
|
|
public ClientViewModel? Insert(ClientBindingModel model)
|
|
{
|
|
var res = Client.Create(model);
|
|
if (res != null)
|
|
{
|
|
_source.Clients.Add(res);
|
|
}
|
|
return res?.GetViewModel;
|
|
}
|
|
|
|
public ClientViewModel? Update(ClientBindingModel model)
|
|
{
|
|
var res = _source.Clients.FirstOrDefault(x => x.Id == model.Id);
|
|
res?.Update(model);
|
|
return res?.GetViewModel;
|
|
}
|
|
}
|
|
}
|