From 6c04c689c8d9c08dba0b60210c6e53c6792362f1 Mon Sep 17 00:00:00 2001 From: Stepan Date: Thu, 16 May 2024 23:32:44 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=207?= =?UTF-8?q?=20=D0=BB=D0=B0=D0=B1=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MotorPlantBusinessLogic/MailKitWorker.cs | 2 + .../MotorPlantBusinessLogic.csproj | 1 + .../Controllers/HomeController.cs | 2 + ... 20240516113707_InitialCreate.Designer.cs} | 41 ++++++++++- ...ate.cs => 20240516113707_InitialCreate.cs} | 29 ++++++++ .../MotorPlantDataBaseModelSnapshot.cs | 39 ++++++++++ .../DataFileSingleton.cs | 4 ++ .../MotorPlantFileImplement/MessageInfo.cs | 71 +++++++++++++++++++ .../MessageInfoStorage.cs | 54 ++++++++++++++ .../DataListSingleton.cs | 2 + .../MotorPlantListImplement/MessageInfo.cs | 46 ++++++++++++ .../MessageInfoStorage.cs | 64 +++++++++++++++++ .../Controllers/ClientController.cs | 20 +++++- 13 files changed, 373 insertions(+), 2 deletions(-) rename MotorPlant/MotorPlantDatabaseImplement/Migrations/{20240502194530_InitialCreate.Designer.cs => 20240516113707_InitialCreate.Designer.cs} (86%) rename MotorPlant/MotorPlantDatabaseImplement/Migrations/{20240502194530_InitialCreate.cs => 20240516113707_InitialCreate.cs} (85%) create mode 100644 MotorPlant/MotorPlantFileImplement/MessageInfo.cs create mode 100644 MotorPlant/MotorPlantFileImplement/MessageInfoStorage.cs create mode 100644 MotorPlant/MotorPlantListImplement/MessageInfo.cs create mode 100644 MotorPlant/MotorPlantListImplement/MessageInfoStorage.cs diff --git a/MotorPlant/MotorPlantBusinessLogic/MailKitWorker.cs b/MotorPlant/MotorPlantBusinessLogic/MailKitWorker.cs index f26e21f..7e04f61 100644 --- a/MotorPlant/MotorPlantBusinessLogic/MailKitWorker.cs +++ b/MotorPlant/MotorPlantBusinessLogic/MailKitWorker.cs @@ -7,6 +7,8 @@ using System.Linq; using System.Net.Mail; using System.Net; using System.Text; +using MailKit.Net.Pop3; +using MailKit.Security; using System.Threading.Tasks; namespace MotorPlantBusinessLogic.MailWorker diff --git a/MotorPlant/MotorPlantBusinessLogic/MotorPlantBusinessLogic.csproj b/MotorPlant/MotorPlantBusinessLogic/MotorPlantBusinessLogic.csproj index 3351672..c48f692 100644 --- a/MotorPlant/MotorPlantBusinessLogic/MotorPlantBusinessLogic.csproj +++ b/MotorPlant/MotorPlantBusinessLogic/MotorPlantBusinessLogic.csproj @@ -21,6 +21,7 @@ + diff --git a/MotorPlant/MotorPlantClientApp/Controllers/HomeController.cs b/MotorPlant/MotorPlantClientApp/Controllers/HomeController.cs index 11ae743..7dc98df 100644 --- a/MotorPlant/MotorPlantClientApp/Controllers/HomeController.cs +++ b/MotorPlant/MotorPlantClientApp/Controllers/HomeController.cs @@ -114,6 +114,8 @@ namespace MotorPlantClientApp.Controllers public IActionResult Create() { var res = APIClient.GetRequest>("api/main/getEnginelist"); + ViewBag.Engines = + APIClient.GetRequest>("api/main/getEnginelist"); return View(); } [HttpPost] diff --git a/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240502194530_InitialCreate.Designer.cs b/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240516113707_InitialCreate.Designer.cs similarity index 86% rename from MotorPlant/MotorPlantDatabaseImplement/Migrations/20240502194530_InitialCreate.Designer.cs rename to MotorPlant/MotorPlantDatabaseImplement/Migrations/20240516113707_InitialCreate.Designer.cs index 443f280..48374ba 100644 --- a/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240502194530_InitialCreate.Designer.cs +++ b/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240516113707_InitialCreate.Designer.cs @@ -12,7 +12,7 @@ using MotorPlantDatabaseImplement; namespace MotorPlantDatabaseImplement.Migrations { [DbContext(typeof(MotorPlantDataBase))] - [Migration("20240502194530_InitialCreate")] + [Migration("20240516113707_InitialCreate")] partial class InitialCreate { /// @@ -143,6 +143,36 @@ namespace MotorPlantDatabaseImplement.Migrations b.ToTable("Implementers"); }); + modelBuilder.Entity("MotorPlantDatabaseImplement.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("MotorPlantDatabaseImplement.Models.Order", b => { b.Property("Id") @@ -205,6 +235,15 @@ namespace MotorPlantDatabaseImplement.Migrations b.Navigation("Engine"); }); + modelBuilder.Entity("MotorPlantDatabaseImplement.Models.MessageInfo", b => + { + b.HasOne("MotorPlantDatabaseImplement.Models.Client", "Client") + .WithMany() + .HasForeignKey("ClientId"); + + b.Navigation("Client"); + }); + modelBuilder.Entity("MotorPlantDatabaseImplement.Models.Order", b => { b.HasOne("MotorPlantDatabaseImplement.Models.Client", "Client") diff --git a/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240502194530_InitialCreate.cs b/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240516113707_InitialCreate.cs similarity index 85% rename from MotorPlant/MotorPlantDatabaseImplement/Migrations/20240502194530_InitialCreate.cs rename to MotorPlant/MotorPlantDatabaseImplement/Migrations/20240516113707_InitialCreate.cs index b390162..7dae505 100644 --- a/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240502194530_InitialCreate.cs +++ b/MotorPlant/MotorPlantDatabaseImplement/Migrations/20240516113707_InitialCreate.cs @@ -70,6 +70,27 @@ namespace MotorPlantDatabaseImplement.Migrations table.PrimaryKey("PK_Implementers", x => x.Id); }); + 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.CreateTable( name: "EngineComponents", columns: table => new @@ -144,6 +165,11 @@ namespace MotorPlantDatabaseImplement.Migrations table: "EngineComponents", column: "EngineId"); + migrationBuilder.CreateIndex( + name: "IX_Messages_ClientId", + table: "Messages", + column: "ClientId"); + migrationBuilder.CreateIndex( name: "IX_Orders_ClientId", table: "Orders", @@ -166,6 +192,9 @@ namespace MotorPlantDatabaseImplement.Migrations migrationBuilder.DropTable( name: "EngineComponents"); + migrationBuilder.DropTable( + name: "Messages"); + migrationBuilder.DropTable( name: "Orders"); diff --git a/MotorPlant/MotorPlantDatabaseImplement/Migrations/MotorPlantDataBaseModelSnapshot.cs b/MotorPlant/MotorPlantDatabaseImplement/Migrations/MotorPlantDataBaseModelSnapshot.cs index 4fb27a6..bc532e5 100644 --- a/MotorPlant/MotorPlantDatabaseImplement/Migrations/MotorPlantDataBaseModelSnapshot.cs +++ b/MotorPlant/MotorPlantDatabaseImplement/Migrations/MotorPlantDataBaseModelSnapshot.cs @@ -140,6 +140,36 @@ namespace MotorPlantDatabaseImplement.Migrations b.ToTable("Implementers"); }); + modelBuilder.Entity("MotorPlantDatabaseImplement.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("MotorPlantDatabaseImplement.Models.Order", b => { b.Property("Id") @@ -202,6 +232,15 @@ namespace MotorPlantDatabaseImplement.Migrations b.Navigation("Engine"); }); + modelBuilder.Entity("MotorPlantDatabaseImplement.Models.MessageInfo", b => + { + b.HasOne("MotorPlantDatabaseImplement.Models.Client", "Client") + .WithMany() + .HasForeignKey("ClientId"); + + b.Navigation("Client"); + }); + modelBuilder.Entity("MotorPlantDatabaseImplement.Models.Order", b => { b.HasOne("MotorPlantDatabaseImplement.Models.Client", "Client") diff --git a/MotorPlant/MotorPlantFileImplement/DataFileSingleton.cs b/MotorPlant/MotorPlantFileImplement/DataFileSingleton.cs index 73045f0..c611b54 100644 --- a/MotorPlant/MotorPlantFileImplement/DataFileSingleton.cs +++ b/MotorPlant/MotorPlantFileImplement/DataFileSingleton.cs @@ -12,11 +12,13 @@ namespace MotorPlantFileImplement private readonly string EngineFileName = "Engine.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 Engines { 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() { if (instance == null) @@ -32,6 +34,7 @@ namespace MotorPlantFileImplement "Clients", x => x.GetXElement); public void SaveImplementers() => SaveData(Implementers, ImplementerFileName, "Implementers", x => x.GetXElement); + public void SaveMessages() => SaveData(Orders, ImplementerFileName, "Messages", x => x.GetXElement); private DataFileSingleton() { Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; @@ -41,6 +44,7 @@ namespace MotorPlantFileImplement 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/MotorPlant/MotorPlantFileImplement/MessageInfo.cs b/MotorPlant/MotorPlantFileImplement/MessageInfo.cs new file mode 100644 index 0000000..fcb68a9 --- /dev/null +++ b/MotorPlant/MotorPlantFileImplement/MessageInfo.cs @@ -0,0 +1,71 @@ +using MotorPlantContracts.BindingModels; +using MotorPlantContracts.ViewModels; +using MotorPlantDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace MotorPlantFileImplement.Models +{ + 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/MotorPlant/MotorPlantFileImplement/MessageInfoStorage.cs b/MotorPlant/MotorPlantFileImplement/MessageInfoStorage.cs new file mode 100644 index 0000000..d62e0b7 --- /dev/null +++ b/MotorPlant/MotorPlantFileImplement/MessageInfoStorage.cs @@ -0,0 +1,54 @@ +using MotorPlantContracts.BindingModels; +using MotorPlantContracts.SearchModels; +using MotorPlantContracts.StoragesContracts; +using MotorPlantContracts.ViewModels; +using MotorPlantFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MotorPlantFileImplement.Implements +{ + 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; + } + } +} diff --git a/MotorPlant/MotorPlantListImplement/DataListSingleton.cs b/MotorPlant/MotorPlantListImplement/DataListSingleton.cs index be17286..ef21443 100644 --- a/MotorPlant/MotorPlantListImplement/DataListSingleton.cs +++ b/MotorPlant/MotorPlantListImplement/DataListSingleton.cs @@ -11,6 +11,7 @@ namespace MotorPlantListImplement public List Engines { get; set; } public List Clients { get; set; } public List Implementers { get; set; } + public List Messages { get; set; } private DataListSingleton() { Components = new List(); @@ -18,6 +19,7 @@ namespace MotorPlantListImplement Engines = new List(); Clients = new List(); Implementers = new List(); + Messages = new List(); } public static DataListSingleton GetInstance() { diff --git a/MotorPlant/MotorPlantListImplement/MessageInfo.cs b/MotorPlant/MotorPlantListImplement/MessageInfo.cs new file mode 100644 index 0000000..6e96b58 --- /dev/null +++ b/MotorPlant/MotorPlantListImplement/MessageInfo.cs @@ -0,0 +1,46 @@ +using MotorPlantContracts.BindingModels; +using MotorPlantContracts.ViewModels; +using MotorPlantDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MotorPlantListImplement.Models +{ + 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/MotorPlant/MotorPlantListImplement/MessageInfoStorage.cs b/MotorPlant/MotorPlantListImplement/MessageInfoStorage.cs new file mode 100644 index 0000000..5a4906f --- /dev/null +++ b/MotorPlant/MotorPlantListImplement/MessageInfoStorage.cs @@ -0,0 +1,64 @@ +using MotorPlantContracts.BindingModels; +using MotorPlantContracts.SearchModels; +using MotorPlantContracts.StoragesContracts; +using MotorPlantContracts.ViewModels; +using MotorPlantListImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MotorPlantListImplement.Implements +{ + public class MessageInfoStorage : IMessageInfoStorage + { + private readonly DataListSingleton _source; + public MessageInfoStorage() + { + _source = DataListSingleton.GetInstance(); + } + public List GetFullList() + { + List result = new(); + foreach (var item in _source.Messages) + { + result.Add(item.GetViewModel); + } + return result; + } + 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 MessageInfoViewModel? GetElement(MessageInfoSearchModel model) + { + foreach (var message in _source.Messages) + { + if (model.MessageId != null && model.MessageId.Equals(message.MessageId)) + { + return message.GetViewModel; + } + } + return null; + } + public MessageInfoViewModel? Insert(MessageInfoBindingModel model) + { + var newMessage = MessageInfo.Create(model); + if (newMessage == null) + { + return null; + } + _source.Messages.Add(newMessage); + return newMessage.GetViewModel; + } + } +} diff --git a/MotorPlant/MotorPlantRestApi/Controllers/ClientController.cs b/MotorPlant/MotorPlantRestApi/Controllers/ClientController.cs index a876f14..2a5888e 100644 --- a/MotorPlant/MotorPlantRestApi/Controllers/ClientController.cs +++ b/MotorPlant/MotorPlantRestApi/Controllers/ClientController.cs @@ -11,11 +11,13 @@ namespace MotorPlantRestApi.Controllers { private readonly ILogger _logger; private readonly IClientLogic _logic; - public ClientController(IClientLogic logic, ILogger + private readonly IMessageInfoLogic _mailLogic; + public ClientController(IClientLogic logic, IMessageInfoLogic mailLogic, ILogger logger) { _logger = logger; _logic = logic; + _mailLogic = mailLogic; } [HttpGet] public ClientViewModel? Login(string login, string password) @@ -34,6 +36,22 @@ namespace MotorPlantRestApi.Controllers throw; } } + [HttpGet] + public List? GetMessages(int clientId) + { + try + { + return _mailLogic.ReadList(new MessageInfoSearchModel + { + ClientId = clientId + }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения писем клиента"); + throw; + } + } [HttpPost] public void Register(ClientBindingModel model) {