From eb82dac496ea7a878b35959bcd71cf4aa29a69ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Mon, 13 Mar 2023 18:39:11 +0400 Subject: [PATCH 01/26] =?UTF-8?q?=D0=BA=D0=BE=D0=BD=D1=82=D1=80=D0=B0?= =?UTF-8?q?=D0=BA=D1=82=20=D0=BC=D0=BE=D0=B4=D0=B5=D0=BB=D0=B8=20=D0=BF?= =?UTF-8?q?=D0=B8=D1=81=D1=8C=D0=BC=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BindingModels/MessageInfoBindingModel.cs | 24 +++++++++++++++ .../IMessageInfoLogic.cs | 18 ++++++++++++ .../SearchModels/MessageInfoSearchModel.cs | 15 ++++++++++ .../StoragesContract/IMessageInfoStorage.cs | 22 ++++++++++++++ .../ViewModels/MessageInfoViewModel.cs | 29 +++++++++++++++++++ ConfectioneryDataModels/IMessageInfoModel.cs | 23 +++++++++++++++ 6 files changed, 131 insertions(+) create mode 100644 ConfectioneryContracts/BindingModels/MessageInfoBindingModel.cs create mode 100644 ConfectioneryContracts/BusinessLogicsContracts/IMessageInfoLogic.cs create mode 100644 ConfectioneryContracts/SearchModels/MessageInfoSearchModel.cs create mode 100644 ConfectioneryContracts/StoragesContract/IMessageInfoStorage.cs create mode 100644 ConfectioneryContracts/ViewModels/MessageInfoViewModel.cs create mode 100644 ConfectioneryDataModels/IMessageInfoModel.cs diff --git a/ConfectioneryContracts/BindingModels/MessageInfoBindingModel.cs b/ConfectioneryContracts/BindingModels/MessageInfoBindingModel.cs new file mode 100644 index 0000000..984939b --- /dev/null +++ b/ConfectioneryContracts/BindingModels/MessageInfoBindingModel.cs @@ -0,0 +1,24 @@ +using ConfectioneryDataModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryContracts.BindingModels +{ + public class MessageInfoBindingModel : IMessageInfoModel + { + public string MessageId { get; set; } = string.Empty; + + public int? ClientId { get; set; } + + public string SenderName { get; set; } = string.Empty; + + public string Subject { get; set; } = string.Empty; + + public string Body { get; set; } = string.Empty; + + public DateTime DateDelivery { get; set; } + } +} diff --git a/ConfectioneryContracts/BusinessLogicsContracts/IMessageInfoLogic.cs b/ConfectioneryContracts/BusinessLogicsContracts/IMessageInfoLogic.cs new file mode 100644 index 0000000..ed4b54c --- /dev/null +++ b/ConfectioneryContracts/BusinessLogicsContracts/IMessageInfoLogic.cs @@ -0,0 +1,18 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryContracts.BusinessLogicsContracts +{ + public interface IMessageInfoLogic + { + List? ReadList(MessageInfoSearchModel? model); + + bool Create(MessageInfoBindingModel model); + } +} diff --git a/ConfectioneryContracts/SearchModels/MessageInfoSearchModel.cs b/ConfectioneryContracts/SearchModels/MessageInfoSearchModel.cs new file mode 100644 index 0000000..e344281 --- /dev/null +++ b/ConfectioneryContracts/SearchModels/MessageInfoSearchModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryContracts.SearchModels +{ + public class MessageInfoSearchModel + { + public int? ClientId { get; set; } + + public string? MessageId { get; set; } + } +} diff --git a/ConfectioneryContracts/StoragesContract/IMessageInfoStorage.cs b/ConfectioneryContracts/StoragesContract/IMessageInfoStorage.cs new file mode 100644 index 0000000..6bb7375 --- /dev/null +++ b/ConfectioneryContracts/StoragesContract/IMessageInfoStorage.cs @@ -0,0 +1,22 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryContracts.StoragesContract +{ + public interface IMessageInfoStorage + { + List GetFullList(); + + List GetFilteredList(MessageInfoSearchModel model); + + MessageInfoViewModel? GetElement(MessageInfoSearchModel model); + + MessageInfoViewModel? Insert(MessageInfoBindingModel model); + } +} diff --git a/ConfectioneryContracts/ViewModels/MessageInfoViewModel.cs b/ConfectioneryContracts/ViewModels/MessageInfoViewModel.cs new file mode 100644 index 0000000..5e38e72 --- /dev/null +++ b/ConfectioneryContracts/ViewModels/MessageInfoViewModel.cs @@ -0,0 +1,29 @@ +using ConfectioneryDataModels; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryContracts.ViewModels +{ + public class MessageInfoViewModel : IMessageInfoModel + { + public string MessageId { get; set; } = string.Empty; + + public int? ClientId { get; set; } + + [DisplayName("Отправитель")] + public string SenderName { get; set; } = string.Empty; + + [DisplayName("Дата письма")] + public DateTime DateDelivery { get; set; } + + [DisplayName("Заголовок")] + public string Subject { get; set; } = string.Empty; + + [DisplayName("Текст")] + public string Body { get; set; } = string.Empty; + } +} diff --git a/ConfectioneryDataModels/IMessageInfoModel.cs b/ConfectioneryDataModels/IMessageInfoModel.cs new file mode 100644 index 0000000..38e68a1 --- /dev/null +++ b/ConfectioneryDataModels/IMessageInfoModel.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryDataModels +{ + public interface IMessageInfoModel + { + string MessageId { get; } + + int? ClientId { get; } + + string SenderName { get; } + + DateTime DateDelivery { get; } + + string Subject { get; } + + string Body { get; } + } +} -- 2.25.1 From 0420774522f5bea1a6985f51c3827d2a039e4963 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Mon, 13 Mar 2023 19:08:51 +0400 Subject: [PATCH 02/26] MessageInfo ListImplement --- .../ConfectioneryBusinessLogic.csproj | 1 + .../DataListSingleton.cs | 2 + ConfectionaryListImplement/MessageInfo.cs | 56 +++++++++++++++++ .../MessageInfoStorage.cs | 61 +++++++++++++++++++ 4 files changed, 120 insertions(+) create mode 100644 ConfectionaryListImplement/MessageInfo.cs create mode 100644 ConfectionaryListImplement/MessageInfoStorage.cs diff --git a/ConfectionaryBusinessLogic/ConfectioneryBusinessLogic.csproj b/ConfectionaryBusinessLogic/ConfectioneryBusinessLogic.csproj index 8454587..4b0949c 100644 --- a/ConfectionaryBusinessLogic/ConfectioneryBusinessLogic.csproj +++ b/ConfectionaryBusinessLogic/ConfectioneryBusinessLogic.csproj @@ -8,6 +8,7 @@ + diff --git a/ConfectionaryListImplement/DataListSingleton.cs b/ConfectionaryListImplement/DataListSingleton.cs index 951d63d..ff1f4d0 100644 --- a/ConfectionaryListImplement/DataListSingleton.cs +++ b/ConfectionaryListImplement/DataListSingleton.cs @@ -10,6 +10,7 @@ namespace ConfectioneryListImplement public List Pastry { get; set; } public List Clients { get; set; } public List Implementers { get; set; } + public List Messages { get; set; } private DataListSingleton() { @@ -18,6 +19,7 @@ namespace ConfectioneryListImplement Pastry = new List(); Clients = new List(); Implementers = new List(); + Messages = new List(); } public static DataListSingleton GetInstance() { diff --git a/ConfectionaryListImplement/MessageInfo.cs b/ConfectionaryListImplement/MessageInfo.cs new file mode 100644 index 0000000..73645a4 --- /dev/null +++ b/ConfectionaryListImplement/MessageInfo.cs @@ -0,0 +1,56 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryListImplement.Models +{ + // Update в этой сущности не нужен, поскольку в логике мы не изменяем никакие поля после создания письма + public class MessageInfo : IMessageInfoModel + { + public string MessageId { get; private set; } = string.Empty; + + public int? ClientId { get; private set; } + + public string SenderName { get; private set; } = string.Empty; + + public DateTime DateDelivery { get; private set; } = DateTime.Now; + + public string Subject { get; private set; } = string.Empty; + + public string Body { get; private set; } = string.Empty; + + public static MessageInfo? Create(MessageInfoBindingModel model) + { + if (model == null) + { + return null; + } + return new() + { + Body = model.Body, + Subject = model.Subject, + ClientId = model.ClientId, + MessageId = model.MessageId, + SenderName = model.SenderName, + DateDelivery = model.DateDelivery, + }; + } + + public MessageInfoViewModel GetViewModel => new() + { + Body = Body, + Subject = Subject, + ClientId = ClientId, + MessageId = MessageId, + SenderName = SenderName, + DateDelivery = DateDelivery, + }; + + } + +} diff --git a/ConfectionaryListImplement/MessageInfoStorage.cs b/ConfectionaryListImplement/MessageInfoStorage.cs new file mode 100644 index 0000000..0dc21ca --- /dev/null +++ b/ConfectionaryListImplement/MessageInfoStorage.cs @@ -0,0 +1,61 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContract; +using ConfectioneryContracts.ViewModels; +using ConfectioneryListImplement.Models; + +namespace ConfectioneryListImplement +{ + public class MessageInfoStorage : IMessageInfoStorage + { + private readonly DataListSingleton _source; + public MessageInfoStorage() + { + _source = DataListSingleton.GetInstance(); + } + + public MessageInfoViewModel? GetElement(MessageInfoSearchModel model) + { + foreach (var message in _source.Messages) + { + if (model.MessageId != null && model.MessageId == message.MessageId) + return message.GetViewModel; + } + return null; + } + + public List GetFilteredList(MessageInfoSearchModel model) + { + List result = new(); + foreach (var item in _source.Messages) + { + if (item.ClientId.HasValue && item.ClientId == model.ClientId) + { + result.Add(item.GetViewModel); + } + } + return result; + } + + public List GetFullList() + { + List result = new(); + foreach (var item in _source.Messages) + { + result.Add(item.GetViewModel); + } + return result; + } + + public MessageInfoViewModel? Insert(MessageInfoBindingModel model) + { + var newMessage = MessageInfo.Create(model); + if (newMessage == null) + { + return null; + } + _source.Messages.Add(newMessage); + return newMessage.GetViewModel; + } + } +} -- 2.25.1 From 582d1876005b7e6f1c13efd66434c28798b3687c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Mon, 13 Mar 2023 19:21:15 +0400 Subject: [PATCH 03/26] MessageInfo FileImplement --- .../DataFileSingleton.cs | 4 + ConfectionaryFileImplement/MessageInfo.cs | 82 +++++++++++++++++++ .../MessageInfoStorage.cs | 54 ++++++++++++ 3 files changed, 140 insertions(+) create mode 100644 ConfectionaryFileImplement/MessageInfo.cs create mode 100644 ConfectionaryFileImplement/MessageInfoStorage.cs diff --git a/ConfectionaryFileImplement/DataFileSingleton.cs b/ConfectionaryFileImplement/DataFileSingleton.cs index 80b0bbd..aee7b7d 100644 --- a/ConfectionaryFileImplement/DataFileSingleton.cs +++ b/ConfectionaryFileImplement/DataFileSingleton.cs @@ -11,11 +11,13 @@ namespace ConfectioneryFileImplement private readonly string PastryFileName = "Pastry.xml"; private readonly string ClientFileName = "Client.xml"; private readonly string ImplementerFileName = "Implementer.xml"; + private readonly string MessageInfoFileName = "MessageInfo.xml"; public List Components { get; private set; } public List Orders { get; private set; } public List Pastries { get; private set; } public List Clients { get; private set; } public List Implementers { get; private set; } + public List Messages { get; private set; } public static DataFileSingleton GetInstance() { @@ -30,6 +32,7 @@ namespace ConfectioneryFileImplement public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); public void SaveClients() => SaveData(Clients, OrderFileName, "Clients", x => x.GetXElement); public void SaveImplementers() => SaveData(Orders, ImplementerFileName, "Implementers", x => x.GetXElement); + public void SaveMessages() => SaveData(Orders, ImplementerFileName, "Messages", x => x.GetXElement); private DataFileSingleton() { @@ -38,6 +41,7 @@ namespace ConfectioneryFileImplement Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; Clients = LoadData(ClientFileName, "Client", x => Client.Create(x)!)!; Implementers = LoadData(ImplementerFileName, "Implementer", x => Implementer.Create(x)!)!; + Messages = LoadData(MessageInfoFileName, "MessageInfo", x => MessageInfo.Create(x)!)!; } private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) { diff --git a/ConfectionaryFileImplement/MessageInfo.cs b/ConfectionaryFileImplement/MessageInfo.cs new file mode 100644 index 0000000..65396ae --- /dev/null +++ b/ConfectionaryFileImplement/MessageInfo.cs @@ -0,0 +1,82 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace ConfectioneryFileImplement.Models +{ + // Update в этой сущности не нужен, поскольку в логике мы не изменяем никакие поля после создания письма + public class MessageInfo : IMessageInfoModel + { + public string MessageId { get; private set; } = string.Empty; + + public int? ClientId { get; private set; } + + public string SenderName { get; private set; } = string.Empty; + + public DateTime DateDelivery { get; private set; } = DateTime.Now; + + public string Subject { get; private set; } = string.Empty; + + public string Body { get; private set; } = string.Empty; + + public static MessageInfo? Create(MessageInfoBindingModel model) + { + if (model == null) + { + return null; + } + return new() + { + Body = model.Body, + Subject = model.Subject, + ClientId = model.ClientId, + MessageId = model.MessageId, + SenderName = model.SenderName, + DateDelivery = model.DateDelivery, + }; + } + + public static MessageInfo? Create(XElement element) + { + if (element == null) + { + return null; + } + return new() + { + Body = element.Attribute("Body")!.Value, + Subject = element.Attribute("Subject")!.Value, + ClientId = Convert.ToInt32(element.Attribute("ClientId")!.Value), + MessageId = element.Attribute("MessageId")!.Value, + SenderName = element.Attribute("SenderName")!.Value, + DateDelivery = Convert.ToDateTime(element.Attribute("DateDelivery")!.Value), + }; + } + + public MessageInfoViewModel GetViewModel => new() + { + Body = Body, + Subject = Subject, + ClientId = ClientId, + MessageId = MessageId, + SenderName = SenderName, + DateDelivery = DateDelivery, + }; + + public XElement GetXElement => new("MessageInfo", + new XAttribute("Body", Body), + new XAttribute("Subject", Subject), + new XAttribute("ClientId", ClientId), + new XAttribute("MessageId", MessageId), + new XAttribute("SenderName", SenderName), + new XAttribute("DateDelivery", DateDelivery) + ); + } + +} diff --git a/ConfectionaryFileImplement/MessageInfoStorage.cs b/ConfectionaryFileImplement/MessageInfoStorage.cs new file mode 100644 index 0000000..adbe169 --- /dev/null +++ b/ConfectionaryFileImplement/MessageInfoStorage.cs @@ -0,0 +1,54 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContract; +using ConfectioneryContracts.ViewModels; +using ConfectioneryFileImplement.Models; +using System.Reflection; + +namespace ConfectioneryFileImplement +{ + public class MessageInfoStorage : IMessageInfoStorage + { + private readonly DataFileSingleton _source; + public MessageInfoStorage() + { + _source = DataFileSingleton.GetInstance(); + } + + public MessageInfoViewModel? GetElement(MessageInfoSearchModel model) + { + if (model.MessageId != null) + { + return _source.Messages.FirstOrDefault(x => x.MessageId == model.MessageId)?.GetViewModel; + } + return null; + } + + public List GetFilteredList(MessageInfoSearchModel model) + { + return _source.Messages + .Where(x => x.ClientId == model.ClientId) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + return _source.Messages + .Select(x => x.GetViewModel) + .ToList(); + } + + public MessageInfoViewModel? Insert(MessageInfoBindingModel model) + { + var newMessage = MessageInfo.Create(model); + if (newMessage == null) + { + return null; + } + _source.Messages.Add(newMessage); + _source.SaveMessages(); + return newMessage.GetViewModel; + } + } +} -- 2.25.1 From d56858c6f681b3dc3a5cc8f9c27099b6fd632d64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Mon, 13 Mar 2023 19:37:04 +0400 Subject: [PATCH 04/26] fix --- ConfectionaryListImplement/MessageInfoStorage.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ConfectionaryListImplement/MessageInfoStorage.cs b/ConfectionaryListImplement/MessageInfoStorage.cs index 0dc21ca..b0834c1 100644 --- a/ConfectionaryListImplement/MessageInfoStorage.cs +++ b/ConfectionaryListImplement/MessageInfoStorage.cs @@ -18,7 +18,7 @@ namespace ConfectioneryListImplement { foreach (var message in _source.Messages) { - if (model.MessageId != null && model.MessageId == message.MessageId) + if (model.MessageId != null && model.MessageId.Equals(message.MessageId)) return message.GetViewModel; } return null; -- 2.25.1 From 3a9459b51b85dd6c205136de055118983723b9d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Mon, 13 Mar 2023 19:59:28 +0400 Subject: [PATCH 05/26] fix --- ConfectioneryRestApi/Controllers/ImplementerController.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ConfectioneryRestApi/Controllers/ImplementerController.cs b/ConfectioneryRestApi/Controllers/ImplementerController.cs index 9664734..719140c 100644 --- a/ConfectioneryRestApi/Controllers/ImplementerController.cs +++ b/ConfectioneryRestApi/Controllers/ImplementerController.cs @@ -49,7 +49,7 @@ namespace ConfectioneryRestApi.Controllers { return _order.ReadList(new OrderSearchModel { - Status = OrderStatus.Принят + Statusses = new() { OrderStatus.Принят } }); } catch (Exception ex) -- 2.25.1 From d68d22c70b1ed8c0bf58295b8704a7dfd7c67d10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Mon, 13 Mar 2023 20:02:18 +0400 Subject: [PATCH 06/26] MessageInfo DB Implement --- ConfectioneryDatabaseImplement/Client.cs | 3 + .../ConfectioneryDatabase.cs | 1 + ConfectioneryDatabaseImplement/MessageInfo.cs | 55 ++++ .../MessageInfoStorage.cs | 52 +++ .../Migrations/20230307072144_add_client.cs | 2 +- .../20230313155013_create_message.Designer.cs | 298 ++++++++++++++++++ .../20230313155013_create_message.cs | 48 +++ .../ConfectioneryDatabaseModelSnapshot.cs | 41 +++ 8 files changed, 499 insertions(+), 1 deletion(-) create mode 100644 ConfectioneryDatabaseImplement/MessageInfo.cs create mode 100644 ConfectioneryDatabaseImplement/MessageInfoStorage.cs create mode 100644 ConfectioneryDatabaseImplement/Migrations/20230313155013_create_message.Designer.cs create mode 100644 ConfectioneryDatabaseImplement/Migrations/20230313155013_create_message.cs diff --git a/ConfectioneryDatabaseImplement/Client.cs b/ConfectioneryDatabaseImplement/Client.cs index 83a5d3a..ce4bdfe 100644 --- a/ConfectioneryDatabaseImplement/Client.cs +++ b/ConfectioneryDatabaseImplement/Client.cs @@ -27,6 +27,9 @@ namespace ConfectioneryDatabaseImplement.Models [ForeignKey("ClientId")] public virtual List Orders { get; set; } = new(); + [ForeignKey("ClientId")] + public virtual List Messages { get; set; } = new(); + public static Client? Create(ClientBindingModel model) { if (model == null) diff --git a/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs b/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs index d1b30ed..81fb1a3 100644 --- a/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs +++ b/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs @@ -26,5 +26,6 @@ namespace ConfectioneryDatabaseImplement public virtual DbSet Orders { set; get; } public virtual DbSet Clients { set; get; } public virtual DbSet Implementers { set; get; } + public virtual DbSet Messages { set; get; } } } diff --git a/ConfectioneryDatabaseImplement/MessageInfo.cs b/ConfectioneryDatabaseImplement/MessageInfo.cs new file mode 100644 index 0000000..988e037 --- /dev/null +++ b/ConfectioneryDatabaseImplement/MessageInfo.cs @@ -0,0 +1,55 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels; +using System.ComponentModel.DataAnnotations; + +namespace ConfectioneryDatabaseImplement.Models +{ + // Update в этой сущности не нужен, поскольку в логике мы не изменяем никакие поля после создания письма + public class MessageInfo : IMessageInfoModel + { + [Key] + public string MessageId { get; private set; } = string.Empty; + + public int? ClientId { get; private set; } + + public string SenderName { get; private set; } = string.Empty; + + public DateTime DateDelivery { get; private set; } = DateTime.Now; + + public string Subject { get; private set; } = string.Empty; + + public string Body { get; private set; } = string.Empty; + + public Client? Client { get; private set; } + + public static MessageInfo? Create(MessageInfoBindingModel model) + { + if (model == null) + { + return null; + } + return new() + { + Body = model.Body, + Subject = model.Subject, + ClientId = model.ClientId, + MessageId = model.MessageId, + SenderName = model.SenderName, + DateDelivery = model.DateDelivery, + }; + } + + public MessageInfoViewModel GetViewModel => new() + { + Body = Body, + Subject = Subject, + ClientId = ClientId, + MessageId = MessageId, + SenderName = SenderName, + DateDelivery = DateDelivery, + }; + + } + +} diff --git a/ConfectioneryDatabaseImplement/MessageInfoStorage.cs b/ConfectioneryDatabaseImplement/MessageInfoStorage.cs new file mode 100644 index 0000000..bd25a7c --- /dev/null +++ b/ConfectioneryDatabaseImplement/MessageInfoStorage.cs @@ -0,0 +1,52 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContract; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDatabaseImplement.Models; + +namespace ConfectioneryDatabaseImplement +{ + public class MessageInfoStorage : IMessageInfoStorage + { + + public MessageInfoViewModel? GetElement(MessageInfoSearchModel model) + { + using var context = new ConfectioneryDatabase(); + if (model.MessageId != null) + { + return context.Messages.FirstOrDefault(x => x.MessageId == model.MessageId)?.GetViewModel; + } + return null; + } + + public List GetFilteredList(MessageInfoSearchModel model) + { + using var context = new ConfectioneryDatabase(); + return context.Messages + .Where(x => x.ClientId == model.ClientId) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new ConfectioneryDatabase(); + return context.Messages + .Select(x => x.GetViewModel) + .ToList(); + } + + public MessageInfoViewModel? Insert(MessageInfoBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var newMessage = MessageInfo.Create(model); + if (newMessage == null) + { + return null; + } + context.Messages.Add(newMessage); + context.SaveChanges(); + return newMessage.GetViewModel; + } + } +} diff --git a/ConfectioneryDatabaseImplement/Migrations/20230307072144_add_client.cs b/ConfectioneryDatabaseImplement/Migrations/20230307072144_add_client.cs index d125130..c58fac2 100644 --- a/ConfectioneryDatabaseImplement/Migrations/20230307072144_add_client.cs +++ b/ConfectioneryDatabaseImplement/Migrations/20230307072144_add_client.cs @@ -10,7 +10,7 @@ namespace ConfectioneryDatabaseImplement.Migrations /// protected override void Up(MigrationBuilder migrationBuilder) { - migrationBuilder.Sql("TRUNCATE Orders"); + migrationBuilder.Sql("TRUNCATE TABLE Orders"); migrationBuilder.AddColumn( name: "ClientId", table: "Orders", diff --git a/ConfectioneryDatabaseImplement/Migrations/20230313155013_create_message.Designer.cs b/ConfectioneryDatabaseImplement/Migrations/20230313155013_create_message.Designer.cs new file mode 100644 index 0000000..877a422 --- /dev/null +++ b/ConfectioneryDatabaseImplement/Migrations/20230313155013_create_message.Designer.cs @@ -0,0 +1,298 @@ +// +using System; +using ConfectioneryDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace ConfectioneryDatabaseImplement.Migrations +{ + [DbContext(typeof(ConfectioneryDatabase))] + [Migration("20230313155013_create_message")] + partial class create_message + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.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("ConfectioneryDatabaseImplement.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("ConfectioneryDatabaseImplement.Models.Implementer", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ImplementerFIO") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Password") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Qualification") + .HasColumnType("int"); + + b.Property("WorkExperience") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.ToTable("Implementers"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.MessageInfo", b => + { + b.Property("MessageId") + .HasColumnType("nvarchar(450)"); + + b.Property("Body") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ClientId") + .HasColumnType("int"); + + b.Property("DateDelivery") + .HasColumnType("datetime2"); + + b.Property("SenderName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Subject") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("MessageId"); + + b.HasIndex("ClientId"); + + b.ToTable("Messages"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.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("ImplementerId") + .HasColumnType("int"); + + b.Property("PastryId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("ClientId"); + + b.HasIndex("ImplementerId"); + + b.HasIndex("PastryId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Pastry", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("PastryName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Pastries"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.PastryComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("PastryId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("PastryId"); + + b.ToTable("PastryComponents"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.MessageInfo", b => + { + b.HasOne("ConfectioneryDatabaseImplement.Models.Client", "Client") + .WithMany("Messages") + .HasForeignKey("ClientId"); + + b.Navigation("Client"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Order", b => + { + b.HasOne("ConfectioneryDatabaseImplement.Models.Client", "Client") + .WithMany("Orders") + .HasForeignKey("ClientId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ConfectioneryDatabaseImplement.Models.Implementer", "Implementer") + .WithMany("Orders") + .HasForeignKey("ImplementerId"); + + b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", "Pastry") + .WithMany("Orders") + .HasForeignKey("PastryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Client"); + + b.Navigation("Implementer"); + + b.Navigation("Pastry"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.PastryComponent", b => + { + b.HasOne("ConfectioneryDatabaseImplement.Models.Component", "Component") + .WithMany("PastryComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", "Pastry") + .WithMany("Components") + .HasForeignKey("PastryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Pastry"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Client", b => + { + b.Navigation("Messages"); + + b.Navigation("Orders"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Component", b => + { + b.Navigation("PastryComponents"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Implementer", b => + { + b.Navigation("Orders"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Pastry", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/ConfectioneryDatabaseImplement/Migrations/20230313155013_create_message.cs b/ConfectioneryDatabaseImplement/Migrations/20230313155013_create_message.cs new file mode 100644 index 0000000..9e35314 --- /dev/null +++ b/ConfectioneryDatabaseImplement/Migrations/20230313155013_create_message.cs @@ -0,0 +1,48 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace ConfectioneryDatabaseImplement.Migrations +{ + /// + public partial class create_message : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Messages", + columns: table => new + { + MessageId = table.Column(type: "nvarchar(450)", nullable: false), + ClientId = table.Column(type: "int", nullable: true), + SenderName = table.Column(type: "nvarchar(max)", nullable: false), + DateDelivery = table.Column(type: "datetime2", nullable: false), + Subject = table.Column(type: "nvarchar(max)", nullable: false), + Body = table.Column(type: "nvarchar(max)", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Messages", x => x.MessageId); + table.ForeignKey( + name: "FK_Messages_Clients_ClientId", + column: x => x.ClientId, + principalTable: "Clients", + principalColumn: "Id"); + }); + + migrationBuilder.CreateIndex( + name: "IX_Messages_ClientId", + table: "Messages", + column: "ClientId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Messages"); + } + } +} diff --git a/ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs b/ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs index 4797be9..5fd41b9 100644 --- a/ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs +++ b/ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs @@ -94,6 +94,36 @@ namespace ConfectioneryDatabaseImplement.Migrations b.ToTable("Implementers"); }); + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.MessageInfo", b => + { + b.Property("MessageId") + .HasColumnType("nvarchar(450)"); + + b.Property("Body") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ClientId") + .HasColumnType("int"); + + b.Property("DateDelivery") + .HasColumnType("datetime2"); + + b.Property("SenderName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Subject") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("MessageId"); + + b.HasIndex("ClientId"); + + b.ToTable("Messages"); + }); + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Order", b => { b.Property("Id") @@ -183,6 +213,15 @@ namespace ConfectioneryDatabaseImplement.Migrations b.ToTable("PastryComponents"); }); + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.MessageInfo", b => + { + b.HasOne("ConfectioneryDatabaseImplement.Models.Client", "Client") + .WithMany("Messages") + .HasForeignKey("ClientId"); + + b.Navigation("Client"); + }); + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Order", b => { b.HasOne("ConfectioneryDatabaseImplement.Models.Client", "Client") @@ -229,6 +268,8 @@ namespace ConfectioneryDatabaseImplement.Migrations modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Client", b => { + b.Navigation("Messages"); + b.Navigation("Orders"); }); -- 2.25.1 From 3dabf3982978b4b0734b82d6ba51228b2855f57c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Mon, 13 Mar 2023 20:12:51 +0400 Subject: [PATCH 07/26] MessageInfo Logic --- .../MessageInfoLogic.cs | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 ConfectionaryBusinessLogic/MessageInfoLogic.cs diff --git a/ConfectionaryBusinessLogic/MessageInfoLogic.cs b/ConfectionaryBusinessLogic/MessageInfoLogic.cs new file mode 100644 index 0000000..013ffe0 --- /dev/null +++ b/ConfectionaryBusinessLogic/MessageInfoLogic.cs @@ -0,0 +1,48 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.BusinessLogicsContracts; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContract; +using ConfectioneryContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryBusinessLogic +{ + public class MessageInfoLogic : IMessageInfoLogic + { + private readonly ILogger _logger; + private readonly IMessageInfoStorage _messageInfoStorage; + public MessageInfoLogic(ILogger logger, IMessageInfoStorage MessageInfoStorage) + { + _logger = logger; + _messageInfoStorage = MessageInfoStorage; + } + + public bool Create(MessageInfoBindingModel model) + { + if (_messageInfoStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public List? ReadList(MessageInfoSearchModel? model) + { + _logger.LogInformation("ReadList. MessageId:{MessageId}.ClientId:{ClientId} ", model?.MessageId, model?.ClientId); + var list = (model == null) ? _messageInfoStorage.GetFullList() : _messageInfoStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + } +} -- 2.25.1 From 6519ed129638c47a992dc243573fddd5a1bfd9c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Mon, 13 Mar 2023 20:46:39 +0400 Subject: [PATCH 08/26] =?UTF-8?q?=D0=9E=D1=82=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BA=D0=B0=20=D1=81=D1=82=D0=B0=D1=82=D1=83=D1=81=D0=B0=20?= =?UTF-8?q?=D0=B7=D0=B0=D0=BA=D0=B0=D0=B7=D0=B0=20=D0=BA=D0=BB=D0=B8=D0=B5?= =?UTF-8?q?=D0=BD=D1=82=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MailWorker/AbstractMailWorker.cs | 97 +++++++++++++++++++ .../MailWorker/MailKitWorker.cs | 82 ++++++++++++++++ ConfectionaryBusinessLogic/OrderLogic.cs | 29 +++++- .../BindingModels/MailConfigBindingModel.cs | 18 ++++ .../BindingModels/MailSendInfoBindingModel.cs | 15 +++ 5 files changed, 238 insertions(+), 3 deletions(-) create mode 100644 ConfectionaryBusinessLogic/MailWorker/AbstractMailWorker.cs create mode 100644 ConfectionaryBusinessLogic/MailWorker/MailKitWorker.cs create mode 100644 ConfectioneryContracts/BindingModels/MailConfigBindingModel.cs create mode 100644 ConfectioneryContracts/BindingModels/MailSendInfoBindingModel.cs diff --git a/ConfectionaryBusinessLogic/MailWorker/AbstractMailWorker.cs b/ConfectionaryBusinessLogic/MailWorker/AbstractMailWorker.cs new file mode 100644 index 0000000..390315e --- /dev/null +++ b/ConfectionaryBusinessLogic/MailWorker/AbstractMailWorker.cs @@ -0,0 +1,97 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryBusinessLogic.MailWorker +{ + public abstract class AbstractMailWorker + { + protected string _mailLogin = string.Empty; + + protected string _mailPassword = string.Empty; + + protected string _smtpClientHost = string.Empty; + + protected int _smtpClientPort; + + protected string _popHost = string.Empty; + + protected int _popPort; + + private readonly IMessageInfoLogic _messageInfoLogic; + + private readonly ILogger _logger; + + public AbstractMailWorker(ILogger logger, IMessageInfoLogic messageInfoLogic) + { + _logger = logger; + _messageInfoLogic = messageInfoLogic; + } + + public void MailConfig(MailConfigBindingModel config) + { + _mailLogin = config.MailLogin; + _mailPassword = config.MailPassword; + _smtpClientHost = config.SmtpClientHost; + _smtpClientPort = config.SmtpClientPort; + _popHost = config.PopHost; + _popPort = config.PopPort; + _logger.LogDebug("Config: {login}, {password}, {clientHost}, {clientPOrt}, {popHost}, {popPort}", _mailLogin, _mailPassword, _smtpClientHost, _smtpClientPort, _popHost, _popPort); + } + + public async void MailSendAsync(MailSendInfoBindingModel info) + { + if (string.IsNullOrEmpty(_mailLogin) || string.IsNullOrEmpty(_mailPassword)) + { + return; + } + + if (string.IsNullOrEmpty(_smtpClientHost) || _smtpClientPort == 0) + { + return; + } + + if (string.IsNullOrEmpty(info.MailAddress) || string.IsNullOrEmpty(info.Subject) || string.IsNullOrEmpty(info.Text)) + { + return; + } + + _logger.LogDebug("Send Mail: {To}, {Subject}", info.MailAddress, info.Subject); + await SendMailAsync(info); + } + + public async void MailCheck() + { + if (string.IsNullOrEmpty(_mailLogin) || string.IsNullOrEmpty(_mailPassword)) + { + return; + } + + if (string.IsNullOrEmpty(_popHost) || _popPort == 0) + { + return; + } + + if (_messageInfoLogic == null) + { + return; + } + + var list = await ReceiveMailAsync(); + _logger.LogDebug("Check Mail: {Count} new mails", list.Count); + foreach (var mail in list) + { + _messageInfoLogic.Create(mail); + } + } + + protected abstract Task SendMailAsync(MailSendInfoBindingModel info); + + protected abstract Task> ReceiveMailAsync(); + } +} diff --git a/ConfectionaryBusinessLogic/MailWorker/MailKitWorker.cs b/ConfectionaryBusinessLogic/MailWorker/MailKitWorker.cs new file mode 100644 index 0000000..5acecf2 --- /dev/null +++ b/ConfectionaryBusinessLogic/MailWorker/MailKitWorker.cs @@ -0,0 +1,82 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.BusinessLogicsContracts; +using MailKit.Net.Pop3; +using MailKit.Security; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Mail; +using System.Net; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryBusinessLogic.MailWorker +{ + public class MailKitWorker : AbstractMailWorker + { + public MailKitWorker(ILogger logger, IMessageInfoLogic messageInfoLogic) : base(logger, messageInfoLogic) { } + + protected override async Task SendMailAsync(MailSendInfoBindingModel info) + { + using var objMailMessage = new MailMessage(); + using var objSmtpClient = new SmtpClient(_smtpClientHost, _smtpClientPort); + try + { + objMailMessage.From = new MailAddress(_mailLogin); + objMailMessage.To.Add(new MailAddress(info.MailAddress)); + objMailMessage.Subject = info.Subject; + objMailMessage.Body = info.Text; + objMailMessage.SubjectEncoding = Encoding.UTF8; + objMailMessage.BodyEncoding = Encoding.UTF8; + + objSmtpClient.UseDefaultCredentials = false; + objSmtpClient.EnableSsl = true; + objSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; + objSmtpClient.Credentials = new NetworkCredential(_mailLogin, _mailPassword); + + await Task.Run(() => objSmtpClient.Send(objMailMessage)); + } + catch (Exception) + { + throw; + } + } + + protected override async Task> ReceiveMailAsync() + { + var list = new List(); + using var client = new Pop3Client(); + await Task.Run(() => + { + try + { + client.Connect(_popHost, _popPort, SecureSocketOptions.SslOnConnect); + client.Authenticate(_mailLogin, _mailPassword); + for (int i = 0; i < client.Count; i++) + { + var message = client.GetMessage(i); + foreach (var mail in message.From.Mailboxes) + { + list.Add(new MessageInfoBindingModel + { + DateDelivery = message.Date.DateTime, + MessageId = message.MessageId, + SenderName = mail.Address, + Subject = message.Subject, + Body = message.TextBody + }); + } + } + } + catch (AuthenticationException) + { } + finally + { + client.Disconnect(true); + } + }); + return list; + } + } +} diff --git a/ConfectionaryBusinessLogic/OrderLogic.cs b/ConfectionaryBusinessLogic/OrderLogic.cs index 45bd78c..ab2b86d 100644 --- a/ConfectionaryBusinessLogic/OrderLogic.cs +++ b/ConfectionaryBusinessLogic/OrderLogic.cs @@ -1,4 +1,5 @@ -using ConfectioneryContracts.BindingModels; +using ConfectioneryBusinessLogic.MailWorker; +using ConfectioneryContracts.BindingModels; using ConfectioneryContracts.BusinessLogicsContracts; using ConfectioneryContracts.SearchModels; using ConfectioneryContracts.StoragesContract; @@ -12,11 +13,15 @@ namespace ConfectioneryBusinessLogic.BusinessLogics { private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; + private readonly AbstractMailWorker _mailWorker; + private readonly ClientLogic _clientLogic; - public OrderLogic(ILogger logger, IOrderStorage orderStorage) + public OrderLogic(ILogger logger, IOrderStorage orderStorage, AbstractMailWorker mailWorker, ClientLogic clientLogic) { _logger = logger; _orderStorage = orderStorage; + _mailWorker = mailWorker; + _clientLogic = clientLogic; } public bool CreateOrder(OrderBindingModel model) @@ -34,6 +39,7 @@ namespace ConfectioneryBusinessLogic.BusinessLogics _logger.LogWarning("Insert operation failed"); return false; } + SendOrderStatusMail(model, $"Новый заказ создан. Номер заказа #{model.Id}", $"Заказ #{model.Id} от {model.DateCreate} на сумму {model.Sum:.02f} принят"); return true; } @@ -109,7 +115,8 @@ namespace ConfectioneryBusinessLogic.BusinessLogics _logger.LogWarning("Update operation failed"); return false; } - return true; + SendOrderStatusMail(model, $"Изменен статус заказа #{model.Id}", $"Заказ #{model.Id} изменен статус на {model.Status}"); + return true; } public OrderViewModel? ReadElement(OrderSearchModel model) @@ -128,5 +135,21 @@ namespace ConfectioneryBusinessLogic.BusinessLogics _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); return element; } + + private bool SendOrderStatusMail(OrderBindingModel model, string subject, string text) + { + var client = _clientLogic.ReadElement(new() { Id = model.ClientId }); + if (client == null) + { + return false; + } + _mailWorker.MailSendAsync(new() + { + MailAddress = client.Email, + Subject = subject, + Text = text + }); + return true; + } } } diff --git a/ConfectioneryContracts/BindingModels/MailConfigBindingModel.cs b/ConfectioneryContracts/BindingModels/MailConfigBindingModel.cs new file mode 100644 index 0000000..0d2aa83 --- /dev/null +++ b/ConfectioneryContracts/BindingModels/MailConfigBindingModel.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryContracts.BindingModels +{ + public class MailConfigBindingModel + { + public string MailLogin { get; set; } = string.Empty; + public string MailPassword { get; set; } = string.Empty; + public string SmtpClientHost { get; set; } = string.Empty; + public int SmtpClientPort { get; set; } + public string PopHost { get; set; } = string.Empty; + public int PopPort { get; set; } + } +} diff --git a/ConfectioneryContracts/BindingModels/MailSendInfoBindingModel.cs b/ConfectioneryContracts/BindingModels/MailSendInfoBindingModel.cs new file mode 100644 index 0000000..8fd8e76 --- /dev/null +++ b/ConfectioneryContracts/BindingModels/MailSendInfoBindingModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryContracts.BindingModels +{ + public class MailSendInfoBindingModel + { + public string MailAddress { get; set; } = string.Empty; + public string Subject { get; set; } = string.Empty; + public string Text { get; set; } = string.Empty; + } +} -- 2.25.1 From b1abff7997a15787e62456a270f598eaf806d41c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Mon, 13 Mar 2023 21:33:49 +0400 Subject: [PATCH 09/26] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D1=81=D0=BC=D0=BE?= =?UTF-8?q?=D1=82=D1=80=20=D0=BF=D0=B8=D1=81=D0=B5=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConfectionaryBusinessLogic/OrderLogic.cs | 4 +- Confectionery/App.config | 11 +++++ Confectionery/ConfectioneryView.csproj | 3 ++ Confectionery/FormMain.Designer.cs | 35 ++++++++----- Confectionery/FormMain.cs | 9 ++++ Confectionery/FormViewMail.Designer.cs | 62 ++++++++++++++++++++++++ Confectionery/FormViewMail.cs | 50 +++++++++++++++++++ Confectionery/FormViewMail.resx | 60 +++++++++++++++++++++++ Confectionery/Program.cs | 40 +++++++++++++-- 9 files changed, 255 insertions(+), 19 deletions(-) create mode 100644 Confectionery/App.config create mode 100644 Confectionery/FormViewMail.Designer.cs create mode 100644 Confectionery/FormViewMail.cs create mode 100644 Confectionery/FormViewMail.resx diff --git a/ConfectionaryBusinessLogic/OrderLogic.cs b/ConfectionaryBusinessLogic/OrderLogic.cs index ab2b86d..d917771 100644 --- a/ConfectionaryBusinessLogic/OrderLogic.cs +++ b/ConfectionaryBusinessLogic/OrderLogic.cs @@ -14,9 +14,9 @@ namespace ConfectioneryBusinessLogic.BusinessLogics private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; private readonly AbstractMailWorker _mailWorker; - private readonly ClientLogic _clientLogic; + private readonly IClientLogic _clientLogic; - public OrderLogic(ILogger logger, IOrderStorage orderStorage, AbstractMailWorker mailWorker, ClientLogic clientLogic) + public OrderLogic(ILogger logger, IOrderStorage orderStorage, AbstractMailWorker mailWorker, IClientLogic clientLogic) { _logger = logger; _orderStorage = orderStorage; diff --git a/Confectionery/App.config b/Confectionery/App.config new file mode 100644 index 0000000..347c78c --- /dev/null +++ b/Confectionery/App.config @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/Confectionery/ConfectioneryView.csproj b/Confectionery/ConfectioneryView.csproj index 803eeb2..c6c394d 100644 --- a/Confectionery/ConfectioneryView.csproj +++ b/Confectionery/ConfectioneryView.csproj @@ -36,6 +36,9 @@ + + Always + Always diff --git a/Confectionery/FormMain.Designer.cs b/Confectionery/FormMain.Designer.cs index 71443e1..e72176d 100644 --- a/Confectionery/FormMain.Designer.cs +++ b/Confectionery/FormMain.Designer.cs @@ -38,18 +38,19 @@ pastriesToolStripMenuItem = new ToolStripMenuItem(); pastryComponentsToolStripMenuItem = new ToolStripMenuItem(); ordersToolStripMenuItem = new ToolStripMenuItem(); + DoWorkToolStripMenuItem = new ToolStripMenuItem(); + mailToolStripMenuItem = new ToolStripMenuItem(); dataGridView = new DataGridView(); buttonCreateOrder = new Button(); button4 = new Button(); button3 = new Button(); - DoWorkToolStripMenuItem = new ToolStripMenuItem(); menuStrip1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); SuspendLayout(); // // menuStrip1 // - menuStrip1.Items.AddRange(new ToolStripItem[] { справочникиToolStripMenuItem, reportsToolStripMenuItem, DoWorkToolStripMenuItem }); + menuStrip1.Items.AddRange(new ToolStripItem[] { справочникиToolStripMenuItem, reportsToolStripMenuItem, DoWorkToolStripMenuItem, mailToolStripMenuItem }); menuStrip1.Location = new Point(0, 0); menuStrip1.Name = "menuStrip1"; menuStrip1.Size = new Size(783, 24); @@ -66,28 +67,28 @@ // pastryToolStripMenuItem // pastryToolStripMenuItem.Name = "pastryToolStripMenuItem"; - pastryToolStripMenuItem.Size = new Size(180, 22); + pastryToolStripMenuItem.Size = new Size(149, 22); pastryToolStripMenuItem.Text = "Изделия"; pastryToolStripMenuItem.Click += PastryToolStripMenuItem_Click; // // componentToolStripMenuItem // componentToolStripMenuItem.Name = "componentToolStripMenuItem"; - componentToolStripMenuItem.Size = new Size(180, 22); + componentToolStripMenuItem.Size = new Size(149, 22); componentToolStripMenuItem.Text = "Компоненты"; componentToolStripMenuItem.Click += ComponentsToolStripMenuItem_Click; // // clientsToolStripMenuItem // clientsToolStripMenuItem.Name = "clientsToolStripMenuItem"; - clientsToolStripMenuItem.Size = new Size(180, 22); + clientsToolStripMenuItem.Size = new Size(149, 22); clientsToolStripMenuItem.Text = "Клиенты"; clientsToolStripMenuItem.Click += ClientsToolStripMenuItem_Click; // // ImplementersToolStripMenuItem // ImplementersToolStripMenuItem.Name = "ImplementersToolStripMenuItem"; - ImplementersToolStripMenuItem.Size = new Size(180, 22); + ImplementersToolStripMenuItem.Size = new Size(149, 22); ImplementersToolStripMenuItem.Text = "Исполнители"; ImplementersToolStripMenuItem.Click += ImplementersToolStripMenuItem_Click; // @@ -119,6 +120,20 @@ ordersToolStripMenuItem.Text = "Список заказов"; ordersToolStripMenuItem.Click += OrdersToolStripMenuItem_Click; // + // DoWorkToolStripMenuItem + // + DoWorkToolStripMenuItem.Name = "DoWorkToolStripMenuItem"; + DoWorkToolStripMenuItem.Size = new Size(92, 20); + DoWorkToolStripMenuItem.Text = "Запуск работ"; + DoWorkToolStripMenuItem.Click += DoWorkToolStripMenuItem_Click; + // + // mailToolStripMenuItem + // + mailToolStripMenuItem.Name = "mailToolStripMenuItem"; + mailToolStripMenuItem.Size = new Size(62, 20); + mailToolStripMenuItem.Text = "Письма"; + mailToolStripMenuItem.Click += MailToolStripMenuItem_Click; + // // dataGridView // dataGridView.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right; @@ -162,13 +177,6 @@ button3.UseVisualStyleBackColor = true; button3.Click += ButtonIssuedOrder_Click; // - // DoWorkToolStripMenuItem - // - DoWorkToolStripMenuItem.Name = "DoWorkToolStripMenuItem"; - DoWorkToolStripMenuItem.Size = new Size(92, 20); - DoWorkToolStripMenuItem.Text = "Запуск работ"; - DoWorkToolStripMenuItem.Click += DoWorkToolStripMenuItem_Click; - // // FormMain // AutoScaleDimensions = new SizeF(7F, 15F); @@ -207,5 +215,6 @@ private ToolStripMenuItem ImplementersToolStripMenuItem; private ToolStripMenuItem DoWorkToolStripMenuItem; private Button button3; + private ToolStripMenuItem mailToolStripMenuItem; } } \ No newline at end of file diff --git a/Confectionery/FormMain.cs b/Confectionery/FormMain.cs index cfb2afd..870ee07 100644 --- a/Confectionery/FormMain.cs +++ b/Confectionery/FormMain.cs @@ -216,5 +216,14 @@ namespace ConfectioneryView MessageBox.Show(" ", "", MessageBoxButtons.OK, MessageBoxIcon.Information); } + + private void MailToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormViewMail)); + if (service is FormViewMail form) + { + form.ShowDialog(); + } + } } } \ No newline at end of file diff --git a/Confectionery/FormViewMail.Designer.cs b/Confectionery/FormViewMail.Designer.cs new file mode 100644 index 0000000..021a64d --- /dev/null +++ b/Confectionery/FormViewMail.Designer.cs @@ -0,0 +1,62 @@ +namespace ConfectioneryView +{ + partial class FormViewMail + { + /// + /// 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() + { + dataGridView = new DataGridView(); + ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); + SuspendLayout(); + // + // dataGridView + // + dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + dataGridView.Dock = DockStyle.Fill; + dataGridView.Location = new Point(0, 0); + dataGridView.Name = "dataGridView"; + dataGridView.RowTemplate.Height = 25; + dataGridView.Size = new Size(803, 450); + dataGridView.TabIndex = 0; + // + // FormViewMail + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(800, 450); + Controls.Add(dataGridView); + Name = "FormViewMail"; + Text = "Письма"; + Load += FormViewMail_Load; + ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); + ResumeLayout(false); + } + + #endregion + + private DataGridView dataGridView; + } +} \ No newline at end of file diff --git a/Confectionery/FormViewMail.cs b/Confectionery/FormViewMail.cs new file mode 100644 index 0000000..619bac5 --- /dev/null +++ b/Confectionery/FormViewMail.cs @@ -0,0 +1,50 @@ +using ConfectioneryBusinessLogic.MailWorker; +using ConfectioneryContracts.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 ConfectioneryView +{ + public partial class FormViewMail : Form + { + private readonly ILogger _logger; + private readonly IMessageInfoLogic _logic; + + public FormViewMail(ILogger logger, IMessageInfoLogic logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + } + + private void FormViewMail_Load(object sender, EventArgs e) + { + try + { + var list = _logic.ReadList(null); + if (list != null) + { + dataGridView.DataSource = list; + dataGridView.Columns["ClientId"].Visible = false; + dataGridView.Columns["MessageId"].Visible = false; + dataGridView.Columns["Body"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + } + _logger.LogInformation("Загрузка списка писем"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки писем"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + } +} diff --git a/Confectionery/FormViewMail.resx b/Confectionery/FormViewMail.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/Confectionery/FormViewMail.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/Confectionery/Program.cs b/Confectionery/Program.cs index f081352..3596c05 100644 --- a/Confectionery/Program.cs +++ b/Confectionery/Program.cs @@ -9,6 +9,8 @@ using NLog.Extensions.Logging; using ConfectioneryBusinessLogic; using ConfectioneryBusinessLogic.OfficePackage.Implements; using ConfectioneryBusinessLogic.OfficePackage; +using ConfectioneryBusinessLogic.MailWorker; +using ConfectioneryContracts.BindingModels; namespace ConfectioneryView { @@ -28,7 +30,30 @@ namespace ConfectioneryView var services = new ServiceCollection(); ConfigureServices(services); _serviceProvider = services.BuildServiceProvider(); - Application.Run(_serviceProvider.GetRequiredService()); + + try + { + var mailSender = _serviceProvider.GetService(); + mailSender?.MailConfig(new MailConfigBindingModel + { + MailLogin = System.Configuration.ConfigurationManager.AppSettings["MailLogin"] ?? string.Empty, + MailPassword = System.Configuration.ConfigurationManager.AppSettings["MailPassword"] ?? string.Empty, + SmtpClientHost = System.Configuration.ConfigurationManager.AppSettings["SmtpClientHost"] ?? string.Empty, + SmtpClientPort = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["SmtpClientPort"]), + PopHost = System.Configuration.ConfigurationManager.AppSettings["PopHost"] ?? string.Empty, + PopPort = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["PopPort"]) + }); + + // + var timer = new System.Threading.Timer(new TimerCallback(MailCheck!), null, 0, 100000); + } + catch (Exception ex) + { + var logger = _serviceProvider.GetService(); + logger?.LogError(ex, " "); + } + + Application.Run(_serviceProvider.GetRequiredService()); } private static void ConfigureServices(ServiceCollection services) { @@ -42,16 +67,20 @@ namespace ConfectioneryView services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); - services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); - services.AddTransient(); + services.AddSingleton(); + + services.AddTransient(); services.AddTransient(); services.AddTransient(); @@ -67,6 +96,9 @@ namespace ConfectioneryView services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); } - } + + private static void MailCheck(object obj) => ServiceProvider?.GetService()?.MailCheck(); + } } \ No newline at end of file -- 2.25.1 From 6d2dccd4df29458c21ded329e20ec709e852e164 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Mon, 13 Mar 2023 22:59:37 +0400 Subject: [PATCH 10/26] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=BE=20=D0=B2=D1=81=D0=B5,=20=D0=BE=D1=81=D1=82?= =?UTF-8?q?=D0=B0=D0=BB=D0=BE=D1=81=D1=8C=20=D0=B4=D0=BE=D0=BF=D0=B8=D0=BB?= =?UTF-8?q?=D0=B8=D1=82=D1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConfectionaryBusinessLogic/OrderLogic.cs | 14 ++--- Confectionery/App.config | 6 +-- .../Controllers/HomeController.cs | 10 ++++ .../Views/Home/Mails.cshtml | 54 +++++++++++++++++++ .../Views/Shared/_Layout.cshtml | 3 ++ .../MessageInfoStorage.cs | 2 +- .../Controllers/ClientController.cs | 23 +++++++- ConfectioneryRestApi/Program.cs | 19 +++++++ ConfectioneryRestApi/appsettings.json | 9 +++- 9 files changed, 127 insertions(+), 13 deletions(-) create mode 100644 ConfectioneryClientApp/Views/Home/Mails.cshtml diff --git a/ConfectionaryBusinessLogic/OrderLogic.cs b/ConfectionaryBusinessLogic/OrderLogic.cs index d917771..d12e142 100644 --- a/ConfectionaryBusinessLogic/OrderLogic.cs +++ b/ConfectionaryBusinessLogic/OrderLogic.cs @@ -34,12 +34,13 @@ namespace ConfectioneryBusinessLogic.BusinessLogics } model.Status = OrderStatus.Принят; model.DateCreate = DateTime.Now; - if (_orderStorage.Insert(model) == null) + var result = _orderStorage.Insert(model); + if (result == null) { _logger.LogWarning("Insert operation failed"); return false; } - SendOrderStatusMail(model, $"Новый заказ создан. Номер заказа #{model.Id}", $"Заказ #{model.Id} от {model.DateCreate} на сумму {model.Sum:.02f} принят"); + SendOrderStatusMail(result.ClientId, $"Новый заказ создан. Номер заказа #{result.Id}", $"Заказ #{result.Id} от {result.DateCreate} на сумму {result.Sum:0.00} принят"); return true; } @@ -110,12 +111,13 @@ namespace ConfectioneryBusinessLogic.BusinessLogics model.PastryId = vmodel.PastryId; model.Sum = vmodel.Sum; model.Count= vmodel.Count; - if (_orderStorage.Update(model) == null) + var result = _orderStorage.Update(model); + if (result == null) { _logger.LogWarning("Update operation failed"); return false; } - SendOrderStatusMail(model, $"Изменен статус заказа #{model.Id}", $"Заказ #{model.Id} изменен статус на {model.Status}"); + SendOrderStatusMail(result.ClientId, $"Изменен статус заказа #{result.Id}", $"Заказ #{model.Id} изменен статус на {result.Status}"); return true; } @@ -136,9 +138,9 @@ namespace ConfectioneryBusinessLogic.BusinessLogics return element; } - private bool SendOrderStatusMail(OrderBindingModel model, string subject, string text) + private bool SendOrderStatusMail(int clientId, string subject, string text) { - var client = _clientLogic.ReadElement(new() { Id = model.ClientId }); + var client = _clientLogic.ReadElement(new() { Id = clientId }); if (client == null) { return false; diff --git a/Confectionery/App.config b/Confectionery/App.config index 347c78c..400d322 100644 --- a/Confectionery/App.config +++ b/Confectionery/App.config @@ -1,11 +1,11 @@  - + - + - + \ No newline at end of file diff --git a/ConfectioneryClientApp/Controllers/HomeController.cs b/ConfectioneryClientApp/Controllers/HomeController.cs index 83f23d6..c48230a 100644 --- a/ConfectioneryClientApp/Controllers/HomeController.cs +++ b/ConfectioneryClientApp/Controllers/HomeController.cs @@ -144,5 +144,15 @@ namespace ConfectioneryClientApp.Controllers var prod = APIClient.GetRequest($"api/main/getpastry?pastryId={pastry}"); return count * (prod?.Price ?? 1); } + + [HttpGet] + public IActionResult Mails() + { + if (APIClient.Client == null) + { + return Redirect("~/Home/Enter"); + } + return View(APIClient.GetRequest>($"api/client/getmessages?clientId={APIClient.Client.Id}")); + } } } \ No newline at end of file diff --git a/ConfectioneryClientApp/Views/Home/Mails.cshtml b/ConfectioneryClientApp/Views/Home/Mails.cshtml new file mode 100644 index 0000000..e881061 --- /dev/null +++ b/ConfectioneryClientApp/Views/Home/Mails.cshtml @@ -0,0 +1,54 @@ +@using ConfectioneryContracts.ViewModels + +@model List + +@{ + ViewData["Title"] = "Mails"; +} + +
+

Заказы

+
+ + +
+ @{ + if (Model == null) + { +

Авторизируйтесь

+ return; + } + + + + + + + + + + + @foreach (var item in Model) + { + + + + + + } + +
+ Дата письма + + Заголовок + + Текст +
+ @Html.DisplayFor(modelItem => item.DateDelivery) + + @Html.DisplayFor(modelItem => item.Subject) + + @Html.DisplayFor(modelItem => item.Body) +
+ } +
diff --git a/ConfectioneryClientApp/Views/Shared/_Layout.cshtml b/ConfectioneryClientApp/Views/Shared/_Layout.cshtml index 8801497..b329d8b 100644 --- a/ConfectioneryClientApp/Views/Shared/_Layout.cshtml +++ b/ConfectioneryClientApp/Views/Shared/_Layout.cshtml @@ -28,6 +28,9 @@ + diff --git a/ConfectioneryDatabaseImplement/MessageInfoStorage.cs b/ConfectioneryDatabaseImplement/MessageInfoStorage.cs index bd25a7c..0e5a4bf 100644 --- a/ConfectioneryDatabaseImplement/MessageInfoStorage.cs +++ b/ConfectioneryDatabaseImplement/MessageInfoStorage.cs @@ -40,7 +40,7 @@ namespace ConfectioneryDatabaseImplement { using var context = new ConfectioneryDatabase(); var newMessage = MessageInfo.Create(model); - if (newMessage == null) + if (newMessage == null || context.Messages.Any(x => x.MessageId.Equals(model.MessageId))) { return null; } diff --git a/ConfectioneryRestApi/Controllers/ClientController.cs b/ConfectioneryRestApi/Controllers/ClientController.cs index 6e165ca..70ab65a 100644 --- a/ConfectioneryRestApi/Controllers/ClientController.cs +++ b/ConfectioneryRestApi/Controllers/ClientController.cs @@ -13,11 +13,13 @@ namespace ConfectioneryRestApi.Controllers private readonly ILogger _logger; private readonly IClientLogic _logic; + private readonly IMessageInfoLogic _mailLogic; - public ClientController(IClientLogic logic, ILogger logger) + public ClientController(IClientLogic logic, IMessageInfoLogic mailLogic, ILogger logger) { _logger = logger; _logic = logic; + _mailLogic = mailLogic; } [HttpGet] @@ -66,5 +68,22 @@ namespace ConfectioneryRestApi.Controllers throw; } } - } + + [HttpGet] + public List? GetMessages(int clientId) + { + try + { + return _mailLogic.ReadList(new MessageInfoSearchModel + { + ClientId = clientId + }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения писем клиента"); + throw; + } + } + } } diff --git a/ConfectioneryRestApi/Program.cs b/ConfectioneryRestApi/Program.cs index da7b812..865a728 100644 --- a/ConfectioneryRestApi/Program.cs +++ b/ConfectioneryRestApi/Program.cs @@ -1,7 +1,10 @@ using ConfectioneryBusinessLogic; using ConfectioneryBusinessLogic.BusinessLogics; +using ConfectioneryBusinessLogic.MailWorker; +using ConfectioneryContracts.BindingModels; using ConfectioneryContracts.BusinessLogicsContracts; using ConfectioneryContracts.StoragesContract; +using ConfectioneryDatabaseImplement; using ConfectioneryDatabaseImplement.Implements; using Microsoft.OpenApi.Models; @@ -14,7 +17,10 @@ builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddSingleton(); builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddControllers(); @@ -31,6 +37,19 @@ builder.Services.AddSwaggerGen(c => }); }); var app = builder.Build(); + +var mailSender = app.Services.GetService(); + +mailSender?.MailConfig(new MailConfigBindingModel +{ + MailLogin = builder.Configuration?.GetSection("MailLogin")?.Value?.ToString() ?? string.Empty, + MailPassword = builder.Configuration?.GetSection("MailPassword")?.Value?.ToString() ?? string.Empty, + SmtpClientHost = builder.Configuration?.GetSection("SmtpClientHost")?.Value?.ToString() ?? string.Empty, + SmtpClientPort = Convert.ToInt32(builder.Configuration?.GetSection("SmtpClientPort")?.Value?.ToString()), + PopHost = builder.Configuration?.GetSection("PopHost")?.Value?.ToString() ?? string.Empty, + PopPort = Convert.ToInt32(builder.Configuration?.GetSection("PopPort")?.Value?.ToString()) +}); + // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { diff --git a/ConfectioneryRestApi/appsettings.json b/ConfectioneryRestApi/appsettings.json index 10f68b8..697acfb 100644 --- a/ConfectioneryRestApi/appsettings.json +++ b/ConfectioneryRestApi/appsettings.json @@ -5,5 +5,12 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + + "SmtpClientHost": "smtp.mail.ru", + "SmtpClientPort": "587", + "PopHost": "pop.mail.ru", + "PopPort": "995", + "MailLogin": "ordersender228@mail.ru", + "MailPassword": "v8czsQ8zztJc5wEHxKPN" } -- 2.25.1 From dc981b478242825642c60ec62a0627cab7279acf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Tue, 14 Mar 2023 00:31:06 +0400 Subject: [PATCH 11/26] =?UTF-8?q?=D0=92=D0=9B=D0=90=D0=94=D0=98=D0=9C?= =?UTF-8?q?=D0=98=D0=A0=20=D0=9F=D0=A3=D0=A2=D0=98=D0=9D=20=D0=9C=D0=9E?= =?UTF-8?q?=D0=9B=D0=9E=D0=94=D0=95=D0=A6!!!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ⣿⣿⣿⣻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿ ⣿⣿⣿⣵⣿⣿⣿⠿⡟⣛⣧⣿⣯⣿⣝⡻⢿⣿⣿⣿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⠋⠁⣴⣶⣿⣿⣿⣿⣿⣿⣿⣦⣍⢿⣿⣿⣿⣿⣿ ⣿⣿⣿⣿⢷⠄⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣏⢼⣿⣿⣿⣿ ⢹⣿⣿⢻⠎⠔⣛⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡏⣿⣿⣿⣿ ⢸⣿⣿⠇⡶⠄⣿⣿⠿⠟⡛⠛⠻⣿⡿⠿⠿⣿⣗⢣⣿⣿⣿⣿ ⠐⣿⣿⡿⣷⣾⣿⣿⣿⣾⣶⣶⣶⣿⣁⣔⣤⣀⣼⢲⣿⣿⣿⣿ ⠄⣿⣿⣿⣿⣾⣟⣿⣿⣿⣿⣿⣿⣿⡿⣿⣿⣿⢟⣾⣿⣿⣿⣿ ⠄⣟⣿⣿⣿⡷⣿⣿⣿⣿⣿⣮⣽⠛⢻⣽⣿⡇⣾⣿⣿⣿⣿⣿ ⠄⢻⣿⣿⣿⡷⠻⢻⡻⣯⣝⢿⣟⣛⣛⣛⠝⢻⣿⣿⣿⣿⣿⣿ ⠄⠸⣿⣿⡟⣹⣦⠄⠋⠻⢿⣶⣶⣶⡾⠃⡂⢾⣿⣿⣿⣿⣿⣿ ⠄⠄⠟⠋⠄⢻⣿⣧⣲⡀⡀⠄⠉⠱⣠⣾⡇⠄⠉⠛⢿⣿⣿⣿ ⠄⠄⠄⠄⠄⠈⣿⣿⣿⣷⣿⣿⢾⣾⣿⣿⣇⠄⠄⠄⠄⠄⠉⠉ ⠄⠄⠄⠄⠄⠄⠸⣿⣿⠟⠃⠄⠄⢈⣻⣿⣿⠄⠄⠄⠄⠄⠄⠄ ⠄⠄⠄⠄⠄⠄⠄⢿⣿⣾⣷⡄⠄⢾⣿⣿⣿⡄⠄⠄⠄⠄⠄⠄ ⠄⠄⠄⠄⠄⠄⠄⠸⣿⣿⣿⠃⠄⠈⢿⣿⣿⠄⠄⠄⠄⠄ --- ConfectionaryBusinessLogic/MailWorker/AbstractMailWorker.cs | 5 ++++- ConfectionaryBusinessLogic/MailWorker/MailKitWorker.cs | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/ConfectionaryBusinessLogic/MailWorker/AbstractMailWorker.cs b/ConfectionaryBusinessLogic/MailWorker/AbstractMailWorker.cs index 390315e..55e56d2 100644 --- a/ConfectionaryBusinessLogic/MailWorker/AbstractMailWorker.cs +++ b/ConfectionaryBusinessLogic/MailWorker/AbstractMailWorker.cs @@ -24,13 +24,15 @@ namespace ConfectioneryBusinessLogic.MailWorker protected int _popPort; private readonly IMessageInfoLogic _messageInfoLogic; + private readonly IClientLogic _clientLogic; private readonly ILogger _logger; - public AbstractMailWorker(ILogger logger, IMessageInfoLogic messageInfoLogic) + public AbstractMailWorker(ILogger logger, IMessageInfoLogic messageInfoLogic, IClientLogic clientLogic) { _logger = logger; _messageInfoLogic = messageInfoLogic; + _clientLogic = clientLogic; } public void MailConfig(MailConfigBindingModel config) @@ -86,6 +88,7 @@ namespace ConfectioneryBusinessLogic.MailWorker _logger.LogDebug("Check Mail: {Count} new mails", list.Count); foreach (var mail in list) { + mail.ClientId = _clientLogic.ReadElement(new() { Email = mail.SenderName })?.Id; _messageInfoLogic.Create(mail); } } diff --git a/ConfectionaryBusinessLogic/MailWorker/MailKitWorker.cs b/ConfectionaryBusinessLogic/MailWorker/MailKitWorker.cs index 5acecf2..52fbcf2 100644 --- a/ConfectionaryBusinessLogic/MailWorker/MailKitWorker.cs +++ b/ConfectionaryBusinessLogic/MailWorker/MailKitWorker.cs @@ -15,7 +15,7 @@ namespace ConfectioneryBusinessLogic.MailWorker { public class MailKitWorker : AbstractMailWorker { - public MailKitWorker(ILogger logger, IMessageInfoLogic messageInfoLogic) : base(logger, messageInfoLogic) { } + public MailKitWorker(ILogger logger, IMessageInfoLogic messageInfoLogic, IClientLogic clientLogic) : base(logger, messageInfoLogic, clientLogic) { } protected override async Task SendMailAsync(MailSendInfoBindingModel info) { -- 2.25.1 From 368716d3ba636c540ad4f709475f4d987023028e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Fri, 17 Mar 2023 16:47:49 +0400 Subject: [PATCH 12/26] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D0=B2=D0=B0=D0=BB=D0=B8=D0=B4=D0=B0=D1=86?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B=D1=85=20=D0=BA?= =?UTF-8?q?=D0=BB=D0=B8=D0=B5=D0=BD=D1=82=D0=B0=20(regex)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConfectionaryBusinessLogic/ClientLogic.cs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/ConfectionaryBusinessLogic/ClientLogic.cs b/ConfectionaryBusinessLogic/ClientLogic.cs index 1a9b6c8..be32b1f 100644 --- a/ConfectionaryBusinessLogic/ClientLogic.cs +++ b/ConfectionaryBusinessLogic/ClientLogic.cs @@ -1,10 +1,12 @@ -using ConfectioneryBusinessLogic.BusinessLogics; +using System.Text.RegularExpressions; +using ConfectioneryBusinessLogic.BusinessLogics; using ConfectioneryContracts.BindingModels; using ConfectioneryContracts.BusinessLogicsContracts; using ConfectioneryContracts.SearchModels; using ConfectioneryContracts.StoragesContract; using ConfectioneryContracts.ViewModels; using Microsoft.Extensions.Logging; +// ReSharper disable All namespace ConfectioneryBusinessLogic { @@ -102,6 +104,16 @@ namespace ConfectioneryBusinessLogic { throw new ArgumentNullException("Нет логина клиента", nameof(model.Email)); } + if (!Regex.IsMatch(model.Email, @"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$")) + { + throw new ArgumentException("Некорретно введенный email", nameof(model.Email)); + } + // Исходный: ^((\w+\d+\W+)|(\w+\W+\d+)|(\d+\w+\W+)|(\d+\W+\w+)|(\W+\w+\d+)|(\W+\d+\w +))+$ + // Просматриваем наперед последовательность (де-факто оператор &&) цифр, небуквенных символов и букв (не цифр и не пробельных символов) + if (!Regex.IsMatch(model.Password, @"^(?=.*\d)(?=.*\W)(?=.*[^\d\s]).+$")) + { + throw new ArgumentException("Некорректно введенный пароль. Пароль должен содержать хотя бы одну букву, цифру и не буквенный символ", nameof(model.Password)); + } _logger.LogInformation("Client. Id: {Id}, FIO: {fio}, email: {email}", model.Id, model.ClientFIO, model.Email ); var element = _clientStorage.GetElement(new ClientSearchModel { -- 2.25.1 From 9eae1097fcf9e2445143a5a563b47c818d2a5212 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Fri, 17 Mar 2023 16:49:36 +0400 Subject: [PATCH 13/26] =?UTF-8?q?=D0=B4=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConfectionaryBusinessLogic/ClientLogic.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ConfectionaryBusinessLogic/ClientLogic.cs b/ConfectionaryBusinessLogic/ClientLogic.cs index be32b1f..28f5ef6 100644 --- a/ConfectionaryBusinessLogic/ClientLogic.cs +++ b/ConfectionaryBusinessLogic/ClientLogic.cs @@ -108,7 +108,7 @@ namespace ConfectioneryBusinessLogic { throw new ArgumentException("Некорретно введенный email", nameof(model.Email)); } - // Исходный: ^((\w+\d+\W+)|(\w+\W+\d+)|(\d+\w+\W+)|(\d+\W+\w+)|(\W+\w+\d+)|(\W+\d+\w +))+$ + // Запасной (вероятно более правильный): ^((\w+\d+\W+)|(\w+\W+\d+)|(\d+\w+\W+)|(\d+\W+\w+)|(\W+\w+\d+)|(\W+\d+\w+))+$ // Просматриваем наперед последовательность (де-факто оператор &&) цифр, небуквенных символов и букв (не цифр и не пробельных символов) if (!Regex.IsMatch(model.Password, @"^(?=.*\d)(?=.*\W)(?=.*[^\d\s]).+$")) { -- 2.25.1 From 57aecf6cb68b7b8f705415f5d9a0b2fed314e615 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Fri, 17 Mar 2023 19:15:58 +0400 Subject: [PATCH 14/26] =?UTF-8?q?=D0=9F=D0=B0=D0=B3=D0=B8=D0=BD=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D1=8F=20=D0=B4=D0=BB=D1=8F=20desktop?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MessageInfoLogic.cs | 18 ++++++- .../MessageInfoStorage.cs | 11 ++++ .../MessageInfoStorage.cs | 14 ++++++ Confectionery/FormViewMail.Designer.cs | 47 +++++++++++++++-- Confectionery/FormViewMail.cs | 50 ++++++++++++++++++- .../IMessageInfoLogic.cs | 2 + .../StoragesContract/IMessageInfoStorage.cs | 2 + .../MessageInfoStorage.cs | 15 ++++++ 8 files changed, 152 insertions(+), 7 deletions(-) diff --git a/ConfectionaryBusinessLogic/MessageInfoLogic.cs b/ConfectionaryBusinessLogic/MessageInfoLogic.cs index 013ffe0..0e6cf2f 100644 --- a/ConfectionaryBusinessLogic/MessageInfoLogic.cs +++ b/ConfectionaryBusinessLogic/MessageInfoLogic.cs @@ -3,6 +3,7 @@ using ConfectioneryContracts.BusinessLogicsContracts; using ConfectioneryContracts.SearchModels; using ConfectioneryContracts.StoragesContract; using ConfectioneryContracts.ViewModels; +using DocumentFormat.OpenXml.EMMA; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; @@ -16,10 +17,10 @@ namespace ConfectioneryBusinessLogic { private readonly ILogger _logger; private readonly IMessageInfoStorage _messageInfoStorage; - public MessageInfoLogic(ILogger logger, IMessageInfoStorage MessageInfoStorage) + public MessageInfoLogic(ILogger logger, IMessageInfoStorage messageInfoStorage) { _logger = logger; - _messageInfoStorage = MessageInfoStorage; + _messageInfoStorage = messageInfoStorage; } public bool Create(MessageInfoBindingModel model) @@ -44,5 +45,18 @@ namespace ConfectioneryBusinessLogic _logger.LogInformation("ReadList. Count:{Count}", list.Count); return list; } + + public List? ReadPage(int page, int pageSize) + { + _logger.LogInformation("ReadPage. page: {page}, pageSize: {pageSize} ", page, pageSize); + var list = _messageInfoStorage.GetListOnPage(page, pageSize); + if (list == null) + { + _logger.LogWarning("ReadPage. Uncorrect range messages for page"); + return null; + } + _logger.LogInformation("ReadPage. Count:{Count}", list.Count); + return list; + } } } diff --git a/ConfectionaryFileImplement/MessageInfoStorage.cs b/ConfectionaryFileImplement/MessageInfoStorage.cs index adbe169..ebbe603 100644 --- a/ConfectionaryFileImplement/MessageInfoStorage.cs +++ b/ConfectionaryFileImplement/MessageInfoStorage.cs @@ -24,6 +24,17 @@ namespace ConfectioneryFileImplement return null; } + public List? GetListOnPage(int page, int pageSize) + { + if (page * pageSize >= _source.Messages.Count) + { + return null; + } + return _source.Messages.Skip((page - 1) * pageSize).Take(pageSize) + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(MessageInfoSearchModel model) { return _source.Messages diff --git a/ConfectionaryListImplement/MessageInfoStorage.cs b/ConfectionaryListImplement/MessageInfoStorage.cs index b0834c1..798bf2d 100644 --- a/ConfectionaryListImplement/MessageInfoStorage.cs +++ b/ConfectionaryListImplement/MessageInfoStorage.cs @@ -24,6 +24,20 @@ namespace ConfectioneryListImplement return null; } + public List? GetListOnPage(int page, int pageSize) + { + if (page * pageSize >= _source.Messages.Count) + { + return null; + } + List result = new(); + for (var i = (page - 1) * pageSize; i < page * pageSize; i++) + { + result.Add(_source.Messages[i].GetViewModel); + } + return result; + } + public List GetFilteredList(MessageInfoSearchModel model) { List result = new(); diff --git a/Confectionery/FormViewMail.Designer.cs b/Confectionery/FormViewMail.Designer.cs index 021a64d..22af24b 100644 --- a/Confectionery/FormViewMail.Designer.cs +++ b/Confectionery/FormViewMail.Designer.cs @@ -29,24 +29,62 @@ private void InitializeComponent() { dataGridView = new DataGridView(); + buttonPrevPage = new Button(); + buttonNextPage = new Button(); + labelInfoPages = new Label(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); SuspendLayout(); // // dataGridView // + dataGridView.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right; dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; - dataGridView.Dock = DockStyle.Fill; dataGridView.Location = new Point(0, 0); dataGridView.Name = "dataGridView"; dataGridView.RowTemplate.Height = 25; - dataGridView.Size = new Size(803, 450); + dataGridView.Size = new Size(730, 454); dataGridView.TabIndex = 0; // + // buttonPrevPage + // + buttonPrevPage.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonPrevPage.Location = new Point(12, 460); + buttonPrevPage.Name = "buttonPrevPage"; + buttonPrevPage.Size = new Size(75, 23); + buttonPrevPage.TabIndex = 1; + buttonPrevPage.Text = "<<<"; + buttonPrevPage.UseVisualStyleBackColor = true; + buttonPrevPage.Click += ButtonPrevPage_Click; + // + // buttonNextPage + // + buttonNextPage.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + buttonNextPage.Location = new Point(203, 460); + buttonNextPage.Name = "buttonNextPage"; + buttonNextPage.Size = new Size(75, 23); + buttonNextPage.TabIndex = 2; + buttonNextPage.Text = ">>>"; + buttonNextPage.UseVisualStyleBackColor = true; + buttonNextPage.Click += ButtonNextPage_Click; + // + // labelInfoPages + // + labelInfoPages.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + labelInfoPages.Location = new Point(93, 464); + labelInfoPages.Name = "labelInfoPages"; + labelInfoPages.Size = new Size(104, 19); + labelInfoPages.TabIndex = 3; + labelInfoPages.Text = "{0} страница"; + labelInfoPages.TextAlign = ContentAlignment.MiddleCenter; + // // FormViewMail // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(800, 450); + ClientSize = new Size(727, 498); + Controls.Add(labelInfoPages); + Controls.Add(buttonNextPage); + Controls.Add(buttonPrevPage); Controls.Add(dataGridView); Name = "FormViewMail"; Text = "Письма"; @@ -58,5 +96,8 @@ #endregion private DataGridView dataGridView; + private Button buttonPrevPage; + private Button buttonNextPage; + private Label labelInfoPages; } } \ No newline at end of file diff --git a/Confectionery/FormViewMail.cs b/Confectionery/FormViewMail.cs index 619bac5..cfa4eef 100644 --- a/Confectionery/FormViewMail.cs +++ b/Confectionery/FormViewMail.cs @@ -10,6 +10,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; +using ConfectioneryContracts.ViewModels; namespace ConfectioneryView { @@ -17,19 +18,27 @@ namespace ConfectioneryView { private readonly ILogger _logger; private readonly IMessageInfoLogic _logic; + private int currentPage = 1; + public int pageSize = 5; public FormViewMail(ILogger logger, IMessageInfoLogic logic) { InitializeComponent(); _logger = logger; _logic = logic; + buttonPrevPage.Enabled = false; } private void FormViewMail_Load(object sender, EventArgs e) + { + MailLoad(); + } + + private bool MailLoad() { try { - var list = _logic.ReadList(null); + var list = _logic.ReadPage(currentPage, pageSize); if (list != null) { dataGridView.DataSource = list; @@ -38,12 +47,49 @@ namespace ConfectioneryView dataGridView.Columns["Body"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; } _logger.LogInformation("Загрузка списка писем"); + labelInfoPages.Text = $"{currentPage} страница"; + return true; } catch (Exception ex) { _logger.LogError(ex, "Ошибка загрузки писем"); MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, - MessageBoxIcon.Error); + MessageBoxIcon.Error); + return false; + } + } + + private void ButtonPrevPage_Click(object sender, EventArgs e) + { + if (currentPage == 1) + { + _logger.LogWarning("Неккоректный номер страницы {page}", currentPage - 1); + return; + } + currentPage--; + if (MailLoad()) + { + buttonNextPage.Enabled = true; + if (currentPage == 1) + { + buttonPrevPage.Enabled = false; + } + } + } + + private void ButtonNextPage_Click(object sender, EventArgs e) + { + currentPage++; + if (!MailLoad() || ((List)dataGridView.DataSource).Count == 0) + { + _logger.LogWarning("Out of range messages"); + currentPage--; + MailLoad(); + buttonNextPage.Enabled = false; + } + else + { + buttonPrevPage.Enabled = true; } } } diff --git a/ConfectioneryContracts/BusinessLogicsContracts/IMessageInfoLogic.cs b/ConfectioneryContracts/BusinessLogicsContracts/IMessageInfoLogic.cs index ed4b54c..64280a6 100644 --- a/ConfectioneryContracts/BusinessLogicsContracts/IMessageInfoLogic.cs +++ b/ConfectioneryContracts/BusinessLogicsContracts/IMessageInfoLogic.cs @@ -14,5 +14,7 @@ namespace ConfectioneryContracts.BusinessLogicsContracts List? ReadList(MessageInfoSearchModel? model); bool Create(MessageInfoBindingModel model); + + public List? ReadPage(int page, int pageSize); } } diff --git a/ConfectioneryContracts/StoragesContract/IMessageInfoStorage.cs b/ConfectioneryContracts/StoragesContract/IMessageInfoStorage.cs index 6bb7375..7b3ef6c 100644 --- a/ConfectioneryContracts/StoragesContract/IMessageInfoStorage.cs +++ b/ConfectioneryContracts/StoragesContract/IMessageInfoStorage.cs @@ -13,6 +13,8 @@ namespace ConfectioneryContracts.StoragesContract { List GetFullList(); + List? GetListOnPage(int page, int pageSize); + List GetFilteredList(MessageInfoSearchModel model); MessageInfoViewModel? GetElement(MessageInfoSearchModel model); diff --git a/ConfectioneryDatabaseImplement/MessageInfoStorage.cs b/ConfectioneryDatabaseImplement/MessageInfoStorage.cs index 0e5a4bf..5ac50f7 100644 --- a/ConfectioneryDatabaseImplement/MessageInfoStorage.cs +++ b/ConfectioneryDatabaseImplement/MessageInfoStorage.cs @@ -19,6 +19,21 @@ namespace ConfectioneryDatabaseImplement return null; } + public List? GetListOnPage(int page, int pageSize) + { + using var context = new ConfectioneryDatabase(); + try + { + return context.Messages.Skip((page - 1) * pageSize).Take(pageSize) + .Select(x => x.GetViewModel) + .ToList(); + } + catch (Exception) + { + return null; + } + } + public List GetFilteredList(MessageInfoSearchModel model) { using var context = new ConfectioneryDatabase(); -- 2.25.1 From 72a1f2e75dd25411f359d8c3ff3cda24d8070d1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Fri, 17 Mar 2023 19:49:06 +0400 Subject: [PATCH 15/26] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D1=81=D0=BC?= =?UTF-8?q?=D0=BE=D1=82=D1=80=D0=B5=D0=BD=D0=B0=20=D0=BC=D0=B5=D1=81=D1=82?= =?UTF-8?q?=D0=BE=20=D0=B2=D1=8B=D0=BF=D0=BE=D0=BB=D0=BD=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D0=BF=D0=B0=D0=B3=D0=B8=D0=BD=D0=B0=D1=86=D0=B8=D0=B8?= =?UTF-8?q?=20=D0=B2=20=D0=BF=D1=80=D0=BE=D0=B5=D0=BA=D1=82=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MessageInfoLogic.cs | 13 -------- .../MessageInfoStorage.cs | 24 ++++++-------- .../MessageInfoStorage.cs | 31 ++++++++++--------- Confectionery/FormViewMail.cs | 6 +++- .../IMessageInfoLogic.cs | 2 -- .../SearchModels/MessageInfoSearchModel.cs | 4 +++ .../StoragesContract/IMessageInfoStorage.cs | 2 -- .../MessageInfoStorage.cs | 27 +++++----------- 8 files changed, 42 insertions(+), 67 deletions(-) diff --git a/ConfectionaryBusinessLogic/MessageInfoLogic.cs b/ConfectionaryBusinessLogic/MessageInfoLogic.cs index 0e6cf2f..99b9bb6 100644 --- a/ConfectionaryBusinessLogic/MessageInfoLogic.cs +++ b/ConfectionaryBusinessLogic/MessageInfoLogic.cs @@ -45,18 +45,5 @@ namespace ConfectioneryBusinessLogic _logger.LogInformation("ReadList. Count:{Count}", list.Count); return list; } - - public List? ReadPage(int page, int pageSize) - { - _logger.LogInformation("ReadPage. page: {page}, pageSize: {pageSize} ", page, pageSize); - var list = _messageInfoStorage.GetListOnPage(page, pageSize); - if (list == null) - { - _logger.LogWarning("ReadPage. Uncorrect range messages for page"); - return null; - } - _logger.LogInformation("ReadPage. Count:{Count}", list.Count); - return list; - } } } diff --git a/ConfectionaryFileImplement/MessageInfoStorage.cs b/ConfectionaryFileImplement/MessageInfoStorage.cs index ebbe603..75505d2 100644 --- a/ConfectionaryFileImplement/MessageInfoStorage.cs +++ b/ConfectionaryFileImplement/MessageInfoStorage.cs @@ -24,23 +24,17 @@ namespace ConfectioneryFileImplement return null; } - public List? GetListOnPage(int page, int pageSize) - { - if (page * pageSize >= _source.Messages.Count) - { - return null; - } - return _source.Messages.Skip((page - 1) * pageSize).Take(pageSize) - .Select(x => x.GetViewModel) - .ToList(); - } - public List GetFilteredList(MessageInfoSearchModel model) { - return _source.Messages - .Where(x => x.ClientId == model.ClientId) - .Select(x => x.GetViewModel) - .ToList(); + var res = _source.Messages + .Where(x => !model.ClientId.HasValue || x.ClientId == model.ClientId) + .Select(x => x.GetViewModel); + if (!(model.Page.HasValue && model.PageSize.HasValue)) + { + return res.ToList(); + } + return res.Skip((model.Page.Value - 1) * model.PageSize.Value).Take(model.PageSize.Value).ToList(); + } public List GetFullList() diff --git a/ConfectionaryListImplement/MessageInfoStorage.cs b/ConfectionaryListImplement/MessageInfoStorage.cs index 798bf2d..249ea29 100644 --- a/ConfectionaryListImplement/MessageInfoStorage.cs +++ b/ConfectionaryListImplement/MessageInfoStorage.cs @@ -3,6 +3,7 @@ using ConfectioneryContracts.SearchModels; using ConfectioneryContracts.StoragesContract; using ConfectioneryContracts.ViewModels; using ConfectioneryListImplement.Models; +using System.Collections.Generic; namespace ConfectioneryListImplement { @@ -24,20 +25,6 @@ namespace ConfectioneryListImplement return null; } - public List? GetListOnPage(int page, int pageSize) - { - if (page * pageSize >= _source.Messages.Count) - { - return null; - } - List result = new(); - for (var i = (page - 1) * pageSize; i < page * pageSize; i++) - { - result.Add(_source.Messages[i].GetViewModel); - } - return result; - } - public List GetFilteredList(MessageInfoSearchModel model) { List result = new(); @@ -48,7 +35,21 @@ namespace ConfectioneryListImplement result.Add(item.GetViewModel); } } - return result; + + if (!(model.Page.HasValue && model.PageSize.HasValue)) + { + return result; + } + if (model.Page * model.PageSize >= result.Count) + { + return null; + } + List filteredResult = new(); + for (var i = (model.Page.Value - 1) * model.PageSize.Value; i < model.Page.Value * model.PageSize.Value; i++) + { + filteredResult.Add(result[i]); + } + return filteredResult; } public List GetFullList() diff --git a/Confectionery/FormViewMail.cs b/Confectionery/FormViewMail.cs index cfa4eef..004b1b3 100644 --- a/Confectionery/FormViewMail.cs +++ b/Confectionery/FormViewMail.cs @@ -38,7 +38,11 @@ namespace ConfectioneryView { try { - var list = _logic.ReadPage(currentPage, pageSize); + var list = _logic.ReadList(new() + { + Page = currentPage, + PageSize = pageSize, + }); if (list != null) { dataGridView.DataSource = list; diff --git a/ConfectioneryContracts/BusinessLogicsContracts/IMessageInfoLogic.cs b/ConfectioneryContracts/BusinessLogicsContracts/IMessageInfoLogic.cs index 64280a6..ed4b54c 100644 --- a/ConfectioneryContracts/BusinessLogicsContracts/IMessageInfoLogic.cs +++ b/ConfectioneryContracts/BusinessLogicsContracts/IMessageInfoLogic.cs @@ -14,7 +14,5 @@ namespace ConfectioneryContracts.BusinessLogicsContracts List? ReadList(MessageInfoSearchModel? model); bool Create(MessageInfoBindingModel model); - - public List? ReadPage(int page, int pageSize); } } diff --git a/ConfectioneryContracts/SearchModels/MessageInfoSearchModel.cs b/ConfectioneryContracts/SearchModels/MessageInfoSearchModel.cs index e344281..ca96e1d 100644 --- a/ConfectioneryContracts/SearchModels/MessageInfoSearchModel.cs +++ b/ConfectioneryContracts/SearchModels/MessageInfoSearchModel.cs @@ -11,5 +11,9 @@ namespace ConfectioneryContracts.SearchModels public int? ClientId { get; set; } public string? MessageId { get; set; } + + public int? Page { get; set; } + + public int? PageSize { get; set; } } } diff --git a/ConfectioneryContracts/StoragesContract/IMessageInfoStorage.cs b/ConfectioneryContracts/StoragesContract/IMessageInfoStorage.cs index 7b3ef6c..6bb7375 100644 --- a/ConfectioneryContracts/StoragesContract/IMessageInfoStorage.cs +++ b/ConfectioneryContracts/StoragesContract/IMessageInfoStorage.cs @@ -13,8 +13,6 @@ namespace ConfectioneryContracts.StoragesContract { List GetFullList(); - List? GetListOnPage(int page, int pageSize); - List GetFilteredList(MessageInfoSearchModel model); MessageInfoViewModel? GetElement(MessageInfoSearchModel model); diff --git a/ConfectioneryDatabaseImplement/MessageInfoStorage.cs b/ConfectioneryDatabaseImplement/MessageInfoStorage.cs index 5ac50f7..d60e1c5 100644 --- a/ConfectioneryDatabaseImplement/MessageInfoStorage.cs +++ b/ConfectioneryDatabaseImplement/MessageInfoStorage.cs @@ -19,28 +19,17 @@ namespace ConfectioneryDatabaseImplement return null; } - public List? GetListOnPage(int page, int pageSize) - { - using var context = new ConfectioneryDatabase(); - try - { - return context.Messages.Skip((page - 1) * pageSize).Take(pageSize) - .Select(x => x.GetViewModel) - .ToList(); - } - catch (Exception) - { - return null; - } - } - public List GetFilteredList(MessageInfoSearchModel model) { using var context = new ConfectioneryDatabase(); - return context.Messages - .Where(x => x.ClientId == model.ClientId) - .Select(x => x.GetViewModel) - .ToList(); + var res = context.Messages + .Where(x => !model.ClientId.HasValue || x.ClientId == model.ClientId) + .Select(x => x.GetViewModel); + if (!(model.Page.HasValue && model.PageSize.HasValue)) + { + return res.ToList(); + } + return res.Skip((model.Page.Value - 1) * model.PageSize.Value).Take(model.PageSize.Value).ToList(); } public List GetFullList() -- 2.25.1 From fd6e638e70b361b41733640ee59b30c4eca76990 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Fri, 17 Mar 2023 20:32:41 +0400 Subject: [PATCH 16/26] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=20=D0=BD=D0=B5=D0=B8=D1=81=D0=BF=D1=80=D0=B0?= =?UTF-8?q?=D0=B2=D0=BB=D0=B5=D0=BD=D0=BD=D1=8B=D0=B9=20=D0=BA=D0=BE=D0=BD?= =?UTF-8?q?=D1=84=D0=BB=D0=B8=D0=BA=D1=82=20=D1=81=20=D0=B1=D0=B0=D0=B7?= =?UTF-8?q?=D0=BE=D0=B2=D0=BE=D0=B9=20=D1=87=D0=B0=D1=81=D1=82=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConfectionaryBusinessLogic/OrderLogic.cs | 33 +++++++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/ConfectionaryBusinessLogic/OrderLogic.cs b/ConfectionaryBusinessLogic/OrderLogic.cs index 2548654..2a8f463 100644 --- a/ConfectionaryBusinessLogic/OrderLogic.cs +++ b/ConfectionaryBusinessLogic/OrderLogic.cs @@ -1,4 +1,5 @@ -using ConfectioneryContracts.BindingModels; +using ConfectioneryBusinessLogic.MailWorker; +using ConfectioneryContracts.BindingModels; using ConfectioneryContracts.BusinessLogicsContracts; using ConfectioneryContracts.SearchModels; using ConfectioneryContracts.StoragesContract; @@ -14,13 +15,17 @@ namespace ConfectioneryBusinessLogic.BusinessLogics private readonly IOrderStorage _orderStorage; private readonly IPastryStorage _pastryStorage; private readonly IShopLogic _shopLogic; + private readonly AbstractMailWorker _mailWorker; + private readonly IClientLogic _clientLogic; - public OrderLogic(ILogger logger, IOrderStorage orderStorage, IPastryStorage pastryStorage, IShopLogic shopLogic) + public OrderLogic(ILogger logger, IOrderStorage orderStorage, IPastryStorage pastryStorage, IShopLogic shopLogic, IClientLogic clientLogic, AbstractMailWorker mailWorker) { _logger = logger; _shopLogic = shopLogic; _pastryStorage = pastryStorage; _orderStorage = orderStorage; + _mailWorker = mailWorker; + _clientLogic = clientLogic; } public bool CreateOrder(OrderBindingModel model) @@ -33,11 +38,13 @@ namespace ConfectioneryBusinessLogic.BusinessLogics } model.Status = OrderStatus.Принят; model.DateCreate = DateTime.Now; - if (_orderStorage.Insert(model) == null) + var result = _orderStorage.Insert(model); + if (result == null) { _logger.LogWarning("Insert operation failed"); return false; } + SendOrderStatusMail(result.ClientId, $"Новый заказ создан. Номер заказа #{result.Id}", $"Заказ #{result.Id} от {result.DateCreate} на сумму {result.Sum:0.00} принят"); return true; } @@ -122,11 +129,13 @@ namespace ConfectioneryBusinessLogic.BusinessLogics model.PastryId = vmodel.PastryId; model.Sum = vmodel.Sum; model.Count= vmodel.Count; - if (_orderStorage.Update(model) == null) + var result = _orderStorage.Update(model); + if (result == null) { _logger.LogWarning("Update operation failed"); return false; } + SendOrderStatusMail(result.ClientId, $"Изменен статус заказа #{result.Id}", $"Заказ #{model.Id} изменен статус на {result.Status}"); return true; } @@ -146,5 +155,21 @@ namespace ConfectioneryBusinessLogic.BusinessLogics _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); return element; } + + private bool SendOrderStatusMail(int clientId, string subject, string text) + { + var client = _clientLogic.ReadElement(new() { Id = clientId }); + if (client == null) + { + return false; + } + _mailWorker.MailSendAsync(new() + { + MailAddress = client.Email, + Subject = subject, + Text = text + }); + return true; + } } } -- 2.25.1 From 85bef73abac407f83964211e681e673506d2232f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Fri, 17 Mar 2023 21:43:59 +0400 Subject: [PATCH 17/26] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D0=BD=D0=B0=20=D0=BF=D0=B0=D0=B3=D0=B8=D0=BD?= =?UTF-8?q?=D0=B0=D1=86=D0=B8=D1=8F=20=D1=81=D0=B0=D0=B9=D1=82=D0=B0=20?= =?UTF-8?q?=D1=87=D0=B5=D1=80=D0=B5=D0=B7=20=D0=B5=D0=B3=D0=BE=20=D0=BF?= =?UTF-8?q?=D0=B5=D1=80=D0=B5=D0=B7=D0=B0=D0=B3=D1=80=D1=83=D0=B7=D0=BA?= =?UTF-8?q?=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConfectioneryClientApp/APIClient.cs | 1 + .../Controllers/HomeController.cs | 22 +++- .../Views/Home/Mails.cshtml | 120 +++++++++++------- .../Controllers/ClientController.cs | 9 +- 4 files changed, 104 insertions(+), 48 deletions(-) diff --git a/ConfectioneryClientApp/APIClient.cs b/ConfectioneryClientApp/APIClient.cs index e4620e6..2ca7837 100644 --- a/ConfectioneryClientApp/APIClient.cs +++ b/ConfectioneryClientApp/APIClient.cs @@ -10,6 +10,7 @@ namespace ConfectioneryClientApp private static readonly HttpClient _client = new(); public static ClientViewModel? Client { get; set; } = null; + public static int CurrentPage { get; set; } = 1; public static void Connect(IConfiguration configuration) { diff --git a/ConfectioneryClientApp/Controllers/HomeController.cs b/ConfectioneryClientApp/Controllers/HomeController.cs index c48230a..8e44e22 100644 --- a/ConfectioneryClientApp/Controllers/HomeController.cs +++ b/ConfectioneryClientApp/Controllers/HomeController.cs @@ -4,6 +4,7 @@ using ConfectioneryContracts.BindingModels; using ConfectioneryContracts.ViewModels; using Microsoft.AspNetCore.Mvc; using System.Diagnostics; +using ConfectioneryContracts.SearchModels; namespace ConfectioneryClientApp.Controllers { @@ -152,7 +153,26 @@ namespace ConfectioneryClientApp.Controllers { return Redirect("~/Home/Enter"); } - return View(APIClient.GetRequest>($"api/client/getmessages?clientId={APIClient.Client.Id}")); + ViewBag.CurrentPage = APIClient.CurrentPage; + return View(APIClient.GetRequest>($"api/client/getmessages?clientId={APIClient.Client.Id}&page={APIClient.CurrentPage}")); } + + [HttpGet] + public void SwitchPage(bool isNext) + { + if (isNext) + { + APIClient.CurrentPage++; + } + else + { + if (APIClient.CurrentPage == 1) + { + return; + } + APIClient.CurrentPage--; + } + Mails(); + } } } \ No newline at end of file diff --git a/ConfectioneryClientApp/Views/Home/Mails.cshtml b/ConfectioneryClientApp/Views/Home/Mails.cshtml index e881061..64df4b7 100644 --- a/ConfectioneryClientApp/Views/Home/Mails.cshtml +++ b/ConfectioneryClientApp/Views/Home/Mails.cshtml @@ -5,50 +5,82 @@ @{ ViewData["Title"] = "Mails"; } +
+ @{ + if (Model == null) + { +

Авторизируйтесь

+ return; + } + + + + + + + + + + + @foreach (var item in Model) + { + + + + + + } + +
+ Дата письма + + Заголовок + + Текст +
+ @Html.DisplayFor(modelItem => item.DateDelivery) + + @Html.DisplayFor(modelItem => item.Subject) + + @Html.DisplayFor(modelItem => item.Body) +
+ + } +
+ +

Заказы

-
- - -
- @{ - if (Model == null) - { -

Авторизируйтесь

- return; - } - - - - - - - - - - - @foreach (var item in Model) - { - - - - - - } - -
- Дата письма - - Заголовок - - Текст -
- @Html.DisplayFor(modelItem => item.DateDelivery) - - @Html.DisplayFor(modelItem => item.Subject) - - @Html.DisplayFor(modelItem => item.Body) -
- } -
+ \ No newline at end of file diff --git a/ConfectioneryRestApi/Controllers/ClientController.cs b/ConfectioneryRestApi/Controllers/ClientController.cs index 70ab65a..1d49bdb 100644 --- a/ConfectioneryRestApi/Controllers/ClientController.cs +++ b/ConfectioneryRestApi/Controllers/ClientController.cs @@ -14,6 +14,7 @@ namespace ConfectioneryRestApi.Controllers private readonly IClientLogic _logic; private readonly IMessageInfoLogic _mailLogic; + public int pageSize = 3; public ClientController(IClientLogic logic, IMessageInfoLogic mailLogic, ILogger logger) { @@ -70,13 +71,15 @@ namespace ConfectioneryRestApi.Controllers } [HttpGet] - public List? GetMessages(int clientId) + public List? GetMessages(int clientId, int page) { try { - return _mailLogic.ReadList(new MessageInfoSearchModel + return _mailLogic.ReadList(new() { - ClientId = clientId + ClientId = clientId, + Page = page, + PageSize = pageSize }); } catch (Exception ex) -- 2.25.1 From 5c58ac9317862005ac9bc5a033232a4064ae91ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Sat, 18 Mar 2023 00:02:03 +0400 Subject: [PATCH 18/26] =?UTF-8?q?=D0=B4=D0=BE=D0=B2=D0=B5=D0=B4=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=B4=D0=BE=20=D1=83=D0=BC=D0=B0=20=D0=BF=D0=B0?= =?UTF-8?q?=D0=B3=D0=B8=D0=BD=D0=B0=D1=86=D0=B8=D1=8F=20=D0=BD=D0=B0=20?= =?UTF-8?q?=D1=81=D0=B0=D0=B9=D1=82=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConfectioneryClientApp/APIClient.cs | 2 +- .../Controllers/HomeController.cs | 32 ++++++++-- .../Views/Home/Mails.cshtml | 60 ++++++++----------- 3 files changed, 52 insertions(+), 42 deletions(-) diff --git a/ConfectioneryClientApp/APIClient.cs b/ConfectioneryClientApp/APIClient.cs index 2ca7837..ed25668 100644 --- a/ConfectioneryClientApp/APIClient.cs +++ b/ConfectioneryClientApp/APIClient.cs @@ -10,7 +10,7 @@ namespace ConfectioneryClientApp private static readonly HttpClient _client = new(); public static ClientViewModel? Client { get; set; } = null; - public static int CurrentPage { get; set; } = 1; + public static int CurrentPage { get; set; } = 0; public static void Connect(IConfiguration configuration) { diff --git a/ConfectioneryClientApp/Controllers/HomeController.cs b/ConfectioneryClientApp/Controllers/HomeController.cs index 8e44e22..ab48d93 100644 --- a/ConfectioneryClientApp/Controllers/HomeController.cs +++ b/ConfectioneryClientApp/Controllers/HomeController.cs @@ -4,7 +4,9 @@ using ConfectioneryContracts.BindingModels; using ConfectioneryContracts.ViewModels; using Microsoft.AspNetCore.Mvc; using System.Diagnostics; +using System.Text; using ConfectioneryContracts.SearchModels; +using Microsoft.AspNetCore.Mvc.Rendering; namespace ConfectioneryClientApp.Controllers { @@ -153,12 +155,15 @@ namespace ConfectioneryClientApp.Controllers { return Redirect("~/Home/Enter"); } - ViewBag.CurrentPage = APIClient.CurrentPage; - return View(APIClient.GetRequest>($"api/client/getmessages?clientId={APIClient.Client.Id}&page={APIClient.CurrentPage}")); + return View(); } + /// + /// Switches the page. + /// + /// Возвращает кортеж с таблицой в html, текущей страницей писем, выключать ли кнопку пред. страницы, выключать ли кнопку след. страницы [HttpGet] - public void SwitchPage(bool isNext) + public Tuple? SwitchPage(bool isNext) { if (isNext) { @@ -168,11 +173,28 @@ namespace ConfectioneryClientApp.Controllers { if (APIClient.CurrentPage == 1) { - return; + return null; } APIClient.CurrentPage--; } - Mails(); + + var res = APIClient.GetRequest>($"api/client/getmessages?clientId={APIClient.Client!.Id}&page={APIClient.CurrentPage}"); + if (isNext && (res == null || res.Count == 0)) + { + APIClient.CurrentPage--; + return Tuple.Create(null, null, APIClient.CurrentPage != 1, false); + } + + StringBuilder htmlTable = new(); + foreach (var mail in res) + { + htmlTable.Append("" + + $"{mail.DateDelivery}" + + $"{mail.Subject}" + + $"{mail.Body}" + + ""); + } + return Tuple.Create(htmlTable.ToString(), APIClient.CurrentPage.ToString(), APIClient.CurrentPage != 1, true); } } } \ No newline at end of file diff --git a/ConfectioneryClientApp/Views/Home/Mails.cshtml b/ConfectioneryClientApp/Views/Home/Mails.cshtml index 64df4b7..1926238 100644 --- a/ConfectioneryClientApp/Views/Home/Mails.cshtml +++ b/ConfectioneryClientApp/Views/Home/Mails.cshtml @@ -1,19 +1,8 @@ -@using ConfectioneryContracts.ViewModels - -@model List - -@{ +@{ ViewData["Title"] = "Mails"; }
- @{ - if (Model == null) - { -

Авторизируйтесь

- return; - } - - +
- - @foreach (var item in Model) - { - - - - - - } +
@@ -27,21 +16,7 @@
- @Html.DisplayFor(modelItem => item.DateDelivery) - - @Html.DisplayFor(modelItem => item.Subject) - - @Html.DisplayFor(modelItem => item.Body) -
- }
- -
-

Заказы

-
\ No newline at end of file + \ No newline at end of file -- 2.25.1