storage layer

This commit is contained in:
MaxKarme 2023-02-13 19:46:36 +03:00
parent 83296f3216
commit b616902c19
8 changed files with 545 additions and 0 deletions

View File

@ -0,0 +1,27 @@
using PizzeriaListImplement.Models;
namespace PizzeriaListImplement
{
public class DataListSingleton
{
private static DataListSingleton? _instance;
public List<Component> Components { get; set; }
public List<Order> Orders { get; set; }
public List<Pizza> Pizzas { get; set; }
private DataListSingleton()
{
Components = new List<Component>();
Orders = new List<Order>();
Pizzas = new List<Pizza>();
}
public static DataListSingleton GetInstance()
{
if (_instance == null)
{
_instance = new DataListSingleton();
}
return _instance;
}
}
}

View File

@ -0,0 +1,103 @@
using PizzeriaContracts.BindingModels;
using PizzeriaContracts.SearchModels;
using PizzeriaContracts.StoragesContracts;
using PizzeriaContracts.ViewModels;
using PizzeriaListImplement.Models;
namespace PizzeriaListImplement.Implements
{
public class ComponentStorage : IComponentStorage
{
private readonly DataListSingleton _source;
public ComponentStorage()
{
_source = DataListSingleton.GetInstance();
}
public List<ComponentViewModel> GetFullList()
{
var result = new List<ComponentViewModel>();
foreach (var component in _source.Components)
{
result.Add(component.GetViewModel);
}
return result;
}
public List<ComponentViewModel> GetFilteredList(ComponentSearchModel
model)
{
var result = new List<ComponentViewModel>();
if (string.IsNullOrEmpty(model.ComponentName))
{
return result;
}
foreach (var component in _source.Components)
{
if (component.ComponentName.Contains(model.ComponentName))
{
result.Add(component.GetViewModel);
}
}
return result;
}
public ComponentViewModel? GetElement(ComponentSearchModel model)
{
if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue)
{
return null;
}
foreach (var component in _source.Components)
{
if ((!string.IsNullOrEmpty(model.ComponentName) &&
component.ComponentName == model.ComponentName) ||
(model.Id.HasValue && component.Id == model.Id))
{
return component.GetViewModel;
}
}
return null;
}
public ComponentViewModel? Insert(ComponentBindingModel model)
{
model.Id = 1;
foreach (var component in _source.Components)
{
if (model.Id <= component.Id)
{
model.Id = component.Id + 1;
}
}
var newComponent = Component.Create(model);
if (newComponent == null)
{
return null;
}
_source.Components.Add(newComponent);
return newComponent.GetViewModel;
}
public ComponentViewModel? Update(ComponentBindingModel model)
{
foreach (var component in _source.Components)
{
if (component.Id == model.Id)
{
component.Update(model);
return component.GetViewModel;
}
}
return null;
}
public ComponentViewModel? Delete(ComponentBindingModel model)
{
for (int i = 0; i < _source.Components.Count; ++i)
{
if (_source.Components[i].Id == model.Id)
{
var element = _source.Components[i];
_source.Components.RemoveAt(i);
return element.GetViewModel;
}
}
return null;
}
}
}

View File

