diff --git a/SushiBarListImplements/DataListSingleton.cs b/SushiBarListImplements/DataListSingleton.cs index 309dfcd..7074ae2 100644 --- a/SushiBarListImplements/DataListSingleton.cs +++ b/SushiBarListImplements/DataListSingleton.cs @@ -8,11 +8,13 @@ namespace SushiBarListImplement public List Components { get; set; } public List Orders { get; set; } public List Sushis { get; set; } + public List Clients { get; set; } private DataListSingleton() { Components = new List(); Orders = new List(); Sushis = new List(); + Clients = new List(); } public static DataListSingleton GetInstance() { diff --git a/SushiBarListImplements/Implements/ClientStorage.cs b/SushiBarListImplements/Implements/ClientStorage.cs new file mode 100644 index 0000000..22ea4d9 --- /dev/null +++ b/SushiBarListImplements/Implements/ClientStorage.cs @@ -0,0 +1,106 @@ +using SushiBarContracts.BindingModel; +using SushiBarContracts.SearchModel; +using SushiBarContracts.ViewModels; +using SushiBarListImplement; +using SushiBarListImplements.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SushiBarListImplements.Implements +{ + public class ClientStorage + { + private readonly DataListSingleton _source; + public ClientStorage() + { + _source = DataListSingleton.GetInstance(); + } + public List GetFullList() + { + var result = new List(); + foreach (var client in _source.Clients) + { + result.Add(client.GetViewModel); + } + return result; + } + public List GetFilteredList(ClientSearchModel model) + { + var result = new List(); + 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 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.Email == 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; + } + } +} diff --git a/SushiBarListImplements/Models/Client.cs b/SushiBarListImplements/Models/Client.cs new file mode 100644 index 0000000..19359c7 --- /dev/null +++ b/SushiBarListImplements/Models/Client.cs @@ -0,0 +1,45 @@ +using SushiBarContracts.BindingModel; +using SushiBarContracts.ViewModels; +using SushiBarDataModels; + +namespace SushiBarListImplements.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 + }; + } +} diff --git a/SushiBarListImplements/Models/Order.cs b/SushiBarListImplements/Models/Order.cs index a0a3e5a..b346a95 100644 --- a/SushiBarListImplements/Models/Order.cs +++ b/SushiBarListImplements/Models/Order.cs @@ -10,6 +10,7 @@ namespace SushiBarListImplements.Models { public int Id { get; private set; } public int SushiId { get; private set; } + public int ClientId { get; private set; } public int Count { get; private set; } public double Sum { get; private set; } public OrderStatus Status { get; private set; } @@ -30,7 +31,8 @@ namespace SushiBarListImplements.Models Sum = model.Sum, Status = model.Status, DateCreate = model.DateCreate, - DateImplement = model.DateImplement + DateImplement = model.DateImplement, + ClientId = model.ClientId, }; } @@ -46,6 +48,7 @@ namespace SushiBarListImplements.Models Status = model.Status; DateCreate = model.DateCreate; DateImplement = model.DateImplement; + ClientId = model.ClientId; } public OrderViewModel GetViewModel => new() @@ -56,7 +59,8 @@ namespace SushiBarListImplements.Models Sum = Sum, Status = Status, DateCreate = DateCreate, - DateImplement = DateImplement + DateImplement = DateImplement, + ClientId = ClientId, }; } }