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; return instance;
} }
public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement); 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); public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement);
private DataFileSingleton() private DataFileSingleton()
{ {

View File

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

View File

@ -44,10 +44,10 @@ namespace PizzeriaFileImplement.Models
Id = Convert.ToInt32(element.Attribute("Id")!.Value), Id = Convert.ToInt32(element.Attribute("Id")!.Value),
PizzaId = Convert.ToInt32(element.Element("PizzaId")!.Value), PizzaId = Convert.ToInt32(element.Element("PizzaId")!.Value),
Count = Convert.ToInt32(element.Element("Count")!.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)), Status = (OrderStatus)(Enum.Parse(typeof(OrderStatus), element.Element("Status")!.Value)),
DateCreate = Convert.ToDateTime(element.Element("DateCreate")!.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; return;
} }
Status = model.Status; Status = model.Status;
if (model.Status == OrderStatus.Выдан) DateImplement = model.DateImplement;
} }
public OrderViewModel GetViewModel => new() public OrderViewModel GetViewModel => new()
{ {

View File

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