@ -0,0 +1,126 @@
using PizzeriaContracts.BindingModels;
using PizzeriaContracts.SearchModels;
using PizzeriaContracts.StoragesContracts;
using PizzeriaContracts.ViewModels;
using PizzeriaListImplement.Models;
namespace PizzeriaListImplement.Implements
{
public class OrderStorage : IOrderStorage
{
private readonly DataListSingleton _source;
private IPizzaStorage _pizzaStorage;
public OrderStorage(IPizzaStorage pizzaStorage)
{
_pizzaStorage = pizzaStorage;
_source = DataListSingleton.GetInstance();
}
public List<OrderViewModel> GetFullList()
{
var result = new List<OrderViewModel>();
foreach (var order in _source.Orders)
{
OrderViewModel viewModel = order.GetViewModel;
viewModel.PizzaName = _pizzaStorage.GetElement(new PizzaSearchModel { Id = order.PizzaId }).PizzaName;
result.Add(viewModel);
}
return result;
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
var result = new List<OrderViewModel>();
if (!model.Id.HasValue)
{
return result;
}
foreach (var order in _source.Orders)
{
if (model.Id.HasValue && order.Id == model.Id)
{
OrderViewModel viewModel = order.GetViewModel;
viewModel.PizzaName = _pizzaStorage.GetElement(new PizzaSearchModel { Id = order.PizzaId }).PizzaName;
result.Add(viewModel);
}
}
return result;
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
foreach (var order in _source.Orders)
{
if (model.Id.HasValue && order.Id == model.Id)
{
OrderViewModel viewModel = order.GetViewModel;
viewModel.PizzaName = _pizzaStorage.GetElement(new PizzaSearchModel { Id = order.PizzaId }).PizzaName;
return viewModel;
}
}
return null;
}
public OrderViewModel? Insert(OrderBindingModel model)
{
model.Id = 1;
foreach (var order in _source.Orders)
{
if (model.Id <= order.Id)
{
model.Id = order.Id + 1;
}
}
var newOrder = Order.Create(model);
if (newOrder == null)
{
return null;
}
_source.Orders.Add(newOrder);
return newOrder.GetViewModel;
}
public OrderViewModel? Update(OrderBindingModel model)
{
foreach (var order in _source.Orders)
{
if (order.Id == model.Id)
{
order.Update(model);
OrderViewModel viewModel = order.GetViewModel;
viewModel.PizzaName = _pizzaStorage.GetElement(new PizzaSearchModel { Id = order.PizzaId }).PizzaName;
return viewModel;
}
}
return null;
}
public OrderViewModel? Delete(OrderBindingModel model)
{
for (int i = 0; i < _source.Orders.Count; ++i)
{
if (_source.Orders[i].Id == model.Id)
{
var element = _source.Orders[i];
_source.Orders.RemoveAt(i);
OrderViewModel viewModel = element.GetViewModel;
viewModel.PizzaName = _pizzaStorage.GetElement(new PizzaSearchModel { Id = element.PizzaId }).PizzaName;
return viewModel;
}
}
return null;
}
}
}

View File

@ -0,0 +1,117 @@
using PizzeriaContracts.BindingModels;
using PizzeriaContracts.SearchModels;
using PizzeriaContracts.StoragesContracts;
using PizzeriaContracts.ViewModels;
using PizzeriaListImplement.Models;
namespace PizzeriaListImplement.Implements
{
public class PizzaStorage : IPizzaStorage
{
private readonly DataListSingleton _source;
public PizzaStorage()
{
_source = DataListSingleton.GetInstance();
}
public List<PizzaViewModel> GetFullList()
{
var result = new List<PizzaViewModel>();
foreach (var pizza in _source.Pizzas)
{
result.Add(pizza.GetViewModel);
}
return result;
}
public List<PizzaViewModel> GetFilteredList(PizzaSearchModel model)
{
var result = new List<PizzaViewModel>();
if (string.IsNullOrEmpty(model.PizzaName))
{
return result;
}
foreach (var pizza in _source.Pizzas)
{
if (pizza.PizzaName.Contains(model.PizzaName))
{
result.Add(pizza.GetViewModel);
}
}
return result;
}
public PizzaViewModel? GetElement(PizzaSearchModel model)
{
if (string.IsNullOrEmpty(model.PizzaName) && !model.Id.HasValue)
{
return null;
}
foreach (var pizza in _source.Pizzas)
{
if ((!string.IsNullOrEmpty(model.PizzaName) && pizza.PizzaName == model.PizzaName) || (model.Id.HasValue && pizza.Id == model.Id))
{
return pizza.GetViewModel;
}
}
return null;
}
public PizzaViewModel? Insert(PizzaBindingModel model)
{
model.Id = 1;
foreach (var pizza in _source.Pizzas)
{
if (model.Id <= pizza.Id)
{
model.Id = pizza.Id + 1;
}
}
var newPizza = Pizza.Create(model);
if (newPizza == null)
{
return null;
}
_source.Pizzas.Add(newPizza);
return newPizza.GetViewModel;
}
public PizzaViewModel? Update(PizzaBindingModel model)
{
foreach (var pizza in _source.Pizzas)
{
if (pizza.Id == model.Id)
{
pizza.Update(model);
return pizza.GetViewModel;
}
}
return null;
}
public PizzaViewModel? Delete(PizzaBindingModel model)
{
for (int i = 0; i < _source.Pizzas.Count; ++i)
{
if (_source.Pizzas[i].Id == model.Id)
{
var element = _source.Pizzas[i];
_source.Pizzas.RemoveAt(i);
return element.GetViewModel;
}
}
return null;
}
}
}

