client for list
This commit is contained in:
parent
50b96821df
commit
1235676a41
@ -1,4 +1,5 @@
|
||||
using JewelryStoreContracts.BindingModels;
|
||||
using JewelryStoreContracts.SearchModels;
|
||||
using JewelryStoreContracts.ViewModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
@ -1,4 +1,5 @@
|
||||
using JewelryStoreContracts.BindingModels;
|
||||
using JewelryStoreContracts.SearchModels;
|
||||
using JewelryStoreContracts.ViewModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
@ -13,6 +13,8 @@ namespace JewelryStoreListImplement
|
||||
public List<Component> Components { get; set; }
|
||||
public List<Order> Orders { get; set; }
|
||||
public List<Jewel> Jewels { get; set; }
|
||||
public List<Client> Clients { get; set; }
|
||||
|
||||
private DataListSingleton()
|
||||
{
|
||||
Components = new List<Component>();
|
||||
|
104
JewelryStoreListImplement/Implements/ClientStorage.cs
Normal file
104
JewelryStoreListImplement/Implements/ClientStorage.cs
Normal file
@ -0,0 +1,104 @@
|
||||
using JewelryStoreContracts.BindingModels;
|
||||
using JewelryStoreContracts.SearchModels;
|
||||
using JewelryStoreContracts.StoragesContracts;
|
||||
using JewelryStoreContracts.ViewModels;
|
||||
using JewelryStoreListImplement.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace JewelryStoreListImplement.Implements
|
||||
{
|
||||
public class ClientStorage : IClientStorage
|
||||
{
|
||||
private readonly DataListSingleton _source;
|
||||
public ClientStorage()
|
||||
{
|
||||
_source = DataListSingleton.GetInstance();
|
||||
}
|
||||
public List<ClientViewModel> GetFullList()
|
||||
{
|
||||
var result = new List<ClientViewModel>();
|
||||
foreach (var client in _source.Clients)
|
||||
{
|
||||
result.Add(client.GetViewModel);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
public List<ClientViewModel> GetFilteredList(ClientSearchModel model)
|
||||
{
|
||||
var result = new List<ClientViewModel>();
|
||||
if (string.IsNullOrEmpty(model.ClientFIO))
|
||||
{
|
||||
return result;
|
||||
}
|
||||
foreach (var client in _source.Clients)
|
||||
{
|
||||
if (client.ClientFIO.Contains(model.ClientFIO))
|
||||
{
|
||||
result.Add(client.GetViewModel);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
public ClientViewModel? GetElement(ClientSearchModel model)
|
||||
{
|
||||
foreach (var client in _source.Clients)
|
||||
{
|
||||
if (model.Id.HasValue && model.Id == client.Id)
|
||||
return client.GetViewModel;
|
||||
if (!string.IsNullOrEmpty(model.Email) && !string.IsNullOrEmpty(model.Password) &&
|
||||
client.Email.Equals(model.Email) && client.Password.Equals(model.Password))
|
||||
return client.GetViewModel;
|
||||
if (!string.IsNullOrEmpty(model.Email) && client.Email.Equals(model.Email))
|
||||
return client.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
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 (_source.Clients[i].Id == model.Id)
|
||||
{
|
||||
var element = _source.Clients[i];
|
||||
_source.Clients.RemoveAt(i);
|
||||
return element.GetViewModel;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
50
JewelryStoreListImplement/Models/Client.cs
Normal file
50
JewelryStoreListImplement/Models/Client.cs
Normal file
@ -0,0 +1,50 @@
|
||||
using JewelryStoreContracts.BindingModels;
|
||||
using JewelryStoreContracts.ViewModels;
|
||||
using JewelryStoreDataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace JewelryStoreListImplement.Models
|
||||
{
|
||||
public class Client : IClientModel
|
||||
{
|
||||
public int Id { get; private set; }
|
||||
public string ClientFIO { get; private set; } = string.Empty;
|
||||
public string Email { get; set; } = string.Empty;
|
||||
public string Password { get; set; } = string.Empty;
|
||||
public static Client? Create(ClientBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new Client()
|
||||
{
|
||||
Id = model.Id,
|
||||
ClientFIO = model.ClientFIO,
|
||||
Email = model.Email,
|
||||
Password = model.Password
|
||||
};
|
||||
}
|
||||
public void Update(ClientBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
ClientFIO = model.ClientFIO;
|
||||
Email = model.Email;
|
||||
Password = model.Password;
|
||||
}
|
||||
public ClientViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
ClientFIO = ClientFIO,
|
||||
Email = Email,
|
||||
Password = Password,
|
||||
};
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user