From 505a708ddf6d7b9c177214af0f3468284c4c287c Mon Sep 17 00:00:00 2001 From: Oleg Shabunov Date: Fri, 19 Apr 2024 22:47:28 +0400 Subject: [PATCH] Messages DatabaseImplement --- .../BusinessLogics/MessageInfoLogic.cs | 36 ++++++++++++ .../BindingModels/MailConfigBindingModel.cs | 10 +--- .../BindingModels/MailSendInfoBindingModel.cs | 10 +--- .../SearchModels/MessageInfoSearchModel.cs | 2 - .../ViewModels/OrderViewModel.cs | 3 + .../AutoWorkshopDatabase.cs | 2 + .../Implements/MessageInfoStorage.cs | 56 +++++++++++++++++++ .../Models/Client.cs | 5 +- .../Models/MessageInfo.cs | 47 ++++++++++++++++ AutoWorkshopDatabaseImplement/Models/Order.cs | 1 + AutoWorkshopRestApi/Program.cs | 18 ++++++ AutoWorkshopRestApi/appsettings.json | 9 ++- AutoWorkshopView/App.config | 11 ++++ 13 files changed, 190 insertions(+), 20 deletions(-) create mode 100644 AutoWorkshopBusinessLogic/BusinessLogics/MessageInfoLogic.cs create mode 100644 AutoWorkshopDatabaseImplement/Implements/MessageInfoStorage.cs create mode 100644 AutoWorkshopDatabaseImplement/Models/MessageInfo.cs create mode 100644 AutoWorkshopView/App.config diff --git a/AutoWorkshopBusinessLogic/BusinessLogics/MessageInfoLogic.cs b/AutoWorkshopBusinessLogic/BusinessLogics/MessageInfoLogic.cs new file mode 100644 index 0000000..c19898a --- /dev/null +++ b/AutoWorkshopBusinessLogic/BusinessLogics/MessageInfoLogic.cs @@ -0,0 +1,36 @@ +using AutoWorkshopContracts.BindingModels; +using AutoWorkshopContracts.BusinessLogicContracts; +using AutoWorkshopContracts.SearchModels; +using AutoWorkshopContracts.StoragesContracts; +using AutoWorkshopContracts.ViewModels; +using Microsoft.Extensions.Logging; + +namespace AutoWorkshopBusinessLogic.BusinessLogics +{ + public class MessageInfoLogic : IMessageInfoLogic + { + private readonly ILogger _logger; + + private readonly IMessageInfoStorage _messageInfoStorage; + private readonly IClientStorage _clientStorage; + + public MessageInfoLogic(ILogger Logger, IMessageInfoStorage MessageInfoStorage, IClientStorage ClientStorage) + { + _logger = Logger; + _messageInfoStorage = MessageInfoStorage; + _clientStorage = ClientStorage; + } + + public List? ReadList(MessageInfoSearchModel? Model) + { + // TODO + return new(); + } + + public bool Create(MessageInfoBindingModel Model) + { + // TODO + return false; + } + } +} diff --git a/AutoWorkshopContracts/BindingModels/MailConfigBindingModel.cs b/AutoWorkshopContracts/BindingModels/MailConfigBindingModel.cs index e5b83ac..d013148 100644 --- a/AutoWorkshopContracts/BindingModels/MailConfigBindingModel.cs +++ b/AutoWorkshopContracts/BindingModels/MailConfigBindingModel.cs @@ -1,12 +1,6 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace AutoWorkshopContracts.BindingModels +namespace AutoWorkshopContracts.BindingModels { - public class MailConfigBindingModel + public class MailConfigBindingModel { public string MailLogin { get; set; } = string.Empty; diff --git a/AutoWorkshopContracts/BindingModels/MailSendInfoBindingModel.cs b/AutoWorkshopContracts/BindingModels/MailSendInfoBindingModel.cs index f392764..c2deed9 100644 --- a/AutoWorkshopContracts/BindingModels/MailSendInfoBindingModel.cs +++ b/AutoWorkshopContracts/BindingModels/MailSendInfoBindingModel.cs @@ -1,12 +1,6 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace AutoWorkshopContracts.BindingModels +namespace AutoWorkshopContracts.BindingModels { - public class MailSendInfoBindingModel + public class MailSendInfoBindingModel { public string MailAddress { get; set; } = string.Empty; diff --git a/AutoWorkshopContracts/SearchModels/MessageInfoSearchModel.cs b/AutoWorkshopContracts/SearchModels/MessageInfoSearchModel.cs index 86f770f..de273ae 100644 --- a/AutoWorkshopContracts/SearchModels/MessageInfoSearchModel.cs +++ b/AutoWorkshopContracts/SearchModels/MessageInfoSearchModel.cs @@ -5,7 +5,5 @@ public string? MessageId { get; set; } public int? ClientId { get; set; } - - public string? SenderName { get; set; } } } diff --git a/AutoWorkshopContracts/ViewModels/OrderViewModel.cs b/AutoWorkshopContracts/ViewModels/OrderViewModel.cs index 2a423b0..ed8da9e 100644 --- a/AutoWorkshopContracts/ViewModels/OrderViewModel.cs +++ b/AutoWorkshopContracts/ViewModels/OrderViewModel.cs @@ -19,6 +19,9 @@ namespace AutoWorkshopContracts.ViewModels [DisplayName("Клиент")] public string ClientFIO { get; set; } = string.Empty; + [DisplayName("Почта клиента")] + public string ClientEmail { get; set; } = string.Empty; + public int? ImplementerId { get; set; } [DisplayName("Исполнитель")] diff --git a/AutoWorkshopDatabaseImplement/AutoWorkshopDatabase.cs b/AutoWorkshopDatabaseImplement/AutoWorkshopDatabase.cs index 6ded373..04cc320 100644 --- a/AutoWorkshopDatabaseImplement/AutoWorkshopDatabase.cs +++ b/AutoWorkshopDatabaseImplement/AutoWorkshopDatabase.cs @@ -29,5 +29,7 @@ namespace AutoWorkshopDatabaseImplement public virtual DbSet Clients { set; get; } public virtual DbSet Implementers { set; get; } + + public virtual DbSet Messages { set; get; } } } diff --git a/AutoWorkshopDatabaseImplement/Implements/MessageInfoStorage.cs b/AutoWorkshopDatabaseImplement/Implements/MessageInfoStorage.cs new file mode 100644 index 0000000..47de70d --- /dev/null +++ b/AutoWorkshopDatabaseImplement/Implements/MessageInfoStorage.cs @@ -0,0 +1,56 @@ +using AutoWorkshopContracts.BindingModels; +using AutoWorkshopContracts.SearchModels; +using AutoWorkshopContracts.StoragesContracts; +using AutoWorkshopContracts.ViewModels; +using AutoWorkshopDatabaseImplement.Models; + +namespace AutoWorkshopDatabaseImplement.Implements +{ + public class MessageInfoStorage : IMessageInfoStorage + { + public List GetFullList() + { + using var Context = new AutoWorkshopDatabase(); + + return Context.Messages + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(MessageInfoSearchModel Model) + { + using var Context = new AutoWorkshopDatabase(); + + return Context.Messages + .Where(x => x.ClientId.HasValue && x.ClientId == Model.ClientId) + .Select(x => x.GetViewModel) + .ToList(); + } + + public MessageInfoViewModel? GetElement(MessageInfoSearchModel Model) + { + if (string.IsNullOrEmpty(Model.MessageId)) + return null; + + using var Context = new AutoWorkshopDatabase(); + + return Context.Messages + .FirstOrDefault(x => x.MessageId == Model.MessageId)? + .GetViewModel; + } + + public MessageInfoViewModel? Insert(MessageInfoBindingModel Model) + { + var NewMessageInfo = MessageInfo.Create(Model); + + if (NewMessageInfo == null) + return null; + + using var Context = new AutoWorkshopDatabase(); + Context.Messages.Add(NewMessageInfo); + + Context.SaveChanges(); + return NewMessageInfo.GetViewModel; + } + } +} diff --git a/AutoWorkshopDatabaseImplement/Models/Client.cs b/AutoWorkshopDatabaseImplement/Models/Client.cs index 56d700a..98b44e1 100644 --- a/AutoWorkshopDatabaseImplement/Models/Client.cs +++ b/AutoWorkshopDatabaseImplement/Models/Client.cs @@ -20,7 +20,10 @@ namespace AutoWorkshopDatabaseImplement.Models public string Password { get; set; } = string.Empty; [ForeignKey("ClientId")] - public virtual List Orders { get; set; } = new(); + public virtual List ClientOrders { get; set; } = new(); + + [ForeignKey("ClientId")] + public virtual List ClientMessages { get; set; } = new(); public static Client? Create(ClientBindingModel Model) { diff --git a/AutoWorkshopDatabaseImplement/Models/MessageInfo.cs b/AutoWorkshopDatabaseImplement/Models/MessageInfo.cs new file mode 100644 index 0000000..5b4be1e --- /dev/null +++ b/AutoWorkshopDatabaseImplement/Models/MessageInfo.cs @@ -0,0 +1,47 @@ +using AutoWorkshopContracts.BindingModels; +using AutoWorkshopContracts.ViewModels; +using AutoWorkshopDataModels.Models; + +namespace AutoWorkshopDatabaseImplement.Models +{ + public class MessageInfo : IMessageInfoModel + { + public string MessageId { get; set; } = string.Empty; + + public int? ClientId { get; set; } + + public string SenderName { get; set; } = string.Empty; + + public DateTime DateDelivery { get; set; } + + public string Subject { get; set; } = string.Empty; + + public string Body { get; set; } = string.Empty; + + public static MessageInfo? Create(MessageInfoBindingModel? Model) + { + if (Model == null) + return null; + + return new() + { + MessageId = Model.MessageId, + ClientId = Model.ClientId, + SenderName = Model.SenderName, + DateDelivery = Model.DateDelivery, + Subject = Model.Subject, + Body = Model.Body + }; + } + + public MessageInfoViewModel GetViewModel => new() + { + MessageId = MessageId, + ClientId = ClientId, + SenderName = SenderName, + DateDelivery = DateDelivery, + Subject = Subject, + Body = Body + }; + } +} diff --git a/AutoWorkshopDatabaseImplement/Models/Order.cs b/AutoWorkshopDatabaseImplement/Models/Order.cs index 2bc3c9b..7a0b071 100644 --- a/AutoWorkshopDatabaseImplement/Models/Order.cs +++ b/AutoWorkshopDatabaseImplement/Models/Order.cs @@ -75,6 +75,7 @@ namespace AutoWorkshopDatabaseImplement.Models ImplementerId = ImplementerId, RepairName = Repair.RepairName, ClientFIO = Client.ClientFIO, + ClientEmail = Client.Email, ImplementerFIO = Implementer?.ImplementerFIO, Count = Count, Sum = Sum, diff --git a/AutoWorkshopRestApi/Program.cs b/AutoWorkshopRestApi/Program.cs index a84505b..20a0587 100644 --- a/AutoWorkshopRestApi/Program.cs +++ b/AutoWorkshopRestApi/Program.cs @@ -1,4 +1,6 @@ using AutoWorkshopBusinessLogic.BusinessLogics; +using AutoWorkshopBusinessLogic.MailWorker; +using AutoWorkshopContracts.BindingModels; using AutoWorkshopContracts.BusinessLogicContracts; using AutoWorkshopContracts.BusinessLogicsContracts; using AutoWorkshopContracts.StoragesContracts; @@ -14,11 +16,15 @@ Builder.Services.AddTransient(); Builder.Services.AddTransient(); Builder.Services.AddTransient(); Builder.Services.AddTransient(); +Builder.Services.AddTransient(); Builder.Services.AddTransient(); Builder.Services.AddTransient(); Builder.Services.AddTransient(); Builder.Services.AddTransient(); +Builder.Services.AddTransient(); + +Builder.Services.AddTransient(); Builder.Services.AddControllers(); @@ -34,6 +40,18 @@ 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()) +}); + + if (App.Environment.IsDevelopment()) { App.UseSwagger(); diff --git a/AutoWorkshopRestApi/appsettings.json b/AutoWorkshopRestApi/appsettings.json index 10f68b8..ec2ee9a 100644 --- a/AutoWorkshopRestApi/appsettings.json +++ b/AutoWorkshopRestApi/appsettings.json @@ -5,5 +5,12 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + + "SmtpClientHost": "smtp.gmail.com", + "SmtpClientPort": "587", + "PopHost": "pop.gmail.com", + "PopPort": "995", + "MailLogin": "pasxalko1488ventilyator@gmail.com", + "MailPassword": "MsCutiePatootie" } diff --git a/AutoWorkshopView/App.config b/AutoWorkshopView/App.config new file mode 100644 index 0000000..cbd42ca --- /dev/null +++ b/AutoWorkshopView/App.config @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file