diff --git a/DressAtelierBusinessLogic/BusinessLogic/ClientLogic.cs b/DressAtelierBusinessLogic/BusinessLogic/ClientLogic.cs index ef9ab11..effe265 100644 --- a/DressAtelierBusinessLogic/BusinessLogic/ClientLogic.cs +++ b/DressAtelierBusinessLogic/BusinessLogic/ClientLogic.cs @@ -8,6 +8,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Text; +using System.Text.RegularExpressions; using System.Threading.Tasks; namespace DressAtelierBusinessLogic.BusinessLogic @@ -101,11 +102,11 @@ namespace DressAtelierBusinessLogic.BusinessLogic { throw new ArgumentNullException("Invalid fullname of user", nameof(model.FullName)); } - if (string.IsNullOrEmpty(model.Email)) + if (string.IsNullOrEmpty(model.Email) || !Regex.IsMatch(model.Email, @"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$", RegexOptions.IgnoreCase)) { throw new ArgumentNullException("Invalid email of user", nameof(model.Email)); } - if (string.IsNullOrEmpty(model.Password)) + if (string.IsNullOrEmpty(model.Password) || !Regex.IsMatch(model.Password, @"^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$", RegexOptions.IgnoreCase)) { throw new ArgumentNullException("Invalid password of user", nameof(model.Password)); } diff --git a/DressAtelierBusinessLogic/BusinessLogic/MessageInfoLogic.cs b/DressAtelierBusinessLogic/BusinessLogic/MessageInfoLogic.cs new file mode 100644 index 0000000..96cafb6 --- /dev/null +++ b/DressAtelierBusinessLogic/BusinessLogic/MessageInfoLogic.cs @@ -0,0 +1,52 @@ +using DressAtelierContracts.BindingModels; +using DressAtelierContracts.BusinessLogicContracts; +using DressAtelierContracts.SearchModels; +using DressAtelierContracts.StorageContracts; +using DressAtelierContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DressAtelierBusinessLogic.BusinessLogic +{ + + public class MessageInfoLogic : IMessageInfoLogic + { + private readonly ILogger _logger; + + private readonly IMessageInfoStorage _messageStorage; + + public MessageInfoLogic(IMessageInfoStorage messageStorage,ILogger logger) + { + _messageStorage = messageStorage; + _logger = logger; + } + public bool Create(MessageInfoBindingModel model) + { + if (_messageStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public List? ReadList(MessageInfoSearchModel? model) + { + _logger.LogInformation("ReadList. ClientID:{ClientID}. ID:{ ID}", model?.ClientID, model?.ID); + + var list = model == null ? _messageStorage.GetFullList() : _messageStorage.GetFilteredList(model); + + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + } +} diff --git a/DressAtelierBusinessLogic/BusinessLogic/OrderLogic.cs b/DressAtelierBusinessLogic/BusinessLogic/OrderLogic.cs index a67c18a..d364bc4 100644 --- a/DressAtelierBusinessLogic/BusinessLogic/OrderLogic.cs +++ b/DressAtelierBusinessLogic/BusinessLogic/OrderLogic.cs @@ -1,4 +1,5 @@ -using DressAtelierContracts.BindingModels; +using DressAtelierBusinessLogic.MailEmployee; +using DressAtelierContracts.BindingModels; using DressAtelierContracts.BusinessLogicContracts; using DressAtelierContracts.SearchModels; using DressAtelierContracts.StorageContracts; @@ -18,11 +19,14 @@ namespace DressAtelierBusinessLogic.BusinessLogic private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; - - public OrderLogic(ILogger logger, IOrderStorage orderStorage) + private readonly IClientStorage _clientStorage; + private readonly AbstractMailEmployee _mailLogic; + public OrderLogic(ILogger logger, IOrderStorage orderStorage, IClientStorage clientStorage, AbstractMailEmployee mailLogic) { _logger = logger; _orderStorage = orderStorage; + _clientStorage = clientStorage; + _mailLogic = mailLogic; } public bool CreateOrder(OrderBindingModel model) @@ -36,11 +40,19 @@ namespace DressAtelierBusinessLogic.BusinessLogic } model.Status = OrderStatus.Accepted; - if (_orderStorage.Insert(model) == null) + + var order = _orderStorage.Insert(model); + if (order == null) { _logger.LogWarning("Insert operation failed"); return false; } + + OrderViewModel orderViewModel = _orderStorage.GetElement(new OrderSearchModel { ID = order.ID}); + ClientViewModel clientViewModel = _clientStorage.GetElement(new ClientSearchModel { ID = orderViewModel.ClientID }); + + SendEmail(clientViewModel,orderViewModel); + return true; } @@ -55,8 +67,14 @@ namespace DressAtelierBusinessLogic.BusinessLogic { ID = model.ID }).EmployeeID; + _orderStorage.Update(model); + OrderViewModel orderViewModel = _orderStorage.GetElement(new OrderSearchModel { ID = model.ID }); + ClientViewModel clientViewModel = _clientStorage.GetElement(new ClientSearchModel { ID = orderViewModel.ClientID }); + + SendEmail(clientViewModel, orderViewModel); + return true; } @@ -69,7 +87,14 @@ namespace DressAtelierBusinessLogic.BusinessLogic { ID = model.ID }).EmployeeID; + _orderStorage.Update(model); + + OrderViewModel orderViewModel = _orderStorage.GetElement(new OrderSearchModel { ID = model.ID }); + ClientViewModel clientViewModel = _clientStorage.GetElement(new ClientSearchModel { ID = orderViewModel.ClientID }); + + SendEmail(clientViewModel, orderViewModel); + return true; } @@ -89,6 +114,12 @@ namespace DressAtelierBusinessLogic.BusinessLogic _logger.LogInformation("Update to in process status. ID:{ID}", model.ID); model.Status = OrderStatus.InProcess; _orderStorage.Update(model); + + OrderViewModel orderViewModel = _orderStorage.GetElement(new OrderSearchModel { ID = model.ID }); + ClientViewModel clientViewModel = _clientStorage.GetElement(new ClientSearchModel { ID = orderViewModel.ClientID }); + + SendEmail(clientViewModel, orderViewModel); + return true; } @@ -146,5 +177,37 @@ namespace DressAtelierBusinessLogic.BusinessLogic throw new InvalidOperationException("Order with such name already exists."); } } + + public void SendEmail(ClientViewModel clientModel ,OrderViewModel orderModel) + { + if(clientModel == null && orderModel == null) + { + return; + } + + MailSendInfoBindingModel mailModel; + + if (orderModel.Status == OrderStatus.Accepted) + { + mailModel = new MailSendInfoBindingModel + { + Address = clientModel.Email, + Subject = $"Order №{orderModel.ID}", + Text = $"Your order №{orderModel.ID} by {orderModel.DateCreate} on {orderModel.Sum} was accepted!" + }; + } + else + { + mailModel = new MailSendInfoBindingModel + { + Address = clientModel.Email, + Subject = $"Order №{orderModel.ID}", + Text = $"Order №{orderModel.ID} status was changed to {orderModel.Status}" + }; + } + + + _mailLogic.MailSendAsync(mailModel); + } } } diff --git a/DressAtelierBusinessLogic/DressAtelierBusinessLogic.csproj b/DressAtelierBusinessLogic/DressAtelierBusinessLogic.csproj index 3ba55b7..901e09a 100644 --- a/DressAtelierBusinessLogic/DressAtelierBusinessLogic.csproj +++ b/DressAtelierBusinessLogic/DressAtelierBusinessLogic.csproj @@ -8,6 +8,7 @@ + diff --git a/DressAtelierBusinessLogic/MailEmployee/AbstractMailEmployee.cs b/DressAtelierBusinessLogic/MailEmployee/AbstractMailEmployee.cs new file mode 100644 index 0000000..75ea19f --- /dev/null +++ b/DressAtelierBusinessLogic/MailEmployee/AbstractMailEmployee.cs @@ -0,0 +1,84 @@ +using DressAtelierContracts.BindingModels; +using DressAtelierContracts.BusinessLogicContracts; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DressAtelierBusinessLogic.MailEmployee +{ + public abstract class AbstractMailEmployee + { + 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 AbstractMailEmployee(ILogger logger, IMessageInfoLogic messageInfoLogic) + { + _logger = logger; + _messageInfoLogic = messageInfoLogic; + } + + public void MailConfig(MailConfigBindingModel config) + { + _mailLogin = config.Login; + _mailPassword = config.Password; + _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.Address) || string.IsNullOrEmpty(info.Subject) || string.IsNullOrEmpty(info.Text)) + { + return; + } + _logger.LogDebug("Send E-mail: {To}, {Subject}", info.Address, 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 E-Mail: {Count} new emails", list.Count); + foreach (var mail in list) + { + _messageInfoLogic.Create(mail); + } + } + + protected abstract Task SendMailAsync(MailSendInfoBindingModel info); + protected abstract Task> ReceiveMailAsync(); + } +} diff --git a/DressAtelierBusinessLogic/MailEmployee/MailKitEmployee.cs b/DressAtelierBusinessLogic/MailEmployee/MailKitEmployee.cs new file mode 100644 index 0000000..1e95fd1 --- /dev/null +++ b/DressAtelierBusinessLogic/MailEmployee/MailKitEmployee.cs @@ -0,0 +1,84 @@ +using DressAtelierContracts.BindingModels; +using DressAtelierContracts.BusinessLogicContracts; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Mail; +using System.Net; +using System.Security.Authentication; +using System.Text; +using System.Threading.Tasks; +using MailKit.Net; +using MailKit.Security; +using MailKit.Net.Pop3; + +namespace DressAtelierBusinessLogic.MailEmployee +{ + public class MailKitEmployee : AbstractMailEmployee + { + public MailKitEmployee(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.Address)); + 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 + { + DeliveryDate = message.Date.DateTime, + ID = message.MessageId, + SenderName = mail.Address, + Subject = message.Subject, + Body = message.TextBody != null ? message.TextBody : message.HtmlBody + }); + } + } + } + catch (MailKit.Security.AuthenticationException) + { + + } + finally + { + client.Disconnect(true); + } + }); + return list; + } + } +} diff --git a/DressAtelierClientApp/Controllers/HomeController.cs b/DressAtelierClientApp/Controllers/HomeController.cs index f00279c..77d5de6 100644 --- a/DressAtelierClientApp/Controllers/HomeController.cs +++ b/DressAtelierClientApp/Controllers/HomeController.cs @@ -109,5 +109,14 @@ namespace DressAtelierClientApp.Controllers var prod = APIClient.GetRequest($"api/main/getdress?dressID={dress}"); return count * (prod?.Price ?? 1); } + + [HttpGet] public IActionResult Mail() + { + 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/DressAtelierClientApp/Views/Home/Mail.cshtml b/DressAtelierClientApp/Views/Home/Mail.cshtml new file mode 100644 index 0000000..fa896d6 --- /dev/null +++ b/DressAtelierClientApp/Views/Home/Mail.cshtml @@ -0,0 +1,48 @@ +@using DressAtelierContracts.ViewModels +@model List +@{ + ViewData["Title"] = "Mail"; +} +
+

Orders

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

Log in

+ return; + } + + + + + + + + + + @foreach (var item in Model) + { + + + + + + } + +
+ Date + + Title + + Text +
+ @Html.DisplayFor(modelItem => item.DeliveryDate) + + @Html.DisplayFor(modelItem => item.Subject) + + @Html.DisplayFor(modelItem => item.Body) +
+ } +
\ No newline at end of file diff --git a/DressAtelierClientApp/Views/Shared/_Layout.cshtml b/DressAtelierClientApp/Views/Shared/_Layout.cshtml index 6da5a0e..e7d2d08 100644 --- a/DressAtelierClientApp/Views/Shared/_Layout.cshtml +++ b/DressAtelierClientApp/Views/Shared/_Layout.cshtml @@ -25,6 +25,9 @@ + diff --git a/DressAtelierContracts/BindingModels/MailConfigBindingModel.cs b/DressAtelierContracts/BindingModels/MailConfigBindingModel.cs new file mode 100644 index 0000000..ee69b9f --- /dev/null +++ b/DressAtelierContracts/BindingModels/MailConfigBindingModel.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DressAtelierContracts.BindingModels +{ + public class MailConfigBindingModel + { + public string Login { get; set; } = string.Empty; + public string Password { 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/DressAtelierContracts/BindingModels/MailSendInfoBindingModel.cs b/DressAtelierContracts/BindingModels/MailSendInfoBindingModel.cs new file mode 100644 index 0000000..2dd37c6 --- /dev/null +++ b/DressAtelierContracts/BindingModels/MailSendInfoBindingModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DressAtelierContracts.BindingModels +{ + public class MailSendInfoBindingModel + { + public string Address { get; set; } = string.Empty; + public string Subject { get; set; } = string.Empty; + public string Text { get; set; } = string.Empty; + } +} diff --git a/DressAtelierContracts/BindingModels/MessageInfoBindingModel.cs b/DressAtelierContracts/BindingModels/MessageInfoBindingModel.cs new file mode 100644 index 0000000..f1d9975 --- /dev/null +++ b/DressAtelierContracts/BindingModels/MessageInfoBindingModel.cs @@ -0,0 +1,24 @@ +using DressAtelierDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DressAtelierContracts.BindingModels +{ + public class MessageInfoBindingModel : IMessageInfoModel + { + public string ID { get; set; } = string.Empty; + + public int? ClientID { get; set; } + + public string SenderName { get; set; } = string.Empty; + + public DateTime DeliveryDate { get; set; } + + public string Subject { get; set; } = string.Empty; + + public string Body { get; set; } = string.Empty; + } +} diff --git a/DressAtelierContracts/BusinessLogicContracts/IMessageInfoLogic.cs b/DressAtelierContracts/BusinessLogicContracts/IMessageInfoLogic.cs new file mode 100644 index 0000000..ea2d1f1 --- /dev/null +++ b/DressAtelierContracts/BusinessLogicContracts/IMessageInfoLogic.cs @@ -0,0 +1,17 @@ +using DressAtelierContracts.BindingModels; +using DressAtelierContracts.SearchModels; +using DressAtelierContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DressAtelierContracts.BusinessLogicContracts +{ + public interface IMessageInfoLogic + { + List? ReadList(MessageInfoSearchModel? model); + bool Create(MessageInfoBindingModel model); + } +} diff --git a/DressAtelierContracts/SearchModels/MessageInfoSearchModel.cs b/DressAtelierContracts/SearchModels/MessageInfoSearchModel.cs new file mode 100644 index 0000000..11e26c5 --- /dev/null +++ b/DressAtelierContracts/SearchModels/MessageInfoSearchModel.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DressAtelierContracts.SearchModels +{ + public class MessageInfoSearchModel + { + public string? ID { get; set; } + public int? ClientID { get; set; } + + } + +} diff --git a/DressAtelierContracts/StorageContracts/IMessageInfoStorage.cs b/DressAtelierContracts/StorageContracts/IMessageInfoStorage.cs new file mode 100644 index 0000000..4c8c3f7 --- /dev/null +++ b/DressAtelierContracts/StorageContracts/IMessageInfoStorage.cs @@ -0,0 +1,19 @@ +using DressAtelierContracts.BindingModels; +using DressAtelierContracts.SearchModels; +using DressAtelierContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DressAtelierContracts.StorageContracts +{ + public interface IMessageInfoStorage + { + List GetFullList(); + List GetFilteredList(MessageInfoSearchModel model); + MessageInfoViewModel? GetElement(MessageInfoSearchModel model); + MessageInfoViewModel? Insert(MessageInfoBindingModel model); + } +} diff --git a/DressAtelierContracts/ViewModels/MessageInfoViewModel.cs b/DressAtelierContracts/ViewModels/MessageInfoViewModel.cs new file mode 100644 index 0000000..94be7e7 --- /dev/null +++ b/DressAtelierContracts/ViewModels/MessageInfoViewModel.cs @@ -0,0 +1,29 @@ +using DressAtelierDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DressAtelierContracts.ViewModels +{ + public class MessageInfoViewModel : IMessageInfoModel + { + public string ID { get; set; } = string.Empty; + + public int? ClientID { get; set; } + + [DisplayName("Sender's name")] + public string SenderName { get; set; } = string.Empty; + + [DisplayName("Delivery date")] + public DateTime DeliveryDate { get; set; } + + [DisplayName("Message subject")] + public string Subject { get; set; } = string.Empty; + + [DisplayName("Message content")] + public string Body { get; set; } = string.Empty; + } +} diff --git a/DressAtelierDataModels/Models/IMessageInfoModel.cs b/DressAtelierDataModels/Models/IMessageInfoModel.cs new file mode 100644 index 0000000..5959599 --- /dev/null +++ b/DressAtelierDataModels/Models/IMessageInfoModel.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DressAtelierDataModels.Models +{ + public interface IMessageInfoModel + { + string ID { get; } + int? ClientID { get; } + string SenderName { get; } + DateTime DeliveryDate { get; } + string Subject { get; } + string Body { get; } + } +} diff --git a/DressAtelierDatabaseImplement/DressAtelierDatabase.cs b/DressAtelierDatabaseImplement/DressAtelierDatabase.cs index f24f973..59216c8 100644 --- a/DressAtelierDatabaseImplement/DressAtelierDatabase.cs +++ b/DressAtelierDatabaseImplement/DressAtelierDatabase.cs @@ -26,5 +26,6 @@ namespace DressAtelierDatabaseImplementation public virtual DbSet Orders { set; get; } public virtual DbSet Clients { set; get; } public virtual DbSet Employees { set; get; } + public virtual DbSet Messages { set; get; } } } diff --git a/DressAtelierDatabaseImplement/Implements/ClientStorage.cs b/DressAtelierDatabaseImplement/Implements/ClientStorage.cs index f1db70b..4f248f0 100644 --- a/DressAtelierDatabaseImplement/Implements/ClientStorage.cs +++ b/DressAtelierDatabaseImplement/Implements/ClientStorage.cs @@ -48,7 +48,7 @@ namespace DressAtelierDatabaseImplement.Implements { if (string.IsNullOrEmpty(model.Email) && !model.ID.HasValue) { return null; } using var context = new DressAtelierDatabase(); - return context.Clients.FirstOrDefault(x => (!(string.IsNullOrEmpty(model.Email)) && model.Email == x.Email) || (model.ID.HasValue && model.ID == x.ID))?.GetViewModel; + return context.Clients.FirstOrDefault(x => (!(string.IsNullOrEmpty(model.Email)) && model.Email.Equals(x.Email)) || (model.ID.HasValue && model.ID == x.ID))?.GetViewModel; } public List GetFilteredList(ClientSearchModel model) diff --git a/DressAtelierDatabaseImplement/Implements/MessageInfoStorage.cs b/DressAtelierDatabaseImplement/Implements/MessageInfoStorage.cs new file mode 100644 index 0000000..923eb63 --- /dev/null +++ b/DressAtelierDatabaseImplement/Implements/MessageInfoStorage.cs @@ -0,0 +1,67 @@ +using DressAtelierContracts.BindingModels; +using DressAtelierContracts.SearchModels; +using DressAtelierContracts.StorageContracts; +using DressAtelierContracts.ViewModels; +using DressAtelierDatabaseImplement.Models; +using DressAtelierDatabaseImplementation; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DressAtelierDatabaseImplement.Implements +{ + public class MessageInfoStorage : IMessageInfoStorage + { + public MessageInfoViewModel? GetElement(MessageInfoSearchModel model) + { + if(string.IsNullOrEmpty(model.ID)) + { + return null; + } + using var context = new DressAtelierDatabase(); + return context.Messages.Include(x => x.Client).FirstOrDefault(x => x.ID.Equals(model.ID))?.GetViewModel; + } + + public List GetFilteredList(MessageInfoSearchModel model) + { + if (!model.ClientID.HasValue) + { + return new(); + } + using var context = new DressAtelierDatabase(); + return context.Messages.Include(x => x.Client).Where(x => x.ClientID == model.ClientID).Select(x => x.GetViewModel).ToList() ; + } + + public List GetFullList() + { + using var context = new DressAtelierDatabase(); + return context.Messages.Include(x => x.Client).Select(x => x.GetViewModel).ToList(); + } + + public MessageInfoViewModel? Insert(MessageInfoBindingModel model) + { + + using var context = new DressAtelierDatabase(); + + model.ClientID = context.Clients.FirstOrDefault(x => x.Email.Equals(model.SenderName))?.ID; + + var message = MessageInfo.Create(model); + if (message == null) + { + return null; + } + + if (GetElement(new MessageInfoSearchModel { ID = model.ID }) != null) + { + return null; + } + + context.Messages.Add(message); + context.SaveChanges(); + return message.GetViewModel; + } + } +} diff --git a/DressAtelierDatabaseImplement/Migrations/20230422104256_Messages.Designer.cs b/DressAtelierDatabaseImplement/Migrations/20230422104256_Messages.Designer.cs new file mode 100644 index 0000000..b217bc9 --- /dev/null +++ b/DressAtelierDatabaseImplement/Migrations/20230422104256_Messages.Designer.cs @@ -0,0 +1,296 @@ +// +using System; +using DressAtelierDatabaseImplementation; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace DressAtelierDatabaseImplement.Migrations +{ + [DbContext(typeof(DressAtelierDatabase))] + [Migration("20230422104256_Messages")] + partial class Messages + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.4") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("DressAtelierDatabaseImplement.Models.Client", b => + { + b.Property("ID") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); + + b.Property("Email") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("FullName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Password") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("ID"); + + b.ToTable("Clients"); + }); + + modelBuilder.Entity("DressAtelierDatabaseImplement.Models.Employee", b => + { + b.Property("ID") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); + + b.Property("FullName") + .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("Employees"); + }); + + modelBuilder.Entity("DressAtelierDatabaseImplement.Models.MessageInfo", b => + { + b.Property("ID") + .HasColumnType("nvarchar(450)"); + + b.Property("Body") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ClientID") + .HasColumnType("int"); + + b.Property("DeliveryDate") + .HasColumnType("datetime2"); + + b.Property("SenderName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Subject") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("ID"); + + b.HasIndex("ClientID"); + + b.ToTable("Messages"); + }); + + modelBuilder.Entity("DressAtelierDatabaseImplementation.Models.Dress", b => + { + b.Property("ID") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); + + b.Property("DressName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("ID"); + + b.ToTable("Dresses"); + }); + + modelBuilder.Entity("DressAtelierDatabaseImplementation.Models.DressMaterial", b => + { + b.Property("ID") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("ID")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DressID") + .HasColumnType("int"); + + b.Property("MaterialID") + .HasColumnType("int"); + + b.HasKey("ID"); + + b.HasIndex("DressID"); + + b.HasIndex("MaterialID"); + + b.ToTable("DressMaterials"); + }); + + modelBuilder.Entity("DressAtelierDatabaseImplementation.Models.Material", 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("Materials"); + }); + + modelBuilder.Entity("DressAtelierDatabaseImplementation.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("DressID") + .HasColumnType("int"); + + b.Property("EmployeeID") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("ID"); + + b.HasIndex("ClientID"); + + b.HasIndex("DressID"); + + b.HasIndex("EmployeeID"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("DressAtelierDatabaseImplement.Models.MessageInfo", b => + { + b.HasOne("DressAtelierDatabaseImplement.Models.Client", "Client") + .WithMany() + .HasForeignKey("ClientID"); + + b.Navigation("Client"); + }); + + modelBuilder.Entity("DressAtelierDatabaseImplementation.Models.DressMaterial", b => + { + b.HasOne("DressAtelierDatabaseImplementation.Models.Dress", "Dress") + .WithMany("Materials") + .HasForeignKey("DressID") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DressAtelierDatabaseImplementation.Models.Material", "Material") + .WithMany("DressComponents") + .HasForeignKey("MaterialID") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Dress"); + + b.Navigation("Material"); + }); + + modelBuilder.Entity("DressAtelierDatabaseImplementation.Models.Order", b => + { + b.HasOne("DressAtelierDatabaseImplement.Models.Client", "Client") + .WithMany("Orders") + .HasForeignKey("ClientID") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DressAtelierDatabaseImplementation.Models.Dress", "Dress") + .WithMany("Orders") + .HasForeignKey("DressID") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DressAtelierDatabaseImplement.Models.Employee", "Employee") + .WithMany("Orders") + .HasForeignKey("EmployeeID"); + + b.Navigation("Client"); + + b.Navigation("Dress"); + + b.Navigation("Employee"); + }); + + modelBuilder.Entity("DressAtelierDatabaseImplement.Models.Client", b => + { + b.Navigation("Orders"); + }); + + modelBuilder.Entity("DressAtelierDatabaseImplement.Models.Employee", b => + { + b.Navigation("Orders"); + }); + + modelBuilder.Entity("DressAtelierDatabaseImplementation.Models.Dress", b => + { + b.Navigation("Materials"); + + b.Navigation("Orders"); + }); + + modelBuilder.Entity("DressAtelierDatabaseImplementation.Models.Material", b => + { + b.Navigation("DressComponents"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/DressAtelierDatabaseImplement/Migrations/20230422104256_Messages.cs b/DressAtelierDatabaseImplement/Migrations/20230422104256_Messages.cs new file mode 100644 index 0000000..2befa14 --- /dev/null +++ b/DressAtelierDatabaseImplement/Migrations/20230422104256_Messages.cs @@ -0,0 +1,48 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace DressAtelierDatabaseImplement.Migrations +{ + /// + public partial class Messages : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Messages", + columns: table => new + { + ID = table.Column(type: "nvarchar(450)", nullable: false), + ClientID = table.Column(type: "int", nullable: true), + SenderName = table.Column(type: "nvarchar(max)", nullable: false), + DeliveryDate = 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.ID); + 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/DressAtelierDatabaseImplement/Migrations/DressAtelierDatabaseModelSnapshot.cs b/DressAtelierDatabaseImplement/Migrations/DressAtelierDatabaseModelSnapshot.cs index cc462db..9262014 100644 --- a/DressAtelierDatabaseImplement/Migrations/DressAtelierDatabaseModelSnapshot.cs +++ b/DressAtelierDatabaseImplement/Migrations/DressAtelierDatabaseModelSnapshot.cs @@ -74,6 +74,36 @@ namespace DressAtelierDatabaseImplement.Migrations b.ToTable("Employees"); }); + modelBuilder.Entity("DressAtelierDatabaseImplement.Models.MessageInfo", b => + { + b.Property("ID") + .HasColumnType("nvarchar(450)"); + + b.Property("Body") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ClientID") + .HasColumnType("int"); + + b.Property("DeliveryDate") + .HasColumnType("datetime2"); + + b.Property("SenderName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Subject") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("ID"); + + b.HasIndex("ClientID"); + + b.ToTable("Messages"); + }); + modelBuilder.Entity("DressAtelierDatabaseImplementation.Models.Dress", b => { b.Property("ID") @@ -183,6 +213,15 @@ namespace DressAtelierDatabaseImplement.Migrations b.ToTable("Orders"); }); + modelBuilder.Entity("DressAtelierDatabaseImplement.Models.MessageInfo", b => + { + b.HasOne("DressAtelierDatabaseImplement.Models.Client", "Client") + .WithMany() + .HasForeignKey("ClientID"); + + b.Navigation("Client"); + }); + modelBuilder.Entity("DressAtelierDatabaseImplementation.Models.DressMaterial", b => { b.HasOne("DressAtelierDatabaseImplementation.Models.Dress", "Dress") diff --git a/DressAtelierDatabaseImplement/Models/Client.cs b/DressAtelierDatabaseImplement/Models/Client.cs index 1a7a423..871686e 100644 --- a/DressAtelierDatabaseImplement/Models/Client.cs +++ b/DressAtelierDatabaseImplement/Models/Client.cs @@ -26,6 +26,9 @@ namespace DressAtelierDatabaseImplement.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/DressAtelierDatabaseImplement/Models/MessageInfo.cs b/DressAtelierDatabaseImplement/Models/MessageInfo.cs new file mode 100644 index 0000000..29fa5eb --- /dev/null +++ b/DressAtelierDatabaseImplement/Models/MessageInfo.cs @@ -0,0 +1,55 @@ +using DressAtelierContracts.BindingModels; +using DressAtelierContracts.ViewModels; +using DressAtelierDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DressAtelierDatabaseImplement.Models +{ + public class MessageInfo : IMessageInfoModel + { + + public string ID { get; private set; } = string.Empty; + + public int? ClientID { get; private set; } + + public string SenderName { get; private set; } = string.Empty; + public DateTime DeliveryDate { get; private set; } + + public string Subject { get; private set; } = string.Empty; + + public string Body { get; private set; } = string.Empty; + + public virtual Client? Client { get; private set; } + + public static MessageInfo? Create(MessageInfoBindingModel model) + { + if(model == null) + { + return null; + } + return new MessageInfo + { + ID = model.ID, + ClientID = model.ClientID, + SenderName = model.SenderName, + Body = model.Body, + DeliveryDate = model.DeliveryDate, + Subject = model.Subject, + }; + } + + public MessageInfoViewModel GetViewModel => new() + { + ID = ID, + ClientID = ClientID, + SenderName = SenderName, + Body = Body, + DeliveryDate = DeliveryDate, + Subject = Subject, + }; + } +} diff --git a/DressAtelierFileImplement/DataFileSingleton.cs b/DressAtelierFileImplement/DataFileSingleton.cs index 079729f..979cad7 100644 --- a/DressAtelierFileImplement/DataFileSingleton.cs +++ b/DressAtelierFileImplement/DataFileSingleton.cs @@ -17,11 +17,13 @@ namespace DressAtelierFileImplement private readonly string DressFileName = "Dress.xml"; private readonly string ClientFileName = "Client.xml"; private readonly string EmployeeFileName = "Employee.xml"; + private readonly string MessageInfoFileName = "Message.xml"; public List Components { get; private set; } public List Orders { get; private set; } public List Dresses { get; private set; } public List Clients { get; private set; } public List Employees { get; private set; } + public List Messages { get; private set; } public static DataFileSingleton GetInstance() { if (instance == null) @@ -35,6 +37,7 @@ namespace DressAtelierFileImplement public void SaveOrders() => SaveData(Orders, OrderFileName,"Orders", x => x.GetXElement); public void SaveClients() => SaveData(Clients, ClientFileName, "Clients", x => x.GetXElement); public void SaveEmployees() => SaveData(Employees, EmployeeFileName,"Employees",x => x.GetXElement); + public void SaveMessages() => SaveData(Messages, MessageInfoFileName, "Messages", x => x.GetXElement); private DataFileSingleton() { Components = LoadData(MaterialFileName, "Component", x => Material.Create(x)!)!; @@ -42,6 +45,7 @@ namespace DressAtelierFileImplement Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; Clients = LoadData(ClientFileName, "Client", x => Client.Create(x)!)!; Employees = LoadData(EmployeeFileName, "Employee", x => Employee.Create(x)!)!; + Messages = LoadData(MessageInfoFileName, "Message", x => MessageInfo.Create(x)!)!; } private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) diff --git a/DressAtelierFileImplement/Implements/MessageInfoStorage.cs b/DressAtelierFileImplement/Implements/MessageInfoStorage.cs new file mode 100644 index 0000000..70d98bf --- /dev/null +++ b/DressAtelierFileImplement/Implements/MessageInfoStorage.cs @@ -0,0 +1,59 @@ +using DressAtelierContracts.BindingModels; +using DressAtelierContracts.SearchModels; +using DressAtelierContracts.StorageContracts; +using DressAtelierContracts.ViewModels; +using DressAtelierFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace DressAtelierFileImplement.Implements +{ + public class MessageInfoStorage : IMessageInfoStorage + { + private readonly DataFileSingleton _source; + + public MessageInfoStorage() + { + _source = DataFileSingleton.GetInstance(); + } + + public MessageInfoViewModel? GetElement(MessageInfoSearchModel model) + { + if(string.IsNullOrEmpty(model.ID)) + { + return null; + } + return _source.Messages.FirstOrDefault(x => x.ID.Equals(model.ID))?.GetViewModel; + } + + public List GetFilteredList(MessageInfoSearchModel model) + { + if (!model.ClientID.HasValue) + { + return new(); + } + 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/DressAtelierFileImplement/Models/MessageInfo.cs b/DressAtelierFileImplement/Models/MessageInfo.cs new file mode 100644 index 0000000..1fb916a --- /dev/null +++ b/DressAtelierFileImplement/Models/MessageInfo.cs @@ -0,0 +1,80 @@ +using DressAtelierContracts.BindingModels; +using DressAtelierContracts.ViewModels; +using DressAtelierDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace DressAtelierFileImplement.Models +{ + public class MessageInfo : IMessageInfoModel + { + public string ID { get; set; } = string.Empty; + + public int? ClientID { get; set; } + + public string SenderName { get; set; } = string.Empty; + + public DateTime DeliveryDate { 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 MessageInfo() + { + ID = model.ID, + ClientID = model.ClientID, + SenderName = model.SenderName, + DeliveryDate = model.DeliveryDate, + Subject = model.Subject, + Body = model.Body, + + }; + } + + public static MessageInfo? Create(XElement element) + { + if (element == null) + { + return null; + } + return new MessageInfo() + { + ID = element.Attribute("ID")!.Value, + ClientID = Convert.ToInt32(element.Attribute("ClientID")!.Value), + SenderName = element.Element("SenderName")!.Value, + DeliveryDate = Convert.ToDateTime(element.Element("DeliveryDate")!.Value), + Subject = element.Attribute("Subject")!.Value, + Body = element.Attribute("Body")!.Value, + }; + } + + public MessageInfoViewModel GetViewModel => new() + { + ID = ID, + ClientID = ClientID, + SenderName = SenderName, + DeliveryDate = DeliveryDate, + Subject = Subject, + Body = Body + }; + + public XElement GetXElement => new("MessageInfo", new XAttribute("ID", ID), + new XElement("ClientID", ClientID.ToString()), + new XElement("SenderName", SenderName), + new XElement("DeliveryDate", DeliveryDate), + new XElement("Subject", Subject), + new XElement("Body", Body), + new XElement("SenderName", SenderName)); + } +} diff --git a/DressAtelierListImplement/DataListSingleton.cs b/DressAtelierListImplement/DataListSingleton.cs index 6537e83..cd93058 100644 --- a/DressAtelierListImplement/DataListSingleton.cs +++ b/DressAtelierListImplement/DataListSingleton.cs @@ -15,6 +15,7 @@ namespace DressAtelierListImplement public List Dresses { get; set; } public List Clients { get; set; } public List Employees { get; set; } + public List Messages { get; set; } private DataListSingleton() { Components = new List(); @@ -22,6 +23,7 @@ namespace DressAtelierListImplement Dresses = new List(); Clients = new List(); Employees = new List(); + Messages = new List(); } public static DataListSingleton GetInstance() { diff --git a/DressAtelierListImplement/Implements/MessageInfoStorage.cs b/DressAtelierListImplement/Implements/MessageInfoStorage.cs new file mode 100644 index 0000000..2292c41 --- /dev/null +++ b/DressAtelierListImplement/Implements/MessageInfoStorage.cs @@ -0,0 +1,66 @@ +using DressAtelierContracts.BindingModels; +using DressAtelierContracts.SearchModels; +using DressAtelierContracts.StorageContracts; +using DressAtelierContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DressAtelierListImplement.Implements +{ + public class MessageInfoStorage : IMessageInfoStorage + { + private readonly DataListSingleton _source; + public MessageInfoStorage(DataListSingleton source) + { + _source = DataListSingleton.GetInstance(); + } + + public MessageInfoViewModel? GetElement(MessageInfoSearchModel model) + { + if(string.IsNullOrEmpty(model.ID)) + { + return null; + } + + foreach(var msg in _source.Messages) + { + if(msg.ID.Equals(model.ID)) + { + return msg.GetViewModel; + } + } + return null; + } + + public List GetFilteredList(MessageInfoSearchModel model) + { + var list = new List(); + foreach(var msg in _source.Messages) + { + if(msg.ClientID.HasValue && model.ClientID == msg.ClientID) + { + list.Add(msg.GetViewModel); + } + } + return list; + } + + public List GetFullList() + { + var list = new List(); + foreach(var msg in _source.Messages) + { + list.Add(msg.GetViewModel); + } + return list; + } + + public MessageInfoViewModel? Insert(MessageInfoBindingModel model) + { + return null; + } + } +} diff --git a/DressAtelierListImplement/Models/MessageInfo.cs b/DressAtelierListImplement/Models/MessageInfo.cs new file mode 100644 index 0000000..d43904a --- /dev/null +++ b/DressAtelierListImplement/Models/MessageInfo.cs @@ -0,0 +1,51 @@ +using DressAtelierContracts.BindingModels; +using DressAtelierContracts.ViewModels; +using DressAtelierDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DressAtelierListImplement.Models +{ + public class MessageInfo : IMessageInfoModel + { + public string ID { get; set; } = string.Empty; + + public int? ClientID { get; set; } + + public string SenderName { get; set; } = string.Empty; + + public DateTime DeliveryDate { 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() + { + ID = model.ID, + SenderName = model.SenderName, + DeliveryDate = model.DeliveryDate, + Subject = model.Subject, + Body = model.Body, + }; + } + + public MessageInfoViewModel GetViewModel => new() + { + ID = ID, + SenderName = SenderName, + DeliveryDate = DeliveryDate, + Subject = Subject, + Body = Body + }; + } +} diff --git a/DressAtelierRestApi/Controllers/ClientController.cs b/DressAtelierRestApi/Controllers/ClientController.cs index d3a403f..256a306 100644 --- a/DressAtelierRestApi/Controllers/ClientController.cs +++ b/DressAtelierRestApi/Controllers/ClientController.cs @@ -11,11 +11,13 @@ namespace DressAtelierRestApi.Controllers public class ClientController : Controller { private readonly ILogger _logger; - private readonly IClientLogic _logic; - public ClientController(IClientLogic logic, ILogger logger) + private readonly IClientLogic _logic; + private readonly IMessageInfoLogic _mailLogic; + public ClientController(IClientLogic logic, ILogger logger, IMessageInfoLogic mailLogic) { _logger = logger; _logic = logic; + _mailLogic = mailLogic; } [HttpGet] @@ -58,5 +60,22 @@ namespace DressAtelierRestApi.Controllers throw; } } + + [HttpGet] + public List? GetMessages(int clientID) + { + try + { + return _mailLogic.ReadList(new MessageInfoSearchModel + { + ClientID = clientID + }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Receiving letters for client error"); + throw; + } + } } } diff --git a/DressAtelierRestApi/Program.cs b/DressAtelierRestApi/Program.cs index 9afafeb..8717c40 100644 --- a/DressAtelierRestApi/Program.cs +++ b/DressAtelierRestApi/Program.cs @@ -1,4 +1,6 @@ using DressAtelierBusinessLogic.BusinessLogic; +using DressAtelierBusinessLogic.MailEmployee; +using DressAtelierContracts.BindingModels; using DressAtelierContracts.BusinessLogicContracts; using DressAtelierContracts.StorageContracts; using DressAtelierDatabaseImplement.Implements; @@ -16,12 +18,16 @@ builder.Logging.AddLog4Net("log4net.config"); 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.AddSingleton(); builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle @@ -32,6 +38,16 @@ builder.Services.AddSwaggerGen(c => }); var app = builder.Build(); +var mailSender = app.Services.GetService(); +mailSender?.MailConfig(new MailConfigBindingModel +{ + Login = builder.Configuration?.GetSection("MailLogin")?.Value?.ToString() ?? string.Empty, + Password = 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/DressAtelierRestApi/appsettings.json b/DressAtelierRestApi/appsettings.json index 10f68b8..8182671 100644 --- a/DressAtelierRestApi/appsettings.json +++ b/DressAtelierRestApi/appsettings.json @@ -5,5 +5,12 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + + "SmtpClientHost": "smtp.rambler.ru", + "SmtpClientPort": "587", + "PopHost": "pop.rambler.ru", + "PopPort": "995", + "MailLogin": "chelovek.labov@rambler.ru", + "MailPassword": "Iloverpp4real" } diff --git a/SewingDresses/App.config b/SewingDresses/App.config new file mode 100644 index 0000000..eb50a14 --- /dev/null +++ b/SewingDresses/App.config @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/SewingDresses/FormEmails.Designer.cs b/SewingDresses/FormEmails.Designer.cs new file mode 100644 index 0000000..b6cfca6 --- /dev/null +++ b/SewingDresses/FormEmails.Designer.cs @@ -0,0 +1,64 @@ +namespace SewingDresses +{ + partial class FormEmails + { + /// + /// 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() + { + this.emailsGridView = new System.Windows.Forms.DataGridView(); + ((System.ComponentModel.ISupportInitialize)(this.emailsGridView)).BeginInit(); + this.SuspendLayout(); + // + // emailsGridView + // + this.emailsGridView.BackgroundColor = System.Drawing.SystemColors.ControlLightLight; + this.emailsGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.emailsGridView.GridColor = System.Drawing.SystemColors.AppWorkspace; + this.emailsGridView.Location = new System.Drawing.Point(12, 12); + this.emailsGridView.Name = "emailsGridView"; + this.emailsGridView.RowTemplate.Height = 25; + this.emailsGridView.Size = new System.Drawing.Size(776, 426); + this.emailsGridView.TabIndex = 0; + // + // FormEmails + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(800, 450); + this.Controls.Add(this.emailsGridView); + this.Name = "FormEmails"; + this.Text = "FormEmails"; + this.Load += new System.EventHandler(this.FormEmails_Load); + ((System.ComponentModel.ISupportInitialize)(this.emailsGridView)).EndInit(); + this.ResumeLayout(false); + + } + + #endregion + + private DataGridView emailsGridView; + } +} \ No newline at end of file diff --git a/SewingDresses/FormEmails.cs b/SewingDresses/FormEmails.cs new file mode 100644 index 0000000..6990d4b --- /dev/null +++ b/SewingDresses/FormEmails.cs @@ -0,0 +1,53 @@ +using DressAtelierContracts.BusinessLogicContracts; +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 SewingDresses +{ + public partial class FormEmails : Form + { + private readonly ILogger _logger; + private readonly IMessageInfoLogic _logic; + + public FormEmails(ILogger logger, IMessageInfoLogic logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + } + + private void FormEmails_Load(object sender, EventArgs e) + { + LoadData(); + } + + private void LoadData() + { + try + { + var list = _logic.ReadList(null); + if (list != null) + { + emailsGridView.DataSource = list; + emailsGridView.Columns["ClientID"].Visible = false; + emailsGridView.Columns["ID"].Visible = false; + emailsGridView.Columns["Body"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + } + _logger.LogInformation("Loading materials"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error loading materials"); + MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } +} diff --git a/SewingDresses/FormEmails.resx b/SewingDresses/FormEmails.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/SewingDresses/FormEmails.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/SewingDresses/FormMain.Designer.cs b/SewingDresses/FormMain.Designer.cs index f740e76..ae1aa81 100644 --- a/SewingDresses/FormMain.Designer.cs +++ b/SewingDresses/FormMain.Designer.cs @@ -33,6 +33,7 @@ materialsToolStripMenuItem = new ToolStripMenuItem(); dressesToolStripMenuItem = new ToolStripMenuItem(); clientsToolStripMenuItem = new ToolStripMenuItem(); + employeesToolStripMenuItem = new ToolStripMenuItem(); reportsToolStripMenuItem = new ToolStripMenuItem(); materialsListToolStripMenuItem = new ToolStripMenuItem(); materialsByDressesToolStripMenuItem = new ToolStripMenuItem(); @@ -42,7 +43,7 @@ createOrderButton = new Button(); givenOrderButton = new Button(); refreshOrdersButton = new Button(); - employeesToolStripMenuItem = new ToolStripMenuItem(); + emailsToolStripMenuItem = new ToolStripMenuItem(); menuStrip.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); SuspendLayout(); @@ -58,7 +59,7 @@ // // directoriesToolStripMenuItem // - directoriesToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { materialsToolStripMenuItem, dressesToolStripMenuItem, clientsToolStripMenuItem, employeesToolStripMenuItem }); + directoriesToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { materialsToolStripMenuItem, dressesToolStripMenuItem, clientsToolStripMenuItem, employeesToolStripMenuItem, emailsToolStripMenuItem }); directoriesToolStripMenuItem.Name = "directoriesToolStripMenuItem"; directoriesToolStripMenuItem.Size = new Size(75, 20); directoriesToolStripMenuItem.Text = "Directories"; @@ -84,6 +85,13 @@ clientsToolStripMenuItem.Text = "Clients"; clientsToolStripMenuItem.Click += clientsToolStripMenuItem_Click; // + // employeesToolStripMenuItem + // + employeesToolStripMenuItem.Name = "employeesToolStripMenuItem"; + employeesToolStripMenuItem.Size = new Size(180, 22); + employeesToolStripMenuItem.Text = "Employees"; + employeesToolStripMenuItem.Click += employeesToolStripMenuItem_Click; + // // reportsToolStripMenuItem // reportsToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { materialsListToolStripMenuItem, materialsByDressesToolStripMenuItem, ordersListToolStripMenuItem }); @@ -159,12 +167,12 @@ refreshOrdersButton.UseVisualStyleBackColor = true; refreshOrdersButton.Click += ButtonRef_Click; // - // employeesToolStripMenuItem + // emailsToolStripMenuItem // - employeesToolStripMenuItem.Name = "employeesToolStripMenuItem"; - employeesToolStripMenuItem.Size = new Size(180, 22); - employeesToolStripMenuItem.Text = "Employees"; - employeesToolStripMenuItem.Click += employeesToolStripMenuItem_Click; + emailsToolStripMenuItem.Name = "emailsToolStripMenuItem"; + emailsToolStripMenuItem.Size = new Size(180, 22); + emailsToolStripMenuItem.Text = "Emails"; + emailsToolStripMenuItem.Click += emailsToolStripMenuItem_Click; // // FormMain // @@ -204,5 +212,6 @@ private ToolStripMenuItem clientsToolStripMenuItem; private ToolStripMenuItem startWorkingToolStripMenuItem; private ToolStripMenuItem employeesToolStripMenuItem; + private ToolStripMenuItem emailsToolStripMenuItem; } } \ No newline at end of file diff --git a/SewingDresses/FormMain.cs b/SewingDresses/FormMain.cs index 253c5eb..6c8d942 100644 --- a/SewingDresses/FormMain.cs +++ b/SewingDresses/FormMain.cs @@ -177,5 +177,14 @@ namespace SewingDresses form.ShowDialog(); } } + + private void emailsToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormEmails)); + if (service is FormEmails form) + { + form.ShowDialog(); + } + } } } diff --git a/SewingDresses/Program.cs b/SewingDresses/Program.cs index 1ae40f4..b107036 100644 --- a/SewingDresses/Program.cs +++ b/SewingDresses/Program.cs @@ -1,6 +1,8 @@ using DressAtelierBusinessLogic.BusinessLogic; +using DressAtelierBusinessLogic.MailEmployee; using DressAtelierBusinessLogic.OfficePackage; using DressAtelierBusinessLogic.OfficePackage.Implements; +using DressAtelierContracts.BindingModels; using DressAtelierContracts.BusinessLogicContracts; using DressAtelierContracts.StorageContracts; using DressAtelierDatabaseImplement.Implements; @@ -28,6 +30,28 @@ namespace SewingDresses var services = new ServiceCollection(); ConfigureServices(services); _serviceProvider = services.BuildServiceProvider(); + + try + { + var mailSender = _serviceProvider.GetService(); + mailSender?.MailConfig(new MailConfigBindingModel + { + Login = System.Configuration.ConfigurationManager.AppSettings["MailLogin"] ?? string.Empty, + Password = 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, "Working with email error"); + } + Application.Run(_serviceProvider.GetRequiredService()); } @@ -43,6 +67,7 @@ namespace SewingDresses services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); @@ -50,7 +75,10 @@ namespace SewingDresses services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddSingleton(); services.AddTransient(); services.AddTransient(); @@ -68,7 +96,10 @@ namespace SewingDresses services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); } + private static void MailCheck(object obj) => ServiceProvider?.GetService()?.MailCheck(); + } } \ No newline at end of file diff --git a/SewingDresses/SewingDresses.csproj b/SewingDresses/SewingDresses.csproj index 2225373..ee74cab 100644 --- a/SewingDresses/SewingDresses.csproj +++ b/SewingDresses/SewingDresses.csproj @@ -59,6 +59,9 @@ + + Always + Always