diff --git a/ComputerShopBusinessLogic/BusinessLogics/OrderLogic.cs b/ComputerShopBusinessLogic/BusinessLogics/OrderLogic.cs index 3c1adc2..d8696a0 100644 --- a/ComputerShopBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/ComputerShopBusinessLogic/BusinessLogics/OrderLogic.cs @@ -44,25 +44,48 @@ namespace ComputerShopBusinessLogic.BusinessLogics public bool StatusUpdate(OrderBindingModel model, OrderStatus newStatus) { - CheckModel(model); - if (model.Status + 1 != newStatus) + var viewModel = _orderStorage.GetElement(new OrderSearchModel + { + Id = model.Id + }); + + if (viewModel == null) + { + _logger.LogWarning("Order model not found"); + return false; + } + + OrderBindingModel model1 = new OrderBindingModel + { + Id = viewModel.Id, + ComputerId = viewModel.ComputerId, + Status = viewModel.Status, + DateCreate = viewModel.DateCreate, + DateImplement = viewModel.DateImplement, + Count = viewModel.Count, + Sum = viewModel.Sum + }; + if (model.ImplementerId.HasValue) + { + model1.ImplementerId = model.ImplementerId; + } + + CheckModel(model1, false); + if (model1.Status + 1 != newStatus) { _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Order status incorrect."); return false; } - model.Status = newStatus; - if (model.Status == OrderStatus.Выдан) model.DateImplement = DateTime.Now; - if (_orderStorage.Update(model) == null) + model1.Status = newStatus; + if (model1.Status == OrderStatus.Выдан) model1.DateImplement = DateTime.Now; + if (_orderStorage.Update(model1) == null) { - model.Status--; + model1.Status--; _logger.LogWarning("Update operation failed"); return false; } - if (model.ImplementerId.HasValue) - { - model.ImplementerId = model.ImplementerId; - } return true; + } public bool TakeOrderInWork(OrderBindingModel model) diff --git a/ComputerShopFileImplement/DataFileSingleton.cs b/ComputerShopFileImplement/DataFileSingleton.cs index aafad08..110e6cc 100644 --- a/ComputerShopFileImplement/DataFileSingleton.cs +++ b/ComputerShopFileImplement/DataFileSingleton.cs @@ -14,9 +14,13 @@ namespace ComputerShopFileImplement 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"; 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 Implementers { get; private set; } public static DataFileSingleton GetInstance() { if (instance == null) @@ -30,11 +34,15 @@ namespace ComputerShopFileImplement 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, ClientFileName, "Clients", x => x.GetXElement); + public void SaveImplementers() => SaveData(Orders, ImplementerFileName, "Implementers", 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)!)!; + Implementers = LoadData(ImplementerFileName, "Impleneter", x => Implementer.Create(x)!)!; } private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) diff --git a/ComputerShopFileImplement/Implements/ImplementerStorage.cs b/ComputerShopFileImplement/Implements/ImplementerStorage.cs new file mode 100644 index 0000000..06df787 --- /dev/null +++ b/ComputerShopFileImplement/Implements/ImplementerStorage.cs @@ -0,0 +1,83 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.SearchModels; +using ComputerShopContracts.StoragesContracts; +using ComputerShopContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopFileImplement.Implements +{ + public class ImplementerStorage : IImplementerStorage + { + private readonly DataFileSingleton _source; + public ImplementerStorage() + { + _source = DataFileSingleton.GetInstance(); + } + public ImplementerViewModel? GetElement(ImplementerSearchModel model) + { + if (model.Id.HasValue) 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 null; + } + + public List GetFilteredList(ImplementerSearchModel model) + { + if (model == null) + { + return new(); + } + if (model.ImplementerFIO != null) + { + return _source.Implementers + .Where(x => x.ImplementerFIO.Contains(model.ImplementerFIO)) + .Select(x => x.GetViewModel) + .ToList(); + } + return new(); + } + + public List GetFullList() + { + return _source.Implementers.Select(x => x.GetViewModel).ToList(); + } + + public ImplementerViewModel? Insert(ImplementerBindingModel model) + { + model.Id = _source.Implementers.Count > 0 ? _source.Implementers.Max(x => x.Id) + 1 : 1; + var res = Implementer.Create(model); + if (res != null) + { + _source.Implementers.Add(res); + _source.SaveImplementers(); + } + 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; + } + } +} diff --git a/ComputerShopFileImplement/Implements/OrderStorage.cs b/ComputerShopFileImplement/Implements/OrderStorage.cs index 65bc162..56e358b 100644 --- a/ComputerShopFileImplement/Implements/OrderStorage.cs +++ b/ComputerShopFileImplement/Implements/OrderStorage.cs @@ -23,24 +23,43 @@ namespace ComputerShopFileImplement.Implements public OrderViewModel? GetElement(OrderSearchModel model) { + if (model.ImplementerId.HasValue && model.Status != null) return source.Orders.FirstOrDefault(x => x.ImplementerId == model.ImplementerId && model.Status.Equals(x.Status))?.GetViewModel; + if (model.ImplementerId.HasValue) + { + return source.Orders.FirstOrDefault(x => x.ImplementerId == model.ImplementerId)?.GetViewModel; + } if (!model.Id.HasValue) { return null; } - return source.Orders - .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + return source.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id)).GetViewModel; } public List GetFilteredList(OrderSearchModel model) { - if (!model.Id.HasValue) + if (!model.Id.HasValue && !model.DateFrom.HasValue && !model.DateTo.HasValue && !model.ClientId.HasValue && model.Status == null) { return new(); } + if (model.ClientId.HasValue) + { + return source.Orders + .Where(x => x.ClientId == model.ClientId) + .Select(x => x.GetViewModel) + .ToList(); + } + if (model.Status != null) + { + return source.Orders + .Where(x => model.Status.Equals(x.Status)) + .Select(x => x.GetViewModel) + .ToList(); + } return source.Orders - .Where(x => x.Id == model.Id || model.DateFrom <= x.DateCreate && x.DateCreate <= model.DateTo) - .Select(x => x.GetViewModel) - .ToList(); + .Where(x => x.Id == model.Id || model.DateFrom <= x.DateCreate && x.DateCreate <= model.DateTo) + .Select(x => x.GetViewModel) + .ToList(); + } public List GetFullList() diff --git a/ComputerShopFileImplement/Models/Implementer.cs b/ComputerShopFileImplement/Models/Implementer.cs new file mode 100644 index 0000000..d8ac2e0 --- /dev/null +++ b/ComputerShopFileImplement/Models/Implementer.cs @@ -0,0 +1,87 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.ViewModels; +using ComputerShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace ComputerShopFileImplement.Models +{ + public class Implementer : IImplementerModel + { + public int Id { get; private set; } + + public string ImplementerFIO { get; private set; } = string.Empty; + + public string Password { get; private set; } = string.Empty; + + public int WorkExperience { get; private set; } + + public int Qualification { get; private set; } + + public static Implementer? Create(XElement element) + { + if (element == null) + { + return null; + } + return new() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ImplementerFIO = element.Element("FIO")!.Value, + Password = element.Element("Password")!.Value, + Qualification = Convert.ToInt32(element.Element("Qualification")!.Value), + WorkExperience = Convert.ToInt32(element.Element("WorkExperience")!.Value), + }; + } + + public static Implementer? Create(ImplementerBindingModel model) + { + if (model == null) + { + return null; + } + return new() + { + Id = model.Id, + ImplementerFIO = model.ImplementerFIO, + Password = model.Password, + Qualification = model.Qualification, + WorkExperience = model.WorkExperience, + }; + } + + + + public void Update(ImplementerBindingModel model) + { + if (model == null) + { + return; + } + ImplementerFIO = model.ImplementerFIO; + Password = model.Password; + Qualification = model.Qualification; + WorkExperience = model.WorkExperience; + } + + public ImplementerViewModel GetViewModel => new() + { + Id = Id, + ImplementerFIO = ImplementerFIO, + Password = Password, + Qualification = Qualification, + }; + + public XElement GetXElement => new("Client", + new XAttribute("Id", Id), + new XElement("FIO", ImplementerFIO), + new XElement("Password", Password), + new XElement("Qualification", Qualification), + new XElement("WorkExperience", WorkExperience) + ); + } +} diff --git a/ComputerShopFileImplement/Models/Order.cs b/ComputerShopFileImplement/Models/Order.cs index b8e5412..178f919 100644 --- a/ComputerShopFileImplement/Models/Order.cs +++ b/ComputerShopFileImplement/Models/Order.cs @@ -41,6 +41,8 @@ namespace ComputerShopFileImplement.Models { ComputerId = model.ComputerId, ComputerName = model.ComputerName, + ImplementerId = model.ImplementerId, + ClientId = model.ClientId, Count = model.Count, Sum = model.Sum, Status = model.Status, @@ -61,6 +63,8 @@ namespace ComputerShopFileImplement.Models Id = Convert.ToInt32(element.Attribute("Id")!.Value), ComputerName = element.Element("ComputerName")!.Value, ComputerId = Convert.ToInt32(element.Element("ComputerId")!.Value), + ClientId = Convert.ToInt32(element.Element("ClientId")!.Value), + ImplementerId = Convert.ToInt32(element.Element("ImplementerId")!.Value), Sum = Convert.ToDouble(element.Element("Sum")!.Value), Count = Convert.ToInt32(element.Element("Count")!.Value), Status = (OrderStatus)Enum.Parse(typeof(OrderStatus), element.Element("Status")!.Value), @@ -83,6 +87,7 @@ namespace ComputerShopFileImplement.Models DateCreate = model.DateCreate; DateImplement = model.DateImplement; Id = model.Id; + ImplementerId = model.ImplementerId; } public OrderViewModel GetViewModel => new() @@ -95,6 +100,8 @@ namespace ComputerShopFileImplement.Models DateImplement = DateImplement, Id = Id, Status = Status, + ImplementerId = ImplementerId, + ImplementerFIO = DataFileSingleton.GetInstance().Implementers.FirstOrDefault(x => x.Id == ImplementerId)?.ImplementerFIO ?? string.Empty, }; public XElement GetXElement => new( @@ -103,6 +110,8 @@ namespace ComputerShopFileImplement.Models new XElement("ComputerName", ComputerName), new XElement("ComputerId", ComputerId.ToString()), new XElement("Count", Count.ToString()), + new XElement("ClientId", ClientId), + new XElement("ImplementerId", ImplementerId), new XElement("Sum", Sum.ToString()), new XElement("Status", Status.ToString()), new XElement("DateCreate", DateCreate.ToString()),