fix
This commit is contained in:
parent
9e8a3d4e87
commit
3a777a93df
@ -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()));
|
||||
}
|
||||
}
|
@ -6,4 +6,9 @@
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\ConfectioneryContracts\ConfectioneryContracts.csproj" />
|
||||
<ProjectReference Include="..\ConfectioneryDataModels\ConfectioneryDataModels.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
@ -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,24 @@ namespace ConfectioneryFileImplement
|
||||
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)
|
||||
{
|
||||
if (model == null)
|
||||
@ -68,7 +86,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)
|
||||
);
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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()));
|
||||
}
|
||||
}
|
||||
}
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user