PIbd - 21 Bakalskaya E.D. LabWork01 HARD #8

Closed
ekallin wants to merge 8 commits from LabWork11 into LabWork01
5 changed files with 244 additions and 10 deletions
Showing only changes of commit 4c1af6acb5 - Show all commits

View File

@ -5,6 +5,7 @@ using SushiBarContracts.SearchModel;
using SushiBarContracts.StoragesContracts;
using SushiBarContracts.ViewModels;
using SushiBarDataModels;
using SushiBarDataModels.Models;
namespace SushiBarBusinessLogic
{
@ -52,19 +53,91 @@ namespace SushiBarBusinessLogic
public bool Create(ShopBindingModel model)
{
CheckModel(model);
if(_shopStorage.Insert(model) == null)
{
_logger.LogWarning("Вставка в хранилище прервана");
return false;
}
return true;
}
private bool CheckModel(ShopBindingModel model, bool withParams = true)
public bool Update(ShopBindingModel model)
{
CheckModel(model);
if(_shopStorage.Update(model) == null)
{
_logger.LogWarning("Обновление прервано");
return false;
}
return true;
}
public bool Delete(ShopBindingModel model)
{
CheckModel(model);
if(_shopStorage?.Delete(model) == null)
{
_logger.LogWarning("Удаление прервано");
return false;
}
return true;
}
public bool AddSushiInShop(ShopSearchModel model, ISushiModel sushi, int count)
{
if (model == null) throw new ArgumentNullException(nameof(model));
if(count <= 0)
throw new ArgumentException(nameof(count));
_logger.LogInformation("AddSushiInShop. ShopName:{ShopName}.Id:{ Id}", model.Name, model.Id);
var element = _shopStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("Не добавлено");
return false;
}
if (element.ShopSushis.TryGetValue(sushi.Id, out var samesushi))
{
element.ShopSushis[sushi.Id] = (sushi, samesushi.Item2 + count);
_logger.LogInformation("Same sushi found by supply. Added {0} of {1} in {2} shop", count, sushi.SushiName, element.Name);
}
else
{
element.ShopSushis[sushi.Id] = (sushi, count);
_logger.LogInformation("New sushi added by supply. Added {0} of {1} in {2} shop", count, sushi.SushiName, element.Name);
}
_shopStorage.Update(new()
{
Id = element.Id,
Name = element.Name,
Address = element.Address,
DateOpening = element.DateOpening,
ShopSushis = element.ShopSushis
});
return true;
}
private void CheckModel(ShopBindingModel model, bool withParams = true)
{
if(model == null)
throw new ArgumentNullException($"{nameof(model)} is null");
if (!withParams) return false;
throw new ArgumentNullException($"{nameof(model)} является null");
if (!withParams) return;
if (string.IsNullOrEmpty(model.Name))
{
throw new ArgumentNullException("Нет такого магазина", nameof(model.Name));
}
_logger.LogInformation("Shop. ShopName:{ShopName}.Address:{ Address}. Id:{ Id}",
model.ShopName, model.Address, model.Id);
model.Name, model.Address, model.Id);
var element = _shopStorage.GetElement(new ShopSearchModel
{
Name = model.Name,
});
if(element != null && element.Id != model.Id && element.Name == model.Name)
{
throw new InvalidOperationException("Такой магазин с таким названием уже есть");
}
}
}
}

View File

@ -1,6 +0,0 @@
namespace SushiBarDataModels
{
public class ShopBindingModel : IShopModel
{
}
}

View File

@ -8,11 +8,14 @@ namespace SushiBarListImplement
public List<Component> Components { get; set; }
public List<Order> Orders { get; set; }
public List<Sushi> Sushis { get; set; }
public List<Shop> Shops { get; set; }
private DataListSingleton()
{
Components = new List<Component>();
Orders = new List<Order>();
Sushis = new List<Sushi>();
Shops = new List<Shop>();
}
public static DataListSingleton GetInstance()
{

View File

@ -0,0 +1,108 @@
using SushiBarContracts.BindingModel;
using SushiBarContracts.SearchModel;
using SushiBarContracts.StoragesContracts;
using SushiBarContracts.ViewModels;
using SushiBarListImplement;
using SushiBarListImplements.Models;
namespace SushiBarListImplements.Implements
{
public class ShopStorage : IShopStorage
{
private DataListSingleton _source;
public ShopStorage(DataListSingleton source)
{
_source = DataListSingleton.GetInstance();
}
public List<ShopViewModel> GetFullList()
{
var result = new List<ShopViewModel>();
foreach (var shop in _source.Shops)
{
result.Add(shop.GetViewModel);
}
return result;
}
public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
{
var result = new List<ShopViewModel>();
if (model == null || !model.Id.HasValue)
{
return result;
}
foreach (var shop in _source.Shops)
{
if (shop.Id == model.Id)
{
result.Add(shop.GetViewModel);
}
}
return result;
}
public ShopViewModel? GetElement(ShopSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
foreach (var shop in _source.Shops)
{
if (shop.Id == model.Id)
{
return shop.GetViewModel;
}
}
return null;
}
public ShopViewModel? Insert(ShopBindingModel model)
{
model.Id = 1;
foreach (var shop in _source.Shops)
{
if (model.Id <= shop.Id)
{
model.Id = shop.Id + 1;
}
}
var newShop = Shop.Create(model);
if (newShop == null)
{
return null;
}
_source.Shops.Add(newShop);
return newShop.GetViewModel;
}
public ShopViewModel? Update(ShopBindingModel model)
{
foreach (var shop in _source.Shops)
{
if (shop.Id == model.Id)
{
shop.Update(model);
return shop.GetViewModel;
}
}
return null;
}
public ShopViewModel? Delete(ShopBindingModel model)
{
for (int i = 0; i < _source.Shops.Count; ++i)
{
if (_source.Shops[i].Id == model.Id)
{
var element = _source.Shops[i];
_source.Shops.RemoveAt(i);
return element.GetViewModel;
}
}
return null;
}
}
}

View File

@ -0,0 +1,56 @@
using SushiBarContracts.BindingModel;
using SushiBarContracts.ViewModels;
using SushiBarDataModels;
using SushiBarDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SushiBarListImplements.Models
{
public class Shop : IShopModel
{
public int Id { get; private set; }
public string Name { get; private set; }
public string Address { get; private set; }
public DateTime DateOpening { get; private set; } = DateTime.Now;
public Dictionary<int, (ISushiModel, int)> ShopSushis
{
get;
private set;
} = new Dictionary<int, (ISushiModel, int)>();
public static Shop? Create(ShopBindingModel? model)
{
if(model == null) return null;
return new Shop()
{
Id = model.Id,
Name = model.Name,
Address = model.Address,
DateOpening = model.DateOpening,
ShopSushis = model.ShopSushis
};
}
public void Update(ShopBindingModel? model)
{
if (model == null) return;
Name = model.Name;
Address = model.Address;
DateOpening = model.DateOpening;
ShopSushis = model.ShopSushis;
}
public ShopViewModel GetViewModel => new()
{
Id = Id,
Name = Name,
Address = Address,
DateOpening = DateOpening,
ShopSushis = ShopSushis
};
}
}