From 79a61aca116c6d2773ed8160d1d04f622e3577a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D0=B5=D0=BC=20=D0=A5=D0=B0=D1=80=D0=BB?= =?UTF-8?q?=D0=B0=D0=BC=D0=BE=D0=B2?= Date: Thu, 27 Apr 2023 23:54:56 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=B0=D1=8F=20=E2=84=965=20-=20=D0=A4=D0=B8?= =?UTF-8?q?=D0=BA=D1=81=D0=B0=D1=86=D0=B8=D1=8F=20=D0=B8=D0=B7=D0=BC=D0=B5?= =?UTF-8?q?=D0=BD=D0=B5=D0=BD=D0=B8=D0=B9=200.2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/ClientLogic.cs | 122 ++++++++++ .../BindingModels/ClientBindingModel.cs | 18 ++ .../BindingModels/OrderBindingModel.cs | 1 + .../BusinessLogicsContracts/IClientLogic.cs | 20 ++ .../SearchModels/ClientSearchModel.cs | 15 ++ .../SearchModels/OrderSearchModel.cs | 1 + .../StoragesContracts/IClientStorage.cs | 21 ++ .../ViewModels/ClientViewModel.cs | 22 ++ .../ViewModels/OrderViewModel.cs | 3 + .../Models/IClientModel.cs | 16 ++ .../AbstractFoodOrdersDatabase.cs | 1 + .../Implements/ClientStorage.cs | 95 ++++++++ .../Implements/OrderStorage.cs | 35 ++- .../20230427091253_ClientUpdate.Designer.cs | 214 ++++++++++++++++++ .../Migrations/20230427091253_ClientUpdate.cs | 68 ++++++ ...AbstractFoodOrdersDatabaseModelSnapshot.cs | 45 +++- .../Models/Client.cs | 67 ++++++ .../Models/Order.cs | 9 +- .../DataListSingleton.cs | 2 + .../Implements/ClientStorage.cs | 2 +- .../Implements/OrderStorage.cs | 31 ++- .../Models/Order.cs | 4 + .../DataFileSingleton.cs | 14 +- .../Implements/ClientStorage.cs | 85 +++++++ .../Implements/OrderStorage.cs | 10 +- .../Models/Client.cs | 70 ++++++ .../AbstractShopFileImplement/Models/Order.cs | 6 + FoodOrders/FoodOrders/FormClients.Designer.cs | 92 ++++++++ FoodOrders/FoodOrders/FormClients.cs | 84 +++++++ FoodOrders/FoodOrders/FormClients.resx | 60 +++++ .../FoodOrders/FormCreateOrder.Designer.cs | 166 ++++++++------ FoodOrders/FoodOrders/FormCreateOrder.cs | 35 ++- 32 files changed, 1302 insertions(+), 132 deletions(-) create mode 100644 FoodOrders/AbstractFoodOrdersBusinessLogic/BusinessLogics/ClientLogic.cs create mode 100644 FoodOrders/AbstractFoodOrdersContracts/BindingModels/ClientBindingModel.cs create mode 100644 FoodOrders/AbstractFoodOrdersContracts/BusinessLogicsContracts/IClientLogic.cs create mode 100644 FoodOrders/AbstractFoodOrdersContracts/SearchModels/ClientSearchModel.cs create mode 100644 FoodOrders/AbstractFoodOrdersContracts/StoragesContracts/IClientStorage.cs create mode 100644 FoodOrders/AbstractFoodOrdersContracts/ViewModels/ClientViewModel.cs create mode 100644 FoodOrders/AbstractFoodOrdersDataModels/Models/IClientModel.cs create mode 100644 FoodOrders/AbstractFoodOrdersDatabaseImplement/Implements/ClientStorage.cs create mode 100644 FoodOrders/AbstractFoodOrdersDatabaseImplement/Migrations/20230427091253_ClientUpdate.Designer.cs create mode 100644 FoodOrders/AbstractFoodOrdersDatabaseImplement/Migrations/20230427091253_ClientUpdate.cs create mode 100644 FoodOrders/AbstractFoodOrdersDatabaseImplement/Models/Client.cs create mode 100644 FoodOrders/AbstractShopFileImplement/Implements/ClientStorage.cs create mode 100644 FoodOrders/AbstractShopFileImplement/Models/Client.cs create mode 100644 FoodOrders/FoodOrders/FormClients.Designer.cs create mode 100644 FoodOrders/FoodOrders/FormClients.cs create mode 100644 FoodOrders/FoodOrders/FormClients.resx diff --git a/FoodOrders/AbstractFoodOrdersBusinessLogic/BusinessLogics/ClientLogic.cs b/FoodOrders/AbstractFoodOrdersBusinessLogic/BusinessLogics/ClientLogic.cs new file mode 100644 index 0000000..ddd7420 --- /dev/null +++ b/FoodOrders/AbstractFoodOrdersBusinessLogic/BusinessLogics/ClientLogic.cs @@ -0,0 +1,122 @@ +using AbstractFoodOrdersContracts.BindingModels; +using AbstractFoodOrdersContracts.BusinessLogicsContracts; +using AbstractFoodOrdersContracts.SearchModels; +using AbstractFoodOrdersContracts.StoragesContracts; +using AbstractFoodOrdersContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AbstractFoodOrdersBusinessLogic.BusinessLogics +{ + public class ClientLogic:IClientLogic + { + private readonly ILogger _logger; + private readonly IClientStorage _clientStorage; + + public ClientLogic(ILogger logger, IClientStorage clientStorage) + { + _logger = logger; + _clientStorage = clientStorage; + } + + public List? ReadList(ClientSearchModel? model) + { + _logger.LogInformation("ReadList. Id:{Id}. Email:{Email}. Password:{Password}", model?.Id, model?.Email, model?.Password); + 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 ClientViewModel? ReadElement(ClientSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. Id:{Id}. Email:{Email}. Password:{Password}", model.Id, model.Email, model.Password); + 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 bool Create(ClientBindingModel model) + { + CheckModel(model); + if (_clientStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(ClientBindingModel model) + { + CheckModel(model); + if (_clientStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool Delete(ClientBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_clientStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + private void CheckModel(ClientBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.ClientFIO)) + { + throw new ArgumentNullException("Нет ФИО клиента", nameof(model.ClientFIO)); + } + if (string.IsNullOrEmpty(model.Password)) + { + throw new ArgumentNullException("Нет пароля пользователя", nameof(model.Password)); + } + if (string.IsNullOrEmpty(model.Email)) + { + throw new ArgumentNullException("Нет электронной почты клиента", nameof(model.Email)); + } + _logger.LogInformation("Client. ClientFIO:{ClientFIO}. Password:{Password}. Email:{Email}. Id:{Id}", model.ClientFIO, model.Password, model.Email, model.Id); + var element = _clientStorage.GetElement(new ClientSearchModel + { + Email = model.Email + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Клиент с таким логином уже зарегистрирован"); + } + } + } +} diff --git a/FoodOrders/AbstractFoodOrdersContracts/BindingModels/ClientBindingModel.cs b/FoodOrders/AbstractFoodOrdersContracts/BindingModels/ClientBindingModel.cs new file mode 100644 index 0000000..7cd9e96 --- /dev/null +++ b/FoodOrders/AbstractFoodOrdersContracts/BindingModels/ClientBindingModel.cs @@ -0,0 +1,18 @@ +using AbstractFoodOrdersDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AbstractFoodOrdersContracts.BindingModels +{ + public class ClientBindingModel:IClientModel + { + 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/FoodOrders/AbstractFoodOrdersContracts/BindingModels/OrderBindingModel.cs b/FoodOrders/AbstractFoodOrdersContracts/BindingModels/OrderBindingModel.cs index bdac97b..dcf95a5 100644 --- a/FoodOrders/AbstractFoodOrdersContracts/BindingModels/OrderBindingModel.cs +++ b/FoodOrders/AbstractFoodOrdersContracts/BindingModels/OrderBindingModel.cs @@ -12,6 +12,7 @@ namespace AbstractFoodOrdersContracts.BindingModels { public int Id { get; set; } public int DishId { get; set; } + public int ClientId { get; set; } public int Count { get; set; } public double Sum { get; set; } public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; diff --git a/FoodOrders/AbstractFoodOrdersContracts/BusinessLogicsContracts/IClientLogic.cs b/FoodOrders/AbstractFoodOrdersContracts/BusinessLogicsContracts/IClientLogic.cs new file mode 100644 index 0000000..c1d5e59 --- /dev/null +++ b/FoodOrders/AbstractFoodOrdersContracts/BusinessLogicsContracts/IClientLogic.cs @@ -0,0 +1,20 @@ +using AbstractFoodOrdersContracts.BindingModels; +using AbstractFoodOrdersContracts.SearchModels; +using AbstractFoodOrdersContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AbstractFoodOrdersContracts.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/FoodOrders/AbstractFoodOrdersContracts/SearchModels/ClientSearchModel.cs b/FoodOrders/AbstractFoodOrdersContracts/SearchModels/ClientSearchModel.cs new file mode 100644 index 0000000..3fe4401 --- /dev/null +++ b/FoodOrders/AbstractFoodOrdersContracts/SearchModels/ClientSearchModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AbstractFoodOrdersContracts.SearchModels +{ + public class ClientSearchModel + { + public int? Id { get; set; } + public string? Email { get; set; } + public string? Password { get; set; } + } +} diff --git a/FoodOrders/AbstractFoodOrdersContracts/SearchModels/OrderSearchModel.cs b/FoodOrders/AbstractFoodOrdersContracts/SearchModels/OrderSearchModel.cs index ee8dc4a..7b6c7c1 100644 --- a/FoodOrders/AbstractFoodOrdersContracts/SearchModels/OrderSearchModel.cs +++ b/FoodOrders/AbstractFoodOrdersContracts/SearchModels/OrderSearchModel.cs @@ -11,5 +11,6 @@ namespace AbstractFoodOrdersContracts.SearchModels public int? Id { get; set; } public DateTime? DateFrom { get; set; } public DateTime? DateTo { get; set; } + public int? ClientId { get; set; } } } diff --git a/FoodOrders/AbstractFoodOrdersContracts/StoragesContracts/IClientStorage.cs b/FoodOrders/AbstractFoodOrdersContracts/StoragesContracts/IClientStorage.cs new file mode 100644 index 0000000..11fdf65 --- /dev/null +++ b/FoodOrders/AbstractFoodOrdersContracts/StoragesContracts/IClientStorage.cs @@ -0,0 +1,21 @@ +using AbstractFoodOrdersContracts.BindingModels; +using AbstractFoodOrdersContracts.SearchModels; +using AbstractFoodOrdersContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AbstractFoodOrdersContracts.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/FoodOrders/AbstractFoodOrdersContracts/ViewModels/ClientViewModel.cs b/FoodOrders/AbstractFoodOrdersContracts/ViewModels/ClientViewModel.cs new file mode 100644 index 0000000..2420e0d --- /dev/null +++ b/FoodOrders/AbstractFoodOrdersContracts/ViewModels/ClientViewModel.cs @@ -0,0 +1,22 @@ +using AbstractFoodOrdersDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AbstractFoodOrdersContracts.ViewModels +{ + public class ClientViewModel:IClientModel + { + 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/FoodOrders/AbstractFoodOrdersContracts/ViewModels/OrderViewModel.cs b/FoodOrders/AbstractFoodOrdersContracts/ViewModels/OrderViewModel.cs index 15a83a9..db1708f 100644 --- a/FoodOrders/AbstractFoodOrdersContracts/ViewModels/OrderViewModel.cs +++ b/FoodOrders/AbstractFoodOrdersContracts/ViewModels/OrderViewModel.cs @@ -16,6 +16,9 @@ namespace AbstractFoodOrdersContracts.ViewModels public int DishId { get; set; } [DisplayName("Изделие")] public string DishName { get; set; } = string.Empty; + public int ClientId { get; set; } + [DisplayName("ФИО клиента")] + public string ClientFIO { get; set; } = string.Empty; [DisplayName("Количество")] public int Count { get; set; } [DisplayName("Сумма")] diff --git a/FoodOrders/AbstractFoodOrdersDataModels/Models/IClientModel.cs b/FoodOrders/AbstractFoodOrdersDataModels/Models/IClientModel.cs new file mode 100644 index 0000000..233e9c7 --- /dev/null +++ b/FoodOrders/AbstractFoodOrdersDataModels/Models/IClientModel.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AbstractFoodOrdersDataModels.Models +{ + public interface IClientModel:IId + { + string ClientFIO { get; } + string Email { get; } + string Password { get; } + + } +} diff --git a/FoodOrders/AbstractFoodOrdersDatabaseImplement/AbstractFoodOrdersDatabase.cs b/FoodOrders/AbstractFoodOrdersDatabaseImplement/AbstractFoodOrdersDatabase.cs index 977eb92..4c29590 100644 --- a/FoodOrders/AbstractFoodOrdersDatabaseImplement/AbstractFoodOrdersDatabase.cs +++ b/FoodOrders/AbstractFoodOrdersDatabaseImplement/AbstractFoodOrdersDatabase.cs @@ -25,5 +25,6 @@ namespace AbstractFoodOrdersDatabaseImplement public virtual DbSet Dishes { set; get; } public virtual DbSet DishComponents { set; get; } public virtual DbSet Orders { set; get; } + public virtual DbSet Clients { set; get; } } } diff --git a/FoodOrders/AbstractFoodOrdersDatabaseImplement/Implements/ClientStorage.cs b/FoodOrders/AbstractFoodOrdersDatabaseImplement/Implements/ClientStorage.cs new file mode 100644 index 0000000..ec4811b --- /dev/null +++ b/FoodOrders/AbstractFoodOrdersDatabaseImplement/Implements/ClientStorage.cs @@ -0,0 +1,95 @@ +using AbstractFoodOrdersContracts.BindingModels; +using AbstractFoodOrdersContracts.SearchModels; +using AbstractFoodOrdersContracts.StoragesContracts; +using AbstractFoodOrdersContracts.ViewModels; +using AbstractFoodOrdersDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AbstractFoodOrdersDatabaseImplement.Implements +{ + public class ClientStorage:IClientStorage + { + public List GetFullList() + { + using var context = new AbstractFoodOrdersDatabase(); + return context.Clients + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(ClientSearchModel model) + { + if (string.IsNullOrEmpty(model.Email) && !model.Id.HasValue && string.IsNullOrEmpty(model.Password)) + { + return new(); + } + if (!string.IsNullOrEmpty(model.Email) && !string.IsNullOrEmpty(model.Password)) + { + using var context = new AbstractFoodOrdersDatabase(); + return context.Clients + .Where(x => x.Email.Equals(model.Email) && x.Password.Equals(model.Email)) + .Select(x => x.GetViewModel) + .ToList(); + } + else + { + using var context = new AbstractFoodOrdersDatabase(); + return context.Clients + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + } + public ClientViewModel? GetElement(ClientSearchModel model) + { + if (string.IsNullOrEmpty(model.Email) && !model.Id.HasValue) + { + return null; + } + using var context = new AbstractFoodOrdersDatabase(); + return context.Clients + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.Email) && x.Email == model.Email) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + public ClientViewModel? Insert(ClientBindingModel model) + { + var newClient = Client.Create(model); + if (newClient == null) + { + return null; + } + using var context = new AbstractFoodOrdersDatabase(); + context.Clients.Add(newClient); + context.SaveChanges(); + return newClient.GetViewModel; + } + public ClientViewModel? Update(ClientBindingModel model) + { + using var context = new AbstractFoodOrdersDatabase(); + 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 AbstractFoodOrdersDatabase(); + 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/FoodOrders/AbstractFoodOrdersDatabaseImplement/Implements/OrderStorage.cs b/FoodOrders/AbstractFoodOrdersDatabaseImplement/Implements/OrderStorage.cs index 085e33a..5f575fc 100644 --- a/FoodOrders/AbstractFoodOrdersDatabaseImplement/Implements/OrderStorage.cs +++ b/FoodOrders/AbstractFoodOrdersDatabaseImplement/Implements/OrderStorage.cs @@ -17,32 +17,24 @@ namespace AbstractFoodOrdersDatabaseImplement.Implements public List GetFullList() { using var context = new AbstractFoodOrdersDatabase(); - return context.Orders + return context.Orders.Include(x => x.Client) .Select(x => AccessDishStorage(x.GetViewModel, context)) .ToList(); } public List GetFilteredList(OrderSearchModel model) { - if (!model.Id.HasValue && !model.DateFrom.HasValue && !model.DateTo.HasValue) + if ((!model.DateFrom.HasValue || !model.DateTo.HasValue) && !model.ClientId.HasValue) { return new(); } - if (!model.DateFrom.HasValue || !model.DateTo.HasValue) - { - using var context = new AbstractFoodOrdersDatabase(); - return context.Orders - .Where(x => x.Id == model.Id) - .Select(x => AccessDishStorage(x.GetViewModel, context)) + using var context = new AbstractFoodOrdersDatabase(); + return context.Orders + .Include(x => x.Dish) + .Include(x => x.Client) + .Where(x => (model.DateFrom.HasValue && model.DateTo.HasValue && x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo) || + (x.ClientId == model.ClientId)) + .Select(x => x.GetViewModel) .ToList(); - } - else - { - using var context = new AbstractFoodOrdersDatabase(); - return context.Orders - .Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo) - .Select(x => AccessDishStorage(x.GetViewModel, context)) - .ToList(); - } } public OrderViewModel? GetElement(OrderSearchModel model) { @@ -51,18 +43,19 @@ namespace AbstractFoodOrdersDatabaseImplement.Implements return new(); } using var context = new AbstractFoodOrdersDatabase(); - return AccessDishStorage(context.Orders + return AccessDishStorage(context.Orders.Include(x => x.Client) .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) ?.GetViewModel, context); } public OrderViewModel? Insert(OrderBindingModel model) - { - var newOrder = Order.Create(model); + { + using var context = new AbstractFoodOrdersDatabase(); + var newOrder = Order.Create(context, model); if (newOrder == null) { return null; } - using var context = new AbstractFoodOrdersDatabase(); + context.Orders.Add(newOrder); context.SaveChanges(); return AccessDishStorage(newOrder.GetViewModel, context); diff --git a/FoodOrders/AbstractFoodOrdersDatabaseImplement/Migrations/20230427091253_ClientUpdate.Designer.cs b/FoodOrders/AbstractFoodOrdersDatabaseImplement/Migrations/20230427091253_ClientUpdate.Designer.cs new file mode 100644 index 0000000..e3e80b4 --- /dev/null +++ b/FoodOrders/AbstractFoodOrdersDatabaseImplement/Migrations/20230427091253_ClientUpdate.Designer.cs @@ -0,0 +1,214 @@ +// +using System; +using AbstractFoodOrdersDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace AbstractFoodOrdersDatabaseImplement.Migrations +{ + [DbContext(typeof(AbstractFoodOrdersDatabase))] + [Migration("20230427091253_ClientUpdate")] + partial class ClientUpdate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.5") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("AbstractFoodOrdersDatabaseImplement.Models.Client", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ClientFIO") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Email") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Password") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Clients"); + }); + + modelBuilder.Entity("AbstractFoodOrdersDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("AbstractFoodOrdersDatabaseImplement.Models.Dish", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("DishName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Dishes"); + }); + + modelBuilder.Entity("AbstractFoodOrdersDatabaseImplement.Models.DishComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DishId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("DishId"); + + b.ToTable("DishComponents"); + }); + + modelBuilder.Entity("AbstractFoodOrdersDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ClientId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("DishId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("ClientId"); + + b.HasIndex("DishId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("AbstractFoodOrdersDatabaseImplement.Models.DishComponent", b => + { + b.HasOne("AbstractFoodOrdersDatabaseImplement.Models.Component", "Component") + .WithMany("DishComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("AbstractFoodOrdersDatabaseImplement.Models.Dish", "Dish") + .WithMany("Components") + .HasForeignKey("DishId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Dish"); + }); + + modelBuilder.Entity("AbstractFoodOrdersDatabaseImplement.Models.Order", b => + { + b.HasOne("AbstractFoodOrdersDatabaseImplement.Models.Client", "Client") + .WithMany("ClientOrders") + .HasForeignKey("ClientId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("AbstractFoodOrdersDatabaseImplement.Models.Dish", "Dish") + .WithMany("Orders") + .HasForeignKey("DishId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Client"); + + b.Navigation("Dish"); + }); + + modelBuilder.Entity("AbstractFoodOrdersDatabaseImplement.Models.Client", b => + { + b.Navigation("ClientOrders"); + }); + + modelBuilder.Entity("AbstractFoodOrdersDatabaseImplement.Models.Component", b => + { + b.Navigation("DishComponents"); + }); + + modelBuilder.Entity("AbstractFoodOrdersDatabaseImplement.Models.Dish", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/FoodOrders/AbstractFoodOrdersDatabaseImplement/Migrations/20230427091253_ClientUpdate.cs b/FoodOrders/AbstractFoodOrdersDatabaseImplement/Migrations/20230427091253_ClientUpdate.cs new file mode 100644 index 0000000..3663687 --- /dev/null +++ b/FoodOrders/AbstractFoodOrdersDatabaseImplement/Migrations/20230427091253_ClientUpdate.cs @@ -0,0 +1,68 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace AbstractFoodOrdersDatabaseImplement.Migrations +{ + /// + public partial class ClientUpdate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "ClientId", + table: "Orders", + type: "int", + nullable: false, + defaultValue: 0); + + migrationBuilder.CreateTable( + name: "Clients", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ClientFIO = table.Column(type: "nvarchar(max)", nullable: false), + Password = table.Column(type: "nvarchar(max)", nullable: false), + Email = table.Column(type: "nvarchar(max)", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Clients", x => x.Id); + }); + + migrationBuilder.CreateIndex( + name: "IX_Orders_ClientId", + table: "Orders", + column: "ClientId"); + + migrationBuilder.AddForeignKey( + name: "FK_Orders_Clients_ClientId", + table: "Orders", + column: "ClientId", + principalTable: "Clients", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_Orders_Clients_ClientId", + table: "Orders"); + + migrationBuilder.DropTable( + name: "Clients"); + + migrationBuilder.DropIndex( + name: "IX_Orders_ClientId", + table: "Orders"); + + migrationBuilder.DropColumn( + name: "ClientId", + table: "Orders"); + } + } +} diff --git a/FoodOrders/AbstractFoodOrdersDatabaseImplement/Migrations/AbstractFoodOrdersDatabaseModelSnapshot.cs b/FoodOrders/AbstractFoodOrdersDatabaseImplement/Migrations/AbstractFoodOrdersDatabaseModelSnapshot.cs index 3098f2b..9afb950 100644 --- a/FoodOrders/AbstractFoodOrdersDatabaseImplement/Migrations/AbstractFoodOrdersDatabaseModelSnapshot.cs +++ b/FoodOrders/AbstractFoodOrdersDatabaseImplement/Migrations/AbstractFoodOrdersDatabaseModelSnapshot.cs @@ -17,11 +17,36 @@ namespace AbstractFoodOrdersDatabaseImplement.Migrations { #pragma warning disable 612, 618 modelBuilder - .HasAnnotation("ProductVersion", "7.0.4") + .HasAnnotation("ProductVersion", "7.0.5") .HasAnnotation("Relational:MaxIdentifierLength", 128); SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + modelBuilder.Entity("AbstractFoodOrdersDatabaseImplement.Models.Client", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ClientFIO") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Email") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Password") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Clients"); + }); + modelBuilder.Entity("AbstractFoodOrdersDatabaseImplement.Models.Component", b => { b.Property("Id") @@ -96,6 +121,9 @@ namespace AbstractFoodOrdersDatabaseImplement.Migrations SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + b.Property("ClientId") + .HasColumnType("int"); + b.Property("Count") .HasColumnType("int"); @@ -116,6 +144,8 @@ namespace AbstractFoodOrdersDatabaseImplement.Migrations b.HasKey("Id"); + b.HasIndex("ClientId"); + b.HasIndex("DishId"); b.ToTable("Orders"); @@ -142,15 +172,28 @@ namespace AbstractFoodOrdersDatabaseImplement.Migrations modelBuilder.Entity("AbstractFoodOrdersDatabaseImplement.Models.Order", b => { + b.HasOne("AbstractFoodOrdersDatabaseImplement.Models.Client", "Client") + .WithMany("ClientOrders") + .HasForeignKey("ClientId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + b.HasOne("AbstractFoodOrdersDatabaseImplement.Models.Dish", "Dish") .WithMany("Orders") .HasForeignKey("DishId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); + b.Navigation("Client"); + b.Navigation("Dish"); }); + modelBuilder.Entity("AbstractFoodOrdersDatabaseImplement.Models.Client", b => + { + b.Navigation("ClientOrders"); + }); + modelBuilder.Entity("AbstractFoodOrdersDatabaseImplement.Models.Component", b => { b.Navigation("DishComponents"); diff --git a/FoodOrders/AbstractFoodOrdersDatabaseImplement/Models/Client.cs b/FoodOrders/AbstractFoodOrdersDatabaseImplement/Models/Client.cs new file mode 100644 index 0000000..2adcc17 --- /dev/null +++ b/FoodOrders/AbstractFoodOrdersDatabaseImplement/Models/Client.cs @@ -0,0 +1,67 @@ +using AbstractFoodOrdersContracts.BindingModels; +using AbstractFoodOrdersContracts.ViewModels; +using AbstractFoodOrdersDataModels.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; + +namespace AbstractFoodOrdersDatabaseImplement.Models +{ + public class Client:IClientModel + { + public int Id { get; set; } + [Required] + public string ClientFIO { get; private set; } = string.Empty; + [Required] + public string Password { get; set; } = string.Empty; + [Required] + public string Email { get; private set; } = string.Empty; + [ForeignKey("ClientId")] + public virtual List ClientOrders { get; set; } = new(); + + 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(ClientViewModel? model) + { + return new Client() + { + Id = model.Id, + ClientFIO = model.ClientFIO, + Password = model.Password, + Email = model.Email, + }; + } + public void Update(ClientBindingModel? model) + { + if (model == null) + { + return; + } + ClientFIO = model.ClientFIO; + Password = model.Password; + } + public ClientViewModel GetViewModel => new() + { + Id = Id, + ClientFIO = ClientFIO, + Password = Password, + Email = Email, + }; + } +} diff --git a/FoodOrders/AbstractFoodOrdersDatabaseImplement/Models/Order.cs b/FoodOrders/AbstractFoodOrdersDatabaseImplement/Models/Order.cs index f605720..a77ab34 100644 --- a/FoodOrders/AbstractFoodOrdersDatabaseImplement/Models/Order.cs +++ b/FoodOrders/AbstractFoodOrdersDatabaseImplement/Models/Order.cs @@ -18,6 +18,9 @@ namespace AbstractFoodOrdersDatabaseImplement.Models [Required] public int DishId { get; private set; } [Required] + public int ClientId { get; set; } + public virtual Client Client { get; set; } = new(); + [Required] public int Count { get; private set; } [Required] public double Sum { get; private set; } @@ -27,7 +30,7 @@ namespace AbstractFoodOrdersDatabaseImplement.Models public DateTime DateCreate { get; private set; } public DateTime? DateImplement { get; private set; } public virtual Dish Dish { get; set; } - public static Order? Create(OrderBindingModel? model) + public static Order? Create(AbstractFoodOrdersDatabase data, OrderBindingModel? model) { if (model == null) { @@ -37,6 +40,8 @@ namespace AbstractFoodOrdersDatabaseImplement.Models { Id = model.Id, DishId = model.DishId, + ClientId = model.ClientId, + Client = data.Clients.First(x => x.Id == model.ClientId), Count = model.Count, Sum = model.Sum, Status = model.Status, @@ -57,6 +62,8 @@ namespace AbstractFoodOrdersDatabaseImplement.Models { Id = Id, DishId = DishId, + ClientId = ClientId, + ClientFIO = Client.ClientFIO, Count = Count, Sum = Sum, Status = Status, diff --git a/FoodOrders/AbstractFoodOrdersListImplement/DataListSingleton.cs b/FoodOrders/AbstractFoodOrdersListImplement/DataListSingleton.cs index b4e1a01..2eb90c1 100644 --- a/FoodOrders/AbstractFoodOrdersListImplement/DataListSingleton.cs +++ b/FoodOrders/AbstractFoodOrdersListImplement/DataListSingleton.cs @@ -13,11 +13,13 @@ namespace AbstractFoodOrdersListImplement public List Components { get; set; } public List Orders { get; set; } public List Dishes { get; set; } + public List Clients { get; set; } private DataListSingleton() { Components = new List(); Orders = new List(); Dishes = new List(); + Clients = new List(); } public static DataListSingleton GetInstance() { diff --git a/FoodOrders/AbstractFoodOrdersListImplement/Implements/ClientStorage.cs b/FoodOrders/AbstractFoodOrdersListImplement/Implements/ClientStorage.cs index c327baa..0aeb153 100644 --- a/FoodOrders/AbstractFoodOrdersListImplement/Implements/ClientStorage.cs +++ b/FoodOrders/AbstractFoodOrdersListImplement/Implements/ClientStorage.cs @@ -39,7 +39,7 @@ namespace AbstractFoodOrdersListImplement.Implements } foreach (var client in _source.Clients) { - if (!string.IsNullOrEmpty(model.Email) && client.ClientFIO.Contains(model.Email)) + if (!string.IsNullOrEmpty(model.Email) && client.Email.Contains(model.Email)) { result.Add(client.GetViewModel); } diff --git a/FoodOrders/AbstractFoodOrdersListImplement/Implements/OrderStorage.cs b/FoodOrders/AbstractFoodOrdersListImplement/Implements/OrderStorage.cs index eadf6f6..a35cd76 100644 --- a/FoodOrders/AbstractFoodOrdersListImplement/Implements/OrderStorage.cs +++ b/FoodOrders/AbstractFoodOrdersListImplement/Implements/OrderStorage.cs @@ -30,28 +30,15 @@ namespace AbstractFoodOrdersListImplement.Implements public List GetFilteredList(OrderSearchModel model) { var result = new List(); - if (!model.Id.HasValue && !model.DateFrom.HasValue && !model.DateTo.HasValue) + if (!model.Id.HasValue && !model.DateFrom.HasValue && !model.DateTo.HasValue && !model.ClientId.HasValue) { return result; } - if (!model.DateFrom.HasValue || !model.DateTo.HasValue) + foreach (var order in _source.Orders) { - foreach (var order in _source.Orders) + if ((order.DateCreate >= model.DateFrom && order.DateCreate <= model.DateTo) || order.ClientId == model.ClientId) { - if (order.Id == model.Id) - { - result.Add(AttachReinforcedName(order.GetViewModel)); - } - } - } - else - { - foreach (var order in _source.Orders) - { - if (order.DateCreate >= model.DateFrom && order.DateCreate <= model.DateTo) - { - result.Add(AttachReinforcedName(order.GetViewModel)); - } + result.Add(AttachReinforcedName(order.GetViewModel)); } } return result; @@ -126,7 +113,15 @@ namespace AbstractFoodOrdersListImplement.Implements if (reinforced.Id == model.DishId) { model.DishName = reinforced.DishName; - return model; + break; + } + } + foreach (var client in _source.Clients) + { + if (client.Id == model.ClientId) + { + model.ClientFIO = client.ClientFIO; + break; } } return model; diff --git a/FoodOrders/AbstractFoodOrdersListImplement/Models/Order.cs b/FoodOrders/AbstractFoodOrdersListImplement/Models/Order.cs index 293b645..08133c5 100644 --- a/FoodOrders/AbstractFoodOrdersListImplement/Models/Order.cs +++ b/FoodOrders/AbstractFoodOrdersListImplement/Models/Order.cs @@ -14,6 +14,7 @@ namespace AbstractFoodOrdersListImplement.Models { public int Id { get; private set; } public int DishId { 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; } = OrderStatus.Неизвестен; @@ -29,6 +30,7 @@ namespace AbstractFoodOrdersListImplement.Models { Id = model.Id, DishId = model.DishId, + ClientId = model.ClientId, Count = model.Count, Sum = model.Sum, Status = model.Status, @@ -43,6 +45,7 @@ namespace AbstractFoodOrdersListImplement.Models return; } DishId = model.DishId; + ClientId = model.ClientId; Count = model.Count; Sum = model.Sum; Status = model.Status; @@ -53,6 +56,7 @@ namespace AbstractFoodOrdersListImplement.Models { Id = Id, DishId = DishId, + ClientId = ClientId, Count = Count, Sum = Sum, Status = Status, diff --git a/FoodOrders/AbstractShopFileImplement/DataFileSingleton.cs b/FoodOrders/AbstractShopFileImplement/DataFileSingleton.cs index a5b82d6..7704e08 100644 --- a/FoodOrders/AbstractShopFileImplement/DataFileSingleton.cs +++ b/FoodOrders/AbstractShopFileImplement/DataFileSingleton.cs @@ -14,9 +14,11 @@ namespace AbstractFoodOrdersFileImplement private readonly string ComponentFileName = "Component.xml"; private readonly string OrderFileName = "Order.xml"; private readonly string DishFileName = "Dish.xml"; + private readonly string ClientFileName = "Client.xml"; public List Components { get; private set; } public List Orders { get; private set; } public List Dishes { get; private set; } + public List Clients { get; private set; } public static DataFileSingleton GetInstance() { if (instance == null) @@ -25,12 +27,10 @@ namespace AbstractFoodOrdersFileImplement } return instance; } - public void SaveComponents() => SaveData(Components, ComponentFileName, - "Components", x => x.GetXElement); - public void SaveDishes() => SaveData(Dishes, DishFileName, - "Dishes", x => x.GetXElement); - public void SaveOrders() => SaveData(Orders, OrderFileName, - "Orders", x => x.GetXElement); + public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement); + public void SaveDishes() => SaveData(Dishes, DishFileName, "Dishes", 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 => @@ -39,6 +39,8 @@ namespace AbstractFoodOrdersFileImplement Dish.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/FoodOrders/AbstractShopFileImplement/Implements/ClientStorage.cs b/FoodOrders/AbstractShopFileImplement/Implements/ClientStorage.cs new file mode 100644 index 0000000..ba16eaf --- /dev/null +++ b/FoodOrders/AbstractShopFileImplement/Implements/ClientStorage.cs @@ -0,0 +1,85 @@ +using AbstractFoodOrdersContracts.BindingModels; +using AbstractFoodOrdersContracts.SearchModels; +using AbstractFoodOrdersContracts.StoragesContracts; +using AbstractFoodOrdersContracts.ViewModels; +using AbstractFoodOrdersFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AbstractFoodOrdersFileImplement.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/FoodOrders/AbstractShopFileImplement/Implements/OrderStorage.cs b/FoodOrders/AbstractShopFileImplement/Implements/OrderStorage.cs index 3c57565..6ba7f7d 100644 --- a/FoodOrders/AbstractShopFileImplement/Implements/OrderStorage.cs +++ b/FoodOrders/AbstractShopFileImplement/Implements/OrderStorage.cs @@ -27,21 +27,23 @@ namespace AbstractFoodOrdersFileImplement.Implements public List GetFilteredList(OrderSearchModel model) { - if (!model.DateFrom.HasValue || !model.DateTo.HasValue) + if ((!model.DateFrom.HasValue || !model.DateTo.HasValue) && !model.ClientId.HasValue) { return new(); } - return source.Orders.Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo).Select(x => GetViewModel(x)).Select(y => { y.DishName = source.Dishes.FirstOrDefault(x => x.Id == y?.DishId)?.DishName; return y; }).ToList(); + return source.Orders.Where(x => (model.DateFrom.HasValue && model.DateTo.HasValue && x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo) ||( x.ClientId == model.ClientId)) + .Select(x => GetViewModel(x)) + .Select(y => { y.DishName = source.Dishes.FirstOrDefault(x => x.Id == y?.DishId)?.DishName; return y; }).ToList(); } public OrderViewModel? GetElement(OrderSearchModel model) { - if (!model.Id.HasValue) + if (!model.Id.HasValue && !model.ClientId.HasValue) { return null; } - return source.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + return source.Orders.FirstOrDefault(x => x.ClientId == model.ClientId && x.Id == model.Id)?.GetViewModel; } //для загрузки названий изделия в заказе diff --git a/FoodOrders/AbstractShopFileImplement/Models/Client.cs b/FoodOrders/AbstractShopFileImplement/Models/Client.cs new file mode 100644 index 0000000..95a84f0 --- /dev/null +++ b/FoodOrders/AbstractShopFileImplement/Models/Client.cs @@ -0,0 +1,70 @@ +using AbstractFoodOrdersContracts.BindingModels; +using AbstractFoodOrdersContracts.ViewModels; +using AbstractFoodOrdersDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace AbstractFoodOrdersFileImplement.Models +{ + 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/FoodOrders/AbstractShopFileImplement/Models/Order.cs b/FoodOrders/AbstractShopFileImplement/Models/Order.cs index 6f03184..61feea8 100644 --- a/FoodOrders/AbstractShopFileImplement/Models/Order.cs +++ b/FoodOrders/AbstractShopFileImplement/Models/Order.cs @@ -16,6 +16,7 @@ namespace AbstractFoodOrdersFileImplement.Models { public int Id { get; private set; } public int DishId { 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; } = OrderStatus.Неизвестен; @@ -32,6 +33,7 @@ namespace AbstractFoodOrdersFileImplement.Models { Id = model.Id, DishId = model.DishId, + ClientId = model.ClientId, Count = model.Count, Sum = model.Sum, Status = model.Status, @@ -49,6 +51,7 @@ namespace AbstractFoodOrdersFileImplement.Models { Id = Convert.ToInt32(element.Attribute("Id")!.Value), DishId = Convert.ToInt32(element.Element("DishId")!.Value), + ClientId = Convert.ToInt32(element.Element("ClientId")!.Value), Count = Convert.ToInt32(element.Element("Count")!.Value), Sum = Convert.ToDouble(element.Element("Sum")!.Value), Status = (OrderStatus)Enum.Parse(typeof(OrderStatus), element.Element("Status")!.Value), @@ -64,6 +67,7 @@ namespace AbstractFoodOrdersFileImplement.Models return; } DishId = model.DishId; + ClientId = model.ClientId; Count = model.Count; Sum = model.Sum; Status = model.Status; @@ -74,6 +78,7 @@ namespace AbstractFoodOrdersFileImplement.Models { Id = Id, DishId = DishId, + ClientId = ClientId, Count = Count, Sum = Sum, Status = Status, @@ -83,6 +88,7 @@ namespace AbstractFoodOrdersFileImplement.Models public XElement GetXElement => new("Order", new XAttribute("Id", Id), new XElement("DishId", DishId), + new XElement("ClientId", ClientId), new XElement("Count", Count.ToString()), new XElement("Sum", Sum.ToString()), new XElement("Status", Status.ToString()), diff --git a/FoodOrders/FoodOrders/FormClients.Designer.cs b/FoodOrders/FoodOrders/FormClients.Designer.cs new file mode 100644 index 0000000..5bc9606 --- /dev/null +++ b/FoodOrders/FoodOrders/FormClients.Designer.cs @@ -0,0 +1,92 @@ +namespace FoodOrders +{ + partial class FormClients + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + dataGridViewClients = new DataGridView(); + buttonDel = new Button(); + buttonRef = new Button(); + ((System.ComponentModel.ISupportInitialize)dataGridViewClients).BeginInit(); + SuspendLayout(); + // + // dataGridViewClients + // + dataGridViewClients.BackgroundColor = SystemColors.ControlLightLight; + dataGridViewClients.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + dataGridViewClients.Location = new Point(1, 1); + dataGridViewClients.Name = "dataGridViewClients"; + dataGridViewClients.RowHeadersVisible = false; + dataGridViewClients.RowHeadersWidth = 51; + dataGridViewClients.RowTemplate.Height = 29; + dataGridViewClients.SelectionMode = DataGridViewSelectionMode.FullRowSelect; + dataGridViewClients.Size = new Size(650, 402); + dataGridViewClients.TabIndex = 0; + // + // buttonDel + // + buttonDel.Location = new Point(668, 12); + buttonDel.Name = "buttonDel"; + buttonDel.Size = new Size(102, 29); + buttonDel.TabIndex = 1; + buttonDel.Text = "Удалить"; + buttonDel.UseVisualStyleBackColor = true; + buttonDel.Click += ButtonDel_Click; + // + // buttonRef + // + buttonRef.Location = new Point(668, 85); + buttonRef.Name = "buttonRef"; + buttonRef.Size = new Size(102, 29); + buttonRef.TabIndex = 2; + buttonRef.Text = "Обновить"; + buttonRef.UseVisualStyleBackColor = true; + buttonRef.Click += ButtonRef_Click; + // + // FormClients + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(782, 403); + Controls.Add(buttonRef); + Controls.Add(buttonDel); + Controls.Add(dataGridViewClients); + Name = "FormClients"; + StartPosition = FormStartPosition.CenterParent; + Text = "Клиенты"; + Load += FormClients_Load; + ((System.ComponentModel.ISupportInitialize)dataGridViewClients).EndInit(); + ResumeLayout(false); + } + + #endregion + + private DataGridView dataGridViewClients; + private Button buttonDel; + private Button buttonRef; + } +} \ No newline at end of file diff --git a/FoodOrders/FoodOrders/FormClients.cs b/FoodOrders/FoodOrders/FormClients.cs new file mode 100644 index 0000000..72e82e3 --- /dev/null +++ b/FoodOrders/FoodOrders/FormClients.cs @@ -0,0 +1,84 @@ +using AbstractFoodOrdersContracts.BindingModels; +using AbstractFoodOrdersContracts.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace FoodOrders +{ + public partial class FormClients : Form + { + private readonly ILogger _logger; + private readonly IClientLogic _logic; + + public FormClients(ILogger logger, IClientLogic logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + } + + private void FormClients_Load(object sender, EventArgs e) + { + LoadData(); + } + + private void LoadData() + { + try + { + var list = _logic.ReadList(null); + if (list != null) + { + dataGridViewClients.DataSource = list; + dataGridViewClients.Columns["Id"].Visible = false; + dataGridViewClients.Columns["ClientFIO"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + } + _logger.LogInformation("Загрузка клиентов"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки клиентов"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void ButtonDel_Click(object sender, EventArgs e) + { + if (dataGridViewClients.SelectedRows.Count == 1) + { + if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == + DialogResult.Yes) + { + int id = Convert.ToInt32(dataGridViewClients.SelectedRows[0].Cells["Id"].Value); + _logger.LogInformation("Удаление клиента"); + try + { + if (!_logic.Delete(new ClientBindingModel { Id = id })) + { + throw new Exception("Ошибка при удалении. Дополнительная информация в логах."); + } + LoadData(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка удаления клиента"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + } + + private void ButtonRef_Click(object sender, EventArgs e) + { + LoadData(); + } + } +} diff --git a/FoodOrders/FoodOrders/FormClients.resx b/FoodOrders/FoodOrders/FormClients.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/FoodOrders/FoodOrders/FormClients.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/FoodOrders/FoodOrders/FormCreateOrder.Designer.cs b/FoodOrders/FoodOrders/FormCreateOrder.Designer.cs index 64f6102..d59d6c2 100644 --- a/FoodOrders/FoodOrders/FormCreateOrder.Designer.cs +++ b/FoodOrders/FoodOrders/FormCreateOrder.Designer.cs @@ -28,105 +28,125 @@ /// private void InitializeComponent() { - this.labelDish = new System.Windows.Forms.Label(); - this.labelCount = new System.Windows.Forms.Label(); - this.labelSum = new System.Windows.Forms.Label(); - this.comboBoxDish = new System.Windows.Forms.ComboBox(); - this.textBoxCount = new System.Windows.Forms.TextBox(); - this.textBoxSum = new System.Windows.Forms.TextBox(); - this.ButtonSave = new System.Windows.Forms.Button(); - this.ButtonCancel = new System.Windows.Forms.Button(); - this.SuspendLayout(); + labelDish = new Label(); + labelCount = new Label(); + labelSum = new Label(); + comboBoxDish = new ComboBox(); + textBoxCount = new TextBox(); + textBoxSum = new TextBox(); + ButtonSave = new Button(); + ButtonCancel = new Button(); + comboBoxClient = new ComboBox(); + labelClient = new Label(); + SuspendLayout(); // // labelDish // - this.labelDish.AutoSize = true; - this.labelDish.Location = new System.Drawing.Point(38, 30); - this.labelDish.Name = "labelDish"; - this.labelDish.Size = new System.Drawing.Size(58, 20); - this.labelDish.TabIndex = 0; - this.labelDish.Text = "Блюдо:"; + labelDish.AutoSize = true; + labelDish.Location = new Point(38, 30); + labelDish.Name = "labelDish"; + labelDish.Size = new Size(58, 20); + labelDish.TabIndex = 0; + labelDish.Text = "Блюдо:"; // // labelCount // - this.labelCount.AutoSize = true; - this.labelCount.Location = new System.Drawing.Point(38, 76); - this.labelCount.Name = "labelCount"; - this.labelCount.Size = new System.Drawing.Size(93, 20); - this.labelCount.TabIndex = 1; - this.labelCount.Text = "Количество:"; + labelCount.AutoSize = true; + labelCount.Location = new Point(38, 122); + labelCount.Name = "labelCount"; + labelCount.Size = new Size(93, 20); + labelCount.TabIndex = 1; + labelCount.Text = "Количество:"; // // labelSum // - this.labelSum.AutoSize = true; - this.labelSum.Location = new System.Drawing.Point(38, 125); - this.labelSum.Name = "labelSum"; - this.labelSum.Size = new System.Drawing.Size(55, 20); - this.labelSum.TabIndex = 2; - this.labelSum.Text = "Сумма"; + labelSum.AutoSize = true; + labelSum.Location = new Point(38, 171); + labelSum.Name = "labelSum"; + labelSum.Size = new Size(55, 20); + labelSum.TabIndex = 2; + labelSum.Text = "Сумма"; // // comboBoxDish // - this.comboBoxDish.FormattingEnabled = true; - this.comboBoxDish.Location = new System.Drawing.Point(148, 27); - this.comboBoxDish.Name = "comboBoxDish"; - this.comboBoxDish.Size = new System.Drawing.Size(286, 28); - this.comboBoxDish.TabIndex = 3; + comboBoxDish.FormattingEnabled = true; + comboBoxDish.Location = new Point(148, 27); + comboBoxDish.Name = "comboBoxDish"; + comboBoxDish.Size = new Size(286, 28); + comboBoxDish.TabIndex = 3; // // textBoxCount // - this.textBoxCount.Location = new System.Drawing.Point(148, 69); - this.textBoxCount.Name = "textBoxCount"; - this.textBoxCount.Size = new System.Drawing.Size(286, 27); - this.textBoxCount.TabIndex = 4; - this.textBoxCount.TextChanged += new System.EventHandler(this.TextBoxCount_TextChanged); + textBoxCount.Location = new Point(148, 115); + textBoxCount.Name = "textBoxCount"; + textBoxCount.Size = new Size(286, 27); + textBoxCount.TabIndex = 4; + textBoxCount.TextChanged += TextBoxCount_TextChanged; // // textBoxSum // - this.textBoxSum.Location = new System.Drawing.Point(148, 118); - this.textBoxSum.Name = "textBoxSum"; - this.textBoxSum.Size = new System.Drawing.Size(286, 27); - this.textBoxSum.TabIndex = 5; + textBoxSum.Location = new Point(148, 164); + textBoxSum.Name = "textBoxSum"; + textBoxSum.Size = new Size(286, 27); + textBoxSum.TabIndex = 5; // // ButtonSave // - this.ButtonSave.Location = new System.Drawing.Point(211, 174); - this.ButtonSave.Name = "ButtonSave"; - this.ButtonSave.Size = new System.Drawing.Size(94, 29); - this.ButtonSave.TabIndex = 6; - this.ButtonSave.Text = "Сохранить"; - this.ButtonSave.UseVisualStyleBackColor = true; - this.ButtonSave.Click += new System.EventHandler(this.ButtonSave_Click); + ButtonSave.Location = new Point(211, 220); + ButtonSave.Name = "ButtonSave"; + ButtonSave.Size = new Size(94, 29); + ButtonSave.TabIndex = 6; + ButtonSave.Text = "Сохранить"; + ButtonSave.UseVisualStyleBackColor = true; + ButtonSave.Click += ButtonSave_Click; // // ButtonCancel // - this.ButtonCancel.Location = new System.Drawing.Point(340, 174); - this.ButtonCancel.Name = "ButtonCancel"; - this.ButtonCancel.Size = new System.Drawing.Size(94, 29); - this.ButtonCancel.TabIndex = 7; - this.ButtonCancel.Text = "Отмена"; - this.ButtonCancel.UseVisualStyleBackColor = true; - this.ButtonCancel.Click += new System.EventHandler(this.ButtonCancel_Click); + ButtonCancel.Location = new Point(340, 220); + ButtonCancel.Name = "ButtonCancel"; + ButtonCancel.Size = new Size(94, 29); + ButtonCancel.TabIndex = 7; + ButtonCancel.Text = "Отмена"; + ButtonCancel.UseVisualStyleBackColor = true; + ButtonCancel.Click += ButtonCancel_Click; + // + // comboBoxClient + // + comboBoxClient.FormattingEnabled = true; + comboBoxClient.Location = new Point(148, 70); + comboBoxClient.Name = "comboBoxClient"; + comboBoxClient.Size = new Size(286, 28); + comboBoxClient.TabIndex = 8; + // + // labelClient + // + labelClient.AutoSize = true; + labelClient.Location = new Point(43, 78); + labelClient.Name = "labelClient"; + labelClient.Size = new Size(61, 20); + labelClient.TabIndex = 9; + labelClient.Text = "Клиент:"; // // FormCreateOrder // - this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(466, 229); - this.Controls.Add(this.ButtonCancel); - this.Controls.Add(this.ButtonSave); - this.Controls.Add(this.textBoxSum); - this.Controls.Add(this.textBoxCount); - this.Controls.Add(this.comboBoxDish); - this.Controls.Add(this.labelSum); - this.Controls.Add(this.labelCount); - this.Controls.Add(this.labelDish); - this.Name = "FormCreateOrder"; - this.Text = "Заказ"; - this.Load += new System.EventHandler(this.FormCreateOrder_Load); - this.ResumeLayout(false); - this.PerformLayout(); - + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(466, 309); + Controls.Add(labelClient); + Controls.Add(comboBoxClient); + Controls.Add(ButtonCancel); + Controls.Add(ButtonSave); + Controls.Add(textBoxSum); + Controls.Add(textBoxCount); + Controls.Add(comboBoxDish); + Controls.Add(labelSum); + Controls.Add(labelCount); + Controls.Add(labelDish); + Name = "FormCreateOrder"; + Text = "Заказ"; + Load += FormCreateOrder_Load; + ResumeLayout(false); + PerformLayout(); } #endregion @@ -139,5 +159,7 @@ private TextBox textBoxSum; private Button ButtonSave; private Button ButtonCancel; + private ComboBox comboBoxClient; + private Label labelClient; } } \ No newline at end of file diff --git a/FoodOrders/FoodOrders/FormCreateOrder.cs b/FoodOrders/FoodOrders/FormCreateOrder.cs index 36de5b0..5acb29a 100644 --- a/FoodOrders/FoodOrders/FormCreateOrder.cs +++ b/FoodOrders/FoodOrders/FormCreateOrder.cs @@ -21,7 +21,8 @@ namespace FoodOrders private readonly ILogger _logger; private readonly IDishLogic _logicD; private readonly IOrderLogic _logicO; - private List? _list; + private readonly IClientLogic _logicC; + private List? _listD; public int Id { get @@ -38,11 +39,11 @@ namespace FoodOrders { get { - if (_list == null) + if (_listD == null) { return null; } - foreach (var elem in _list) + foreach (var elem in _listD) { if (elem.Id == Id) { @@ -54,25 +55,36 @@ namespace FoodOrders } public int Count { get { return Convert.ToInt32(textBoxCount.Text); } set { textBoxCount.Text = value.ToString(); } } public FormCreateOrder(ILogger logger, IDishLogic - logicD, IOrderLogic logicO) + logicD, IOrderLogic logicO, IClientLogic logicC) { InitializeComponent(); _logger = logger; _logicD = logicD; _logicO = logicO; + _logicC = logicC; } private void FormCreateOrder_Load(object sender, EventArgs e) { _logger.LogInformation("Загрузка изделий для заказа"); - _list = _logicD.ReadList(null); + _listD = _logicD.ReadList(null); - if (_list != null) + if (_listD != null) { comboBoxDish.DisplayMember = "DishName"; comboBoxDish.ValueMember = "Id"; - comboBoxDish.DataSource = _list; + comboBoxDish.DataSource = _listD; comboBoxDish.SelectedItem = null; } + _logger.LogInformation("Загрузка клиентов для заказа"); + var _listC = _logicC.ReadList(null); + + if (_listC != null) + { + comboBoxClient.DisplayMember = "ClientFio"; + comboBoxClient.ValueMember = "Id"; + comboBoxClient.DataSource = _listC; + comboBoxClient.SelectedItem = null; + } } private void CalcSum() { @@ -110,7 +122,7 @@ namespace FoodOrders } private void ButtonSave_Click(object sender, EventArgs e) { - if (string.IsNullOrEmpty(textBoxCount.Text)) + if (string.IsNullOrEmpty(textBoxCount.Text)) { MessageBox.Show("Заполните поле Количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); @@ -122,12 +134,19 @@ namespace FoodOrders MessageBoxButtons.OK, MessageBoxIcon.Error); return; } + if (comboBoxClient.SelectedValue == null) + { + MessageBox.Show("Выберите клиента", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } _logger.LogInformation("Создание заказа"); try { var operationResult = _logicO.CreateOrder(new OrderBindingModel { DishId = Convert.ToInt32(comboBoxDish.SelectedValue), + ClientId = Convert.ToInt32(comboBoxClient.SelectedValue), Count = Convert.ToInt32(textBoxCount.Text), Sum = Convert.ToDouble(textBoxSum.Text) });