Реализована логика хранилища магазинов

This commit is contained in:
Данияр Аглиуллов 2023-02-15 06:34:44 +04:00
parent 771985a191
commit 4368d4d6cc
3 changed files with 67 additions and 47 deletions

View File

@ -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);
_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<ShopViewModel> GetFilteredList(ShopSearchModel model)
{
var result = new List<ShopViewModel>();
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<ShopViewModel> GetFullList()
{
var result = new List<ShopViewModel>();
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)
{
shop.Update(model);
return shop.GetViewModel;
}
}
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)
{
return true;
}
}
return true;
}
}
}

View File

@ -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)

View File

@ -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);
}
}