This commit is contained in:
shadowik 2023-03-08 13:33:19 +03:00
parent 682589c187
commit 904f472cac
4 changed files with 31 additions and 56 deletions

View File

@ -21,7 +21,7 @@ namespace PizzeriaFileImplement
return instance;
}
public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement);
public void SaveReinforceds() => SaveData(Pizzas, PizzaFileName, "Reinforceds", x => x.GetXElement);
public void SavePizzas() => SaveData(Pizzas, PizzaFileName, "Pizzas", x => x.GetXElement);
public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement);
private DataFileSingleton()
{

View File

@ -8,35 +8,22 @@ namespace PizzeriaFileImplement.Implements
{
public class PizzaStorage : IPizzaStorage
{
private readonly DataFileSingleton _source;
private readonly DataFileSingleton source;
public PizzaStorage()
{
_source = DataFileSingleton.GetInstance();
source = DataFileSingleton.GetInstance();
}
public List<PizzaViewModel> GetFullList()
{
var result = new List<PizzaViewModel>();
foreach (var pizzas in _source.Pizzas)
{
result.Add(pizzas.GetViewModel);
}
return result;
return source.Pizzas.Select(x => x.GetViewModel).ToList();
}
public List<PizzaViewModel> GetFilteredList(PizzaSearchModel model)
{
var result = new List<PizzaViewModel>();
if (string.IsNullOrEmpty(model.PizzaName))
{
return result;
return new();
}
foreach (var pizzas in _source.Pizzas)
{
if (pizzas.PizzaName.Contains(model.PizzaName))
{
result.Add(pizzas.GetViewModel);
}
}
return result;
return source.Pizzas.Where(x => x.PizzaName.Contains(model.PizzaName)).Select(x => x.GetViewModel).ToList();
}
public PizzaViewModel? GetElement(PizzaSearchModel model)
{
@ -44,56 +31,42 @@ namespace PizzeriaFileImplement.Implements
{
return null;
}
foreach (var pizzas in _source.Pizzas)
{
if ((!string.IsNullOrEmpty(model.PizzaName) && pizzas.PizzaName == model.PizzaName) ||
(model.Id.HasValue && pizzas.Id == model.Id))
{
return pizzas.GetViewModel;
}
}
return null;
return source.Pizzas.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.PizzaName) && x.PizzaName == model.PizzaName) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public PizzaViewModel? Insert(PizzaBindingModel model)
{
model.Id = 1;
foreach (var pizzas in _source.Pizzas)
{
if (model.Id <= pizzas.Id)
{
model.Id = pizzas.Id + 1;
}
}
var newPizzas = Pizza.Create(model);
if (newPizzas == null)
model.Id = source.Pizzas.Count > 0 ? source.Pizzas.Max(x => x.Id) + 1 : 1;
var newPizza = Pizza.Create(model);
if (newPizza == null)
{
return null;
}
_source.Pizzas.Add(newPizzas);
return newPizzas.GetViewModel;
source.Pizzas.Add(newPizza);
source.SavePizzas();
return newPizza.GetViewModel;
}
public PizzaViewModel? Update(PizzaBindingModel model)
{
foreach (var pizzas in _source.Pizzas)
var Pizza = source.Pizzas.FirstOrDefault(x => x.Id == model.Id);
if (Pizza == null)
{
if (pizzas.Id == model.Id)
{
pizzas.Update(model);
return pizzas.GetViewModel;
}
return null;
}
return null;
Pizza.Update(model);
source.SavePizzas();
return Pizza.GetViewModel;
}
public PizzaViewModel? Delete(PizzaBindingModel model)
{
for (int i = 0; i < _source.Pizzas.Count; ++i)
var Pizza = source.Pizzas.FirstOrDefault(x => x.Id == model.Id);
if (Pizza != null)
{
if (_source.Pizzas[i].Id == model.Id)
{
var element = _source.Pizzas[i];
_source.Pizzas.RemoveAt(i);
return element.GetViewModel;
}
source.Pizzas.Remove(Pizza);
source.SavePizzas();
return Pizza.GetViewModel;
}
return null;
}

View File

@ -44,10 +44,10 @@ namespace PizzeriaFileImplement.Models
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
PizzaId = Convert.ToInt32(element.Element("PizzaId")!.Value),
Count = Convert.ToInt32(element.Element("Count")!.Value),
Sum = Convert.ToInt32(element.Element("Sum")!.Value),
Sum = Convert.ToDouble(element.Element("Sum")!.Value),
Status = (OrderStatus)(Enum.Parse(typeof(OrderStatus), element.Element("Status")!.Value)),
DateCreate = Convert.ToDateTime(element.Element("DateCreate")!.Value),
DateImplement = string.IsNullOrEmpty(dateImplement) ? null : Convert.ToDateTime(dateImplement)
DateImplement = (dateImplement == "" || dateImplement is null) ? Convert.ToDateTime(null) : Convert.ToDateTime(dateImplement)
};
}
@ -58,6 +58,7 @@ namespace PizzeriaFileImplement.Models
return;
}
Status = model.Status;
if (model.Status == OrderStatus.Выдан) DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel => new()
{

View File

@ -39,6 +39,7 @@ namespace PizzeriaListImplement.Models
}
Status = model.Status;
if (model.Status == OrderStatus.Выдан) DateImplement = model.DateImplement;
if (model.Status == OrderStatus.Выдан) DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel => new()
{