Emelyanov A.S. LabWork_2_Hard #11

Closed
Emelyanov535 wants to merge 7 commits from LabWork_02_Hard into LabWork_01_Hard
6 changed files with 222 additions and 4 deletions
Showing only changes of commit d8d2daf4ab - Show all commits

View File

@ -14,9 +14,11 @@ namespace FishFactoryFileImplement
private readonly string ComponentFileName = "Component.xml";
private readonly string OrderFileName = "Order.xml";
private readonly string CannedFileName = "Canned.xml";
private readonly string ShopFileName = "Shop.xml";
public List<Component> Components { get; private set; }
public List<Order> Orders { get; private set; }
public List<Canned> Canneds { get; private set; }
public List<Shop> Shops { get; private set; }
public static DataFileSingleton GetInstance()
{
if (instance == null)
@ -28,11 +30,13 @@ namespace FishFactoryFileImplement
public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement);
public void SaveCanneds() => SaveData(Canneds, CannedFileName, "Canneds", 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)!)!;
Canneds = LoadData(CannedFileName, "Canned", x => Canned.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,106 @@
using FishFactoryContracts.BindingModels;
using FishFactoryContracts.SearchModels;
using FishFactoryContracts.StoragesContracts;
using FishFactoryContracts.ViewModels;
using FishFactoryDataModels.Models;
using FishFactoryFileImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FishFactoryFileImplement.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.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 SellCanneds(ICannedModel model, int count)
{
if (source.Shops.Select(x => x.ListCanneds.FirstOrDefault(y => y.Key == model.Id).Value.Item2).Sum() < count)
{
return false;
}
foreach (var shop in source.Shops.Where(x => x.ListCanneds.ContainsKey(model.Id)))
{
int countInCurrentShop = shop.ListCanneds[model.Id].Item2;
if (countInCurrentShop <= count)
{
shop.ListCanneds.Remove(model.Id);
count -= countInCurrentShop;
}
else
{
shop.ListCanneds[model.Id] = (shop.ListCanneds[model.Id].Item1, countInCurrentShop - count);
count = 0;
}
if (count == 0)
{
return true;
}
}
return false;
}
}
}

View File

@ -67,11 +67,7 @@ namespace FishFactoryFileImplement.Models
{
return;
}
CannedId = model.CannedId;
Count = model.Count;
Sum = model.Sum;
Status = model.Status;
DateCreate = model.DateCreate;
DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel => new()

View File

@ -0,0 +1,104 @@
using FishFactoryContracts.BindingModels;
using FishFactoryContracts.ViewModels;
using FishFactoryDataModels;
using FishFactoryDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace FishFactoryFileImplement.Models
{
public class Shop : IShopModel
{
public int Id { get; private set; }
public string ShopName { get; private set; } = string.Empty;
public string Address { get; private set; } = string.Empty;
public DateTime DateOpening { get; private set; }
public int MaxCountCanneds { get; private set; }
private Dictionary<int, (ICannedModel, int)>? _shopCanned = null;
public Dictionary<int, int> CountCanned { get; private set; } = new();
public Dictionary<int, (ICannedModel, int)> ListCanneds {
get
{
if (_shopCanned == null)
{
var source = DataFileSingleton.GetInstance();
_shopCanned = CountCanned.ToDictionary(x => x.Key, y => ((source.Canneds.FirstOrDefault(z => z.Id == y.Key) as ICannedModel)!, y.Value));
}
return _shopCanned;
}
}
public static Shop? Create(ShopBindingModel? model)
{
if (model == null)
{
return null;
}
return new Shop()
{
Id = model.Id,
ShopName = model.ShopName,
Address = model.Address,
MaxCountCanneds = model.MaxCountCanneds,
DateOpening = model.DateOpening,
CountCanned = model.ListCanneds.ToDictionary(x => x.Key, x => x.Value.Item2),
};
}
public static Shop? Create(XElement element)
{
if (element == null)
{
return null;
}
return new()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
ShopName = element.Element("ShopName")!.Value,
Address = element.Element("Address")!.Value,
DateOpening = Convert.ToDateTime(element.Element("DateOpening")!.Value),
MaxCountCanneds = Convert.ToInt32(element.Element("MaxCountCanneds")!.Value),
CountCanned = element.Element("Canneds")!.Elements("Canned").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;
MaxCountCanneds = model.MaxCountCanneds;
CountCanned = model.ListCanneds.ToDictionary(x => x.Key, x => x.Value.Item2);
_shopCanned = null;
}
public ShopViewModel GetViewModel => new()
{
Id = Id,
ShopName = ShopName,
Address = Address,
ListCanneds = ListCanneds,
DateOpening = DateOpening,
MaxCountCanneds = MaxCountCanneds,
};
public XElement GetXElement => new("Shop",
new XAttribute("Id", Id),
new XElement("ShopName", ShopName),
new XElement("Address", Address),
new XElement("DateOpening", DateOpening),
new XElement("MaxCountCanneds", MaxCountCanneds),
new XElement("Canneds", CountCanned
.Select(x => new XElement("Canned",
new XElement("Key", x.Key),
new XElement("Value", x.Value))
).ToArray()));
}
}

View File

@ -2,6 +2,7 @@
using FishFactoryContracts.SearchModels;
using FishFactoryContracts.StoragesContracts;
using FishFactoryContracts.ViewModels;
using FishFactoryDataModels.Models;
using FishFactoryListImplement.Models;
using System;
using System.Collections.Generic;
@ -103,5 +104,10 @@ namespace FishFactoryListImplement.Implements
}
return null;
}
public bool SellCanneds(ICannedModel model, int count)
{
throw new NotImplementedException();
}
}
}

View File

@ -55,5 +55,7 @@ namespace FishFactoryListImplement.Models
ListCanneds = ListCanneds,
DateOpening = DateOpening,
};
public int MaxCountCanneds => throw new NotImplementedException();
}
}