From 4368d4d6ccec34caff488d082a7201848c470f17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Wed, 15 Feb 2023 06:34:44 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=B0=20=D0=BB=D0=BE=D0=B3=D0=B8=D0=BA=D0=B0?= =?UTF-8?q?=20=D1=85=D1=80=D0=B0=D0=BD=D0=B8=D0=BB=D0=B8=D1=89=D0=B0=20?= =?UTF-8?q?=D0=BC=D0=B0=D0=B3=D0=B0=D0=B7=D0=B8=D0=BD=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConfectionaryFileImplement/ShopStorage.cs | 99 ++++++++++--------- ConfectionaryListImplement/ShopStorage.cs | 11 +++ .../StoragesContract/IShopStorage.cs | 4 + 3 files changed, 67 insertions(+), 47 deletions(-) diff --git a/ConfectionaryFileImplement/ShopStorage.cs b/ConfectionaryFileImplement/ShopStorage.cs index 6ae843a..3e9d4da 100644 --- a/ConfectionaryFileImplement/ShopStorage.cs +++ b/ConfectionaryFileImplement/ShopStorage.cs @@ -2,6 +2,7 @@ using ConfectioneryContracts.SearchModels; using ConfectioneryContracts.StoragesContract; using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Models; namespace ConfectioneryFileImplement { @@ -15,93 +16,97 @@ namespace ConfectioneryFileImplement public ShopViewModel? Delete(ShopBindingModel model) { - for (int i = 0; i < _source.Shops.Count; ++i) + var element = _source.Shops.FirstOrDefault(x => x.Id == model.Id); + if (element != null) { - if (_source.Shops[i].Id == model.Id) - { - var element = _source.Shops[i]; - _source.Shops.RemoveAt(i); - return element.GetViewModel; - } + _source.Shops.Remove(element); + _source.SaveShops(); + return element.GetViewModel; } return null; } public ShopViewModel? GetElement(ShopSearchModel model) { - if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) + if (!model.Id.HasValue) { return null; } - foreach (var shop in _source.Shops) - { - if ((!string.IsNullOrEmpty(model.Name) && - shop.Name == model.Name) || - (model.Id.HasValue && shop.Id == model.Id)) - { - return shop.GetViewModel; - } - } - return null; + return _source.Shops.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel; } public List GetFilteredList(ShopSearchModel model) { - var result = new List(); if (string.IsNullOrEmpty(model.Name)) { - return result; + return new(); } - foreach (var shop in _source.Shops) - { - if (shop.Name.Contains(model.Name ?? string.Empty)) - { - result.Add(shop.GetViewModel); - } - } - return result; + return _source.Shops + .Select(x => x.GetViewModel) + .Where(x => x.Name.Contains(model.Name ?? string.Empty)) + .ToList(); } public List GetFullList() { - var result = new List(); - foreach (var shop in _source.Shops) - { - result.Add(shop.GetViewModel); - } - return result; + return _source.Shops + .Select(shop => shop.GetViewModel) + .ToList(); } public ShopViewModel? Insert(ShopBindingModel model) { - model.Id = 1; - foreach (var shop in _source.Shops) - { - if (model.Id <= shop.Id) - { - model.Id = shop.Id + 1; - } - } + model.Id = _source.Shops.Count > 0 ? _source.Shops.Max(x => x.Id) + 1 : 1; var newShop = Shop.Create(model); if (newShop == null) { return null; } _source.Shops.Add(newShop); + _source.SaveShops(); return newShop.GetViewModel; } public ShopViewModel? Update(ShopBindingModel model) { - foreach (var shop in _source.Shops) + var shop = _source.Shops.FirstOrDefault(x => x.Id == model.Id); + if (shop == null) { - if (shop.Id == model.Id) + return null; + } + shop.Update(model); + _source.SaveShops(); + return shop.GetViewModel; + } + + public bool HasNeedPastries(IPastryModel pastry, int needCount) + { + var resultCount = _source.Shops + .Select(shop => shop.Pastries + .FirstOrDefault(x => x.Key == pastry.Id).Value.Item2) + .Sum(); + return resultCount >= needCount; + } + + public bool SellPastries(IPastryModel pastry, int needCount) + { + if (!HasNeedPastries(pastry, needCount)) + { + return false; + } + foreach (var pair in _source.Shops.Where(shop => shop.Pastries.ContainsKey(pastry.Id))) + { + var tuple = pair.Pastries[pastry.Id]; + var diff = Math.Min(tuple.Item2, needCount); + pair.Pastries[pastry.Id] = (tuple.Item1, tuple.Item2 - diff); + needCount -= diff; + if (needCount <= 0) { - shop.Update(model); - return shop.GetViewModel; + return true; } } - return null; + + return true; } } } diff --git a/ConfectionaryListImplement/ShopStorage.cs b/ConfectionaryListImplement/ShopStorage.cs index 893b6ad..c9ff5c3 100644 --- a/ConfectionaryListImplement/ShopStorage.cs +++ b/ConfectionaryListImplement/ShopStorage.cs @@ -2,6 +2,7 @@ using ConfectioneryContracts.SearchModels; using ConfectioneryContracts.StoragesContract; using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Models; namespace ConfectioneryListImplement { @@ -72,6 +73,11 @@ namespace ConfectioneryListImplement return result; } + public bool HasNeedPastries(IPastryModel pastry, int needCount) + { + throw new NotImplementedException(); + } + public ShopViewModel? Insert(ShopBindingModel model) { model.Id = 1; @@ -91,6 +97,11 @@ namespace ConfectioneryListImplement return newShop.GetViewModel; } + public bool SellPastries(IPastryModel pastry, int needCount) + { + throw new NotImplementedException(); + } + public ShopViewModel? Update(ShopBindingModel model) { foreach (var shop in _source.Shops) diff --git a/ConfectioneryContracts/StoragesContract/IShopStorage.cs b/ConfectioneryContracts/StoragesContract/IShopStorage.cs index 4276f2d..1f53aa5 100644 --- a/ConfectioneryContracts/StoragesContract/IShopStorage.cs +++ b/ConfectioneryContracts/StoragesContract/IShopStorage.cs @@ -1,6 +1,7 @@ using ConfectioneryContracts.BindingModels; using ConfectioneryContracts.SearchModels; using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Models; namespace ConfectioneryContracts.StoragesContract { @@ -12,5 +13,8 @@ namespace ConfectioneryContracts.StoragesContract ShopViewModel? Insert(ShopBindingModel model); ShopViewModel? Update(ShopBindingModel model); ShopViewModel? Delete(ShopBindingModel model); + + bool HasNeedPastries(IPastryModel pastry, int needCount); + public bool SellPastries(IPastryModel pastry, int needCount); } }