From df19ccae5ed05b8891f63f0e3d382067c967efda Mon Sep 17 00:00:00 2001 From: Programmist73 Date: Wed, 5 Apr 2023 10:01:27 +0400 Subject: [PATCH] =?UTF-8?q?ListImplement=20=D0=B4=D0=BB=D1=8F=20Implemente?= =?UTF-8?q?r.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DataListSingleton.cs | 6 +- .../Implements/ImplementerStorage.cs | 130 ++++++++++++++++++ .../Models/Implementer.cs | 67 +++++++++ 3 files changed, 202 insertions(+), 1 deletion(-) create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/ImplementerStorage.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/Implementer.cs diff --git a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/DataListSingleton.cs b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/DataListSingleton.cs index c19f551..6185596 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/DataListSingleton.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/DataListSingleton.cs @@ -24,12 +24,16 @@ namespace BlacksmithWorkshopListImplement //список для хранения клиентов public List Clients { get; set; } - public DataListSingleton() + //список для хранения исполнителей + public List Implementers { get; set; } + + public DataListSingleton() { WorkPieces = new List(); Manufactures = new List(); Orders = new List(); Clients = new List(); + Implementers = new List(); } public static DataListSingleton GetInstance() diff --git a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/ImplementerStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/ImplementerStorage.cs new file mode 100644 index 0000000..4cbc518 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/ImplementerStorage.cs @@ -0,0 +1,130 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.SearchModels; +using BlacksmithWorkshopContracts.StoragesContracts; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopListImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopListImplement.Implements +{ + public class ImplementerStorage : IImplementerStorage + { + //поле для работы со списком исполнителей + private readonly DataListSingleton _source; + + public ImplementerStorage() + { + _source = DataListSingleton.GetInstance(); + } + + public List GetFullList() + { + var result = new List(); + + foreach (var implementer in _source.Implementers) + { + result.Add(implementer.GetViewModel); + } + + return result; + } + + public List GetFilteredList(ImplementerSearchModel model) + { + var result = new List(); + + if (string.IsNullOrEmpty(model.ImplementerFIO)) + { + return result; + } + + foreach (var implementer in _source.Implementers) + { + if (implementer.ImplementerFIO.Contains(model.ImplementerFIO)) + { + result.Add(implementer.GetViewModel); + } + } + + return result; + } + + public ImplementerViewModel? GetElement(ImplementerSearchModel model) + { + if (string.IsNullOrEmpty(model.ImplementerFIO) && !model.Id.HasValue) + { + return null; + } + + foreach (var implementer in _source.Implementers) + { + if ((!string.IsNullOrEmpty(model.ImplementerFIO) && implementer.ImplementerFIO == model.ImplementerFIO) || + (model.Id.HasValue && implementer.Id == model.Id)) + { + return implementer.GetViewModel; + } + } + + return null; + } + + public ImplementerViewModel? Insert(ImplementerBindingModel model) + { + model.Id = 1; + + foreach (var implementer in _source.Implementers) + { + if (model.Id <= implementer.Id) + { + model.Id = implementer.Id + 1; + } + } + + var newImplementer = Implementer.Create(model); + + if (newImplementer == null) + { + return null; + } + + _source.Implementers.Add(newImplementer); + + return newImplementer.GetViewModel; + } + + public ImplementerViewModel? Update(ImplementerBindingModel model) + { + foreach (var implementer in _source.Implementers) + { + if (implementer.Id == model.Id) + { + implementer.Update(model); + + return implementer.GetViewModel; + } + } + + return null; + } + + public ImplementerViewModel? Delete(ImplementerBindingModel model) + { + 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; + } + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/Implementer.cs b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/Implementer.cs new file mode 100644 index 0000000..45a14a4 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/Implementer.cs @@ -0,0 +1,67 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopListImplement.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; } + + //метод для создания объекта от класса-компонента на основе класса-BindingModel + public static Implementer? Create(ImplementerBindingModel? model) + { + if (model == null) + { + return null; + } + + return new Implementer() + { + Id = model.Id, + Password = model.Password, + ImplementerFIO = model.ImplementerFIO, + Qualification = model.Qualification, + WorkExperience = model.WorkExperience + }; + } + + //метод изменения существующего объекта + public void Update(ImplementerBindingModel? model) + { + if (model == null) + { + return; + } + + Password = model.Password; + ImplementerFIO = model.ImplementerFIO; + Qualification = model.Qualification; + WorkExperience = model.WorkExperience; + } + + //метод для создания объекта класса ViewModel на основе данных объекта класса-компонента + public ImplementerViewModel GetViewModel => new() + { + Id = Id, + Password = Password, + ImplementerFIO = ImplementerFIO, + Qualification = Qualification, + WorkExperience = WorkExperience + }; + } +}