diff --git a/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogics/OrderLogic.cs b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogics/OrderLogic.cs index 25722bf..a14c3e6 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogics/OrderLogic.cs @@ -72,7 +72,7 @@ namespace BlacksmithWorkShopBusinessLogic.BusinessLogics return true; } } - _logger.LogWarning($"Changing order status of order {model.Id} to {newstatus} failed"); + _logger.LogWarning("Changing order status of order {Id} to {NewStatus} failed", model.Id, newstatus); return false; } public bool CreateOrder(OrderBindingModel model) diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Implements/MessageInfoStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Implements/MessageInfoStorage.cs index 8c98b89..c63c955 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Implements/MessageInfoStorage.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatebaseImplement/Implements/MessageInfoStorage.cs @@ -8,6 +8,21 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements { public class MessageInfoStorage : IMessageInfoStorage { + public List GetFullList() + { + using var context = new BlacksmithWorkshopDatabase(); + return context.MessageInfos + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(MessageInfoSearchModel model) + { + using var context = new BlacksmithWorkshopDatabase(); + return context.MessageInfos + .Where(x => x.ClientId == model.ClientId) + .Select(x => x.GetViewModel) + .ToList(); + } public MessageInfoViewModel? GetElement(MessageInfoSearchModel model) { using var context = new BlacksmithWorkshopDatabase(); @@ -17,24 +32,6 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements } return null; } - - public List GetFilteredList(MessageInfoSearchModel model) - { - using var context = new BlacksmithWorkshopDatabase(); - return context.MessageInfos - .Where(x => x.ClientId == model.ClientId) - .Select(x => x.GetViewModel) - .ToList(); - } - - public List GetFullList() - { - using var context = new BlacksmithWorkshopDatabase(); - return context.MessageInfos - .Select(x => x.GetViewModel) - .ToList(); - } - public MessageInfoViewModel? Insert(MessageInfoBindingModel model) { using var context = new BlacksmithWorkshopDatabase(); @@ -44,10 +41,6 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements return null; } context.MessageInfos.Add(newMessage); - if (newMessage == null || context.MessageInfos.Any(x => x.MessageId.Equals(model.MessageId))) - { - return null; - } context.SaveChanges(); return newMessage.GetViewModel; } diff --git a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/DataFileSingleton.cs b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/DataFileSingleton.cs index ec51aa2..e0f03ac 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/DataFileSingleton.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/DataFileSingleton.cs @@ -10,11 +10,13 @@ namespace BlacksmithWorkshopFileImplement private readonly string ManufactureFileName = "Manufacture.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 Manufactures { 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) @@ -28,6 +30,7 @@ namespace BlacksmithWorkshopFileImplement 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, "Implementer", x => x.GetXElement); + public void SaveMessages() => SaveData(Messages, MessageFileName, "Messages", x => x.GetXElement); private DataFileSingleton() { Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; @@ -35,6 +38,7 @@ namespace BlacksmithWorkshopFileImplement 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 => MessageInfo.Create(x)!)!; } private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) { diff --git a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/MessageInfoStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/MessageInfoStorage.cs new file mode 100644 index 0000000..e95df49 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/MessageInfoStorage.cs @@ -0,0 +1,49 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.SearchModels; +using BlacksmithWorkshopContracts.StoragesContracts; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopFileImplement.Models; + +namespace BlacksmithWorkshopFileImplement.Implements +{ + public class MessageInfoStorage : IMessageInfoStorage + { + private readonly DataFileSingleton _source; + public MessageInfoStorage() + { + _source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return _source.Messages + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(MessageInfoSearchModel model) + { + return _source.Messages + .Where(x => x.ClientId == model.ClientId) + .Select(x => x.GetViewModel) + .ToList(); + } + public MessageInfoViewModel? GetElement(MessageInfoSearchModel model) + { + if (model.MessageId != null) + { + return _source.Messages.FirstOrDefault(x => x.MessageId == model.MessageId)?.GetViewModel; + } + return null; + } + public MessageInfoViewModel? Insert(MessageInfoBindingModel model) + { + var newMessage = MessageInfo.Create(model); + if (newMessage == null) + { + return null; + } + _source.Messages.Add(newMessage); + _source.SaveMessages(); + return newMessage.GetViewModel; + } + } +} \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/MessageInfo.cs b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/MessageInfo.cs new file mode 100644 index 0000000..3542396 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/MessageInfo.cs @@ -0,0 +1,66 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopDataModels.Models; +using System.Xml.Linq; + +namespace BlacksmithWorkshopFileImplement.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 ?? 0), + new XAttribute("MessageId", MessageId), + new XAttribute("SenderName", SenderName), + new XAttribute("DateDelivery", DateDelivery) + ); + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/DataListSingleton.cs b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/DataListSingleton.cs index 6625a94..de32543 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/DataListSingleton.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/DataListSingleton.cs @@ -10,6 +10,7 @@ namespace BlacksmithWorkshopListImplement public List Manufactures { 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 BlacksmithWorkshopListImplement Manufactures = new List(); Clients = new List(); Implementers = new List(); + Messages = new List(); } public static DataListSingleton GetInstance() { diff --git a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/MessageInfoStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/MessageInfoStorage.cs index 49daca1..3bc53e0 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/MessageInfoStorage.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/MessageInfoStorage.cs @@ -2,29 +2,57 @@ using BlacksmithWorkshopContracts.SearchModels; using BlacksmithWorkshopContracts.StoragesContracts; using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopListImplement.Models; namespace BlacksmithWorkshopListImplement.Implements { public class MessageInfoStorage : IMessageInfoStorage { - public MessageInfoViewModel? GetElement(MessageInfoSearchModel model) + private readonly DataListSingleton _source; + public MessageInfoStorage() { - throw new NotImplementedException(); + _source = DataListSingleton.GetInstance(); } - - public List GetFilteredList(MessageInfoSearchModel model) - { - throw new NotImplementedException(); - } - public List GetFullList() { - throw new NotImplementedException(); + 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) { - throw new NotImplementedException(); + var newMessage = MessageInfo.Create(model); + if (newMessage == null) + { + return null; + } + _source.Messages.Add(newMessage); + return newMessage.GetViewModel; } } -} +} \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/MessageInfo.cs b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/MessageInfo.cs new file mode 100644 index 0000000..1d72b96 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/MessageInfo.cs @@ -0,0 +1,41 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopDataModels.Models; + +namespace BlacksmithWorkshopListImplement.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 + }; + } +}