This commit is contained in:
Данияр Аглиуллов 2023-02-15 04:07:03 +04:00
parent 9e8a3d4e87
commit 3a777a93df
6 changed files with 246 additions and 27 deletions

View File

@ -1,7 +1,60 @@
namespace ConfectionaryFileImplement using ConfectioneryContracts.BindingModels;
{ using ConfectioneryContracts.ViewModels;
public class Class1 using ConfectioneryDataModels.Models;
{ using System.Xml.Linq;
namespace ConfectioneryFileImplement.Models
{
public class Component : IComponentModel
{
public int Id { get; private set; }
public string ComponentName { get; private set; } = string.Empty;
public double Cost { get; set; }
public static Component? Create(ComponentBindingModel model)
{
if (model == null)
{
return null;
}
return new Component()
{
Id = model.Id,
ComponentName = model.ComponentName,
Cost = model.Cost
};
}
public static Component? Create(XElement element)
{
if (element == null)
{
return null;
}
return new Component()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
ComponentName = element.Element("ComponentName")!.Value,
Cost = Convert.ToDouble(element.Element("Cost")!.Value)
};
}
public void Update(ComponentBindingModel model)
{
if (model == null)
{
return;
}
ComponentName = model.ComponentName;
Cost = model.Cost;
}
public ComponentViewModel GetViewModel => new()
{
Id = Id,
ComponentName = ComponentName,
Cost = Cost
};
public XElement GetXElement => new("Component",
new XAttribute("Id", Id),
new XElement("ComponentName", ComponentName),
new XElement("Cost", Cost.ToString()));
} }
} }

View File

@ -6,4 +6,9 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\ConfectioneryContracts\ConfectioneryContracts.csproj" />
<ProjectReference Include="..\ConfectioneryDataModels\ConfectioneryDataModels.csproj" />
</ItemGroup>
</Project> </Project>

View File

@ -4,7 +4,7 @@ using ConfectioneryDataModels.Enums;
using ConfectioneryDataModels.Models; using ConfectioneryDataModels.Models;
using System.Xml.Linq; using System.Xml.Linq;
namespace ConfectioneryFileImplement namespace ConfectioneryFileImplement.Models
{ {
public class Order : IOrderModel public class Order : IOrderModel
{ {
@ -39,6 +39,24 @@ namespace ConfectioneryFileImplement
Id = model.Id, Id = model.Id,
}; };
} }
public static Order? Create(XElement element)
{
if (element == null)
{
return null;
}
return new()
{
Id = Convert.ToInt32(element.Attribute("Id")),
Sum = Convert.ToDouble(element.Element("Sum")),
Count = Convert.ToInt32(element.Element("Count")),
Status = (OrderStatus)Convert.ToInt32(element.Element("Status")),
PastryId = Convert.ToInt32(element.Element("PastryId")),
DateCreate = Convert.ToDateTime(element.Element("DateCreate")),
DateImplement = Convert.ToDateTime(element.Element("DateImplement")),
};
}
public void Update(OrderBindingModel? model) public void Update(OrderBindingModel? model)
{ {
if (model == null) if (model == null)
@ -68,7 +86,7 @@ namespace ConfectioneryFileImplement
new XElement("PastryId", PastryId), new XElement("PastryId", PastryId),
new XElement("Count", Count), new XElement("Count", Count),
new XElement("Sum", Sum.ToString()), new XElement("Sum", Sum.ToString()),
new XElement("Status", Status), new XElement("Status", (int)Status),
new XElement("DateCreate", DateCreate), new XElement("DateCreate", DateCreate),
new XElement("DateImplement", DateImplement) new XElement("DateImplement", DateImplement)
); );

View File

@ -1,12 +1,76 @@
using System; using ConfectioneryContracts.BindingModels;
using System.Collections.Generic; using ConfectioneryContracts.SearchModels;
using System.Linq; using ConfectioneryContracts.StoragesContract;
using System.Text; using ConfectioneryContracts.ViewModels;
using System.Threading.Tasks; using ConfectioneryFileImplement.Models;
namespace ConfectioneryFileImplement namespace ConfectioneryFileImplement
{ {
internal class OrderStorage public class OrderStorage : IOrderStorage
{ {
private readonly DataFileSingleton _source;
public OrderStorage()
{
_source = DataFileSingleton.GetInstance();
}
public OrderViewModel? Delete(OrderBindingModel model)
{
var element = _source.Orders.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
_source.Orders.Remove(element);
_source.SaveOrders();
return element.GetViewModel;
}
return null;
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
return _source.Orders.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel;
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
var result = GetElement(model);
return result != null ? new() { result } : new();
}
public List<OrderViewModel> GetFullList()
{
return _source.Orders
.Select(x => x.GetViewModel)
.ToList();
}
public OrderViewModel? Insert(OrderBindingModel model)
{
model.Id = _source.Orders.Count > 0 ? _source.Orders.Max(x => x.Id) + 1 : 1;
var newOrder = Order.Create(model);
if (newOrder == null)
{
return null;
}
_source.Orders.Add(newOrder);
_source.SaveOrders();
return newOrder.GetViewModel;
}
public OrderViewModel? Update(OrderBindingModel model)
{
var pastry = _source.Orders.FirstOrDefault(x => x.Id == model.Id);
if (pastry == null)
{
return null;
}
pastry.Update(model);
_source.SaveOrders();
return pastry.GetViewModel;
}
} }
} }

