готово для пул реквеста лаба 2 сложная

This commit is contained in:
ekallin 2024-04-08 11:28:35 +03:00
parent 2c3b04226c
commit 736bd45bbc
2 changed files with 12 additions and 4 deletions

View File

@ -103,7 +103,7 @@ namespace SushiBarBusinessLogic
if (element.MaxCountSushis - countSushis < count) if (element.MaxCountSushis - countSushis < count)
{ {
_logger.LogWarning("В магазине не хватает места"); _logger.LogWarning("В магазине не хватает места");
throw new ArgumentOutOfRangeException("Не зватает места в магазинах, увы... кушать меньше надо ", nameof(countSushis)); throw new ArgumentOutOfRangeException("Не зватает места в магазине, увы... кушать меньше надо ", nameof(countSushis));
} }
if (element.ShopSushis.TryGetValue(sushi.Id, out var samesushi)) if (element.ShopSushis.TryGetValue(sushi.Id, out var samesushi))
{ {

View File

@ -10,14 +10,17 @@ namespace SushiBarFileImplement.Implements
public class ShopStorage : IShopStorage public class ShopStorage : IShopStorage
{ {
private readonly DataFileSingleton _source; private readonly DataFileSingleton _source;
public ShopStorage() public ShopStorage()
{ {
_source = DataFileSingleton.GetInstance(); _source = DataFileSingleton.GetInstance();
} }
public List<ShopViewModel> GetFullList() public List<ShopViewModel> GetFullList()
{ {
return _source.Shops.Select(x => x.GetViewModel).ToList(); return _source.Shops.Select(x => x.GetViewModel).ToList();
} }
public List<ShopViewModel> GetFilteredList(ShopSearchModel model) public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
{ {
if (string.IsNullOrEmpty(model.ShopName)) if (string.IsNullOrEmpty(model.ShopName))
@ -27,6 +30,7 @@ namespace SushiBarFileImplement.Implements
return _source.Shops.Where(x => x.ShopName.Contains(model.ShopName)).Select(x => x.GetViewModel).ToList(); return _source.Shops.Where(x => x.ShopName.Contains(model.ShopName)).Select(x => x.GetViewModel).ToList();
} }
public ShopViewModel? GetElement(ShopSearchModel model) public ShopViewModel? GetElement(ShopSearchModel model)
{ {
if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue) if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue)
@ -35,6 +39,7 @@ namespace SushiBarFileImplement.Implements
} }
return _source.Shops.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ShopName) && x.ShopName == model.ShopName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; return _source.Shops.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ShopName) && x.ShopName == model.ShopName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
} }
public ShopViewModel? Insert(ShopBindingModel model) public ShopViewModel? Insert(ShopBindingModel model)
{ {
model.Id = _source.Shops.Count > 0 ? _source.Shops.Max(x => x.Id) + 1 : 1; model.Id = _source.Shops.Count > 0 ? _source.Shops.Max(x => x.Id) + 1 : 1;
@ -47,6 +52,7 @@ namespace SushiBarFileImplement.Implements
_source.SaveShops(); _source.SaveShops();
return newShop.GetViewModel; return newShop.GetViewModel;
} }
public ShopViewModel? Update(ShopBindingModel model) public ShopViewModel? Update(ShopBindingModel model)
{ {
var component = _source.Shops.FirstOrDefault(x => x.Id == model.Id); var component = _source.Shops.FirstOrDefault(x => x.Id == model.Id);
@ -58,6 +64,7 @@ namespace SushiBarFileImplement.Implements
_source.SaveShops(); _source.SaveShops();
return component.GetViewModel; return component.GetViewModel;
} }
public ShopViewModel? Delete(ShopBindingModel model) public ShopViewModel? Delete(ShopBindingModel model)
{ {
var element = _source.Shops.FirstOrDefault(x => x.Id == model.Id); var element = _source.Shops.FirstOrDefault(x => x.Id == model.Id);
@ -69,11 +76,14 @@ namespace SushiBarFileImplement.Implements
} }
return null; return null;
} }
public bool CheckCountSushi(ISushiModel model, int count) public bool CheckCountSushi(ISushiModel model, int count)
{ {
int store = _source.Shops.Select(x => x.ShopSushis.Select(y => (y.Value.Item1.Id == model.Id ? y.Value.Item2 : 0)).Sum()).Sum(); int store = _source.Shops.Select(x => x.ShopSushis.Select(y =>
(y.Value.Item1.Id == model.Id ? y.Value.Item2 : 0)).Sum()).Sum();
return store >= count; return store >= count;
} }
public bool SellSushis(ISushiModel model, int count) public bool SellSushis(ISushiModel model, int count)
{ {
var sushi = _source.Sushis.FirstOrDefault(x => x.Id == model.Id); var sushi = _source.Sushis.FirstOrDefault(x => x.Id == model.Id);
@ -93,8 +103,6 @@ namespace SushiBarFileImplement.Implements
if (count <= 0) if (count <= 0)
{ {
//throw new ArgumentNullException("Такого количества суш нет, продать столько низя ");
break; break;
} }
} }