Промежуточное сохранение.
This commit is contained in:
parent
1e56812c08
commit
a0d1981bee
@ -18,5 +18,7 @@ namespace BlacksmithWorkshopContracts.BindingModels
|
||||
public Dictionary<int, (IManufactureModel, int)> Manufactures { get; set; } = new();
|
||||
|
||||
public int Id { get; set; }
|
||||
|
||||
public int MaxCountManufactures { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -23,5 +23,9 @@ namespace BlacksmithWorkshopContracts.BusinessLogicsContracts
|
||||
bool Delete(ShopBindingModel model);
|
||||
|
||||
bool AddManufacture(ShopSearchModel model, IManufactureModel manufacture, int count);
|
||||
|
||||
bool SellManufatures(IManufactureModel model, int count);
|
||||
|
||||
bool AddManufactures(IManufactureModel model, int count);
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
using BlacksmithWorkshopContracts.BindingModels;
|
||||
using BlacksmithWorkshopContracts.SearchModels;
|
||||
using BlacksmithWorkshopContracts.ViewModels;
|
||||
using BlacksmithWorkshopDataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@ -22,5 +23,7 @@ namespace BlacksmithWorkshopContracts.StoragesContracts
|
||||
ShopViewModel? Update(ShopBindingModel model);
|
||||
|
||||
ShopViewModel? Delete(ShopBindingModel model);
|
||||
|
||||
bool SellManufactures(IManufactureModel, int count);
|
||||
}
|
||||
}
|
||||
|
@ -22,5 +22,8 @@ namespace BlacksmithWorkshopContracts.ViewModels
|
||||
public DateTime DateOpen { get; set; } = DateTime.Now;
|
||||
|
||||
public Dictionary<int, (IManufactureModel, int)> Manufactures { get; set; } = new();
|
||||
|
||||
[DisplayName("Вместимость магазина")]
|
||||
public int MaxCountManufactures { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -19,6 +19,9 @@ namespace BlacksmithWorkshopDataModels.Models
|
||||
//дата открытия магазина
|
||||
DateTime DateOpen { get; }
|
||||
|
||||
//максимальное кол-во изделий в магазине
|
||||
int MaxCountManufactures { get; }
|
||||
|
||||
//изделия в магазине
|
||||
Dictionary<int, (IManufactureModel, int)> Manufactures { get; }
|
||||
}
|
||||
|
@ -18,12 +18,16 @@ namespace BlacksmithWorkshopFileImplement
|
||||
|
||||
private readonly string ManufactureFileName = "Manufacture.xml";
|
||||
|
||||
private readonly string ShopFileName = "Shop.xml";
|
||||
|
||||
public List<WorkPiece> WorkPieces { get; private set; }
|
||||
|
||||
public List<Order> Orders { get; private set; }
|
||||
|
||||
public List<Manufacture> Manufactures { get; private set; }
|
||||
|
||||
public List<Manufacture> Shops { get; private set; }
|
||||
|
||||
public static DataFileSingleton GetInstance()
|
||||
{
|
||||
if (instance == null)
|
||||
@ -40,11 +44,14 @@ namespace BlacksmithWorkshopFileImplement
|
||||
|
||||
public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement);
|
||||
|
||||
public void SaveShops() => SaveData(Shops, ShopFileName, "Shops", x => x.GetXElement);
|
||||
|
||||
private DataFileSingleton()
|
||||
{
|
||||
WorkPieces = LoadData(WorkPieceFileName, "WorkPiece", x => WorkPiece.Create(x)!)!;
|
||||
Manufactures = LoadData(ManufactureFileName, "Manufacture", x => Manufacture.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)
|
||||
|
@ -0,0 +1,139 @@
|
||||
using BlacksmithWorkshopContracts.BindingModels;
|
||||
using BlacksmithWorkshopContracts.SearchModels;
|
||||
using BlacksmithWorkshopContracts.StoragesContracts;
|
||||
using BlacksmithWorkshopContracts.ViewModels;
|
||||
using BlacksmithWorkshopDataModels.Models;
|
||||
using BlacksmithWorkshopFileImplement.Models;
|
||||
using System.Net;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BlacksmithWorkshopFileImplement.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 shop = _source.Shops.FirstOrDefault(x => x.ShopName.Contains(model.ShopName) || x.Id == model.Id);
|
||||
|
||||
if (shop == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
shop.Update(model);
|
||||
_source.SaveShops();
|
||||
|
||||
return shop.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 SellIceCreams(IIceCreamModel model, int count)
|
||||
{
|
||||
if (_source.Shops.Select(x => x.IceCreams.FirstOrDefault(x => x.Key == model.Id).Value.Item2).Sum() < count)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var list = _source.Shops.Where(x => x.IceCreams.ContainsKey(model.Id));
|
||||
|
||||
foreach (var shop in list)
|
||||
{
|
||||
if (shop.IceCreams[model.Id].Item2 < count)
|
||||
{
|
||||
count -= shop.IceCreams[model.Id].Item2;
|
||||
shop.IceCreams[model.Id] = (shop.IceCreams[model.Id].Item1, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
shop.IceCreams[model.Id] = (shop.IceCreams[model.Id].Item1, shop.IceCreams[model.Id].Item2 - count);
|
||||
count -= count;
|
||||
}
|
||||
|
||||
Update(new()
|
||||
{
|
||||
ShopName = shop.ShopName,
|
||||
Address = shop.Address,
|
||||
DateOpen = shop.DateOpen,
|
||||
MaxCountManufactures = shop.MaxCountManufacturess,
|
||||
Manufactures = shop.Manufactures
|
||||
});
|
||||
|
||||
if (count == 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,110 @@
|
||||
using BlacksmithWorkshopContracts.BindingModels;
|
||||
using BlacksmithWorkshopContracts.ViewModels;
|
||||
using BlacksmithWorkshopDataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace BlacksmithWorkshopFileImplement.Models
|
||||
{
|
||||
public class Shop : IShopModel
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
public string ShopName { get; set; } = string.Empty;
|
||||
|
||||
public string Address { get; set; } = string.Empty;
|
||||
|
||||
public DateTime DateOpen { get; set; }
|
||||
|
||||
public int MaxCountManufactures { get; set; }
|
||||
|
||||
public Dictionary<int, int> countManufacture { get; private set; } = new();
|
||||
|
||||
public Dictionary<int, (IManufactureModel, int)> _manufactures = null;
|
||||
|
||||
public Dictionary<int, (IManufactureModel, int)> Manufactures
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_manufactures == null)
|
||||
{
|
||||
var source = DataFileSingleton.GetInstance();
|
||||
_manufactures = countManufacture.ToDictionary(x => x.Key, y => ((source.Manufactures.FirstOrDefault(z => z.Id == y.Key) as IManufactureModel)!, y.Value));
|
||||
}
|
||||
|
||||
return _manufactures;
|
||||
}
|
||||
}
|
||||
|
||||
public static Shop? Create(ShopBindingModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new Shop()
|
||||
{
|
||||
Id = model.Id,
|
||||
ShopName = model.ShopName,
|
||||
Address = model.Address,
|
||||
DateOpen = model.DateOpen,
|
||||
MaxCountManufactures = model.MaxCountManufactures,
|
||||
countManufacture = model.Manufactures.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,
|
||||
DateOpen = Convert.ToDateTime(element.Element("DateOpen")!.Value),
|
||||
MaxCountManufactures = Convert.ToInt32(element.Element("MaxCountManufactures")!.Value),
|
||||
countManufacture = element.Element("Manufactures")!.Elements("Manufactures").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;
|
||||
DateOpen = model.DateOpen;
|
||||
MaxCountManufactures = model.MaxCountManufactures;
|
||||
countManufacture = model.Manufactures.ToDictionary(x => x.Key, x => x.Value.Item2);
|
||||
_manufactures = null;
|
||||
}
|
||||
|
||||
public ShopViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
ShopName = ShopName,
|
||||
Address = Address,
|
||||
DateOpen = DateOpen,
|
||||
MaxCountManufactures = MaxCountManufactures,
|
||||
Manufactures = Manufactures
|
||||
};
|
||||
public XElement GetXElement => new("Shop",
|
||||
new XAttribute("Id", Id),
|
||||
new XElement("ShopName", ShopName),
|
||||
new XElement("Address", Address),
|
||||
new XElement("DateOpen", DateOpen.ToString()),
|
||||
new XElement("MaxCountManufactures", MaxCountManufactures.ToString()),
|
||||
new XElement("Manufactures", countManufature.Select(x => new XElement("Manufactures",
|
||||
new XElement("Key", x.Key),
|
||||
new XElement("Value", x.Value))).ToArray()));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user