@RenderBody() diff --git a/ComputersShop/ComputersShopContracts/BindingModels/MailConfigBindingModel.cs b/ComputersShop/ComputersShopContracts/BindingModels/MailConfigBindingModel.cs new file mode 100644 index 0000000..adf3ecd --- /dev/null +++ b/ComputersShop/ComputersShopContracts/BindingModels/MailConfigBindingModel.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopContracts.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/ComputersShop/ComputersShopContracts/BindingModels/MailSendInfoBindingModel.cs b/ComputersShop/ComputersShopContracts/BindingModels/MailSendInfoBindingModel.cs new file mode 100644 index 0000000..f9b4566 --- /dev/null +++ b/ComputersShop/ComputersShopContracts/BindingModels/MailSendInfoBindingModel.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopContracts.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; + } +} diff --git a/ComputersShop/ComputersShopContracts/BindingModels/MessageInfoBindingModel.cs b/ComputersShop/ComputersShopContracts/BindingModels/MessageInfoBindingModel.cs new file mode 100644 index 0000000..2966e42 --- /dev/null +++ b/ComputersShop/ComputersShopContracts/BindingModels/MessageInfoBindingModel.cs @@ -0,0 +1,24 @@ +using ComputersShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopContracts.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/ComputersShop/ComputersShopContracts/BusinessLogicContracts/IMessageInfoLogic.cs b/ComputersShop/ComputersShopContracts/BusinessLogicContracts/IMessageInfoLogic.cs new file mode 100644 index 0000000..cc110be --- /dev/null +++ b/ComputersShop/ComputersShopContracts/BusinessLogicContracts/IMessageInfoLogic.cs @@ -0,0 +1,18 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.SearchModels; +using ComputersShopContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopContracts.BusinessLogicContracts +{ + public interface IMessageInfoLogic + { + List? ReadList(MessageInfoSearchModel? model); + + bool Create(MessageInfoBindingModel model); + } +} diff --git a/ComputersShop/ComputersShopContracts/SearchModels/MessageInfoSearchModel.cs b/ComputersShop/ComputersShopContracts/SearchModels/MessageInfoSearchModel.cs new file mode 100644 index 0000000..5154a94 --- /dev/null +++ b/ComputersShop/ComputersShopContracts/SearchModels/MessageInfoSearchModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopContracts.SearchModels +{ + public class MessageInfoSearchModel + { + public int? ClientId { get; set; } + + public string? MessageId { get; set; } + } +} diff --git a/ComputersShop/ComputersShopContracts/StorageContracts/IMessageInfoStorage.cs b/ComputersShop/ComputersShopContracts/StorageContracts/IMessageInfoStorage.cs new file mode 100644 index 0000000..64dfea8 --- /dev/null +++ b/ComputersShop/ComputersShopContracts/StorageContracts/IMessageInfoStorage.cs @@ -0,0 +1,22 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.SearchModels; +using ComputersShopContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopContracts.StoragesContracts +{ + public interface IMessageInfoStorage + { + List GetFullList(); + + List GetFilteredList(MessageInfoSearchModel model); + + MessageInfoViewModel? GetElement(MessageInfoSearchModel model); + + MessageInfoViewModel? Insert(MessageInfoBindingModel model); + } +} diff --git a/ComputersShop/ComputersShopContracts/ViewModels/MessageInfoViewModel.cs b/ComputersShop/ComputersShopContracts/ViewModels/MessageInfoViewModel.cs new file mode 100644 index 0000000..e9f7ce7 --- /dev/null +++ b/ComputersShop/ComputersShopContracts/ViewModels/MessageInfoViewModel.cs @@ -0,0 +1,29 @@ +using ComputersShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopContracts.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/ComputersShop/ComputersShopDataBaseImplement/ComputersShopDataBase.cs b/ComputersShop/ComputersShopDataBaseImplement/ComputersShopDataBase.cs index 47efc1b..9ede652 100644 --- a/ComputersShop/ComputersShopDataBaseImplement/ComputersShopDataBase.cs +++ b/ComputersShop/ComputersShopDataBaseImplement/ComputersShopDataBase.cs @@ -23,7 +23,8 @@ namespace ComputersShopDataBaseImplement public virtual DbSet ComputerComponents { set; get; } public virtual DbSet Orders { set; get; } public virtual DbSet Clients { set; get; } - public virtual DbSet Implementers { set; get; } + public virtual DbSet Implementers { set; get; } + public virtual DbSet Messages { set; get; } } } diff --git a/ComputersShop/ComputersShopDataBaseImplement/Implements/MessageInfoStorage.cs b/ComputersShop/ComputersShopDataBaseImplement/Implements/MessageInfoStorage.cs new file mode 100644 index 0000000..afdbfaa --- /dev/null +++ b/ComputersShop/ComputersShopDataBaseImplement/Implements/MessageInfoStorage.cs @@ -0,0 +1,60 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.SearchModels; +using ComputersShopContracts.StoragesContracts; +using ComputersShopContracts.ViewModels; +using ComputersShopDataBaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopDataBaseImplement.Implements +{ + public class MessageInfoStorage : IMessageInfoStorage + { + public MessageInfoViewModel? GetElement(MessageInfoSearchModel model) + { + if (string.IsNullOrEmpty(model.MessageId)) + { + return null; + } + using var context = new ComputersShopDataBase(); + return context.Messages + .FirstOrDefault(x => x.MessageId == model.MessageId)? + .GetViewModel; + } + + public List GetFilteredList(MessageInfoSearchModel model) + { + if (!model.ClientId.HasValue) + return new(); + using var context = new ComputersShopDataBase(); + return context.Messages + .Where(x => x.ClientId == model.ClientId) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new ComputersShopDataBase(); + return context.Messages + .Select(x => x.GetViewModel) + .ToList(); + } + + public MessageInfoViewModel? Insert(MessageInfoBindingModel model) + { + using var context = new ComputersShopDataBase(); + var newMessage = Message.Create(model); + if (newMessage == null) + { + return null; + } + context.Messages.Add(newMessage); + context.SaveChanges(); + return newMessage.GetViewModel; + } + } +} diff --git a/ComputersShop/ComputersShopDataBaseImplement/Implements/OrderStorage.cs b/ComputersShop/ComputersShopDataBaseImplement/Implements/OrderStorage.cs index 99d8477..d046d1b 100644 --- a/ComputersShop/ComputersShopDataBaseImplement/Implements/OrderStorage.cs +++ b/ComputersShop/ComputersShopDataBaseImplement/Implements/OrderStorage.cs @@ -12,80 +12,96 @@ using System.Threading.Tasks; namespace ComputersShopDataBaseImplement.Implements { - public class OrderStorage : IOrderStorage - { - public OrderViewModel? Delete(OrderBindingModel model) - { - using var context = new ComputersShopDataBase(); - var element = context.Orders - .FirstOrDefault(rec => rec.Id == model.Id); - if (element != null) - { - var deletedElement = context.Orders - .Include(x => x.Computer) + public class OrderStorage : IOrderStorage + { + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new ComputersShopDataBase(); + var element = context.Orders + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + var deletedElement = context.Orders + .Include(x => x.Computer) .Include(x => x.Client) .Include(x => x.Implementer) .FirstOrDefault(x => x.Id == model.Id) - ?.GetViewModel; - context.Orders.Remove(element); - context.SaveChanges(); - return deletedElement; - } - return null; - } + ?.GetViewModel; + context.Orders.Remove(element); + context.SaveChanges(); + return deletedElement; + } + return null; + } - public OrderViewModel? GetElement(OrderSearchModel model) - { - if (!model.Id.HasValue) - { - return null; - } + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } - using var context = new ComputersShopDataBase(); + using var context = new ComputersShopDataBase(); + if (model.ImplementerId.HasValue && model.Status.HasValue) + { + return context.Orders + .Include(x => x.Computer) + .Include(x => x.Client) + .Include(x => x.Implementer) + .FirstOrDefault(x => x.ImplementerId == model.ImplementerId && x.Status == model.Status) + ?.GetViewModel; + } + if (model.ImplementerId.HasValue) + { + return context.Orders + .Include(x => x.Computer) + .Include(x => x.Client) + .Include(x => x.Implementer) + .FirstOrDefault(x => x.ImplementerId == model.ImplementerId) + ?.GetViewModel; + } - return context.Orders - .Include(x => x.Computer) - .Include(x => x.Client) + return context.Orders + .Include(x => x.Computer) + .Include(x => x.Client) .Include(x => x.Implementer) - .FirstOrDefault(x => (model.Status == null || model.Status != null && model.Status == x.Status) && - model.ImplementerId.HasValue && x.ImplementerId == model.ImplementerId || - model.Id.HasValue && x.Id == model.Id) + .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) ?.GetViewModel; - } + } - public List GetFilteredList(OrderSearchModel model) - { - using var context = new ComputersShopDataBase(); - if (model.Id.HasValue) - { - return context.Orders - .Include(x => x.Computer) - .Include(x => x.Client) + public List GetFilteredList(OrderSearchModel model) + { + using var context = new ComputersShopDataBase(); + if (model.Id.HasValue) + { + return context.Orders + .Include(x => x.Computer) + .Include(x => x.Client) .Include(x => x.Implementer) .Where(x => x.Id == model.Id) - .Select(x => x.GetViewModel) - .ToList(); - } - else if (model.DateFrom != null && model.DateTo != null) - { - return context.Orders - .Include(x => x.Computer) - .Include(x => x.Client) + .Select(x => x.GetViewModel) + .ToList(); + } + else if (model.DateFrom != null && model.DateTo != null) + { + return context.Orders + .Include(x => x.Computer) + .Include(x => x.Client) .Include(x => x.Implementer) .Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo) - .Select(x => x.GetViewModel) - .ToList(); - } - else if (model.ClientId.HasValue) - { - return context.Orders - .Include(x => x.Computer) - .Include(x => x.Client) + .Select(x => x.GetViewModel) + .ToList(); + } + else if (model.ClientId.HasValue) + { + return context.Orders + .Include(x => x.Computer) + .Include(x => x.Client) .Include(x => x.Implementer) .Where(x => x.ClientId == model.ClientId) - .Select(x => x.GetViewModel) - .ToList(); - } + .Select(x => x.GetViewModel) + .ToList(); + } else if (model.ImplementerId.HasValue) { return context.Orders @@ -105,55 +121,55 @@ namespace ComputersShopDataBaseImplement.Implements .ToList(); } - public List GetFullList() - { - using var context = new ComputersShopDataBase(); - return context.Orders - .Include(x => x.Computer) - .Include(x => x.Client) + public List GetFullList() + { + using var context = new ComputersShopDataBase(); + return context.Orders + .Include(x => x.Computer) + .Include(x => x.Client) .Include(x => x.Implementer) .Select(x => x.GetViewModel) - .ToList(); - } + .ToList(); + } - public OrderViewModel? Insert(OrderBindingModel model) - { - var newOrder = Order.Create(model); - if (newOrder == null) - { - return null; - } + public OrderViewModel? Insert(OrderBindingModel model) + { + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } - using var context = new ComputersShopDataBase(); + using var context = new ComputersShopDataBase(); - context.Orders.Add(newOrder); - context.SaveChanges(); - return context.Orders - .Include(x => x.Computer) - .Include(x => x.Client) + context.Orders.Add(newOrder); + context.SaveChanges(); + return context.Orders + .Include(x => x.Computer) + .Include(x => x.Client) .Include(x => x.Implementer) .FirstOrDefault(x => x.Id == newOrder.Id) - ?.GetViewModel; - } + ?.GetViewModel; + } - public OrderViewModel? Update(OrderBindingModel model) - { - using var context = new ComputersShopDataBase(); + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new ComputersShopDataBase(); - var order = context.Orders.Include(x => x.Client).FirstOrDefault(x => x.Id == model.Id); + var order = context.Orders.Include(x => x.Client).FirstOrDefault(x => x.Id == model.Id); - if (order == null) - { - return null; - } - order.Update(model); - context.SaveChanges(); - return context.Orders - .Include(x => x.Computer) - .Include(x => x.Client) + if (order == null) + { + return null; + } + order.Update(model); + context.SaveChanges(); + return context.Orders + .Include(x => x.Computer) + .Include(x => x.Client) .Include(x => x.Implementer) .FirstOrDefault(x => x.Id == model.Id) - ?.GetViewModel; - } - } + ?.GetViewModel; + } + } } diff --git a/ComputersShop/ComputersShopDataBaseImplement/Models/Message.cs b/ComputersShop/ComputersShopDataBaseImplement/Models/Message.cs new file mode 100644 index 0000000..88295d9 --- /dev/null +++ b/ComputersShop/ComputersShopDataBaseImplement/Models/Message.cs @@ -0,0 +1,55 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.ViewModels; +using ComputersShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopDataBaseImplement.Models +{ + public class Message : 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.SpecifyKind(DateTime.Now, DateTimeKind.Utc); + + public string Subject { get; private set; } = string.Empty; + + public string Body { get; private set; } = string.Empty; + + public static Message? 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 = DateTime.SpecifyKind(model.DateDelivery, DateTimeKind.Utc) + }; + } + + public MessageInfoViewModel GetViewModel => new() + { + Body = Body, + Subject = Subject, + ClientId = ClientId, + MessageId = MessageId, + SenderName = SenderName, + DateDelivery = DateDelivery, + }; + } +} diff --git a/ComputersShop/ComputersShopFileImplement/DataFileSingleton.cs b/ComputersShop/ComputersShopFileImplement/DataFileSingleton.cs index d942a54..5795cf2 100644 --- a/ComputersShop/ComputersShopFileImplement/DataFileSingleton.cs +++ b/ComputersShop/ComputersShopFileImplement/DataFileSingleton.cs @@ -8,58 +8,62 @@ using System.Xml.Linq; namespace ComputersShopFileImplement { - public class DataFileSingleton - { - private static DataFileSingleton? instance; - private readonly string ComponentFileName = "Component.xml"; - private readonly string OrderFileName = "Order.xml"; - private readonly string ComputerFileName = "Computer.xml"; - private readonly string ClientFileName = "Client.xml"; + public class DataFileSingleton + { + private static DataFileSingleton? instance; + private readonly string ComponentFileName = "Component.xml"; + private readonly string OrderFileName = "Order.xml"; + private readonly string ComputerFileName = "Computer.xml"; + private readonly string ClientFileName = "Client.xml"; private readonly string ImplementerFileName = "Implementer.xml"; + private readonly string MessageFileName = "Message.xml"; public List Components { get; private set; } - public List Orders { get; private set; } - public List Computers { get; private set; } - public List Clients { get; private set; } + public List Orders { get; private set; } + public List Computers { 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) - { - instance = new DataFileSingleton(); - } - return instance; - } - public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement); - public void SaveComputers() => SaveData(Computers, ComputerFileName, "Computers", x => x.GetXElement); - public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); - public void SaveClients() => SaveData(Clients, OrderFileName, "Clients", x => x.GetXElement); + { + if (instance == null) + { + instance = new DataFileSingleton(); + } + return instance; + } + public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement); + public void SaveComputers() => SaveData(Computers, ComputerFileName, "Computers", x => x.GetXElement); + public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); + public void SaveClients() => SaveData(Clients, OrderFileName, "Clients", x => x.GetXElement); public void SaveImplementers() => SaveData(Implementers, ImplementerFileName, "Implementers", x => x.GetXElement); + public void SaveMessages() => SaveData(Messages, MessageFileName, "Messages", x => x.GetXElement); private DataFileSingleton() - { - Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; - Computers = LoadData(ComputerFileName, "Computer", x => Computer.Create(x)!)!; - Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; - Clients = LoadData(ClientFileName, "Client", x => Client.Create(x)!)!; + { + Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; + Computers = LoadData(ComputerFileName, "Computer", x => Computer.Create(x)!)!; + 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(MessageFileName, "Message", x => Message.Create(x)); } private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) - { - if (File.Exists(filename)) - { - return - XDocument.Load(filename)?.Root?.Elements(xmlNodeName)?.Select(selectFunction)?.ToList(); - } - return new List(); - } - private static void SaveData(List data, string filename, string xmlNodeName, Func selectFunction) - { - if (data != null) - { - new XDocument(new XElement(xmlNodeName, data.Select(selectFunction).ToArray())).Save(filename); - } - } - } + { + if (File.Exists(filename)) + { + return + XDocument.Load(filename)?.Root?.Elements(xmlNodeName)?.Select(selectFunction)?.ToList(); + } + return new List(); + } + private static void SaveData(List data, string filename, string xmlNodeName, Func selectFunction) + { + if (data != null) + { + new XDocument(new XElement(xmlNodeName, data.Select(selectFunction).ToArray())).Save(filename); + } + } + } } diff --git a/ComputersShop/ComputersShopFileImplement/Implements/MessageInfoStorage.cs b/ComputersShop/ComputersShopFileImplement/Implements/MessageInfoStorage.cs new file mode 100644 index 0000000..73b9afd --- /dev/null +++ b/ComputersShop/ComputersShopFileImplement/Implements/MessageInfoStorage.cs @@ -0,0 +1,58 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.SearchModels; +using ComputersShopContracts.StoragesContracts; +using ComputersShopContracts.ViewModels; +using ComputersShopFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopFileImplement.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 = Message.Create(model); + if (newMessage == null) + { + return null; + } + _source.Messages.Add(newMessage); + _source.SaveMessages(); + return newMessage.GetViewModel; + } + } +} diff --git a/ComputersShop/ComputersShopFileImplement/Models/Message.cs b/ComputersShop/ComputersShopFileImplement/Models/Message.cs new file mode 100644 index 0000000..bef34fa --- /dev/null +++ b/ComputersShop/ComputersShopFileImplement/Models/Message.cs @@ -0,0 +1,80 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.ViewModels; +using ComputersShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace ComputersShopFileImplement.Models +{ + public class Message : 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 Message? 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 Message? 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/ComputersShop/ComputersShopListImplement/DataListSingleton.cs b/ComputersShop/ComputersShopListImplement/DataListSingleton.cs index 0083412..a10d1c7 100644 --- a/ComputersShop/ComputersShopListImplement/DataListSingleton.cs +++ b/ComputersShop/ComputersShopListImplement/DataListSingleton.cs @@ -7,29 +7,31 @@ using System.Threading.Tasks; namespace ComputersShopListImplement { - public class DataListSingleton - { - private static DataListSingleton? _instance; - public List Components { get; set; } - public List Orders { get; set; } - public List Computers { get; set; } - public List Clients { get; set; } + public class DataListSingleton + { + private static DataListSingleton? _instance; + public List Components { get; set; } + public List Orders { get; set; } + public List Computers { get; set; } + public List Clients { get; set; } public List Implementers { get; set; } + public List Messages { get; set; } private DataListSingleton() - { - Components = new List(); - Orders = new List(); - Computers = new List(); - Clients = new List(); + { + Components = new List(); + Orders = new List(); + Computers = new List(); + Clients = new List(); Implementers = new List(); + Messages = new List(); } - public static DataListSingleton GetInstance() - { - if (_instance == null) - { - _instance = new DataListSingleton(); - } - return _instance; - } - } + public static DataListSingleton GetInstance() + { + if (_instance == null) + { + _instance = new DataListSingleton(); + } + return _instance; + } + } } diff --git a/ComputersShop/ComputersShopListImplement/Implements/MessageInfoStorage.cs b/ComputersShop/ComputersShopListImplement/Implements/MessageInfoStorage.cs new file mode 100644 index 0000000..bfa8252 --- /dev/null +++ b/ComputersShop/ComputersShopListImplement/Implements/MessageInfoStorage.cs @@ -0,0 +1,66 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.SearchModels; +using ComputersShopContracts.StoragesContracts; +using ComputersShopContracts.ViewModels; +using ComputersShopListImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopListImplement.Implements +{ + 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.Equals(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 = Message.Create(model); + if (newMessage == null) + { + return null; + } + _source.Messages.Add(newMessage); + return newMessage.GetViewModel; + } + } +} diff --git a/ComputersShop/ComputersShopListImplement/Models/Message.cs b/ComputersShop/ComputersShopListImplement/Models/Message.cs new file mode 100644 index 0000000..da24fe7 --- /dev/null +++ b/ComputersShop/ComputersShopListImplement/Models/Message.cs @@ -0,0 +1,53 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.ViewModels; +using ComputersShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopListImplement.Models +{ + public class Message : 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 Message? 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/ComputersShop/ComputersShopRestAPI/appsettings.json b/ComputersShop/ComputersShopRestAPI/appsettings.json index 10f68b8..04d60c9 100644 --- a/ComputersShop/ComputersShopRestAPI/appsettings.json +++ b/ComputersShop/ComputersShopRestAPI/appsettings.json @@ -5,5 +5,12 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + + "SmtpClientHost": "smtp.gmail.com", + "SmtpClientPort": "587", + "PopHost": "pop.gmail.com", + "PopPort": "995", + "MailLogin": "rpplabs24@gmail.com", + "MailPassword": "qqwezxc2024" } diff --git a/ComputersShop/ComputersShopView/App.config b/ComputersShop/ComputersShopView/App.config new file mode 100644 index 0000000..afed655 --- /dev/null +++ b/ComputersShop/ComputersShopView/App.config @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/ComputersShop/ComputersShopView/FormMails.Designer.cs b/ComputersShop/ComputersShopView/FormMails.Designer.cs new file mode 100644 index 0000000..adfc592 --- /dev/null +++ b/ComputersShop/ComputersShopView/FormMails.Designer.cs @@ -0,0 +1,65 @@ +namespace ComputersShopView +{ + partial class FormMails + { + /// + /// 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.Margin = new Padding(3, 2, 3, 2); + dataGridView.Name = "dataGridView"; + dataGridView.RowHeadersWidth = 51; + dataGridView.RowTemplate.Height = 29; + dataGridView.Size = new Size(947, 576); + dataGridView.TabIndex = 0; + // + // FormMails + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(947, 576); + Controls.Add(dataGridView); + Margin = new Padding(3, 2, 3, 2); + Name = "FormMails"; + Text = "Письма"; + Load += FormMails_Load; + ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); + ResumeLayout(false); + } + + #endregion + + private DataGridView dataGridView; + } +} \ No newline at end of file diff --git a/ComputersShop/ComputersShopView/FormMails.cs b/ComputersShop/ComputersShopView/FormMails.cs new file mode 100644 index 0000000..4baad03 --- /dev/null +++ b/ComputersShop/ComputersShopView/FormMails.cs @@ -0,0 +1,49 @@ +using ComputersShopContracts.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 ComputersShopView +{ + public partial class FormMails : Form + { + private readonly ILogger _logger; + private readonly IMessageInfoLogic _logic; + + public FormMails(ILogger logger, IMessageInfoLogic logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + } + + private void FormMails_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/ComputersShop/ComputersShopView/FormMails.resx b/ComputersShop/ComputersShopView/FormMails.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/ComputersShop/ComputersShopView/FormMails.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/ComputersShop/ComputersShopView/Program.cs b/ComputersShop/ComputersShopView/Program.cs index 982586e..e3edee7 100644 --- a/ComputersShop/ComputersShopView/Program.cs +++ b/ComputersShop/ComputersShopView/Program.cs @@ -7,65 +7,92 @@ using ComputersShopDataBaseImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; +using ComputersShopBusinessLogic.MailWorker; +using ComputersShopContracts.BindingModels; namespace ComputersShopView { internal static class Program - { - private static ServiceProvider? _serviceProvider; - public static ServiceProvider? ServiceProvider => _serviceProvider; - /// - /// The main entry point for the application. - /// - [STAThread] - static void Main() - { - // To customize application configuration such as set high DPI settings or default font, - // see https://aka.ms/applicationconfiguration. - ApplicationConfiguration.Initialize(); - var services = new ServiceCollection(); - ConfigureServices(services); - _serviceProvider = services.BuildServiceProvider(); - Application.Run(_serviceProvider.GetRequiredService()); - } + { + private static ServiceProvider? _serviceProvider; + public static ServiceProvider? ServiceProvider => _serviceProvider; + /// + /// The main entry point for the application. + /// + [STAThread] + static void Main() + { + // To customize application configuration such as set high DPI settings or default font, + // see https://aka.ms/applicationconfiguration. + ApplicationConfiguration.Initialize(); + var services = new ServiceCollection(); + ConfigureServices(services); + _serviceProvider = services.BuildServiceProvider(); + 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"]) + }); - private static void ConfigureServices(ServiceCollection services) - { - services.AddLogging(option => - { - option.SetMinimumLevel(LogLevel.Information); - option.AddNLog("nlog.config"); - }); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); + var timer = new System.Threading.Timer(new TimerCallback(MailCheck!), null, 0, 100000); + } + catch (Exception ex) + { + var logger = _serviceProvider.GetService(); + logger?.LogError(ex, "Error"); + } + Application.Run(_serviceProvider.GetRequiredService()); + } - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); + private static void ConfigureServices(ServiceCollection services) + { + services.AddLogging(option => + { + option.SetMinimumLevel(LogLevel.Information); + option.AddNLog("nlog.config"); + }); + 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.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.AddTransient(); - } - } + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddSingleton(); + + 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(); + } + private static void MailCheck(object obj) => ServiceProvider?.GetService()?.MailCheck(); + } } \ No newline at end of file diff --git a/ComputersShop/СomputersShopDataModels/Models/IMessageInfoModel.cs b/ComputersShop/СomputersShopDataModels/Models/IMessageInfoModel.cs new file mode 100644 index 0000000..5985ff7 --- /dev/null +++ b/ComputersShop/СomputersShopDataModels/Models/IMessageInfoModel.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopDataModels.Models +{ + public interface IMessageInfoModel + { + string MessageId { get; } + + int? ClientId { get; } + + string SenderName { get; } + + DateTime DateDelivery { get; } + + string Subject { get; } + + string Body { get; } + } +}