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

View File

@ -1,7 +1,60 @@
namespace ConfectionaryFileImplement
{
public class Class1
{
using ConfectioneryContracts.BindingModels;
using ConfectioneryContracts.ViewModels;
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>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\ConfectioneryContracts\ConfectioneryContracts.csproj" />
<ProjectReference Include="..\ConfectioneryDataModels\ConfectioneryDataModels.csproj" />
</ItemGroup>
</Project>

View File

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

View File

@ -1,12 +1,76 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConfectioneryContracts.BindingModels;
using ConfectioneryContracts.SearchModels;
using ConfectioneryContracts.StoragesContract;
using ConfectioneryContracts.ViewModels;
using ConfectioneryFileImplement.Models;
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 System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConfectioneryContracts.BindingModels;
using ConfectioneryContracts.ViewModels;
using ConfectioneryDataModels.Models;
using ConfectioneryDataModels.Models;
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.StoragesContract;
using ConfectioneryContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConfectioneryFileImplement.Models;
namespace ConfectioneryFileImplement
{
@ -24,7 +20,7 @@ namespace ConfectioneryFileImplement
if (element != null)
{
_source.Pastries.Remove(element);
_source.Savepastrys();
_source.SavePastries();
return element.GetViewModel;
}
return null;
@ -63,7 +59,15 @@ namespace ConfectioneryFileImplement
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)