Mochalov D.V. LabWork02_Hard #6

Closed
b0n3l3sS wants to merge 39 commits from LabWork02_Hard into LabWork01_Hard
Showing only changes of commit 0b3b9b391a - Show all commits

View File

@ -81,7 +81,7 @@ namespace LawFirmFileImplement.Implements
return shop.GetViewModel;
}
public bool SellDocument(IDocumentModel model, int count)
/* public bool SellDocument(IDocumentModel model, int count)
{
// переделать под linq
@ -151,5 +151,69 @@ namespace LawFirmFileImplement.Implements
}
return true;
}
*/
public bool SellDocument(IDocumentModel model, int count)
{
var document = source.Documents.FirstOrDefault(x => x.Id == model.Id);
if (document == null)
{
return false;
}
var countStore = count;
var shopDocuments = source.Shops.SelectMany(shop => shop.ShopDocuments.Where(doc => doc.Value.Item1.Id == document.Id));
foreach (var doc in shopDocuments)
{
count -= doc.Value.Item2;
if (count <= 0)
{
break;
}
}
if (count > 0)
{
return false;
}
count = countStore;
foreach (var shop in source.Shops)
{
var documents = shop.ShopDocuments;
foreach (var doc in documents.Where(x => x.Value.Item1.Id == document.Id))
{
var min = Math.Min(doc.Value.Item2, count);
documents[doc.Value.Item1.Id] = (doc.Value.Item1, doc.Value.Item2 - min);
count -= min;
if (count <= 0)
{
break;
}
}
shop.Update(new ShopBindingModel
{
Id = shop.Id,
Name = shop.Name,
Adress = shop.Adress,
OpeningDate = shop.OpeningDate,
MaxCountDocuments = shop.MaxCountDocuments,
ShopDocuments = documents
});
source.SaveShops();
if (count <= 0) break;
}
return count <= 0;
}
}
}