From 248ba0e38d3e29dd486c7686459abfe18eb9b03b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D1=8F=D1=87=D0=B5=D1=81=D0=BB=D0=B0=D0=B2=20=D0=98?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=BE=D0=B2?= Date: Tue, 2 Apr 2024 17:35:50 +0400 Subject: [PATCH] added --- .../DataFileSingleton.cs | 4 + .../Implements/ImplementerStorage.cs | 78 ++++--------------- .../Models/{Message.cs => MessageInfo.cs} | 42 +++------- .../DataListSingleton.cs | 2 + .../Implements/MessageInfoStorage.cs | 68 ++++++++++++++++ .../Models/MessageInfo.cs | 53 +++++++++++++ 6 files changed, 155 insertions(+), 92 deletions(-) rename Pizzeria/PizzeriaFileImplement/Models/{Message.cs => MessageInfo.cs} (75%) create mode 100644 Pizzeria/PizzeriaListImplement/Implements/MessageInfoStorage.cs create mode 100644 Pizzeria/PizzeriaListImplement/Models/MessageInfo.cs diff --git a/Pizzeria/PizzeriaFileImplement/DataFileSingleton.cs b/Pizzeria/PizzeriaFileImplement/DataFileSingleton.cs index 752eb68..459849d 100644 --- a/Pizzeria/PizzeriaFileImplement/DataFileSingleton.cs +++ b/Pizzeria/PizzeriaFileImplement/DataFileSingleton.cs @@ -16,11 +16,13 @@ namespace PizzeriaFileImplement private readonly string PizzaFileName = "Pizza.xml"; private readonly string ClientFileName = "Client.xml"; private readonly string ImplementerFileName = "Implementer.xml"; + private readonly string MessageInfoFileName = "MessageInfo.xml"; public List Components { get; private set; } public List Orders { get; private set; } public List Pizzas { 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() @@ -37,6 +39,7 @@ namespace PizzeriaFileImplement public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); public void SaveClients() => SaveData(Clients, ClientFileName, "Clients", x => x.GetXElement); public void SaveImplementers() => SaveData(Implementers, ImplementerFileName, "Implementers", x => x.GetXElement); + public void SaveMessages() => SaveData(Orders, ImplementerFileName, "Messages", x => x.GetXElement); private DataFileSingleton() { @@ -45,6 +48,7 @@ namespace PizzeriaFileImplement 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(MessageInfoFileName, "MessageInfo", x => MessageInfo.Create(x)!)!; } private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) diff --git a/Pizzeria/PizzeriaFileImplement/Implements/ImplementerStorage.cs b/Pizzeria/PizzeriaFileImplement/Implements/ImplementerStorage.cs index f5a472a..73fd311 100644 --- a/Pizzeria/PizzeriaFileImplement/Implements/ImplementerStorage.cs +++ b/Pizzeria/PizzeriaFileImplement/Implements/ImplementerStorage.cs @@ -6,89 +6,43 @@ using PizzeriaFileImplement.Models; namespace PizzeriaFileImplement.Implements { - public class ImplementerStorage : IImplementerStorage + public class MessageInfoStorage : IMessageInfoStorage { private readonly DataFileSingleton _source; - public ImplementerStorage() + public MessageInfoStorage() { _source = DataFileSingleton.GetInstance(); } - public List GetFullList() + public List GetFullList() { - return _source.Implementers.Select(x => x.GetViewModel).ToList(); + return _source.Messages.Select(x => x.GetViewModel).ToList(); } - public List GetFilteredList(ImplementerSearchModel model) + public List GetFilteredList(MessageInfoSearchModel model) { - if (model == null) - { - return new(); - } - if (model.Id.HasValue) - { - var res = GetElement(model); - return res != null ? new() { res } : new(); - } - if (model.ImplementerFIO != null) - { - return _source.Implementers - .Where(x => x.ImplementerFIO.Equals(model.ImplementerFIO)) - .Select(x => x.GetViewModel) - .ToList(); - } - return new(); + return _source.Messages.Where(x => x.ClientId == model.ClientId).Select(x => x.GetViewModel).ToList(); } - public ImplementerViewModel? GetElement(ImplementerSearchModel model) + public MessageInfoViewModel? GetElement(MessageInfoSearchModel model) { - if (model.Id.HasValue) + if (model.MessageId != null) { - return _source.Implementers.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; - } - if (model.ImplementerFIO != null && model.Password != null) - { - return _source.Implementers.FirstOrDefault(x => x.ImplementerFIO.Equals(model.ImplementerFIO) && x.Password.Equals(model.Password))?.GetViewModel; - } - if (model.ImplementerFIO != null) - { - return _source.Implementers.FirstOrDefault(x => x.ImplementerFIO.Equals(model.ImplementerFIO))?.GetViewModel; + return _source.Messages.FirstOrDefault(x => x.MessageId == model.MessageId)?.GetViewModel; } return null; } - public ImplementerViewModel? Insert(ImplementerBindingModel model) + public MessageInfoViewModel? Insert(MessageInfoBindingModel model) { - model.Id = _source.Implementers.Count > 0 ? _source.Implementers.Max(x => x.Id) + 1 : 1; - var res = Implementer.Create(model); - if (res != null) + var newMessage = MessageInfo.Create(model); + if (newMessage == null) { - _source.Implementers.Add(res); - _source.SaveImplementers(); + return null; } - return res?.GetViewModel; - } - - public ImplementerViewModel? Update(ImplementerBindingModel model) - { - var res = _source.Implementers.FirstOrDefault(x => x.Id == model.Id); - if (res != null) - { - res.Update(model); - _source.SaveImplementers(); - } - return res?.GetViewModel; - } - - public ImplementerViewModel? Delete(ImplementerBindingModel model) - { - var res = _source.Implementers.FirstOrDefault(x => x.Id == model.Id); - if (res != null) - { - _source.Implementers.Remove(res); - _source.SaveImplementers(); - } - return res?.GetViewModel; + _source.Messages.Add(newMessage); + _source.SaveMessages(); + return newMessage.GetViewModel; } } } diff --git a/Pizzeria/PizzeriaFileImplement/Models/Message.cs b/Pizzeria/PizzeriaFileImplement/Models/MessageInfo.cs similarity index 75% rename from Pizzeria/PizzeriaFileImplement/Models/Message.cs rename to Pizzeria/PizzeriaFileImplement/Models/MessageInfo.cs index ac49aca..db302eb 100644 --- a/Pizzeria/PizzeriaFileImplement/Models/Message.cs +++ b/Pizzeria/PizzeriaFileImplement/Models/MessageInfo.cs @@ -10,7 +10,7 @@ using System.Xml.Linq; namespace PizzeriaFileImplement.Models { - public class Message : IMessageInfoModel + public class MessageInfo : IMessageInfoModel { public string MessageId { get; private set; } = string.Empty; @@ -24,11 +24,7 @@ namespace PizzeriaFileImplement.Models public string Body { get; private set; } = string.Empty; - public bool HasRead { get; private set; } - - public string? Reply { get; private set; } - - public static Message? Create(MessageInfoBindingModel model) + public static MessageInfo? Create(MessageInfoBindingModel model) { if (model == null) { @@ -38,14 +34,14 @@ namespace PizzeriaFileImplement.Models { Body = model.Body, Subject = model.Subject, - DateDelivery = model.DateDelivery, - SenderName = model.SenderName, ClientId = model.ClientId, - MessageId = model.MessageId + MessageId = model.MessageId, + SenderName = model.SenderName, + DateDelivery = model.DateDelivery, }; } - public static Message? Create(XElement element) + public static MessageInfo? Create(XElement element) { if (element == null) { @@ -55,40 +51,26 @@ namespace PizzeriaFileImplement.Models { 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, + SenderName = element.Attribute("SenderName")!.Value, + DateDelivery = Convert.ToDateTime(element.Attribute("DateDelivery")!.Value), }; } - public void Update(MessageInfoBindingModel model) - { - if (model == null) - { - return; - } - Reply = model.Reply; - HasRead = model.HasRead; - } - public MessageInfoViewModel GetViewModel => new() { Body = Body, Subject = Subject, - Reply = Reply, - HasRead = HasRead, - DateDelivery = DateDelivery, - SenderName = SenderName, ClientId = ClientId, - MessageId = MessageId + MessageId = MessageId, + SenderName = SenderName, + DateDelivery = DateDelivery, }; public XElement GetXElement => new("MessageInfo", - new XAttribute("Subject", Subject), new XAttribute("Body", Body), - new XAttribute("Reply", Reply), - new XAttribute("HasRead", HasRead), + new XAttribute("Subject", Subject), new XAttribute("ClientId", ClientId), new XAttribute("MessageId", MessageId), new XAttribute("SenderName", SenderName), diff --git a/Pizzeria/PizzeriaListImplement/DataListSingleton.cs b/Pizzeria/PizzeriaListImplement/DataListSingleton.cs index 781f05b..fbe60f3 100644 --- a/Pizzeria/PizzeriaListImplement/DataListSingleton.cs +++ b/Pizzeria/PizzeriaListImplement/DataListSingleton.cs @@ -15,6 +15,7 @@ namespace PizzeriaListImplement public List Pizzas { get; set; } public List Clients { get; set; } public List Implementers { get; set; } + public List Messages { get; set; } private DataListSingleton() { @@ -23,6 +24,7 @@ namespace PizzeriaListImplement Pizzas = new List(); Clients = new List(); Implementers = new List(); + Messages = new List(); } public static DataListSingleton GetInstance() diff --git a/Pizzeria/PizzeriaListImplement/Implements/MessageInfoStorage.cs b/Pizzeria/PizzeriaListImplement/Implements/MessageInfoStorage.cs new file mode 100644 index 0000000..5c84f5d --- /dev/null +++ b/Pizzeria/PizzeriaListImplement/Implements/MessageInfoStorage.cs @@ -0,0 +1,68 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.SearchModels; +using PizzeriaContracts.StoragesContracts; +using PizzeriaContracts.ViewModels; +using PizzeriaListImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PizzeriaListImplement.Implements +{ + public class MessageInfoStorage : IMessageInfoStorage + { + private readonly DataListSingleton _source; + public MessageInfoStorage() + { + _source = DataListSingleton.GetInstance(); + } + + public List GetFullList() + { + List result = new(); + foreach (var item in _source.Messages) + { + result.Add(item.GetViewModel); + } + return result; + } + + public List GetFilteredList(MessageInfoSearchModel model) + { + List result = new(); + foreach (var item in _source.Messages) + { + if (item.ClientId.HasValue && item.ClientId == model.ClientId) + { + result.Add(item.GetViewModel); + } + } + return result; + } + + public MessageInfoViewModel? GetElement(MessageInfoSearchModel model) + { + foreach (var message in _source.Messages) + { + if (model.MessageId != null && model.MessageId.Equals(message.MessageId)) + { + return message.GetViewModel; + } + } + return null; + } + + public MessageInfoViewModel? Insert(MessageInfoBindingModel model) + { + var newMessage = MessageInfo.Create(model); + if (newMessage == null) + { + return null; + } + _source.Messages.Add(newMessage); + return newMessage.GetViewModel; + } + } +} diff --git a/Pizzeria/PizzeriaListImplement/Models/MessageInfo.cs b/Pizzeria/PizzeriaListImplement/Models/MessageInfo.cs new file mode 100644 index 0000000..b95d32e --- /dev/null +++ b/Pizzeria/PizzeriaListImplement/Models/MessageInfo.cs @@ -0,0 +1,53 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.ViewModels; +using PizzeriaDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PizzeriaListImplement.Models +{ + public class MessageInfo : IMessageInfoModel + { + public string MessageId { get; private set; } = string.Empty; + + public int? ClientId { get; private set; } + + public string SenderName { get; private set; } = string.Empty; + + public DateTime DateDelivery { get; private set; } = DateTime.Now; + + public string Subject { get; private set; } = string.Empty; + + public string Body { get; private set; } = string.Empty; + + public static MessageInfo? Create(MessageInfoBindingModel model) + { + if (model == null) + { + return null; + } + return new() + { + Body = model.Body, + Subject = model.Subject, + ClientId = model.ClientId, + MessageId = model.MessageId, + SenderName = model.SenderName, + DateDelivery = model.DateDelivery, + }; + } + + public MessageInfoViewModel GetViewModel => new() + { + Body = Body, + Subject = Subject, + ClientId = ClientId, + MessageId = MessageId, + SenderName = SenderName, + DateDelivery = DateDelivery, + }; + } +}