diff --git a/LawFirm/LawFirmFileImplement/Implements/ShopStorage.cs b/LawFirm/LawFirmFileImplement/Implements/ShopStorage.cs index 68a4838..fc2f25f 100644 --- a/LawFirm/LawFirmFileImplement/Implements/ShopStorage.cs +++ b/LawFirm/LawFirmFileImplement/Implements/ShopStorage.cs @@ -11,27 +11,6 @@ using System.Threading.Tasks; namespace LawFirmFileImplement.Implements { - - - // Нужно узнать, проверять ли на наличие всех товаров, или только - // наличие определенного товара - // То же самое с продажей - - - - - - - - - - - - - - - - public class ShopStorage : IShopStorage { private readonly DataFileSingleton source; @@ -101,5 +80,90 @@ namespace LawFirmFileImplement.Implements return shop.GetViewModel; } + public bool SellDocument(DocumentSearchModel model, int count) + { + var document = source.Documents.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id); + + var countStore = count; + + if (document == null) + { + return false; + } + + foreach (var shop in source.Shops) + { + foreach (var doc in shop.ShopDocuments) + { + if (doc.Value.Item1.Id == document.Id) + { + if (count > 0) + { + count = count - doc.Value.Item2; + } + } + if (count < 1) + { + break; + } + } + } + + if (count > 0) + { + return false; + } + + count = countStore; + + for (int i = 0; i < source.Shops.Count; i++) + { + var shop = source.Shops[i]; + var documents = shop.ShopDocuments; + + for (int j = 0; j < documents.Count; j++) + { + if (documents[j].Item1.Id == document.Id) + { + while (documents[j].Item2 > 0 || count > 0) + { + int tempItem2 = documents[j].Item2; + tempItem2--; + count--; + documents[j] = (documents[j].Item1, tempItem2); + } + } + + if (count < 1) + { + shop.Update(new ShopBindingModel + { + Id = shop.Id, + Name = shop.Name, + Adress = shop.Adress, + OpeningDate = shop.OpeningDate, + MaxCountDocuments = shop.MaxCountDocuments, + ShopDocuments= documents + }); + break; + } + } + shop.Update(new ShopBindingModel + { + Id = shop.Id, + Name = shop.Name, + Adress = shop.Adress, + OpeningDate = shop.OpeningDate, + MaxCountDocuments = shop.MaxCountDocuments, + ShopDocuments = documents + }); + } + + if (count > 0) + { + return false; + } + return true; + } } }