diff --git a/Pizzeria/PizzeriaFileImplement/DataFileSingleton.cs b/Pizzeria/PizzeriaFileImplement/DataFileSingleton.cs index 1f2c293..752eb68 100644 --- a/Pizzeria/PizzeriaFileImplement/DataFileSingleton.cs +++ b/Pizzeria/PizzeriaFileImplement/DataFileSingleton.cs @@ -15,10 +15,13 @@ namespace PizzeriaFileImplement private readonly string OrderFileName = "Order.xml"; private readonly string PizzaFileName = "Pizza.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 Pizzas { get; private set; } public List Clients { get; private set; } + public List Implementers { get; private set; } + public static DataFileSingleton GetInstance() { @@ -33,6 +36,7 @@ namespace PizzeriaFileImplement public void SavePizzas() => SaveData(Pizzas, PizzaFileName, "Pizzas", 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(Implementers, ImplementerFileName, "Implementers", x => x.GetXElement); private DataFileSingleton() { @@ -40,6 +44,7 @@ namespace PizzeriaFileImplement Pizzas = LoadData(PizzaFileName, "Pizza", x => Pizza.Create(x)!)!; Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; Clients = LoadData(ClientFileName, "Client", x => Client.Create(x)!)!; + Implementers = LoadData(ImplementerFileName, "Implementer", x => Implementer.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 5c7b584..f5a472a 100644 --- a/Pizzeria/PizzeriaFileImplement/Implements/ImplementerStorage.cs +++ b/Pizzeria/PizzeriaFileImplement/Implements/ImplementerStorage.cs @@ -2,39 +2,93 @@ using PizzeriaContracts.SearchModels; using PizzeriaContracts.StoragesContracts; using PizzeriaContracts.ViewModels; +using PizzeriaFileImplement.Models; namespace PizzeriaFileImplement.Implements { public class ImplementerStorage : IImplementerStorage { - public ImplementerViewModel? Delete(ImplementerBindingModel model) + private readonly DataFileSingleton _source; + public ImplementerStorage() { - throw new NotImplementedException(); - } - - public ImplementerViewModel? GetElement(ImplementerSearchModel model) - { - throw new NotImplementedException(); - } - - public List GetFilteredList(ImplementerSearchModel model) - { - throw new NotImplementedException(); + _source = DataFileSingleton.GetInstance(); } public List GetFullList() { - throw new NotImplementedException(); + return _source.Implementers.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(ImplementerSearchModel 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(); + } + + 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 ImplementerViewModel? Insert(ImplementerBindingModel model) { - throw new NotImplementedException(); + 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) { - throw new NotImplementedException(); + 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/Pizzeria/PizzeriaFileImplement/Implements/OrderStorage.cs b/Pizzeria/PizzeriaFileImplement/Implements/OrderStorage.cs index c926a60..0b8dd8c 100644 --- a/Pizzeria/PizzeriaFileImplement/Implements/OrderStorage.cs +++ b/Pizzeria/PizzeriaFileImplement/Implements/OrderStorage.cs @@ -30,6 +30,12 @@ namespace PizzeriaFileImplement.Implements return source.Orders.Where(x => x.ClientId == model.ClientId).Select(x => x.GetViewModel).ToList(); } + if (!model.ImplementerId.HasValue && !model.Id.HasValue) + { + return source.Orders.Where(x => x.ImplementerId == model.ImplementerId).Select(x => x.GetViewModel).ToList(); + + } + if (model.Id.HasValue) { return source.Orders.Where(x => x.Id.Equals(model.Id)).Select(x => GetViewModel(x)).ToList(); @@ -39,6 +45,10 @@ namespace PizzeriaFileImplement.Implements public OrderViewModel? GetElement(OrderSearchModel model) { + if (model.ImplementerId.HasValue) + { + return source.Orders.FirstOrDefault(x => x.ImplementerId == model.ImplementerId)?.GetViewModel; + } if (!model.Id.HasValue) { return new(); diff --git a/Pizzeria/PizzeriaFileImplement/Models/Implementer.cs b/Pizzeria/PizzeriaFileImplement/Models/Implementer.cs new file mode 100644 index 0000000..a2f2384 --- /dev/null +++ b/Pizzeria/PizzeriaFileImplement/Models/Implementer.cs @@ -0,0 +1,87 @@ +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; +using System.Xml.Linq; + +namespace PizzeriaFileImplement.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() + { + ImplementerFIO = element.Element("FIO")!.Value, + Password = element.Element("Password")!.Value, + Id = Convert.ToInt32(element.Attribute("Id")!.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, + Password = model.Password, + Qualification = model.Qualification, + ImplementerFIO = model.ImplementerFIO, + WorkExperience = model.WorkExperience, + }; + } + + + + public void Update(ImplementerBindingModel model) + { + if (model == null) + { + return; + } + Password = model.Password; + Qualification = model.Qualification; + ImplementerFIO = model.ImplementerFIO; + WorkExperience = model.WorkExperience; + } + + public ImplementerViewModel GetViewModel => new() + { + Id = Id, + Password = Password, + Qualification = Qualification, + ImplementerFIO = ImplementerFIO, + }; + + public XElement GetXElement => new("Client", + new XAttribute("Id", Id), + new XElement("Password", Password), + new XElement("FIO", ImplementerFIO), + new XElement("Qualification", Qualification), + new XElement("WorkExperience", WorkExperience) + ); + } +} diff --git a/Pizzeria/PizzeriaFileImplement/Models/Order.cs b/Pizzeria/PizzeriaFileImplement/Models/Order.cs index ba856f8..6614c16 100644 --- a/Pizzeria/PizzeriaFileImplement/Models/Order.cs +++ b/Pizzeria/PizzeriaFileImplement/Models/Order.cs @@ -10,6 +10,7 @@ namespace PizzeriaFileImplement.Models { public int Id { get; private set; } public int ClientId { get; private set; } + public int? ImplementerId { get; set; } public int PizzaId { get; private set; } public int Count { get; private set; } public double Sum { get; private set; } @@ -28,6 +29,7 @@ namespace PizzeriaFileImplement.Models Id = model.Id, PizzaId = model.PizzaId, ClientId = model.ClientId, + ImplementerId = model.ImplementerId, Count = model.Count, Sum = model.Sum, Status = model.Status, @@ -48,6 +50,7 @@ namespace PizzeriaFileImplement.Models Id = Convert.ToInt32(element.Attribute("Id")!.Value), PizzaId = Convert.ToInt32(element.Element("PizzaId")!.Value), ClientId = Convert.ToInt32(element.Element("ClientId")!.Value), + ImplementerId = Convert.ToInt32(element.Element("ImplementerId")!.Value), Count = Convert.ToInt32(element.Element("Count")!.Value), Sum = Convert.ToDouble(element.Element("Sum")!.Value), Status = (OrderStatus)(Enum.Parse(typeof(OrderStatus), element.Element("Status")!.Value)), @@ -72,6 +75,7 @@ namespace PizzeriaFileImplement.Models Id = Id, PizzaId = PizzaId, ClientId = ClientId, + ImplementerId = ImplementerId, Count = Count, Sum = Sum, Status = Status, @@ -83,12 +87,11 @@ namespace PizzeriaFileImplement.Models new XAttribute("Id", Id), new XElement("PizzaId", PizzaId.ToString()), new XElement("ClientId", ClientId.ToString()), + new XElement("ImplementerId", ImplementerId), new XElement("Count", Count.ToString()), new XElement("Sum", Sum.ToString()), new XElement("Status", Status.ToString()), new XElement("DateCreate", DateCreate.ToString()), new XElement("DateImplement", DateImplement.ToString())); - - public int? ImplementerId => throw new NotImplementedException(); } } \ No newline at end of file diff --git a/Pizzeria/PizzeriaListImplement/DataListSingleton.cs b/Pizzeria/PizzeriaListImplement/DataListSingleton.cs index 97fbd34..781f05b 100644 --- a/Pizzeria/PizzeriaListImplement/DataListSingleton.cs +++ b/Pizzeria/PizzeriaListImplement/DataListSingleton.cs @@ -14,6 +14,7 @@ namespace PizzeriaListImplement public List Orders { get; set; } public List Pizzas { get; set; } public List Clients { get; set; } + public List Implementers { get; set; } private DataListSingleton() { @@ -21,6 +22,7 @@ namespace PizzeriaListImplement Orders = new List(); Pizzas = new List(); Clients = new List(); + Implementers = new List(); } public static DataListSingleton GetInstance() diff --git a/Pizzeria/PizzeriaListImplement/Implements/ImplementerStorage.cs b/Pizzeria/PizzeriaListImplement/Implements/ImplementerStorage.cs index 212c350..c243d83 100644 --- a/Pizzeria/PizzeriaListImplement/Implements/ImplementerStorage.cs +++ b/Pizzeria/PizzeriaListImplement/Implements/ImplementerStorage.cs @@ -2,39 +2,117 @@ using PizzeriaContracts.SearchModels; using PizzeriaContracts.StoragesContracts; using PizzeriaContracts.ViewModels; +using PizzeriaListImplement.Models; namespace PizzeriaListImplement.Implements { public class ImplementerStorage : IImplementerStorage { + private readonly DataListSingleton _source; + public ImplementerStorage() + { + _source = DataListSingleton.GetInstance(); + } + public ImplementerViewModel? Delete(ImplementerBindingModel model) { - throw new NotImplementedException(); + for (int i = 0; i < _source.Implementers.Count; ++i) + { + if (_source.Implementers[i].Id == model.Id) + { + var element = _source.Implementers[i]; + _source.Implementers.RemoveAt(i); + return element.GetViewModel; + } + } + return null; } public ImplementerViewModel? GetElement(ImplementerSearchModel model) { - throw new NotImplementedException(); + foreach (var x in _source.Implementers) + { + if (model.Id.HasValue && x.Id == model.Id) + { + return x.GetViewModel; + } + if (model.ImplementerFIO != null && model.Password != null && x.ImplementerFIO.Equals(model.ImplementerFIO) && x.Password.Equals(model.Password)) + { + return x.GetViewModel; + } + if (model.ImplementerFIO != null && x.ImplementerFIO.Equals(model.ImplementerFIO)) + { + return x.GetViewModel; + } + } + return null; } public List GetFilteredList(ImplementerSearchModel model) { - throw new NotImplementedException(); + if (model == null) + { + return new(); + } + if (model.Id.HasValue) + { + var res = GetElement(model); + return res != null ? new() { res } : new(); + } + + List result = new(); + if (model.ImplementerFIO != null) + { + foreach (var implementer in _source.Implementers) + { + if (implementer.ImplementerFIO.Equals(model.ImplementerFIO)) + { + result.Add(implementer.GetViewModel); + } + } + } + return result; } public List GetFullList() { - throw new NotImplementedException(); + var result = new List(); + foreach (var implementer in _source.Implementers) + { + result.Add(implementer.GetViewModel); + } + return result; } public ImplementerViewModel? Insert(ImplementerBindingModel model) { - throw new NotImplementedException(); + model.Id = 1; + foreach (var implementer in _source.Implementers) + { + if (model.Id <= implementer.Id) + { + model.Id = implementer.Id + 1; + } + } + var res = Implementer.Create(model); + if (res != null) + { + _source.Implementers.Add(res); + } + return res?.GetViewModel; } public ImplementerViewModel? Update(ImplementerBindingModel model) { - throw new NotImplementedException(); + foreach (var implementer in _source.Implementers) + { + if (implementer.Id == model.Id) + { + implementer.Update(model); + return implementer.GetViewModel; + } + } + return null; } } } diff --git a/Pizzeria/PizzeriaListImplement/Implements/OrderStorage.cs b/Pizzeria/PizzeriaListImplement/Implements/OrderStorage.cs index 90c7aef..4b29c9d 100644 --- a/Pizzeria/PizzeriaListImplement/Implements/OrderStorage.cs +++ b/Pizzeria/PizzeriaListImplement/Implements/OrderStorage.cs @@ -53,6 +53,16 @@ namespace PizzeriaListImplement.Implements } } } + else if (model.ImplementerId.HasValue && !model.Id.HasValue) + { + foreach (var order in _source.Orders) + { + if (order.ImplementerId == model.ImplementerId) + { + result.Add(GetViewModel(order)); + } + } + } else if (model.Id.HasValue) { foreach (var order in _source.Orders) @@ -100,6 +110,10 @@ namespace PizzeriaListImplement.Implements { return AttachPizzaName(order.GetViewModel); } + else if (model.ImplementerId.HasValue && model.ImplementerId == order.ImplementerId) + { + return GetViewModel(order); + } } return null; } diff --git a/Pizzeria/PizzeriaListImplement/Models/Implementer.cs b/Pizzeria/PizzeriaListImplement/Models/Implementer.cs new file mode 100644 index 0000000..5087fe1 --- /dev/null +++ b/Pizzeria/PizzeriaListImplement/Models/Implementer.cs @@ -0,0 +1,60 @@ +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 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(ImplementerBindingModel model) + { + if (model == null) + { + return null; + } + return new() + { + Id = model.Id, + Password = model.Password, + Qualification = model.Qualification, + ImplementerFIO = model.ImplementerFIO, + WorkExperience = model.WorkExperience, + }; + } + + public void Update(ImplementerBindingModel model) + { + if (model == null) + { + return; + } + Password = model.Password; + Qualification = model.Qualification; + ImplementerFIO = model.ImplementerFIO; + WorkExperience = model.WorkExperience; + } + + public ImplementerViewModel GetViewModel => new() + { + Id = Id, + Password = Password, + Qualification = Qualification, + ImplementerFIO = ImplementerFIO, + }; + } +} diff --git a/Pizzeria/PizzeriaListImplement/Models/Order.cs b/Pizzeria/PizzeriaListImplement/Models/Order.cs index c5357c1..7cd67b1 100644 --- a/Pizzeria/PizzeriaListImplement/Models/Order.cs +++ b/Pizzeria/PizzeriaListImplement/Models/Order.cs @@ -15,6 +15,7 @@ namespace PizzeriaListImplement.Models public int Id { get; private set; } public int PizzaId { get; private set; } public int ClientId { get; private set; } + public int? ImplementerId { get; private set; } public int Count { get; private set; } public double Sum { get; private set; } public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; @@ -32,6 +33,7 @@ namespace PizzeriaListImplement.Models Id = model.Id, PizzaId = model.PizzaId, ClientId = model.ClientId, + ImplementerId = model.ImplementerId, Count = model.Count, Sum = model.Sum, Status = model.Status, @@ -55,13 +57,12 @@ namespace PizzeriaListImplement.Models Id = Id, PizzaId = PizzaId, ClientId = ClientId, + ImplementerId = ImplementerId, Count = Count, Sum = Sum, Status = Status, DateCreate = DateCreate, DateImplement = DateImplement, }; - - public int? ImplementerId => throw new NotImplementedException(); } }