From ad625fc9f9207d2d42980a9239df0874b1ef820d Mon Sep 17 00:00:00 2001 From: Kirill <117719052+KirillFirsof@users.noreply.github.com> Date: Wed, 28 Feb 2024 19:23:20 +0400 Subject: [PATCH] =?UTF-8?q?=D0=AD=D1=82=D0=B0=D0=BF=20=D1=81=D0=BE=D0=B7?= =?UTF-8?q?=D0=B4=D0=B0=D0=BD=D0=B8=D1=8F=20Singleton?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/ComponentStorage.cs | 110 ++++++++++++++++++ .../Models/DataListSingleton.cs | 31 +++++ .../ClothShopListImplement/Models/Order.cs | 65 +++++++++++ .../ClothShopListImplement/Models/Product.cs | 56 +++++++++ 4 files changed, 262 insertions(+) create mode 100644 ClothShop/ClothShopListImplement/Implements/ComponentStorage.cs create mode 100644 ClothShop/ClothShopListImplement/Models/DataListSingleton.cs create mode 100644 ClothShop/ClothShopListImplement/Models/Order.cs create mode 100644 ClothShop/ClothShopListImplement/Models/Product.cs diff --git a/ClothShop/ClothShopListImplement/Implements/ComponentStorage.cs b/ClothShop/ClothShopListImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..a699876 --- /dev/null +++ b/ClothShop/ClothShopListImplement/Implements/ComponentStorage.cs @@ -0,0 +1,110 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ClothShopContracts.BindingModels; +using ClothShopContracts.SearchModels; +using ClothShopContracts.StoragesContracts; +using ClothShopContracts.ViewModels; +using ClothShopListImplement.Models; + + +namespace ClothShopListImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + private readonly DataListSingleton _source; + public ComponentStorage() + { + _source = DataListSingleton.GetInstance(); + } + public List GetFullList() + { + var result = new List(); + foreach (var component in _source.Components) + { + result.Add(component.GetViewModel); + } + return result; + } + public List GetFilteredList(ComponentSearchModel + model) + { + var result = new List(); + if (string.IsNullOrEmpty(model.ComponentName)) + { + return result; + } + foreach (var component in _source.Components) + { + if (component.ComponentName.Contains(model.ComponentName)) + { + result.Add(component.GetViewModel); + } + } + return result; + } + public ComponentViewModel? GetElement(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue) + { + return null; + } + foreach (var component in _source.Components) + { + if ((!string.IsNullOrEmpty(model.ComponentName) && + component.ComponentName == model.ComponentName) || + (model.Id.HasValue && component.Id == model.Id)) + { + return component.GetViewModel; + } + } + return null; + } + public ComponentViewModel? Insert(ComponentBindingModel model) + { + model.Id = 1; + foreach (var component in _source.Components) + { + if (model.Id <= component.Id) + { + model.Id = component.Id + 1; + } + } + var newComponent = Component.Create(model); + if (newComponent == null) + { + return null; + } + _source.Components.Add(newComponent); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + foreach (var component in _source.Components) + { + if (component.Id == model.Id) + { + component.Update(model); + return component.GetViewModel; + } + } + return null; + } + public ComponentViewModel? Delete(ComponentBindingModel model) + { + for (int i = 0; i < _source.Components.Count; ++i) + { + if (_source.Components[i].Id == model.Id) + { + var element = _source.Components[i]; + _source.Components.RemoveAt(i); + return element.GetViewModel; + } + } + return null; + } + } + +} diff --git a/ClothShop/ClothShopListImplement/Models/DataListSingleton.cs b/ClothShop/ClothShopListImplement/Models/DataListSingleton.cs new file mode 100644 index 0000000..e5d9afa --- /dev/null +++ b/ClothShop/ClothShopListImplement/Models/DataListSingleton.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ClothShopListImplement.Models; + +namespace ClothShopListImplement.Models +{ + public class DataListSingleton + { + private static DataListSingleton? _instance; + public List Components { get; set; } + public List Orders { get; set; } + public List Products { get; set; } + private DataListSingleton() + { + Components = new List(); + Orders = new List(); + Products = new List(); + } + public static DataListSingleton GetInstance() + { + if (_instance == null) + { + _instance = new DataListSingleton(); + } + return _instance; + } + } +} diff --git a/ClothShop/ClothShopListImplement/Models/Order.cs b/ClothShop/ClothShopListImplement/Models/Order.cs new file mode 100644 index 0000000..0bd28eb --- /dev/null +++ b/ClothShop/ClothShopListImplement/Models/Order.cs @@ -0,0 +1,65 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ClothShopContracts.BindingModels; +using ClothShopContracts.ViewModels; +using ClothShopDataModels.Models; +using ClothShopDataModels.Enums; + +namespace ClothShopListImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + public int ProductId { get; private set; } + public int Count { get; private set; } + public double Sum { get; private set; } + public OrderStatus Status { get; private set; } + public DateTime DateCreate { get; private set; } + public DateTime? DateImplement { get; set; } + + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + ProductId = model.ProductId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement + }; + } + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + Id = model.Id; + ProductId = model.ProductId; + Count = model.Count; + Sum = model.Sum; + Status = model.Status; + DateCreate = model.DateCreate; + DateImplement = model.DateImplement; + } + public OrderViewModel GetViewModel => new() + { + Id = Id, + ProductId = ProductId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement + }; + } +} diff --git a/ClothShop/ClothShopListImplement/Models/Product.cs b/ClothShop/ClothShopListImplement/Models/Product.cs new file mode 100644 index 0000000..2d7f512 --- /dev/null +++ b/ClothShop/ClothShopListImplement/Models/Product.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ClothShopContracts.BindingModels; +using ClothShopContracts.ViewModels; +using ClothShopDataModels.Models; + + +namespace ClothShopListImplement.Models +{ + public class Product : IProductModel + { + public int Id { get; private set; } + public string ProductName { get; private set; } = string.Empty; + public double Price { get; private set; } + public Dictionary ProductComponents + { + get; + private set; + } = new Dictionary(); + public static Product? Create(ProductBindingModel? model) + { + if (model == null) + { + return null; + } + return new Product() + { + Id = model.Id, + ProductName = model.ProductName, + Price = model.Price, + ProductComponents = model.ProductComponents + }; + } + public void Update(ProductBindingModel? model) + { + if (model == null) + { + return; + } + ProductName = model.ProductName; + Price = model.Price; + ProductComponents = model.ProductComponents; + } + public ProductViewModel GetViewModel => new() + { + Id = Id, + ProductName = ProductName, + Price = Price, + ProductComponents = ProductComponents + }; + + } +}