diff --git a/DressAtelierBusinessLogic/BusinessLogic/ClientLogic.cs b/DressAtelierBusinessLogic/BusinessLogic/ClientLogic.cs new file mode 100644 index 0000000..ef9ab11 --- /dev/null +++ b/DressAtelierBusinessLogic/BusinessLogic/ClientLogic.cs @@ -0,0 +1,126 @@ +using DressAtelierContracts.BindingModels; +using DressAtelierContracts.BusinessLogicContracts; +using DressAtelierContracts.SearchModels; +using DressAtelierContracts.StorageContracts; +using DressAtelierContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DressAtelierBusinessLogic.BusinessLogic +{ + public class ClientLogic : IClientLogic + { + private readonly ILogger _logger; + private readonly IClientStorage _clientStorage; + public ClientLogic(ILogger logger, IClientStorage clientStorage) + { + _logger = logger; + _clientStorage = clientStorage; + } + public bool Create(ClientBindingModel model) + { + CheckUser(model); + if(_clientStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(ClientBindingModel model) + { + CheckUser(model); + if (_clientStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + public bool Delete(ClientBindingModel model) + { + CheckUser(model,false); + _logger.LogInformation("Delete. ID:{ID}", model.ID); + if (_clientStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + public ClientViewModel? ReadElement(ClientSearchModel model) + { + if(model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. UserName:{UserName}.ID:{ ID}", model.Email, model.ID); + var element = _clientStorage.GetElement(model); + if(element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement find. ID:{ID}", element.ID); + return element; + } + + public List? ReadList(ClientSearchModel? model) + { + _logger.LogInformation("ReadList. UserName:{UserName}. ID:{ ID}", model?.Email, model?.ID); + + var list = model == null ? _clientStorage.GetFullList() : _clientStorage.GetFilteredList(model); + + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + + public void CheckUser(ClientBindingModel model,bool withParams = true) + { + if(model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if(!withParams) + { + return; + } + if(string.IsNullOrEmpty(model.FullName)) + { + throw new ArgumentNullException("Invalid fullname of user", nameof(model.FullName)); + } + if (string.IsNullOrEmpty(model.Email)) + { + throw new ArgumentNullException("Invalid email of user", nameof(model.Email)); + } + if (string.IsNullOrEmpty(model.Password)) + { + throw new ArgumentNullException("Invalid password of user", nameof(model.Password)); + } + _logger.LogInformation("Client. ClientName:{ FullName}. Email:{ Email}. ID: { ID} ", model.FullName, model.Email, model.ID); + + var element = _clientStorage.GetElement(new ClientSearchModel + { + Email = model.Email + }); + + if(element != null && element.ID != model.ID) + { + throw new InvalidOperationException("User with such email already exists."); + } + } + + } +} diff --git a/DressAtelierContracts/BindingModels/ClientBindingModel.cs b/DressAtelierContracts/BindingModels/ClientBindingModel.cs new file mode 100644 index 0000000..ee1814c --- /dev/null +++ b/DressAtelierContracts/BindingModels/ClientBindingModel.cs @@ -0,0 +1,17 @@ +using DressAtelierDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DressAtelierContracts.BindingModels +{ + public class ClientBindingModel : IClientModel + { + public int ID { get; set;} + public string FullName { get; set; } = string.Empty; + public string Email { get; set; } = string.Empty; + public string Password { get; set; } = string.Empty; + } +} diff --git a/DressAtelierContracts/BindingModels/OrderBindingModel.cs b/DressAtelierContracts/BindingModels/OrderBindingModel.cs index 396d4e7..7b38061 100644 --- a/DressAtelierContracts/BindingModels/OrderBindingModel.cs +++ b/DressAtelierContracts/BindingModels/OrderBindingModel.cs @@ -13,6 +13,8 @@ namespace DressAtelierContracts.BindingModels public int ID { get; set; } public int DressID { get; set; } + public int ClientID { get; set; } + public string ClientFullName { get; set; } = string.Empty; public int Count { get; set; } diff --git a/DressAtelierContracts/BusinessLogicContracts/IClientLogic.cs b/DressAtelierContracts/BusinessLogicContracts/IClientLogic.cs new file mode 100644 index 0000000..63e92eb --- /dev/null +++ b/DressAtelierContracts/BusinessLogicContracts/IClientLogic.cs @@ -0,0 +1,20 @@ +using DressAtelierContracts.BindingModels; +using DressAtelierContracts.SearchModels; +using DressAtelierContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DressAtelierContracts.BusinessLogicContracts +{ + 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/DressAtelierContracts/SearchModels/ClientSearchModel.cs b/DressAtelierContracts/SearchModels/ClientSearchModel.cs new file mode 100644 index 0000000..21cbb9f --- /dev/null +++ b/DressAtelierContracts/SearchModels/ClientSearchModel.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DressAtelierContracts.SearchModels +{ + public class ClientSearchModel + { + public int? ID { get; set; } + public string? Email { get; set; } + } +} diff --git a/DressAtelierContracts/SearchModels/OrderSearchModel.cs b/DressAtelierContracts/SearchModels/OrderSearchModel.cs index 6fef93e..2fc68eb 100644 --- a/DressAtelierContracts/SearchModels/OrderSearchModel.cs +++ b/DressAtelierContracts/SearchModels/OrderSearchModel.cs @@ -9,6 +9,7 @@ namespace DressAtelierContracts.SearchModels public class OrderSearchModel { public int? ID { get; set; } + public int? ClientID { get; set; } public DateTime? DateFrom { get; set; } public DateTime? DateTo { get; set; } } diff --git a/DressAtelierContracts/StorageContracts/IClientStorage.cs b/DressAtelierContracts/StorageContracts/IClientStorage.cs new file mode 100644 index 0000000..848bf0f --- /dev/null +++ b/DressAtelierContracts/StorageContracts/IClientStorage.cs @@ -0,0 +1,21 @@ +using DressAtelierContracts.BindingModels; +using DressAtelierContracts.SearchModels; +using DressAtelierContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DressAtelierContracts.StorageContracts +{ + 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/DressAtelierContracts/ViewModels/ClientViewModel.cs b/DressAtelierContracts/ViewModels/ClientViewModel.cs new file mode 100644 index 0000000..76efa63 --- /dev/null +++ b/DressAtelierContracts/ViewModels/ClientViewModel.cs @@ -0,0 +1,24 @@ +using DressAtelierDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DressAtelierContracts.ViewModels +{ + public class ClientViewModel : IClientModel + { + public int ID { get; set; } + [DisplayName("Client's full name")] + public string FullName { get; set; } = string.Empty; + + [DisplayName("Login")] + public string Email { get; set; } = string.Empty; + + [DisplayName("Password")] + public string Password { get; set; } = string.Empty; + + } +} diff --git a/DressAtelierContracts/ViewModels/OrderViewModel.cs b/DressAtelierContracts/ViewModels/OrderViewModel.cs index 15969a4..3a3e2fe 100644 --- a/DressAtelierContracts/ViewModels/OrderViewModel.cs +++ b/DressAtelierContracts/ViewModels/OrderViewModel.cs @@ -14,9 +14,12 @@ namespace DressAtelierContracts.ViewModels [DisplayName("ID")] public int ID { get; set; } public int DressID { get; set; } + public int ClientID { get; set; } + [DisplayName("Client's name")] + public string ClientFullName { get; set; } = string.Empty; [DisplayName("DressName")] - public string DressName { get; set; } + public string DressName { get; set; } = string.Empty; [DisplayName("Quantity")] public int Count { get; set; } diff --git a/DressAtelierDataModels/Models/IClientModel.cs b/DressAtelierDataModels/Models/IClientModel.cs new file mode 100644 index 0000000..586545e --- /dev/null +++ b/DressAtelierDataModels/Models/IClientModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DressAtelierDataModels.Models +{ + public interface IClientModel : IID + { + string FullName { get; } + string Email { get; } + string Password { get; } + } +} diff --git a/DressAtelierDatabaseImplement/DressAtelierDatabase.cs b/DressAtelierDatabaseImplement/DressAtelierDatabase.cs index bf8921a..8c7b124 100644 --- a/DressAtelierDatabaseImplement/DressAtelierDatabase.cs +++ b/DressAtelierDatabaseImplement/DressAtelierDatabase.cs @@ -1,4 +1,5 @@ -using DressAtelierDatabaseImplementation.Models; +using DressAtelierDatabaseImplement.Models; +using DressAtelierDatabaseImplementation.Models; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; @@ -23,5 +24,6 @@ namespace DressAtelierDatabaseImplementation public virtual DbSet Dresses { set; get; } public virtual DbSet DressMaterials { set; get; } public virtual DbSet Orders { set; get; } + public virtual DbSet Clients { set; get; } } } diff --git a/DressAtelierDatabaseImplement/Implements/ClientStorage.cs b/DressAtelierDatabaseImplement/Implements/ClientStorage.cs new file mode 100644 index 0000000..f1db70b --- /dev/null +++ b/DressAtelierDatabaseImplement/Implements/ClientStorage.cs @@ -0,0 +1,68 @@ +using DressAtelierContracts.BindingModels; +using DressAtelierContracts.SearchModels; +using DressAtelierContracts.StorageContracts; +using DressAtelierContracts.ViewModels; +using DressAtelierDatabaseImplement.Models; +using DressAtelierDatabaseImplementation; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DressAtelierDatabaseImplement.Implements +{ + public class ClientStorage : IClientStorage + { + public ClientViewModel? Insert(ClientBindingModel model) + { + using var context = new DressAtelierDatabase(); + var newClient = Client.Create(model); + if(newClient == null) { return null; } + context.Clients.Add(newClient); + context.SaveChanges(); + return newClient.GetViewModel; + } + + public ClientViewModel? Update(ClientBindingModel model) + { + using var context = new DressAtelierDatabase(); + var client = context.Clients.FirstOrDefault(x => x.ID == model.ID); + if(client == null) { return null; }; + client.Update(model); + context.SaveChanges(); + return client.GetViewModel; + } + + public ClientViewModel? Delete(ClientBindingModel model) + { + using var context = new DressAtelierDatabase(); + var client = context.Clients.FirstOrDefault(x => x.ID == model.ID); + if(client == null) { return null;} + context.Clients.Remove(client); + context.SaveChanges(); + return client.GetViewModel; + } + + public ClientViewModel? GetElement(ClientSearchModel model) + { + if (string.IsNullOrEmpty(model.Email) && !model.ID.HasValue) { return null; } + using var context = new DressAtelierDatabase(); + return context.Clients.FirstOrDefault(x => (!(string.IsNullOrEmpty(model.Email)) && model.Email == x.Email) || (model.ID.HasValue && model.ID == x.ID))?.GetViewModel; + } + + public List GetFilteredList(ClientSearchModel model) + { + if (string.IsNullOrEmpty(model.Email)) { return new(); } + using var context = new DressAtelierDatabase(); + return context.Clients.Where(x => x.Email.Equals(model.Email)).Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() + { + using var context = new DressAtelierDatabase(); + return context.Clients.Select(x => x.GetViewModel).ToList(); + } + + } +} diff --git a/DressAtelierDatabaseImplement/Implements/OrderStorage.cs b/DressAtelierDatabaseImplement/Implements/OrderStorage.cs index be1eaf3..224e472 100644 --- a/DressAtelierDatabaseImplement/Implements/OrderStorage.cs +++ b/DressAtelierDatabaseImplement/Implements/OrderStorage.cs @@ -18,15 +18,19 @@ namespace DressAtelierDatabaseImplementation.Implements public List GetFullList() { using var context = new DressAtelierDatabase(); - return context.Orders.Include(x => x.Dress).Select(x => x.GetViewModel).ToList(); + return context.Orders.Include(x => x.Client).Include(x => x.Dress).Select(x => x.GetViewModel).ToList(); } public List GetFilteredList(OrderSearchModel model) { - if (!model.ID.HasValue && !model.DateFrom.HasValue && !model.DateTo.HasValue) + if (!model.ID.HasValue && !model.DateFrom.HasValue && !model.DateTo.HasValue && !model.ClientID.HasValue) { return new(); } using var context = new DressAtelierDatabase(); + if(model.ClientID.HasValue) + { + return context.Orders.Include(x => x.Client).Where(x => x.ClientID == model.ClientID).Select(x => x.GetViewModel).ToList(); + } return context.Orders.Include(x => x.Dress).Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo).ToList().Select(x => x.GetViewModel).ToList(); } diff --git a/DressAtelierDatabaseImplement/Models/Client.cs b/DressAtelierDatabaseImplement/Models/Client.cs new file mode 100644 index 0000000..1a7a423 --- /dev/null +++ b/DressAtelierDatabaseImplement/Models/Client.cs @@ -0,0 +1,61 @@ +using DressAtelierContracts.BindingModels; +using DressAtelierContracts.ViewModels; +using DressAtelierDatabaseImplementation.Models; +using DressAtelierDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace DressAtelierDatabaseImplement.Models +{ + public class Client : IClientModel + { + public int ID { get; private set; } + [Required] + public string FullName { get; private set; } = string.Empty; + [Required] + public string Email { get; private set; } = string.Empty; + [Required] + public string Password { get; private 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, + FullName = model.FullName, + Email = model.Email, + Password = model.Password + }; + } + + public void Update(ClientBindingModel? model) + { + if (model == null) { return; } + FullName = model.FullName; + Email = model.Email; + Password = model.Password; + } + + public ClientViewModel GetViewModel => new() + { + ID = ID, + FullName = FullName, + Email = Email, + Password = Password + }; + + } +} diff --git a/DressAtelierDatabaseImplement/Models/Order.cs b/DressAtelierDatabaseImplement/Models/Order.cs index 6297c04..b0e477c 100644 --- a/DressAtelierDatabaseImplement/Models/Order.cs +++ b/DressAtelierDatabaseImplement/Models/Order.cs @@ -1,5 +1,6 @@ using DressAtelierContracts.BindingModels; using DressAtelierContracts.ViewModels; +using DressAtelierDatabaseImplement.Models; using DressAtelierDataModels.Enums; using DressAtelierDataModels.Models; using System; @@ -16,8 +17,11 @@ namespace DressAtelierDatabaseImplementation.Models public class Order : IOrderModel { public int ID { get; private set; } + [Required] public int DressID { get; private set; } - + [Required] + public int ClientID { get; private set; } + [Required] public int Count { get; private set; } @@ -33,6 +37,7 @@ namespace DressAtelierDatabaseImplementation.Models public DateTime? DateImplement { get; private set; } public virtual Dress Dress { get; set; } + public virtual Client Client { get; set; } public static Order? Create(OrderBindingModel? model) { @@ -44,6 +49,7 @@ namespace DressAtelierDatabaseImplementation.Models { ID = model.ID, DressID = model.DressID, + ClientID = model.ClientID, Count = model.Count, Sum = model.Sum, Status = model.Status, @@ -68,13 +74,14 @@ namespace DressAtelierDatabaseImplementation.Models { ID = ID, DressID = DressID, + ClientID = ClientID, + ClientFullName = Client.FullName, DressName = Dress.DressName, Count = Count, Sum = Sum, Status = Status, DateCreate = DateCreate, DateImplement = DateImplement - }; } diff --git a/DressAtelierFileImplement/DataFileSingleton.cs b/DressAtelierFileImplement/DataFileSingleton.cs index 3af9dae..8edabd8 100644 --- a/DressAtelierFileImplement/DataFileSingleton.cs +++ b/DressAtelierFileImplement/DataFileSingleton.cs @@ -15,9 +15,11 @@ namespace DressAtelierFileImplement private readonly string MaterialFileName = "Material.xml"; private readonly string OrderFileName = "Order.xml"; private readonly string DressFileName = "Dress.xml"; + private readonly string ClientFileName = "Client.xml"; public List Components { get; private set; } public List Orders { get; private set; } public List Dresses { get; private set; } + public List Clients { get; private set; } public static DataFileSingleton GetInstance() { if (instance == null) @@ -29,11 +31,13 @@ namespace DressAtelierFileImplement public void SaveComponents() => SaveData(Components, MaterialFileName,"Components", x => x.GetXElement); public void SaveDresses() => SaveData(Dresses, DressFileName,"Dresses", 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(MaterialFileName, "Component", x => Material.Create(x)!)!; Dresses = LoadData(DressFileName, "Dress", x => Dress.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/DressAtelierFileImplement/Implements/ClientStorage.cs b/DressAtelierFileImplement/Implements/ClientStorage.cs new file mode 100644 index 0000000..a151993 --- /dev/null +++ b/DressAtelierFileImplement/Implements/ClientStorage.cs @@ -0,0 +1,68 @@ +using DressAtelierContracts.BindingModels; +using DressAtelierContracts.SearchModels; +using DressAtelierContracts.StorageContracts; +using DressAtelierContracts.ViewModels; +using DressAtelierFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DressAtelierFileImplement.Implements +{ + public class ClientStorage : IClientStorage + { + private readonly DataFileSingleton _source; + + public ClientStorage() + { + _source = DataFileSingleton.GetInstance(); + } + public ClientViewModel? Insert(ClientBindingModel model) + { + model.ID = _source.Clients.Count > 0 ? _source.Clients.Max(client => client.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 client = _source.Clients.FirstOrDefault(x => x.ID == model.ID); + if(client == null) { return null; }; + client.Update(model); + _source.SaveClients(); + return client.GetViewModel; + } + public ClientViewModel? Delete(ClientBindingModel model) + { + var client = _source.Clients.FirstOrDefault(client => client.ID == model.ID); + if(client == null) { return null; } + _source.Clients.Remove(client); + _source.SaveClients(); + return client.GetViewModel; + } + + public ClientViewModel? GetElement(ClientSearchModel model) + { + if(string.IsNullOrEmpty(model.Email) && !model.ID.HasValue) { return null; } + + return _source.Clients.FirstOrDefault(x => (!(string.IsNullOrEmpty(model.Email)) && model.Email == x.Email) || (model.ID.HasValue && model.ID == x.ID))?.GetViewModel; + } + + public List GetFilteredList(ClientSearchModel model) + { + if (string.IsNullOrEmpty(model.Email)) { return new(); } + return _source.Clients.Where(x => x.Email.Equals(model.Email)).Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() + { + return _source.Clients.Select(x => x.GetViewModel).ToList(); + } + + } +} diff --git a/DressAtelierFileImplement/Models/Client.cs b/DressAtelierFileImplement/Models/Client.cs new file mode 100644 index 0000000..3787754 --- /dev/null +++ b/DressAtelierFileImplement/Models/Client.cs @@ -0,0 +1,73 @@ +using DressAtelierContracts.BindingModels; +using DressAtelierContracts.ViewModels; +using DressAtelierDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace DressAtelierFileImplement.Models +{ + public class Client : IClientModel + { + public int ID { get; set; } + public string FullName { get; 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, + FullName = model.FullName, + Email = model.Email, + Password = model.Password + }; + } + + public static Client? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Client() + { + ID = Convert.ToInt32(element.Attribute("ID")!.Value), + FullName = element.Element("FullName")!.Value, + Email = element.Element("Email")!.Value, + Password = element.Element("Password")!.Value + }; + } + + public void Update(ClientBindingModel? model) + { + if (model == null) { return; } + FullName = model.FullName; + Email = model.Email; + Password = model.Password; + } + + public ClientViewModel GetViewModel => new() + { + ID = ID, + FullName = FullName, + Email = Email, + Password = Password + }; + + public XElement GetXElement => new("Client", new XAttribute("ID", ID), + new XElement("FullName", FullName), + new XElement("Email", Email), + new XElement("Password", Password)); + } +} diff --git a/DressAtelierListImplement/DataListSingleton.cs b/DressAtelierListImplement/DataListSingleton.cs index 4c43f36..2070812 100644 --- a/DressAtelierListImplement/DataListSingleton.cs +++ b/DressAtelierListImplement/DataListSingleton.cs @@ -13,11 +13,13 @@ namespace DressAtelierListImplement public List Components { get; set; } public List Orders { get; set; } public List Dresses { get; set; } + public List Clients { get; set; } private DataListSingleton() { Components = new List(); Orders = new List(); Dresses = new List(); + Clients = new List(); } public static DataListSingleton GetInstance() { diff --git a/DressAtelierListImplement/Implements/ClientStorage.cs b/DressAtelierListImplement/Implements/ClientStorage.cs new file mode 100644 index 0000000..6f20f24 --- /dev/null +++ b/DressAtelierListImplement/Implements/ClientStorage.cs @@ -0,0 +1,113 @@ +using DressAtelierContracts.BindingModels; +using DressAtelierContracts.SearchModels; +using DressAtelierContracts.StorageContracts; +using DressAtelierContracts.ViewModels; +using DressAtelierListImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace DressAtelierListImplement.Implements +{ + public class ClientStorage : IClientStorage + { + private readonly DataListSingleton _source; + + public ClientStorage() + { + _source = DataListSingleton.GetInstance(); + } + + 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(model.ID == _source.Clients[i].ID) + { + var element = _source.Clients[i]; + _source.Clients.RemoveAt(i); + return element.GetViewModel; + } + } + return null; + } + + 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) && model.Email == client.Email) || (model.ID.HasValue && model.ID.Value == client.ID)) + { + return client.GetViewModel; + } + } + return null; + } + + 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 List GetFullList() + { + var result = new List(); + foreach (var client in _source.Clients) + { + result.Add(client.GetViewModel); + } + return result; + } + + + } +} diff --git a/DressAtelierListImplement/Models/Client.cs b/DressAtelierListImplement/Models/Client.cs new file mode 100644 index 0000000..8580f51 --- /dev/null +++ b/DressAtelierListImplement/Models/Client.cs @@ -0,0 +1,52 @@ +using DressAtelierContracts.BindingModels; +using DressAtelierContracts.ViewModels; +using DressAtelierDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DressAtelierListImplement.Models +{ + public class Client : IClientModel + { + public int ID { get; set; } + public string FullName { get; 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, + FullName = model.FullName, + Email = model.Email, + Password = model.Password + }; + } + + public void Update(ClientBindingModel? model) + { + if(model == null) { return; } + FullName = model.FullName; + Email = model.Email; + Password = model.Password; + } + + public ClientViewModel GetViewModel => new() + { + ID = ID, + FullName = FullName, + Email = Email, + Password = Password + }; + } +} diff --git a/SewingDresses/FormMain.cs b/SewingDresses/FormMain.cs index 2570840..912b0c2 100644 --- a/SewingDresses/FormMain.cs +++ b/SewingDresses/FormMain.cs @@ -45,6 +45,7 @@ namespace SewingDresses { dataGridView.DataSource = list; dataGridView.Columns["DressID"].Visible = false; + dataGridView.Columns["ClientID"].Visible = false; } } catch (Exception ex) @@ -94,12 +95,7 @@ namespace SewingDresses { var operationResult = _orderLogic.TakeOrderInWork(new OrderBindingModel { - ID = id, - DressID = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["DressID"].Value), - Status = Enum.Parse(dataGridView.SelectedRows[0].Cells["Status"].Value.ToString()), - Count = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Count"].Value), - Sum = double.Parse(dataGridView.SelectedRows[0].Cells["Sum"].Value.ToString()), - DateCreate = DateTime.Parse(dataGridView.SelectedRows[0].Cells["DateCreate"].Value.ToString()), + ID = id, }); if (!operationResult) @@ -128,12 +124,7 @@ namespace SewingDresses { var operationResult = _orderLogic.ReadyOrder(new OrderBindingModel { - ID = id, - DressID = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["DressID"].Value), - Status = Enum.Parse(dataGridView.SelectedRows[0].Cells["Status"].Value.ToString()), - Count = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Count"].Value), - Sum = double.Parse(dataGridView.SelectedRows[0].Cells["Sum"].Value.ToString()), - DateCreate = DateTime.Parse(dataGridView.SelectedRows[0].Cells["DateCreate"].Value.ToString()), + ID = id, }); if (!operationResult) @@ -162,12 +153,7 @@ namespace SewingDresses { var operationResult = _orderLogic.GivenOrder(new OrderBindingModel { - ID = id, - DressID = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["DressID"].Value), - Status = Enum.Parse(dataGridView.SelectedRows[0].Cells["Status"].Value.ToString()), - Count = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Count"].Value), - Sum = double.Parse(dataGridView.SelectedRows[0].Cells["Sum"].Value.ToString()), - DateCreate = DateTime.Parse(dataGridView.SelectedRows[0].Cells["DateCreate"].Value.ToString()), + ID = id, }); if (!operationResult) { diff --git a/SewingDresses/FormOrderCreation.Designer.cs b/SewingDresses/FormOrderCreation.Designer.cs index 5d6c1c7..531971d 100644 --- a/SewingDresses/FormOrderCreation.Designer.cs +++ b/SewingDresses/FormOrderCreation.Designer.cs @@ -28,111 +28,133 @@ /// private void InitializeComponent() { - this.dressLabel = new System.Windows.Forms.Label(); - this.quantityLabel = new System.Windows.Forms.Label(); - this.priceLabel = new System.Windows.Forms.Label(); - this.dressComboBox = new System.Windows.Forms.ComboBox(); - this.quantityTextBox = new System.Windows.Forms.TextBox(); - this.priceTextBox = new System.Windows.Forms.TextBox(); - this.ButtonSave = new System.Windows.Forms.Button(); - this.ButtonCancel = new System.Windows.Forms.Button(); - this.SuspendLayout(); + dressLabel = new Label(); + quantityLabel = new Label(); + priceLabel = new Label(); + dressComboBox = new ComboBox(); + quantityTextBox = new TextBox(); + priceTextBox = new TextBox(); + ButtonSave = new Button(); + ButtonCancel = new Button(); + labelClient = new Label(); + comboBoxClients = new ComboBox(); + SuspendLayout(); // // dressLabel // - this.dressLabel.AutoSize = true; - this.dressLabel.Font = new System.Drawing.Font("Segoe UI", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); - this.dressLabel.Location = new System.Drawing.Point(12, 9); - this.dressLabel.Name = "dressLabel"; - this.dressLabel.Size = new System.Drawing.Size(82, 25); - this.dressLabel.TabIndex = 0; - this.dressLabel.Text = "Product:"; + dressLabel.AutoSize = true; + dressLabel.Font = new Font("Segoe UI", 14.25F, FontStyle.Regular, GraphicsUnit.Point); + dressLabel.Location = new Point(12, 9); + dressLabel.Name = "dressLabel"; + dressLabel.Size = new Size(82, 25); + dressLabel.TabIndex = 0; + dressLabel.Text = "Product:"; // // quantityLabel // - this.quantityLabel.AutoSize = true; - this.quantityLabel.Font = new System.Drawing.Font("Segoe UI", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); - this.quantityLabel.Location = new System.Drawing.Point(12, 50); - this.quantityLabel.Name = "quantityLabel"; - this.quantityLabel.Size = new System.Drawing.Size(88, 25); - this.quantityLabel.TabIndex = 1; - this.quantityLabel.Text = "Quantity:"; + quantityLabel.AutoSize = true; + quantityLabel.Font = new Font("Segoe UI", 14.25F, FontStyle.Regular, GraphicsUnit.Point); + quantityLabel.Location = new Point(12, 78); + quantityLabel.Name = "quantityLabel"; + quantityLabel.Size = new Size(88, 25); + quantityLabel.TabIndex = 1; + quantityLabel.Text = "Quantity:"; // // priceLabel // - this.priceLabel.AutoSize = true; - this.priceLabel.Font = new System.Drawing.Font("Segoe UI", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); - this.priceLabel.Location = new System.Drawing.Point(12, 90); - this.priceLabel.Name = "priceLabel"; - this.priceLabel.Size = new System.Drawing.Size(56, 25); - this.priceLabel.TabIndex = 2; - this.priceLabel.Text = "Total:"; + priceLabel.AutoSize = true; + priceLabel.Font = new Font("Segoe UI", 14.25F, FontStyle.Regular, GraphicsUnit.Point); + priceLabel.Location = new Point(12, 118); + priceLabel.Name = "priceLabel"; + priceLabel.Size = new Size(56, 25); + priceLabel.TabIndex = 2; + priceLabel.Text = "Total:"; // // dressComboBox // - this.dressComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; - this.dressComboBox.FormattingEnabled = true; - this.dressComboBox.Location = new System.Drawing.Point(114, 12); - this.dressComboBox.Name = "dressComboBox"; - this.dressComboBox.Size = new System.Drawing.Size(320, 23); - this.dressComboBox.TabIndex = 3; - this.dressComboBox.SelectedIndexChanged += new System.EventHandler(this.DressComboBox_SelectedIndexChanged); + dressComboBox.DropDownStyle = ComboBoxStyle.DropDownList; + dressComboBox.FormattingEnabled = true; + dressComboBox.Location = new Point(114, 12); + dressComboBox.Name = "dressComboBox"; + dressComboBox.Size = new Size(320, 23); + dressComboBox.TabIndex = 3; + dressComboBox.SelectedIndexChanged += DressComboBox_SelectedIndexChanged; // // quantityTextBox // - this.quantityTextBox.Location = new System.Drawing.Point(114, 52); - this.quantityTextBox.Name = "quantityTextBox"; - this.quantityTextBox.Size = new System.Drawing.Size(320, 23); - this.quantityTextBox.TabIndex = 4; - this.quantityTextBox.TextChanged += new System.EventHandler(this.QuantityTextBox_TextChanged); + quantityTextBox.Location = new Point(114, 80); + quantityTextBox.Name = "quantityTextBox"; + quantityTextBox.Size = new Size(320, 23); + quantityTextBox.TabIndex = 4; + quantityTextBox.TextChanged += QuantityTextBox_TextChanged; // // priceTextBox // - this.priceTextBox.Location = new System.Drawing.Point(114, 90); - this.priceTextBox.Name = "priceTextBox"; - this.priceTextBox.ReadOnly = true; - this.priceTextBox.Size = new System.Drawing.Size(320, 23); - this.priceTextBox.TabIndex = 5; + priceTextBox.Location = new Point(114, 118); + priceTextBox.Name = "priceTextBox"; + priceTextBox.ReadOnly = true; + priceTextBox.Size = new Size(320, 23); + priceTextBox.TabIndex = 5; // // ButtonSave // - this.ButtonSave.Location = new System.Drawing.Point(220, 138); - this.ButtonSave.Name = "ButtonSave"; - this.ButtonSave.Size = new System.Drawing.Size(100, 32); - this.ButtonSave.TabIndex = 6; - this.ButtonSave.Text = "Save"; - this.ButtonSave.UseVisualStyleBackColor = true; - this.ButtonSave.Click += new System.EventHandler(this.ButtonSave_Click); + ButtonSave.Location = new Point(220, 166); + ButtonSave.Name = "ButtonSave"; + ButtonSave.Size = new Size(100, 32); + ButtonSave.TabIndex = 6; + ButtonSave.Text = "Save"; + ButtonSave.UseVisualStyleBackColor = true; + ButtonSave.Click += ButtonSave_Click; // // ButtonCancel // - this.ButtonCancel.Location = new System.Drawing.Point(334, 138); - this.ButtonCancel.Name = "ButtonCancel"; - this.ButtonCancel.Size = new System.Drawing.Size(100, 32); - this.ButtonCancel.TabIndex = 7; - this.ButtonCancel.Text = "Cancel"; - this.ButtonCancel.UseVisualStyleBackColor = true; - this.ButtonCancel.Click += new System.EventHandler(this.ButtonCancel_Click); + ButtonCancel.Location = new Point(334, 166); + ButtonCancel.Name = "ButtonCancel"; + ButtonCancel.Size = new Size(100, 32); + ButtonCancel.TabIndex = 7; + ButtonCancel.Text = "Cancel"; + ButtonCancel.UseVisualStyleBackColor = true; + ButtonCancel.Click += ButtonCancel_Click; + // + // labelClient + // + labelClient.AutoSize = true; + labelClient.Font = new Font("Segoe UI", 14.25F, FontStyle.Regular, GraphicsUnit.Point); + labelClient.Location = new Point(12, 43); + labelClient.Name = "labelClient"; + labelClient.Size = new Size(65, 25); + labelClient.TabIndex = 8; + labelClient.Text = "Client:"; + // + // comboBoxClients + // + comboBoxClients.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxClients.FormattingEnabled = true; + comboBoxClients.Location = new Point(114, 43); + comboBoxClients.Name = "comboBoxClients"; + comboBoxClients.Size = new Size(320, 23); + comboBoxClients.TabIndex = 9; // // FormOrderCreation // - this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(446, 181); - this.Controls.Add(this.ButtonCancel); - this.Controls.Add(this.ButtonSave); - this.Controls.Add(this.priceTextBox); - this.Controls.Add(this.quantityTextBox); - this.Controls.Add(this.dressComboBox); - this.Controls.Add(this.priceLabel); - this.Controls.Add(this.quantityLabel); - this.Controls.Add(this.dressLabel); - this.Name = "FormOrderCreation"; - this.Text = "FormOrderCreation"; - this.Load += new System.EventHandler(this.FormOrderCreation_Load); - this.ResumeLayout(false); - this.PerformLayout(); - + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(446, 224); + Controls.Add(comboBoxClients); + Controls.Add(labelClient); + Controls.Add(ButtonCancel); + Controls.Add(ButtonSave); + Controls.Add(priceTextBox); + Controls.Add(quantityTextBox); + Controls.Add(dressComboBox); + Controls.Add(priceLabel); + Controls.Add(quantityLabel); + Controls.Add(dressLabel); + Name = "FormOrderCreation"; + Text = "FormOrderCreation"; + Load += FormOrderCreation_Load; + ResumeLayout(false); + PerformLayout(); } #endregion @@ -145,5 +167,7 @@ private TextBox priceTextBox; private Button ButtonSave; private Button ButtonCancel; + private Label labelClient; + private ComboBox comboBoxClients; } } \ No newline at end of file diff --git a/SewingDresses/FormOrderCreation.cs b/SewingDresses/FormOrderCreation.cs index 35bbfee..b1f0640 100644 --- a/SewingDresses/FormOrderCreation.cs +++ b/SewingDresses/FormOrderCreation.cs @@ -19,14 +19,16 @@ namespace SewingDresses private readonly ILogger _logger; private readonly IDressLogic _logicDress; + private readonly IClientLogic _logicClient; private readonly IOrderLogic _logicOrder; - public FormOrderCreation(ILogger logger, IDressLogic logicP, IOrderLogic logicO) + public FormOrderCreation(ILogger logger, IDressLogic logicP, IOrderLogic logicO, IClientLogic logicC) { InitializeComponent(); _logger = logger; _logicDress = logicP; _logicOrder = logicO; + _logicClient = logicC; } private void FormOrderCreation_Load(object sender, EventArgs e) @@ -48,6 +50,24 @@ namespace SewingDresses _logger.LogError(ex, "Downloading dresses for order error"); MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } + + _logger.LogInformation("Downloading clients for order"); + try + { + var _list = _logicClient.ReadList(null); + if (_list != null) + { + comboBoxClients.DisplayMember = "FullName"; + comboBoxClients.ValueMember = "ID"; + comboBoxClients.DataSource = _list; + comboBoxClients.SelectedItem = null; + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Downloading clients for order error"); + MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + } } private void CalcSum() @@ -69,7 +89,7 @@ namespace SewingDresses catch (Exception ex) { _logger.LogError(ex, "Calculation total of order error"); - MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK,MessageBoxIcon.Error); + MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } @@ -86,7 +106,7 @@ namespace SewingDresses { if (string.IsNullOrEmpty(quantityTextBox.Text)) { - MessageBox.Show("Fill quantity field", "Error",MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show("Fill quantity field", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (dressComboBox.SelectedValue == null) @@ -95,12 +115,20 @@ namespace SewingDresses MessageBoxButtons.OK, MessageBoxIcon.Error); return; } + if (comboBoxClients.SelectedValue == null) + { + MessageBox.Show("Choose client", "Error", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Creation of order"); try { var operationResult = _logicOrder.CreateOrder(new OrderBindingModel { DressID = Convert.ToInt32(dressComboBox.SelectedValue), + ClientID = Convert.ToInt32(comboBoxClients.SelectedValue), Count = Convert.ToInt32(quantityTextBox.Text), Sum = Convert.ToDouble(priceTextBox.Text) });