немного изменений и добавлены классы для магазина в ...FileImplement

This commit is contained in:
ekallin 2024-04-07 12:19:30 +03:00
parent 7f2811d1cf
commit 7e2dcf663e
13 changed files with 295 additions and 14 deletions

View File

@ -2,6 +2,7 @@
using SushiBar;
using SushiBarContracts.BindingModel;
using SushiBarContracts.BusinessLogicsContracts;
using SushiBarView.Shops;
namespace SushiBarView
{

View File

@ -53,6 +53,7 @@ namespace SushiBarBusinessLogic.BusinessLogic
public bool FinishOrder(OrderBindingModel model)
{
return ChangeStatus(model, OrderStatus.Готов);
}
public bool DeliveryOrder(OrderBindingModel model)

View File

@ -6,6 +6,7 @@ using SushiBarContracts.StoragesContracts;
using SushiBarContracts.ViewModels;
using SushiBarDataModels;
using SushiBarDataModels.Models;
using System.Numerics;
namespace SushiBarBusinessLogic
{
@ -14,12 +15,13 @@ namespace SushiBarBusinessLogic
private readonly ILogger _logger;
private readonly IShopStorage _shopStorage;
public ShopLogic(ILogger<ShopLogic> logger, IShopStorage shopStorage)
{
_logger = logger;
_shopStorage = shopStorage;
}
public List<ShopViewModel>? ReadList(ShopSearchModel? model)
{
_logger.LogInformation("ReadList. Id:{ Id}, ShopName:{ ShopName}", model?.Id, model?.ShopName);
@ -53,7 +55,7 @@ namespace SushiBarBusinessLogic
public bool Create(ShopBindingModel model)
{
CheckModel(model);
if(_shopStorage.Insert(model) == null)
if (_shopStorage.Insert(model) == null)
{
_logger.LogWarning("Вставка в хранилище прервана");
return false;
@ -64,7 +66,7 @@ namespace SushiBarBusinessLogic
public bool Update(ShopBindingModel model)
{
CheckModel(model);
if(_shopStorage.Update(model) == null)
if (_shopStorage.Update(model) == null)
{
_logger.LogWarning("Обновление прервано");
return false;
@ -75,7 +77,7 @@ namespace SushiBarBusinessLogic
public bool Delete(ShopBindingModel model)
{
CheckModel(model);
if(_shopStorage?.Delete(model) == null)
if (_shopStorage?.Delete(model) == null)
{
_logger.LogWarning("Удаление прервано");
return false;
@ -85,15 +87,22 @@ namespace SushiBarBusinessLogic
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.ShopName, model.Id);
if (model == null)
throw new ArgumentNullException(nameof(model));
if (count <= 0)
throw new ArgumentException("Количество суши должно быть больше нуля, ало", nameof(count));
_logger.LogInformation("Добавлены суши в магазин: {ShopName}.Id:{ Id}", model.ShopName, model.Id);
var element = _shopStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("Не добавлено");
_logger.LogWarning("Не добавлено, магазин не найден с таким названием");
return false;
}
var countSushis = element.ShopSushis.Select(x => x.Value.Item2).Sum();
if (element.MaxCountSushis - countSushis < count)
{
_logger.LogWarning("В магазине не хватает места");
return false;
}
if (element.ShopSushis.TryGetValue(sushi.Id, out var samesushi))
@ -119,22 +128,42 @@ namespace SushiBarBusinessLogic
return true;
}
public bool SellSushis(ISushiModel sushi, int count)
{
if (sushi == null)
{
throw new ArgumentNullException(nameof(sushi));
}
if (count <= 0)
{
throw new ArgumentNullException("Количество суши должно быть больше нуля! алло!", nameof(count));
}
if (_shopStorage.SellSushis(sushi, count))
{
_logger.LogInformation("Selling sucsess");
return true;
}
_logger.LogInformation("Selling failed");
return false;
}
private void CheckModel(ShopBindingModel model, bool withParams = true)
{
if(model == null)
if (model == null)
throw new ArgumentNullException($"{nameof(model)} является null");
if (!withParams) return;
if (string.IsNullOrEmpty(model.ShopName))
{
throw new ArgumentNullException("Нет такого магазина", nameof(model.ShopName));
}
_logger.LogInformation("Shop. ShopName:{ShopName}.Address:{ Address}. Id:{ Id}",
_logger.LogInformation("Shop. ShopName:{ShopName}.Address:{ Address}. Id:{ Id}",
model.ShopName, model.Address, model.Id);
var element = _shopStorage.GetElement(new ShopSearchModel
{
ShopName = model.ShopName,
});
if(element != null && element.Id != model.Id && element.ShopName == model.ShopName)
if (element != null && element.Id != model.Id && element.ShopName == model.ShopName)
{
throw new InvalidOperationException("Такой магазин с таким названием уже есть");
}

View File

@ -6,6 +6,7 @@ namespace SushiBarContracts.BindingModel
public class ShopBindingModel : IShopModel
{
public int Id { get; set; }
public int MaxCountSushis { get; set; }
public string ShopName { get; set; }
public string Address { get; set; }
public DateTime DateOpening { get; set; } = DateTime.Now;

View File

@ -13,5 +13,6 @@ namespace SushiBarContracts.BusinessLogicsContracts
bool Update(ShopBindingModel model);
bool Delete(ShopBindingModel model);
bool AddSushiInShop(ShopSearchModel model, ISushiModel sushi, int count);
bool SellSushis(ISushiModel sushi, int count);
}
}

View File

@ -1,6 +1,7 @@
using SushiBarContracts.BindingModel;
using SushiBarContracts.SearchModel;
using SushiBarContracts.ViewModels;
using SushiBarDataModels.Models;
namespace SushiBarContracts.StoragesContracts
{
@ -12,5 +13,9 @@ namespace SushiBarContracts.StoragesContracts
ShopViewModel? Insert(ShopBindingModel model);
ShopViewModel? Update(ShopBindingModel model);
ShopViewModel? Delete(ShopBindingModel model);
bool SellSushis(ISushiModel model, int count);
bool CheckCountSushi(ISushiModel model, int count);
}
}

View File

@ -14,6 +14,9 @@ namespace SushiBarContracts.ViewModels
[DisplayName("Адрес")]
public string Address { get; set; } = string.Empty;
[DisplayName("Максимальное количество суши")]
public int MaxCountSushis { get; set; }
[DisplayName("Дата открытия")]
public DateTime DateOpening { get; set; } = DateTime.Now;
public Dictionary<int, (ISushiModel, int)> ShopSushis { get; set; } = new();

View File

@ -4,6 +4,7 @@ namespace SushiBarDataModels
{
public interface IShopModel : IId
{
int MaxCountSushis { get; }
string ShopName { get; }
string Address { get; }
DateTime DateOpening { get; }

View File

@ -13,9 +13,12 @@ namespace SushiBarFileImplement
private readonly string SushiFileName = "Sushi.xml";
private readonly string ShopFileName = "Shop.xml";
public List<Component> Components { get; private set; }
public List<Order> Orders { get; private set; }
public List<Sushi> Sushis { get; private set; }
public List<Shop> Shops { get; private set; }
public static DataFileSingleton GetInstance()
{
@ -29,12 +32,14 @@ namespace SushiBarFileImplement
public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement);
public void SaveSushis() => SaveData(Sushis, SushiFileName, "Sushis", x => x.GetXElement);
public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement);
public void SaveShops() => SaveData(Shops, ShopFileName, "Shops", x => x.GetXElement);
private DataFileSingleton()
{
Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!;
Sushis = LoadData(SushiFileName, "Sushi", x => Sushi.Create(x)!)!;
Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!;
Shops = LoadData(ShopFileName, "Shop", x => Shop.Create(x)!)!;
}
private static List<T>? LoadData<T>(string filename, string xmlNodeName, Func<XElement, T> selectFunction)

View File

@ -0,0 +1,115 @@
using SushiBarContracts.BindingModel;
using SushiBarContracts.SearchModel;
using SushiBarContracts.StoragesContracts;
using SushiBarContracts.ViewModels;
using SushiBarDataModels.Models;
using SushiBarFileImplement.Models;
namespace SushiBarFileImplement.Implements
{
public class ShopStorage : IShopStorage
{
private readonly DataFileSingleton _source;
public ShopStorage()
{
_source = DataFileSingleton.GetInstance();
}
public List<ShopViewModel> GetFullList()
{
return _source.Shops.Select(x => x.GetViewModel).ToList();
}
public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
{
if (string.IsNullOrEmpty(model.ShopName))
{
return new();
}
return _source.Shops.Where(x => x.ShopName.Contains(model.ShopName)).Select(x => x.GetViewModel).ToList();
}
public ShopViewModel? GetElement(ShopSearchModel model)
{
if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue)
{
return null;
}
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)
{
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)
{
var component = _source.Shops.FirstOrDefault(x => x.Id == model.Id);
if (component == null)
{
return null;
}
component.Update(model);
_source.SaveShops();
return component.GetViewModel;
}
public ShopViewModel? Delete(ShopBindingModel model)
{
var element = _source.Shops.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
_source.Shops.Remove(element);
_source.SaveShops();
return element.GetViewModel;
}
return null;
}
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();
return store >= count;
}
public bool SellSushis(ISushiModel model, int count)
{
var sushi = _source.Sushis.FirstOrDefault(x => x.Id == model.Id);
if (sushi == null || !CheckCountSushi(model, count))
{
return false;
}
foreach (var shop in _source.Shops)
{
var sushis = shop.ShopSushis;
foreach (var elem in sushis.Where(x => x.Value.Item1.Id == sushi.Id))
{
var selling = Math.Min(elem.Value.Item2, count);
sushis[elem.Value.Item1.Id] = (elem.Value.Item1, elem.Value.Item2 - selling);
count -= selling;
if (count <= 0)
{
break;
}
}
shop.Update(new ShopBindingModel
{
Id = model.Id,
ShopName = shop.ShopName,
Address = shop.Address,
DateOpening = shop.DateOpening,
ShopSushis = sushis,
MaxCountSushis = shop.MaxCountSushis
});
}
_source.SaveShops();
return true;
}
}
}

View File

@ -0,0 +1,103 @@
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;
using System.Xml.Linq;
namespace SushiBarFileImplement.Models
{
public class Shop : IShopModel
{
public int Id { get; private set; }
public string ShopName { get; private set; }
public string Address { get; private set; }
public DateTime DateOpening { get; private set; }
public int MaxCountSushis { get; private set; }
public Dictionary<int, int> Sushis { get; private set; } = new();
private Dictionary<int, (ISushiModel, int)>? _shopSushis = null;
public Dictionary<int, (ISushiModel, int)> ShopSushis
{
get
{
if (_shopSushis == null)
{
var source = DataFileSingleton.GetInstance();
_shopSushis = Sushis.ToDictionary(x => x.Key, y => ((source.Sushis.FirstOrDefault(z => z.Id == y.Key) as ISushiModel)!, y.Value));
}
return _shopSushis;
}
}
public static Shop? Create(ShopBindingModel model)
{
if (model == null)
{
return null;
}
return new Shop()
{
Id = model.Id,
ShopName = model.ShopName,
Address = model.Address,
DateOpening = model.DateOpening,
MaxCountSushis = model.MaxCountSushis,
Sushis = model.ShopSushis.ToDictionary(x => x.Key, x => x.Value.Item2)
};
}
public static Shop? Create(XElement element)
{
if (element == null)
{
return null;
}
return new Shop()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
ShopName = element.Element("ShopName")!.Value,
Address = element.Element("Address")!.Value,
MaxCountSushis = Convert.ToInt32(element.Element("MaxCountSushis")!.Value),
DateOpening = Convert.ToDateTime(element.Element("DateOpening")!.Value),
Sushis = element.Element("ShopSushis")!.Elements("ShopSushis").ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value))
};
}
public void Update(ShopBindingModel? model)
{
if (model == null)
{
return;
}
ShopName = model.ShopName;
Address = model.Address;
DateOpening = model.DateOpening;
MaxCountSushis = model.MaxCountSushis;
if (model.ShopSushis.Count > 0)
{
Sushis = model.ShopSushis.ToDictionary(x => x.Key, x => x.Value.Item2);
_shopSushis = null;
}
}
public ShopViewModel GetViewModel => new()
{
Id = Id,
ShopName = ShopName,
Address = Address,
DateOpening = DateOpening,
MaxCountSushis = MaxCountSushis,
ShopSushis = ShopSushis
};
public XElement GetXElement => new XElement("Shop",
new XAttribute("Id", Id),
new XElement("ShopName", ShopName),
new XElement("Address", Address),
new XElement("DateOpening", DateOpening),
new XElement("MaxCountSushis", MaxCountSushis),
new XElement("ShopSushis", Sushis.Select(x => new XElement("ShopSushis", new XElement("Key", x.Key), new XElement("Value", x.Value))).ToArray()));
}
}

View File

@ -2,6 +2,7 @@
using SushiBarContracts.SearchModel;
using SushiBarContracts.StoragesContracts;
using SushiBarContracts.ViewModels;
using SushiBarDataModels;
using SushiBarListImplement;
using SushiBarListImplements.Models;
@ -106,5 +107,15 @@ namespace SushiBarListImplements.Implements
}
return null;
}
public bool SellSushis(IShopModel model, int count)
{
return true;
}
public bool CheckCountSushi(IShopModel model, int count)
{
return true;
}
}
}

View File

@ -5,6 +5,7 @@ using SushiBarDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
@ -13,6 +14,7 @@ namespace SushiBarListImplements.Models
public class Shop : IShopModel
{
public int Id { get; private set; }
public int MaxCountSushis { get; private set; }
public string ShopName { get; private set; }
public string Address { get; private set; }
public DateTime DateOpening { get; private set; } = DateTime.Now;
@ -31,7 +33,8 @@ namespace SushiBarListImplements.Models
ShopName = model.ShopName,
Address = model.Address,
DateOpening = model.DateOpening,
ShopSushis = model.ShopSushis
ShopSushis = model.ShopSushis,
MaxCountSushis = model.MaxCountSushis
};
}
@ -42,6 +45,7 @@ namespace SushiBarListImplements.Models
Address = model.Address;
DateOpening = model.DateOpening;
ShopSushis = model.ShopSushis;
MaxCountSushis = model.MaxCountSushis;
}
public ShopViewModel GetViewModel => new()
@ -50,7 +54,8 @@ namespace SushiBarListImplements.Models
ShopName = ShopName,
Address = Address,
DateOpening = DateOpening,
ShopSushis = ShopSushis
ShopSushis = ShopSushis,
MaxCountSushis = MaxCountSushis
};
}
}