From 46e95496f3271f0779acadc618277b104641788a Mon Sep 17 00:00:00 2001 From: artiogf Date: Wed, 14 Jun 2023 22:53:13 +0400 Subject: [PATCH] Lab6-7_Main --- .../BusinessLogics/ClientLogic.cs | 4 + .../Views/Home/Enter.cshtml | 2 +- .../Views/Home/Register.cshtml | 2 +- .../DataFileSingleton.cs | 4 + .../Implements/MessageInfoStorage.cs | 66 ++++++++++++++ .../Models/Message.cs | 87 +++++++++++++++++++ .../DataListSingleton.cs | 2 + .../Implements/MessageInfoStorage.cs | 81 +++++++++++++++++ .../Models/Message.cs | 58 +++++++++++++ 9 files changed, 304 insertions(+), 2 deletions(-) create mode 100644 CarpentryWorkshopFileImplement/Implements/MessageInfoStorage.cs create mode 100644 CarpentryWorkshopFileImplement/Models/Message.cs create mode 100644 CarpentryWorkshopListImplement/Implements/MessageInfoStorage.cs create mode 100644 CarpentryWorkshopListImplement/Models/Message.cs diff --git a/CarpentryWorkshopBusinessLogic/BusinessLogics/ClientLogic.cs b/CarpentryWorkshopBusinessLogic/BusinessLogics/ClientLogic.cs index cc71555..7b78e6a 100644 --- a/CarpentryWorkshopBusinessLogic/BusinessLogics/ClientLogic.cs +++ b/CarpentryWorkshopBusinessLogic/BusinessLogics/ClientLogic.cs @@ -114,6 +114,10 @@ namespace CarpentryWorkshopBusinessLogic.BusinessLogics { throw new ArgumentException("Неправильно введенный email", nameof(model.Email)); } + if (!Regex.IsMatch(model.Password, @"^^((\w+\d+\W+)|(\w+\W+\d+)|(\d+\w+\W+)|(\d+\W+\w+)|(\W+\w+\d+)|(\W+\d+\w+))[\w\d\W]*$", RegexOptions.IgnoreCase) || model.Password.Length < 10 || model.Password.Length > 50) + { + throw new ArgumentException("Неправильно введенный пароль", nameof(model.Password)); + } _logger.LogInformation("Client. ClientFIO:{ClientFIO}.Email:{Email}.Id:{Id}", model.ClientFIO, model.Email, model.Id); var element = _clientStorage.GetElement(new ClientSearchModel { diff --git a/CarpentryWorkshopClientApp/Views/Home/Enter.cshtml b/CarpentryWorkshopClientApp/Views/Home/Enter.cshtml index bca6d69..c2cde0b 100644 --- a/CarpentryWorkshopClientApp/Views/Home/Enter.cshtml +++ b/CarpentryWorkshopClientApp/Views/Home/Enter.cshtml @@ -12,7 +12,7 @@
Пароль:
-
+
diff --git a/CarpentryWorkshopClientApp/Views/Home/Register.cshtml b/CarpentryWorkshopClientApp/Views/Home/Register.cshtml index 398b516..49b221f 100644 --- a/CarpentryWorkshopClientApp/Views/Home/Register.cshtml +++ b/CarpentryWorkshopClientApp/Views/Home/Register.cshtml @@ -12,7 +12,7 @@
Пароль:
-
+
ФИО:
diff --git a/CarpentryWorkshopFileImplement/DataFileSingleton.cs b/CarpentryWorkshopFileImplement/DataFileSingleton.cs index 0894230..eb6294b 100644 --- a/CarpentryWorkshopFileImplement/DataFileSingleton.cs +++ b/CarpentryWorkshopFileImplement/DataFileSingleton.cs @@ -11,6 +11,8 @@ namespace CarpentryWorkshopFileImplement private readonly string WoodFileName = "Wood.xml"; private readonly string ClientFileName = "Client.xml"; private readonly string ImplementerFileName = "Implementer.xml"; + private readonly string MessageFileName = "Message.xml"; + public List Messages { get; private set; } public List Components { get; private set; } public List Orders { get; private set; } public List Woods { get; private set; } @@ -28,6 +30,7 @@ namespace CarpentryWorkshopFileImplement public void SaveWoods() => SaveData(Woods, WoodFileName, "Woods", x => x.GetXElement); public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); public void SaveClients() => SaveData(Clients, ClientFileName, "Clients", x => x.GetXElement); + public void SaveMessages() => SaveData(Messages, MessageFileName, "Messages", x => x.GetXElement); public void SaveImplementers() => SaveData(Implementers, ImplementerFileName, "Implementers", x => x.GetXElement); private DataFileSingleton() @@ -37,6 +40,7 @@ namespace CarpentryWorkshopFileImplement 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) { diff --git a/CarpentryWorkshopFileImplement/Implements/MessageInfoStorage.cs b/CarpentryWorkshopFileImplement/Implements/MessageInfoStorage.cs new file mode 100644 index 0000000..21d4518 --- /dev/null +++ b/CarpentryWorkshopFileImplement/Implements/MessageInfoStorage.cs @@ -0,0 +1,66 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.SearchModels; +using CarpentryWorkshopContracts.StoragesContracts; +using CarpentryWorkshopContracts.ViewModels; +using CarpentryWorkshopFileImplement.Models; + +namespace CarpentryWorkshopFileImplement.Implements +{ + public class MessageInfoStorage : IMessageInfoStorage + { + private readonly DataFileSingleton source; + public MessageInfoStorage() + { + source = DataFileSingleton.GetInstance(); + } + public MessageInfoViewModel? GetElement(MessageInfoSearchModel model) + { + if (model.MessageId == null) + return null; + return source.Messages.FirstOrDefault(x => + x.MessageId == model.MessageId)?.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 = Message.Create(model); + if (newMessage == null) + { + return null; + } + source.Messages.Add(newMessage); + source.SaveMessages(); + return newMessage.GetViewModel; + } + public MessageInfoViewModel? Update(MessageInfoBindingModel model) + { + var component = source.Messages.FirstOrDefault(x => x.MessageId == model.MessageId); + if (component == null) + { + return null; + } + component.Update(model); + source.SaveMessages(); + return component.GetViewModel; + } + } +} diff --git a/CarpentryWorkshopFileImplement/Models/Message.cs b/CarpentryWorkshopFileImplement/Models/Message.cs new file mode 100644 index 0000000..7c5271b --- /dev/null +++ b/CarpentryWorkshopFileImplement/Models/Message.cs @@ -0,0 +1,87 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.ViewModels; +using CarpentryWorkshopDataModels.Models; +using System.Xml.Linq; + +namespace CarpentryWorkshopFileImplement.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; } + + 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, + DateDelivery = model.DateDelivery, + SenderName = model.SenderName, + ClientId = model.ClientId, + MessageId = model.MessageId + }; + } + + public static Message? Create(XElement element) + { + if (element == null) + { + return null; + } + return new() + { + Body = element.Attribute("Body")!.Value, + Subject = element.Attribute("Subject")!.Value, + DateDelivery = Convert.ToDateTime(element.Attribute("DateDelivery")!.Value), + SenderName = element.Attribute("SenderName")!.Value, + ClientId = Convert.ToInt32(element.Attribute("ClientId")!.Value), + MessageId = element.Attribute("MessageId")!.Value, + }; + } + + internal void Update(MessageInfoBindingModel model) + { + if (model == null) + { + return; + } + SenderName = model.SenderName; + DateDelivery = model.DateDelivery; + Subject = model.Subject; + Body = model.Body; + } + + public MessageInfoViewModel GetViewModel => new() + { + Body = Body, + Subject = Subject, + DateDelivery = DateDelivery, + SenderName = SenderName, + ClientId = ClientId, + MessageId = MessageId + }; + + public XElement GetXElement => new("MessageInfo", + new XAttribute("Subject", Subject), + new XAttribute("Body", Body), + new XAttribute("ClientId", ClientId), + new XAttribute("MessageId", MessageId), + new XAttribute("SenderName", SenderName), + new XAttribute("DateDelivery", DateDelivery) + ); + } +} diff --git a/CarpentryWorkshopListImplement/DataListSingleton.cs b/CarpentryWorkshopListImplement/DataListSingleton.cs index 496dc57..38a0e07 100644 --- a/CarpentryWorkshopListImplement/DataListSingleton.cs +++ b/CarpentryWorkshopListImplement/DataListSingleton.cs @@ -10,6 +10,7 @@ namespace CarpentryWorkshopListImplement public List Woods { get; set; } public List Clients { get; set; } public List Implementers { get; set; } + public List Messages { get; set; } private DataListSingleton() { Components = new List(); @@ -17,6 +18,7 @@ namespace CarpentryWorkshopListImplement Woods = new List(); Clients = new List(); Implementers = new List(); + Messages = new List(); } public static DataListSingleton GetInstance() { diff --git a/CarpentryWorkshopListImplement/Implements/MessageInfoStorage.cs b/CarpentryWorkshopListImplement/Implements/MessageInfoStorage.cs new file mode 100644 index 0000000..8371fee --- /dev/null +++ b/CarpentryWorkshopListImplement/Implements/MessageInfoStorage.cs @@ -0,0 +1,81 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.SearchModels; +using CarpentryWorkshopContracts.StoragesContracts; +using CarpentryWorkshopContracts.ViewModels; +using CarpentryWorkshopListImplement.Models; + +namespace CarpentryWorkshopListImplement.Implements +{ + internal class MessageInfoStorage : IMessageInfoStorage + { + private readonly DataListSingleton _source; + public MessageInfoStorage() + { + _source = DataListSingleton.GetInstance(); + } + public MessageInfoViewModel? GetElement(MessageInfoSearchModel model) + { + if (model.MessageId == null) + { + return null; + } + foreach (var message in _source.Messages) + { + if (model.MessageId.Equals(message.MessageId)) + return message.GetViewModel; + } + return null; + } + + public List GetFilteredList(MessageInfoSearchModel model) + { + if (!model.ClientId.HasValue) + { + return new(); + } + var result = new List(); + foreach (var item in _source.Messages) + { + if (item.ClientId == model.ClientId) + { + result.Add(item.GetViewModel); + } + } + return result; + } + + public List GetFullList() + { + var result = new List(); + 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; + } + public MessageInfoViewModel? Update(MessageInfoBindingModel model) + { + foreach (var massage in _source.Messages) + { + if (massage.MessageId == model.MessageId) + { + massage.Update(model); + + return massage.GetViewModel; + } + } + return null; + } + } +} diff --git a/CarpentryWorkshopListImplement/Models/Message.cs b/CarpentryWorkshopListImplement/Models/Message.cs new file mode 100644 index 0000000..a785872 --- /dev/null +++ b/CarpentryWorkshopListImplement/Models/Message.cs @@ -0,0 +1,58 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.ViewModels; +using CarpentryWorkshopDataModels.Models; + +namespace CarpentryWorkshopListImplement.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, + DateDelivery = model.DateDelivery, + SenderName = model.SenderName, + ClientId = model.ClientId, + MessageId = model.MessageId + }; + } + public void Update(MessageInfoBindingModel model) + { + if (model == null) + { + return; + } + SenderName = model.SenderName; + DateDelivery = model.DateDelivery; + Subject = model.Subject; + Body = model.Body; + } + public MessageInfoViewModel GetViewModel => new() + { + Body = Body, + Subject = Subject, + DateDelivery = DateDelivery, + SenderName = SenderName, + ClientId = ClientId, + MessageId = MessageId + }; + } +}