diff --git a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/BlacksmithWorkshopListImplement.csproj b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/BlacksmithWorkshopListImplement.csproj index 132c02c..b65badc 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/BlacksmithWorkshopListImplement.csproj +++ b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/BlacksmithWorkshopListImplement.csproj @@ -6,4 +6,9 @@ enable + + + + + diff --git a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/DataListSingleton.cs b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/DataListSingleton.cs new file mode 100644 index 0000000..b6da397 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/DataListSingleton.cs @@ -0,0 +1,41 @@ +using BlacksmithWorkshopListImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopListImplement +{ + //класс для списков, в которых будет храниться информация при работе приложения + public class DataListSingleton + { + private static DataListSingleton? _instance; + + //список для хранения заготовок + public List WorkPieces { get; set; } + + //список для хранения изделий + public List
Articles { get; set; } + + //список для хранения заказов + public List Orders { get; set; } + + public DataListSingleton() + { + WorkPieces = new List(); + Articles = new List
(); + Orders = new List(); + } + + public static DataListSingleton GetInstance() + { + if(_instance == null) + { + _instance = new DataListSingleton(); + } + + return _instance; + } + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/ArticleStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/ArticleStorage.cs new file mode 100644 index 0000000..3e5eeb8 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/ArticleStorage.cs @@ -0,0 +1,138 @@ +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 ArticleStorage : IArticleStorage + { + //поле для работы со списком изделий + private readonly DataListSingleton _source; + + //получение в конструкторе объекта DataListSingleton + public ArticleStorage() + { + _source = DataListSingleton.GetInstance(); + } + + //получение полного списка изделий + public List GetFullList() + { + var result = new List(); + + foreach(var article in _source.Articles) + { + result.Add(article.GetViewModel); + } + + return result; + } + + //получение отфильтрованного списка изделий + public List GetFilteredList(ArticleSearchModel model) + { + var result = new List(); + + if (string.IsNullOrEmpty(model.ArticleName)) + { + return result; + } + + foreach(var article in _source.Articles) + { + if(article.ArticleName.Contains(model.ArticleName)) + { + result.Add(article.GetViewModel); + } + } + + return result; + } + + //получение элемента из списка изделий + public ArticleViewModel? GetElement(ArticleSearchModel model) + { + if(string.IsNullOrEmpty(model.ArticleName) && !model.Id.HasValue) + { + return null; + } + + foreach(var article in _source.Articles) + { + if((!string.IsNullOrEmpty(model.ArticleName) && article.ArticleName == model.ArticleName) || + (model.Id.HasValue && article.Id == model.Id)) + { + return article.GetViewModel; + } + } + + return null; + } + + //при создании изделия определяем для него новый id: ищем max id и прибавлляем к нему 1 + public ArticleViewModel? Insert(ArticleBindingModel model) + { + model.Id = 1; + + foreach(var article in _source.Articles) + { + if(model.Id <= article.Id) + { + model.Id = article.Id + 1; + } + } + + var newArticle = Article.Create(model); + + if(newArticle == null) + { + return null; + } + + _source.Articles.Add(newArticle); + + return newArticle.GetViewModel; + } + + //обновление изделия + public ArticleViewModel? Update(ArticleBindingModel model) + { + foreach (var article in _source.Articles) + { + if (article.Id == model.Id) + { + article.Update(model); + + return article.GetViewModel; + } + } + + return null; + } + + //удаление изделия + public ArticleViewModel? Delete(ArticleBindingModel model) + { + for(int i = 0; i < _source.Articles.Count; ++i) + { + if (_source.Articles[i].Id == model.Id) + { + var element = _source.Articles[i]; + _source.Articles.RemoveAt(i); + + return element.GetViewModel; + } + } + + return null; + } + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/OrderStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..2d78549 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/OrderStorage.cs @@ -0,0 +1,137 @@ +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 OrderStorage : IOrderStorage + { + //поле для работы со списком заказов + private readonly DataListSingleton _source; + + //получение в конструкторе объекта DataListSingleton + public OrderStorage() + { + _source = DataListSingleton.GetInstance(); + } + + //получение полного списка заготовок + public List GetFullList() + { + var result = new List(); + + foreach (var order in _source.Orders) + { + result.Add(order.GetViewModel); + } + + return result; + } + + //получение отфильтрованного списка заказов + public List GetFilteredList(OrderSearchModel model) + { + var result = new List(); + + if (!model.Id.HasValue) + { + return result; + } + + foreach (var order in _source.Orders) + { + if (order.Id == model.Id) + { + result.Add(order.GetViewModel); + } + } + + return result; + } + + //получение элемента из списка заказов + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + + foreach (var order in _source.Orders) + { + if (model.Id.HasValue && order.Id == model.Id) + { + return order.GetViewModel; + } + } + + return null; + } + + //при создании заказа определяем для него новый id: ищем max id и прибавляем к нему 1 + public OrderViewModel? Insert(OrderBindingModel model) + { + model.Id = 1; + + foreach (var order in _source.Orders) + { + if (model.Id <= order.Id) + { + model.Id = order.Id + 1; + } + } + + var newOrder = Order.Create(model); + + if (newOrder == null) + { + return null; + } + + _source.Orders.Add(newOrder); + + return newOrder.GetViewModel; + } + + //обновление заказа + public OrderViewModel? Update(OrderBindingModel model) + { + foreach (var order in _source.Orders) + { + if (order.Id == model.Id) + { + order.Update(model); + + return order.GetViewModel; + } + } + + return null; + } + + //удаление заказа + public OrderViewModel? Delete(OrderBindingModel model) + { + for (int i = 0; i < _source.Orders.Count; ++i) + { + if (_source.Orders[i].Id == model.Id) + { + var element = _source.Orders[i]; + _source.Orders.RemoveAt(i); + + return element.GetViewModel; + } + } + + return null; + } + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/WorkPieceStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/WorkPieceStorage.cs new file mode 100644 index 0000000..eacd079 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/WorkPieceStorage.cs @@ -0,0 +1,138 @@ +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 WorkPieceStorage : IWorkPieceStorage + { + //поле для работы со списком заготовок + private readonly DataListSingleton _source; + + //получение в конструкторе объекта DataListSingleton + public WorkPieceStorage() + { + _source = DataListSingleton.GetInstance(); + } + + //получение полного списка заготовок + public List GetFullList() + { + var result = new List(); + + foreach(var workPiece in _source.WorkPieces) + { + result.Add(workPiece.GetViewModel); + } + + return result; + } + + //получение отфильтрованного списка заготовок + public List GetFilteredList(WorkPieceSearchModel model) + { + var result = new List(); + + if(string.IsNullOrEmpty(model.WorkPieceName)) + { + return result; + } + + foreach(var workPiece in _source.WorkPieces) + { + if (workPiece.WorkPieceName.Contains(model.WorkPieceName)) + { + result.Add(workPiece.GetViewModel); + } + } + + return result; + } + + //получение элемента из списка заготовок + public WorkPieceViewModel? GetElement(WorkPieceSearchModel model) + { + if(string.IsNullOrEmpty(model.WorkPieceName) && !model.Id.HasValue) + { + return null; + } + + foreach(var workPiece in _source.WorkPieces) + { + if((!string.IsNullOrEmpty(model.WorkPieceName) && workPiece.WorkPieceName == model.WorkPieceName) || + (model.Id.HasValue && workPiece.Id == model.Id)) + { + return workPiece.GetViewModel; + } + } + + return null; + } + + //при создании заготовки определяем для него новый id: ищем max id и прибавляем к нему 1 + public WorkPieceViewModel? Insert(WorkPieceBindingModel model) + { + model.Id = 1; + + foreach(var workPiece in _source.WorkPieces) + { + if(model.Id <= workPiece.Id) + { + model.Id = workPiece.Id + 1; + } + } + + var newWorkPiece = WorkPiece.Create(model); + + if(newWorkPiece == null) + { + return null; + } + + _source.WorkPieces.Add(newWorkPiece); + + return newWorkPiece.GetViewModel; + } + + //обновление заготовки + public WorkPieceViewModel? Update(WorkPieceBindingModel model) + { + foreach(var workPiece in _source.WorkPieces) + { + if(workPiece.Id == model.Id) + { + workPiece.Update(model); + + return workPiece.GetViewModel; + } + } + + return null; + } + + //удаление заготовки + public WorkPieceViewModel? Delete(WorkPieceBindingModel model) + { + for(int i = 0; i < _source.WorkPieces.Count; ++i) + { + if (_source.WorkPieces[i].Id == model.Id) + { + var element = _source.WorkPieces[i]; + _source.WorkPieces.RemoveAt(i); + + return element.GetViewModel; + } + } + + return null; + } + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/Article.cs b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/Article.cs index 3fb18b4..d8dd647 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/Article.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/Article.cs @@ -52,7 +52,7 @@ namespace BlacksmithWorkshopListImplement.Models } //метод для создания объекта класса ViewModel на основе данных объекта класса-компонента - public ArticleViewModel GetViewModel() => new() + public ArticleViewModel GetViewModel => new() { Id = Id, ArticleName = ArticleName, diff --git a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/Order.cs b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/Order.cs index 13e02d6..84272c5 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/Order.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/Order.cs @@ -64,7 +64,7 @@ namespace BlacksmithWorkshopListImplement.Models } //метод для создания объекта класса ViewModel на основе данных объекта класса-компонента - public OrderViewModel GetViewModel() => new() + public OrderViewModel GetViewModel => new() { ArticleId = ArticleId, Count = Count,