From d27ad2f3232ba21c3af5e11bb33a1f94ca642d6f Mon Sep 17 00:00:00 2001 From: malimova Date: Tue, 30 Apr 2024 00:26:05 +0400 Subject: [PATCH] =?UTF-8?q?+=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D1=8B=20?= =?UTF-8?q?=D0=B8=20=D0=B8=D0=BD=D1=82=D0=B5=D1=80=D1=84=D0=B5=D0=B9=D1=81?= =?UTF-8?q?=D1=8B=20=D0=B4=D0=BB=D1=8F=20=D1=81=D1=83=D1=89=D0=BD=D0=BE?= =?UTF-8?q?=D1=81=D1=82=D0=B8=20=C2=AB=D0=9A=D0=BB=D0=B8=D0=B5=D0=BD=D1=82?= =?UTF-8?q?=C2=BB=20(=D0=BC=D0=BD=D0=BE=D0=B3=D0=BE)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BindingModels/ClientBindingModel.cs | 16 +++ .../BusinessLogicsContracts/IClientLogic.cs | 20 ++++ .../SearchModels/ClientSearchModel.cs | 19 +++ .../StoragesContracts/IClientStorage.cs | 26 ++++ .../ViewModels/ClientViewModel.cs | 20 ++++ .../ConfectioneryDatabaseImplement/Client.cs | 73 ++++++++++++ .../ClientStorage.cs | 84 +++++++++++++ .../ConfectioneryDatabase.cs | 1 + .../ConfectioneryFileImplement/Client.cs | 76 ++++++++++++ .../ClientStorage.cs | 86 ++++++++++++++ .../DataFileSingleton.cs | 5 + .../ConfectioneryListImplement/Client.cs | 53 +++++++++ .../ClientStorage.cs | 111 ++++++++++++++++++ .../DataListSingleton.cs | 2 + 14 files changed, 592 insertions(+) create mode 100644 Confectionery/ConfectioneryContracts/BindingModels/ClientBindingModel.cs create mode 100644 Confectionery/ConfectioneryContracts/BusinessLogicsContracts/IClientLogic.cs create mode 100644 Confectionery/ConfectioneryContracts/SearchModels/ClientSearchModel.cs create mode 100644 Confectionery/ConfectioneryContracts/StoragesContracts/IClientStorage.cs create mode 100644 Confectionery/ConfectioneryContracts/ViewModels/ClientViewModel.cs create mode 100644 Confectionery/ConfectioneryDatabaseImplement/Client.cs create mode 100644 Confectionery/ConfectioneryDatabaseImplement/ClientStorage.cs create mode 100644 Confectionery/ConfectioneryFileImplement/Client.cs create mode 100644 Confectionery/ConfectioneryFileImplement/ClientStorage.cs create mode 100644 Confectionery/ConfectioneryListImplement/Client.cs create mode 100644 Confectionery/ConfectioneryListImplement/ClientStorage.cs diff --git a/Confectionery/ConfectioneryContracts/BindingModels/ClientBindingModel.cs b/Confectionery/ConfectioneryContracts/BindingModels/ClientBindingModel.cs new file mode 100644 index 0000000..18c4070 --- /dev/null +++ b/Confectionery/ConfectioneryContracts/BindingModels/ClientBindingModel.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryContracts.BindingModels +{ + public class ClientBindingModel + { + public int Id { get; set; } + public string ClientFIO { get; set; } = string.Empty; + public string Email { get; set; } = string.Empty; + public string Password { get; set; } = string.Empty; + } +} diff --git a/Confectionery/ConfectioneryContracts/BusinessLogicsContracts/IClientLogic.cs b/Confectionery/ConfectioneryContracts/BusinessLogicsContracts/IClientLogic.cs new file mode 100644 index 0000000..a9eed99 --- /dev/null +++ b/Confectionery/ConfectioneryContracts/BusinessLogicsContracts/IClientLogic.cs @@ -0,0 +1,20 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryContracts.SearchModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryContracts.BusinessLogicsContracts +{ + public interface IClientLogic + { + List? ReadList(ClientSearchModel? model); + ClientViewModel? ReadElement(ClientSearchModel model); + bool Create(ClientBindingModel model); + bool Update(ClientBindingModel model); + bool Delete(ClientBindingModel model); + } +} diff --git a/Confectionery/ConfectioneryContracts/SearchModels/ClientSearchModel.cs b/Confectionery/ConfectioneryContracts/SearchModels/ClientSearchModel.cs new file mode 100644 index 0000000..349f1ee --- /dev/null +++ b/Confectionery/ConfectioneryContracts/SearchModels/ClientSearchModel.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryContracts.SearchModels +{ + public class ClientSearchModel + { + public int? Id { get; set; } + + public string? ClientFIO { get; set; } + + public string? Email { get; set; } + + public string? Password { get; set; } + } +} diff --git a/Confectionery/ConfectioneryContracts/StoragesContracts/IClientStorage.cs b/Confectionery/ConfectioneryContracts/StoragesContracts/IClientStorage.cs new file mode 100644 index 0000000..10e367c --- /dev/null +++ b/Confectionery/ConfectioneryContracts/StoragesContracts/IClientStorage.cs @@ -0,0 +1,26 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryContracts.StoragesContracts +{ + public interface IClientStorage + { + List GetFullList(); + + List GetFilteredList(ClientSearchModel model); + + ClientViewModel? GetElement(ClientSearchModel model); + + ClientViewModel? Insert(ClientBindingModel model); + + ClientViewModel? Update(ClientBindingModel model); + + ClientViewModel? Delete(ClientBindingModel model); + } +} diff --git a/Confectionery/ConfectioneryContracts/ViewModels/ClientViewModel.cs b/Confectionery/ConfectioneryContracts/ViewModels/ClientViewModel.cs new file mode 100644 index 0000000..a8faf87 --- /dev/null +++ b/Confectionery/ConfectioneryContracts/ViewModels/ClientViewModel.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryContracts.ViewModels +{ + public class ClientViewModel + { + public int Id { get; set; } + [DisplayName("ФИО клиента")] + public string ClientFIO { get; set; } = string.Empty; + [DisplayName("Логин (эл. почта)")] + public string Email { get; set; } = string.Empty; + [DisplayName("Пароль")] + public string Password { get; set; } = string.Empty; + } +} diff --git a/Confectionery/ConfectioneryDatabaseImplement/Client.cs b/Confectionery/ConfectioneryDatabaseImplement/Client.cs new file mode 100644 index 0000000..708c1d5 --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Client.cs @@ -0,0 +1,73 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryDatabaseImplement.Models +{ + internal class Client : IClientModel + { + public int Id { get; private set; } + + [Required] + public string ClientFIO { get; set; } = string.Empty; + [Required] + public string Email { get; set; } = string.Empty; + [Required] + public string Password { get; set; } = string.Empty; + + [ForeignKey("ClientId")] + public virtual List Orders { get; set; } = new(); + + 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 static Client Create(ClientViewModel model) + { + 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/Confectionery/ConfectioneryDatabaseImplement/ClientStorage.cs b/Confectionery/ConfectioneryDatabaseImplement/ClientStorage.cs new file mode 100644 index 0000000..444eae4 --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/ClientStorage.cs @@ -0,0 +1,84 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContracts; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDatabaseImplement.Models; + +namespace ConfectioneryDatabaseImplement +{ + public class ClientStorage : IClientStorage + { + public List GetFullList() + { + using var context = new ConfectioneryDatabase(); + return context.Clients.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(ClientSearchModel model) + { + if (string.IsNullOrEmpty(model.ClientFIO)) + { + return new(); + } + using var context = new ConfectioneryDatabase(); + return context.Clients.Where(x => x.ClientFIO.Contains(model.ClientFIO)).Select(x => x.GetViewModel).ToList(); + } + + public ClientViewModel? GetElement(ClientSearchModel model) + { + if (string.IsNullOrEmpty(model.Email) && string.IsNullOrEmpty(model.Password) && !model.Id.HasValue) + { + return null; + } + using var context = new ConfectioneryDatabase(); + return context.Clients.FirstOrDefault(x => + (!string.IsNullOrEmpty(model.Email) && x.Email == model.Email && !string.IsNullOrEmpty(model.Password) && x.Password == model.Password) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public ClientViewModel? Insert(ClientBindingModel model) + { + var newComponent = Client.Create(model); + if (newComponent == null) + { + return null; + } + using var context = new ConfectioneryDatabase(); + context.Clients.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + + public ClientViewModel? Update(ClientBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var component = context.Clients.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + context.SaveChanges(); + return component.GetViewModel; + } + + public ClientViewModel? Delete(ClientBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var element = context.Clients.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Clients.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs b/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs index 5447a27..9d23119 100644 --- a/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs +++ b/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs @@ -23,5 +23,6 @@ namespace ConfectioneryDatabaseImplement public virtual DbSet Pastrys { set; get; } public virtual DbSet PastryComponents { set; get; } public virtual DbSet Orders { set; get; } + public virtual DbSet Clients { set; get; } } } diff --git a/Confectionery/ConfectioneryFileImplement/Client.cs b/Confectionery/ConfectioneryFileImplement/Client.cs new file mode 100644 index 0000000..c87c19f --- /dev/null +++ b/Confectionery/ConfectioneryFileImplement/Client.cs @@ -0,0 +1,76 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace ConfectioneryFileImplement +{ + public class Client : IClientModel + { + public int Id { get; set; } + public string ClientFIO { get; set; } = string.Empty; + public string Password { get; set; } = string.Empty; + public string Email { get; set; } = string.Empty; + + public static Client? Create(ClientBindingModel? model) + { + if (model == null) + { + return null; + } + return new Client() + { + Id = model.Id, + ClientFIO = model.ClientFIO, + Password = model.Password, + Email = model.Email + }; + } + + public static Client? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Client() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ClientFIO = element.Attribute("ClientFIO")!.Value, + Password = element.Attribute("Password")!.Value, + Email = element.Attribute("Email")!.Value + }; + } + + public void Update(ClientBindingModel? model) + { + if (model == null) + { + return; + } + ClientFIO = model.ClientFIO; + Password = model.Password; + Email = model.Email; + } + + public ClientViewModel GetViewModel => new() + { + Id = Id, + ClientFIO = ClientFIO, + Password = Password, + Email = Email + }; + + public XElement GetXElement => new("Client", + new XAttribute("Id", Id), + new XElement("ClientFIO", ClientFIO), + new XElement("Password", Password), + new XElement("Email", Email)); + } +} + diff --git a/Confectionery/ConfectioneryFileImplement/ClientStorage.cs b/Confectionery/ConfectioneryFileImplement/ClientStorage.cs new file mode 100644 index 0000000..a679ab5 --- /dev/null +++ b/Confectionery/ConfectioneryFileImplement/ClientStorage.cs @@ -0,0 +1,86 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContracts; +using ConfectioneryContracts.ViewModels; +using ConfectioneryFileImplement.Models; +using ConfectioneryDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryFileImplement.Implements +{ + public class ClientStorage : IClientStorage + { + private readonly DataFileSingleton source; + + public ClientStorage() + { + source = DataFileSingleton.GetInstance(); + } + + public List GetFullList() + { + return source.Clients.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(ClientSearchModel model) + { + if (string.IsNullOrEmpty(model.Email)) + { + return new(); + } + return source.Clients.Where(x => x.Email.Contains(model.Email)).Select(x => x.GetViewModel).ToList(); + } + + public ClientViewModel? GetElement(ClientSearchModel model) + { + if (string.IsNullOrEmpty(model.Email) && !model.Id.HasValue) + { + return null; + } + return source.Clients + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.Email) && x.Email == model.Email) || + (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public ClientViewModel? Insert(ClientBindingModel model) + { + model.Id = source.Clients.Count > 0 ? source.Clients.Max(x => x.Id) + 1 : 1; + var newClient = Client.Create(model); + if (newClient == null) + { + return null; + } + source.Clients.Add(newClient); + source.SaveClients(); + return newClient.GetViewModel; + } + + public ClientViewModel? Update(ClientBindingModel model) + { + var ingredient = source.Clients.FirstOrDefault(x => x.Id == model.Id); + if (ingredient == null) + { + return null; + } + ingredient.Update(model); + source.SaveClients(); + return ingredient.GetViewModel; + } + + public ClientViewModel? Delete(ClientBindingModel model) + { + var element = source.Clients.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + source.Clients.Remove(element); + source.SaveClients(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/Confectionery/ConfectioneryFileImplement/DataFileSingleton.cs b/Confectionery/ConfectioneryFileImplement/DataFileSingleton.cs index 939a2cc..dcfc264 100644 --- a/Confectionery/ConfectioneryFileImplement/DataFileSingleton.cs +++ b/Confectionery/ConfectioneryFileImplement/DataFileSingleton.cs @@ -14,9 +14,11 @@ namespace ConfectioneryFileImplement private readonly string ComponentFileName = "Component.xml"; private readonly string OrderFileName = "Order.xml"; private readonly string PastryFileName = "Pastry.xml"; + private readonly string ClientFileName = "Client.xml"; public List Components { get; private set; } public List Orders { get; private set; } public List Pastrys { get; private set; } + public List Clients { get; private set; } public static DataFileSingleton GetInstance() { if (instance == null) @@ -31,11 +33,14 @@ namespace ConfectioneryFileImplement "Pastrys", x => x.GetXElement); public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); + public void SaveClients() => SaveData(Clients, ClientFileName, + "Clients", x => x.GetXElement); private DataFileSingleton() { Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; Pastrys = LoadData(PastryFileName, "Pastry", x => Pastry.Create(x)!)!; Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; + Clients = LoadData(ClientFileName, "Client", x => Client.Create(x)!)!; } private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) { diff --git a/Confectionery/ConfectioneryListImplement/Client.cs b/Confectionery/ConfectioneryListImplement/Client.cs new file mode 100644 index 0000000..27ec190 --- /dev/null +++ b/Confectionery/ConfectioneryListImplement/Client.cs @@ -0,0 +1,53 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryListImplement.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/Confectionery/ConfectioneryListImplement/ClientStorage.cs b/Confectionery/ConfectioneryListImplement/ClientStorage.cs new file mode 100644 index 0000000..fd39d75 --- /dev/null +++ b/Confectionery/ConfectioneryListImplement/ClientStorage.cs @@ -0,0 +1,111 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContracts; +using ConfectioneryContracts.ViewModels; +using ConfectioneryListImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryListImplement.Implements +{ + public class ClientStorage : IClientStorage + { + 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.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; + } + } +} diff --git a/Confectionery/ConfectioneryListImplement/DataListSingleton.cs b/Confectionery/ConfectioneryListImplement/DataListSingleton.cs index 37eca03..4229679 100644 --- a/Confectionery/ConfectioneryListImplement/DataListSingleton.cs +++ b/Confectionery/ConfectioneryListImplement/DataListSingleton.cs @@ -13,11 +13,13 @@ namespace ConfectioneryListImplement public List Components { get; set; } public List Orders { get; set; } public List Pastrys { get; set; } + public List Clients { get; set; } private DataListSingleton() { Components = new List(); Orders = new List(); Pastrys = new List(); + Clients = new List(); } public static DataListSingleton GetInstance() {