diff --git a/Pizzeria/PizzeriaListImplement/DataListSingleton.cs b/Pizzeria/PizzeriaListImplement/DataListSingleton.cs index 24824be..4859df6 100644 --- a/Pizzeria/PizzeriaListImplement/DataListSingleton.cs +++ b/Pizzeria/PizzeriaListImplement/DataListSingleton.cs @@ -9,11 +9,13 @@ namespace PizzeriaListImplement public List Components { get; set; } public List Orders { get; set; } public List Pizzas { get; set; } + public List Messages { get; set; } private DataListSingleton() { Components = new List(); Orders = new List(); Pizzas = 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..ba5c84a --- /dev/null +++ b/Pizzeria/PizzeriaListImplement/Implements/MessageInfoStorage.cs @@ -0,0 +1,72 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.SearchModels; +using PizzeriaContracts.StoragesContracts; +using PizzeriaContracts.ViewModels; +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 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 = 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..123817f --- /dev/null +++ b/Pizzeria/PizzeriaListImplement/Models/MessageInfo.cs @@ -0,0 +1,49 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.ViewModels; +using PizzeriaDataModels.Models; + +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, + }; + } +} diff --git a/Pizzeria/PizzeriaShopFileImplement/DataFileSingleton.cs b/Pizzeria/PizzeriaShopFileImplement/DataFileSingleton.cs index d09c25a..b96a519 100644 --- a/Pizzeria/PizzeriaShopFileImplement/DataFileSingleton.cs +++ b/Pizzeria/PizzeriaShopFileImplement/DataFileSingleton.cs @@ -9,9 +9,11 @@ namespace PizzeriaFileImplement private readonly string ComponentFileName = "Component.xml"; private readonly string OrderFileName = "Order.xml"; private readonly string PizzaFileName = "Pizza.xml"; + private readonly string MessageFileName = "Message.xml"; public List Components { get; private set; } public List Orders { get; private set; } public List Pizzas { get; private set; } + public List Messages { get; private set; } public static DataFileSingleton GetInstance() { if (instance == null) @@ -23,6 +25,7 @@ namespace PizzeriaFileImplement public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement); public void SavePizzas() => SaveData(Pizzas, PizzaFileName, "Pizzas", x => x.GetXElement); public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); + public void SaveMessages() => SaveData(Messages, MessageFileName, "Messages", x => x.GetXElement); private DataFileSingleton() { Components = LoadData(ComponentFileName, "Component", x => @@ -30,6 +33,7 @@ namespace PizzeriaFileImplement Pizzas = LoadData(PizzaFileName, "Pizza", x => Pizza.Create(x)!)!; Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; + Messages = LoadData(MessageFileName, "Message", x => MessageInfo.Create(x)!)!; } private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) { diff --git a/Pizzeria/PizzeriaShopFileImplement/Implements/MessageInfoStorage.cs b/Pizzeria/PizzeriaShopFileImplement/Implements/MessageInfoStorage.cs new file mode 100644 index 0000000..ce7318b --- /dev/null +++ b/Pizzeria/PizzeriaShopFileImplement/Implements/MessageInfoStorage.cs @@ -0,0 +1,51 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.SearchModels; +using PizzeriaContracts.StoragesContracts; +using PizzeriaContracts.ViewModels; + +namespace PizzeriaFileImplement.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 = MessageInfo.Create(model); + if (newMessage == null) + { + return null; + } + + _source.Messages.Add(newMessage); + _source.SaveMessages(); + + return newMessage.GetViewModel; + } + } +} diff --git a/Pizzeria/PizzeriaShopFileImplement/models/MessageInfo.cs b/Pizzeria/PizzeriaShopFileImplement/models/MessageInfo.cs new file mode 100644 index 0000000..50252b7 --- /dev/null +++ b/Pizzeria/PizzeriaShopFileImplement/models/MessageInfo.cs @@ -0,0 +1,77 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.ViewModels; +using PizzeriaDataModels.Models; +using System.Xml.Linq; + +namespace PizzeriaFileImplement.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 static MessageInfo? 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) + ); + } +}