View File

@ -1,12 +1,87 @@
using System; using ConfectioneryContracts.BindingModels;
using System.Collections.Generic; using ConfectioneryContracts.ViewModels;
using System.Linq; using ConfectioneryDataModels.Models;
using System.Text; using ConfectioneryDataModels.Models;
using System.Threading.Tasks; using System.Xml.Linq;
namespace ConfectionaryFileImplement namespace ConfectioneryFileImplement.Models
{ {
internal class Pastry public class Pastry : IPastryModel
{ {
public int Id { get; private set; }
public string PastryName { get; private set; } = string.Empty;
public double Price { get; private set; }
public Dictionary<int, int> Components { get; private set; } = new();
private Dictionary<int, (IComponentModel, int)>? _PastryComponents = null;
public Dictionary<int, (IComponentModel, int)> PastryComponents
{
get
{
if (_PastryComponents == null)
{
var source = DataFileSingleton.GetInstance();
_PastryComponents = Components.ToDictionary(x => x.Key, y =>
((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!,
y.Value));
}
return _PastryComponents;
}
}
public static Pastry? Create(PastryBindingModel model)
{
if (model == null)
{
return null;
}
return new Pastry()
{
Id = model.Id,
PastryName = model.PastryName,
Price = model.Price,
Components = model.PastryComponents.ToDictionary(x => x.Key, x => x.Value.Item2)
};
}
public static Pastry? Create(XElement element)
{
if (element == null)
{
return null;
}
return new Pastry()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
PastryName = element.Element("PastryName")!.Value,
Price = Convert.ToDouble(element.Element("Price")!.Value),
Components = element.Element("PastryComponents")!.Elements("PastryComponent").ToDictionary(x =>
Convert.ToInt32(x.Element("Key")?.Value), x =>
Convert.ToInt32(x.Element("Value")?.Value))
};
}
public void Update(PastryBindingModel model)
{
if (model == null)
{
return;
}
PastryName = model.PastryName;
Price = model.Price;
Components = model.PastryComponents.ToDictionary(x => x.Key, x => x.Value.Item2);
_PastryComponents = null;
}
public PastryViewModel GetViewModel => new()
{
Id = Id,
PastryName = PastryName,
Price = Price,
PastryComponents = PastryComponents
};
public XElement GetXElement => new("Pastry",
new XAttribute("Id", Id),
new XElement("PastryName", PastryName),
new XElement("Price", Price.ToString()),
new XElement("PastryComponents", Components.Select(x =>
new XElement("PastryComponent",
new XElement("Key", x.Key),
new XElement("Value", x.Value))).ToArray()));
} }
} }

View File

@ -2,11 +2,7 @@
using ConfectioneryContracts.SearchModels; using ConfectioneryContracts.SearchModels;
using ConfectioneryContracts.StoragesContract; using ConfectioneryContracts.StoragesContract;
using ConfectioneryContracts.ViewModels; using ConfectioneryContracts.ViewModels;
using System; using ConfectioneryFileImplement.Models;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConfectioneryFileImplement namespace ConfectioneryFileImplement
{ {
@ -24,7 +20,7 @@ namespace ConfectioneryFileImplement
if (element != null) if (element != null)
{ {
_source.Pastries.Remove(element); _source.Pastries.Remove(element);
_source.Savepastrys(); _source.SavePastries();
return element.GetViewModel; return element.GetViewModel;
} }
return null; return null;
@ -63,7 +59,15 @@ namespace ConfectioneryFileImplement
public PastryViewModel? Insert(PastryBindingModel model) public PastryViewModel? Insert(PastryBindingModel model)
{ {
throw new NotImplementedException(); model.Id = _source.Pastries.Count > 0 ? _source.Pastries.Max(x => x.Id) + 1 : 1;
var newPastry = Pastry.Create(model);
if (newPastry == null)
{
return null;
}
_source.Pastries.Add(newPastry);
_source.SavePastries();
return newPastry.GetViewModel;
} }
public PastryViewModel? Update(PastryBindingModel model) public PastryViewModel? Update(PastryBindingModel model)