View File

@ -0,0 +1,41 @@
using PizzeriaContracts.BindingModels;
using PizzeriaContracts.ViewModels;
using PizzeriaDataModels;
namespace PizzeriaListImplement.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 void Update(ComponentBindingModel? model)
{
if (model == null)
{
return;
}
ComponentName = model.ComponentName;
Cost = model.Cost;
}
public ComponentViewModel GetViewModel => new()
{
Id = Id,
ComponentName = ComponentName,
Cost = Cost
};
}
}

View File

@ -0,0 +1,66 @@
using PizzeriaContracts.BindingModels;
using PizzeriaContracts.ViewModels;
using PizzeriaDataModels;
namespace PizzeriaListImplement.Models
{
public class Order : IOrderModel
{
public int PizzaId { get; private set; }
public int Count { get; private set; }
public double Sum { get; private set; }
public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен;
public DateTime DateCreate { get; private set; } = DateTime.Now;
public DateTime? DateImplement { get; private set; }
public int Id { get; private set; }
public static Order? Create(OrderBindingModel? model)
{
if (model == null)
{
return null;
}
return new Order()
{
Id = model.Id,
PizzaId = model.PizzaId,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement
};
}
public void Update(OrderBindingModel? model)
{
if (model == null)
{
return;
}
PizzaId = model.PizzaId;
Count = model.Count;
Sum = model.Sum;
Status = model.Status;
DateCreate = model.DateCreate;
DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel => new()
{
Id = Id,
PizzaId = PizzaId,
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement
};
}
}

View File

@ -0,0 +1,51 @@
using PizzeriaContracts.BindingModels;
using PizzeriaContracts.ViewModels;
using PizzeriaDataModels;
namespace PizzeriaListImplement.Models
{
public class Pizza : IPizzaModel
{
public int Id { get; private set; }
public string PizzaName { get; private set; } = string.Empty;
public double Price { get; private set; }
public Dictionary<int, (IComponentModel, int)> PizzaComponents
{
get;
private set;
} = new Dictionary<int, (IComponentModel, int)>();
public static Pizza? Create(PizzaBindingModel? model)
{
if (model == null)
{
return null;
}
return new Pizza()
{
Id = model.Id,
PizzaName = model.PizzaName,
Price = model.Price,
PizzaComponents = model.PizzaComponents
};
}
public void Update(PizzaBindingModel? model)
{
if (model == null)
{
return;
}
PizzaName = model.PizzaName;
Price = model.Price;
PizzaComponents = model.PizzaComponents;
}
public PizzaViewModel GetViewModel => new()
{
Id = Id,
PizzaName = PizzaName,
Price = Price,
PizzaComponents = PizzaComponents
};
}
}

View File

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\PizzeriaContracts\PizzeriaContracts.csproj" />
<ProjectReference Include="..\PizzeriaDataModels\PizzeriaDataModels.csproj" />
</ItemGroup>
</Project>