From daca7d493138dda9b41cbe22104b34765c808484 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D0=B5=D0=BC=20=D0=A5=D0=B0=D1=80=D0=BB?= =?UTF-8?q?=D0=B0=D0=BC=D0=BE=D0=B2?= Date: Fri, 2 Jun 2023 20:28:07 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=B0=D0=B11=20=D1=83=D1=81=D0=BB=D0=BE?= =?UTF-8?q?=D0=B6=D0=BD=D1=91=D0=BD=D0=BD=D0=B0=D1=8F-=20=D0=B2=D0=BD?= =?UTF-8?q?=D0=B5=D0=B4=D1=80=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BC=D0=B0=D0=B3?= =?UTF-8?q?=D0=B0=D0=B7=D0=B8=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/ShopStorage.cs | 111 ++++++++++++++++++ .../Models/Shop.cs | 54 +++++++++ 2 files changed, 165 insertions(+) create mode 100644 FoodOrders/AbstractFoodOrdersListImplement/Implements/ShopStorage.cs create mode 100644 FoodOrders/AbstractFoodOrdersListImplement/Models/Shop.cs diff --git a/FoodOrders/AbstractFoodOrdersListImplement/Implements/ShopStorage.cs b/FoodOrders/AbstractFoodOrdersListImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..f0f17b9 --- /dev/null +++ b/FoodOrders/AbstractFoodOrdersListImplement/Implements/ShopStorage.cs @@ -0,0 +1,111 @@ +using AbstractFoodOrdersContracts.BindingModels; +using AbstractFoodOrdersContracts.SearchModels; +using AbstractFoodOrdersContracts.StoragesContracts; +using AbstractFoodOrdersContracts.ViewModels; +using AbstractFoodOrdersListImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AbstractFoodOrdersListImplement.Implements +{ + public class ShopStorage : IShopStorage + { + private readonly DataListSingleton _source; + public ShopStorage() + { + _source = DataListSingleton.GetInstance(); + } + public List GetFullList() + { + var result = new List(); + foreach (var shop in _source.Shops) + { + result.Add(shop.GetViewModel); + } + return result; + } + + public List GetFilteredList(ShopSearchModel model) + { + var result = new List(); + if (string.IsNullOrEmpty(model.ShopName)) + { + return result; + } + foreach (var shop in _source.Shops) + { + if (shop.ShopName.Contains(model.ShopName)) + { + result.Add(shop.GetViewModel); + } + } + return result; + } + + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue) + { + return null; + } + foreach (var shop in _source.Shops) + { + if ((!string.IsNullOrEmpty(model.ShopName) && shop.ShopName == model.ShopName) || + (model.Id.HasValue && shop.Id == model.Id)) + { + return shop.GetViewModel; + } + } + return null; + } + + public ShopViewModel? Insert(ShopBindingModel model) + { + model.Id = 1; + foreach (var shop in _source.Shops) + { + if (model.Id <= shop.Id) + { + model.Id = shop.Id + 1; + } + } + var newShop = Shop.Create(model); + if (newShop == null) + { + return null; + } + _source.Shops.Add(newShop); + return newShop.GetViewModel; + } + + public ShopViewModel? Update(ShopBindingModel model) + { + foreach (var shop in _source.Shops) + { + if (shop.Id == model.Id) + { + shop.Update(model); + return shop.GetViewModel; + } + } + return null; + } + + public ShopViewModel? Delete(ShopBindingModel model) + { + for (int i = 0; i < _source.Shops.Count; ++i) + { + if (_source.Shops[i].Id == model.Id) + { + var element = _source.Shops[i]; + _source.Shops.RemoveAt(i); + return element.GetViewModel; + } + } + return null; + } + } +} diff --git a/FoodOrders/AbstractFoodOrdersListImplement/Models/Shop.cs b/FoodOrders/AbstractFoodOrdersListImplement/Models/Shop.cs new file mode 100644 index 0000000..735d1dc --- /dev/null +++ b/FoodOrders/AbstractFoodOrdersListImplement/Models/Shop.cs @@ -0,0 +1,54 @@ +using AbstractFoodOrdersContracts.BindingModels; +using AbstractFoodOrdersContracts.ViewModels; +using AbstractFoodOrdersDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AbstractFoodOrdersListImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; private set; } + public string ShopName { get; private set; } = string.Empty; + public string ShopAdress { get; private set; } = string.Empty; + public DateTime OpeningDate { get; private set; } + public Dictionary ShopDishes { get; private set; } = new Dictionary(); + public static Shop? Create(ShopBindingModel? model) + { + if (model == null) + { + return null; + } + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + ShopAdress = model.ShopAdress, + OpeningDate = model.OpeningDate, + ShopDishes = model.ShopDishes + }; + } + public void Update(ShopBindingModel? model) + { + if (model == null) + { + return; + } + ShopName = model.ShopName; + ShopAdress = model.ShopAdress; + OpeningDate = model.OpeningDate; + ShopDishes = model.ShopDishes; + } + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + ShopAdress = ShopAdress, + ShopDishes = ShopDishes, + OpeningDate = OpeningDate + }; + } +}