не до конца рабочая 2
This commit is contained in:
parent
ca830e0109
commit
1ceb6d9089
@ -9,14 +9,14 @@ namespace FoodOrdersFileImplement.Implements
|
||||
{
|
||||
public class ComponentStorage : IComponentStorage
|
||||
{
|
||||
private readonly DataFileSingleton source;
|
||||
private readonly DataFileSingleton _source;
|
||||
public ComponentStorage()
|
||||
{
|
||||
source = DataFileSingleton.GetInstance();
|
||||
_source = DataFileSingleton.GetInstance();
|
||||
}
|
||||
public List<ComponentViewModel> GetFullList()
|
||||
{
|
||||
return source.Components.Select(x => x.GetViewModel).ToList();
|
||||
return _source.Components.Select(x => x.GetViewModel).ToList();
|
||||
}
|
||||
public List<ComponentViewModel> GetFilteredList(ComponentSearchModel
|
||||
model)
|
||||
@ -25,50 +25,49 @@ namespace FoodOrdersFileImplement.Implements
|
||||
{
|
||||
return new();
|
||||
}
|
||||
return source.Components.Where(x => x.ComponentName.Contains(model.ComponentName)).Select(x => x.GetViewModel).ToList();
|
||||
return _source.Components.Where(x => x.ComponentName.Contains(model.ComponentName)).Select(x => x.GetViewModel).ToList();
|
||||
}
|
||||
public ComponentViewModel? GetElement(ComponentSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.DishName) && !model.Id.HasValue)
|
||||
if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return source.Dishes
|
||||
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.DishName) && x.DishName == model.DishName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
|
||||
return _source.Components.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == model.ComponentName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
|
||||
}
|
||||
public ComponentViewModel? Insert(ComponentBindingModel model)
|
||||
{
|
||||
model.Id = source.Components.Count > 0 ? source.Components.Max(x =>
|
||||
model.Id = _source.Components.Count > 0 ? _source.Components.Max(x =>
|
||||
x.Id) + 1 : 1;
|
||||
var newComponent = Component.Create(model);
|
||||
if (newComponent == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
source.Components.Add(newComponent);
|
||||
source.SaveComponents();
|
||||
_source.Components.Add(newComponent);
|
||||
_source.SaveComponents();
|
||||
return newComponent.GetViewModel;
|
||||
}
|
||||
public ComponentViewModel? Update(ComponentBindingModel model)
|
||||
{
|
||||
var component = source.Components.FirstOrDefault(x => x.Id ==
|
||||
var component = _source.Components.FirstOrDefault(x => x.Id ==
|
||||
model.Id);
|
||||
if (component == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
component.Update(model);
|
||||
source.SaveComponents();
|
||||
_source.SaveComponents();
|
||||
return component.GetViewModel;
|
||||
}
|
||||
public ComponentViewModel? Delete(ComponentBindingModel model)
|
||||
{
|
||||
var element = source.Components.FirstOrDefault(x => x.Id ==
|
||||
var element = _source.Components.FirstOrDefault(x => x.Id ==
|
||||
model.Id);
|
||||
if (element != null)
|
||||
{
|
||||
source.Components.Remove(element);
|
||||
source.SaveComponents();
|
||||
_source.Components.Remove(element);
|
||||
_source.SaveComponents();
|
||||
return element.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
|
@ -3,9 +3,9 @@ using FoodOrdersContracts.SearchModels;
|
||||
using FoodOrdersContracts.StoragesContracts;
|
||||
using FoodOrdersContracts.ViewModels;
|
||||
using FoodOrdersFileImplement.Models;
|
||||
using FoodOrdersFileImplement;
|
||||
using System.Reflection.Metadata;
|
||||
|
||||
namespace FoodOrdersListImplement.Implements
|
||||
namespace FoodOrdersFileImplement.Implements
|
||||
{
|
||||
public class DishStorage : IDishStorage
|
||||
{
|
||||
@ -16,24 +16,15 @@ namespace FoodOrdersListImplement.Implements
|
||||
}
|
||||
public List<DishViewModel> GetFullList()
|
||||
{
|
||||
return source.Components.Select(x => x.GetViewModel).ToList();
|
||||
return _source.Dishes.Select(x => x.GetViewModel).ToList();
|
||||
}
|
||||
public List<DishViewModel> GetFilteredList(DishSearchModel
|
||||
model)
|
||||
public List<DishViewModel> GetFilteredList(DishSearchModel model)
|
||||
{
|
||||
var result = new List<DishViewModel>();
|
||||
if (string.IsNullOrEmpty(model.DishName))
|
||||
{
|
||||
return result;
|
||||
return new();
|
||||
}
|
||||
foreach (var dish in _source.Dishes)
|
||||
{
|
||||
if (dish.DishName.Contains(model.DishName))
|
||||
{
|
||||
result.Add(dish.GetViewModel);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
return _source.Dishes.Where(x => x.DishName.Contains(model.DishName)).Select(x => x.GetViewModel).ToList();
|
||||
}
|
||||
public DishViewModel? GetElement(DishSearchModel model)
|
||||
{
|
||||
@ -41,59 +32,44 @@ namespace FoodOrdersListImplement.Implements
|
||||
{
|
||||
return null;
|
||||
}
|
||||
foreach (var dish in _source.Dishes)
|
||||
{
|
||||
if ((!string.IsNullOrEmpty(model.DishName) &&
|
||||
dish.DishName == model.DishName) ||
|
||||
(model.Id.HasValue && dish.Id == model.Id))
|
||||
{
|
||||
return dish.GetViewModel;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return _source.Dishes
|
||||
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.DishName) && x.DishName == model.DishName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
|
||||
}
|
||||
public DishViewModel? Insert(DishBindingModel model)
|
||||
{
|
||||
model.Id = 1;
|
||||
foreach (var dish in _source.Dishes)
|
||||
{
|
||||
if (model.Id <= dish.Id)
|
||||
{
|
||||
model.Id = dish.Id + 1;
|
||||
}
|
||||
}
|
||||
var newDish = Dish.Create(model);
|
||||
if (newDish == null)
|
||||
model.Id = _source.Dishes.Count > 0 ? _source.Dishes.Max(x => x.Id) + 1 : 1;
|
||||
var newDoc = Dish.Create(model);
|
||||
if (newDoc == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
_source.Dishes.Add(newDish);
|
||||
return newDish.GetViewModel;
|
||||
_source.Dishes.Add(newDoc);
|
||||
_source.SaveDishes();
|
||||
return newDoc.GetViewModel;
|
||||
}
|
||||
|
||||
public DishViewModel? Update(DishBindingModel model)
|
||||
{
|
||||
foreach (var dish in _source.Dishes)
|
||||
{
|
||||
if (dish.Id == model.Id)
|
||||
var dish = _source.Dishes.FirstOrDefault(x => x.Id == model.Id);
|
||||
if (dish == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
dish.Update(model);
|
||||
_source.SaveDishes();
|
||||
return dish.GetViewModel;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public DishViewModel? Delete(DishBindingModel model)
|
||||
{
|
||||
for (int i = 0; i < _source.Dishes.Count; ++i)
|
||||
var document = _source.Dishes.FirstOrDefault(x => x.Id == model.Id);
|
||||
if (document == null)
|
||||
{
|
||||
if (_source.Dishes[i].Id == model.Id)
|
||||
{
|
||||
var element = _source.Dishes[i];
|
||||
_source.Dishes.RemoveAt(i);
|
||||
return element.GetViewModel;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
document.Update(model);
|
||||
_source.SaveDishes();
|
||||
return document.GetViewModel;
|
||||
}
|
||||
}
|
||||
}
|
@ -3,8 +3,9 @@ using FoodOrdersContracts.SearchModels;
|
||||
using FoodOrdersContracts.StoragesContracts;
|
||||
using FoodOrdersContracts.ViewModels;
|
||||
using FoodOrdersFileImplement;
|
||||
using FoodOrdersFileImplement.Models;
|
||||
|
||||
namespace FoodOrdersListImplement.Implements
|
||||
namespace FoodOrdersFileImplement.Implements
|
||||
{
|
||||
public class OrderStorage : IOrderStorage
|
||||
{
|
||||
@ -15,28 +16,15 @@ namespace FoodOrdersListImplement.Implements
|
||||
}
|
||||
public List<OrderViewModel> GetFullList()
|
||||
{
|
||||
var result = new List<OrderViewModel>();
|
||||
foreach (var order in _source.Orders)
|
||||
{
|
||||
result.Add(GetViewModel(order));
|
||||
}
|
||||
return result;
|
||||
return _source.Orders.Select(x => x.GetViewModel).ToList();
|
||||
}
|
||||
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
|
||||
{
|
||||
var result = new List<OrderViewModel>();
|
||||
if (!model.Id.HasValue)
|
||||
{
|
||||
return result;
|
||||
return new();
|
||||
}
|
||||
foreach (var order in _source.Orders)
|
||||
{
|
||||
if (order.Id == model.Id)
|
||||
{
|
||||
result.Add(GetViewModel(order));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
return _source.Orders.Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList();
|
||||
}
|
||||
|
||||
public OrderViewModel? GetElement(OrderSearchModel model)
|
||||
@ -45,20 +33,14 @@ namespace FoodOrdersListImplement.Implements
|
||||
{
|
||||
return null;
|
||||
}
|
||||
foreach (var order in _source.Orders)
|
||||
{
|
||||
if (model.Id.HasValue && order.Id == model.Id)
|
||||
{
|
||||
return GetViewModel(order);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return _source.Orders
|
||||
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
|
||||
}
|
||||
|
||||
private OrderViewModel GetViewModel(Order order)
|
||||
{
|
||||
var viewModel = order.GetViewModel;
|
||||
foreach (var iceCream in _source.Dish)
|
||||
foreach (var iceCream in _source.Dishes)
|
||||
{
|
||||
if (iceCream.Id == order.DishId)
|
||||
{
|
||||
@ -71,48 +53,39 @@ namespace FoodOrdersListImplement.Implements
|
||||
|
||||
public OrderViewModel? Delete(OrderBindingModel model)
|
||||
{
|
||||
for (int i = 0; i < _source.Orders.Count; ++i)
|
||||
var order = _source.Orders.FirstOrDefault(x => x.Id == model.Id);
|
||||
if (order == null)
|
||||
{
|
||||
if (_source.Orders[i].Id == model.Id)
|
||||
{
|
||||
var element = _source.Orders[i];
|
||||
_source.Orders.RemoveAt(i);
|
||||
return GetViewModel(element);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
order.Update(model);
|
||||
_source.SaveOrders();
|
||||
return order.GetViewModel;
|
||||
}
|
||||
|
||||
public OrderViewModel? Insert(OrderBindingModel model)
|
||||
{
|
||||
model.Id = 1;
|
||||
foreach (var order in _source.Orders)
|
||||
{
|
||||
if (model.Id <= order.Id)
|
||||
{
|
||||
model.Id = order.Id + 1;
|
||||
}
|
||||
}
|
||||
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);
|
||||
return GetViewModel(newOrder);
|
||||
_source.SaveOrders();
|
||||
return newOrder.GetViewModel;
|
||||
}
|
||||
|
||||
public OrderViewModel? Update(OrderBindingModel model)
|
||||
{
|
||||
foreach (var order in _source.Orders)
|
||||
var order = _source.Orders.FirstOrDefault(x => x.Id == model.Id);
|
||||
if (order == null)
|
||||
{
|
||||
if (order.Id == model.Id)
|
||||
{
|
||||
order.Update(model);
|
||||
return GetViewModel(order);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
order.Update(model);
|
||||
_source.SaveOrders();
|
||||
return order.GetViewModel;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -19,9 +19,9 @@ namespace FoodOrdersFileImplement.Models
|
||||
{
|
||||
if (_dishComponents == null)
|
||||
{
|
||||
var source = DataFileSingleton.GetInstance();
|
||||
var _source = DataFileSingleton.GetInstance();
|
||||
_dishComponents = Components.ToDictionary(x => x.Key, y =>
|
||||
((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!,
|
||||
((_source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!,
|
||||
y.Value));
|
||||
}
|
||||
return _dishComponents;
|
||||
|
@ -23,15 +23,40 @@ namespace FoodOrdersFileImplement.Models
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new Order()
|
||||
var order = new Order()
|
||||
{
|
||||
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
|
||||
DishId = Convert.ToInt32(element.Element("DocumentId")!.Value),
|
||||
Sum = Convert.ToDouble(element.Element("Sum")!.Value),
|
||||
DishId = Convert.ToInt32(element.Element("DishId")!.Value),
|
||||
Count = Convert.ToInt32(element.Element("Count")!.Value),
|
||||
Status = (OrderStatus)Enum.Parse(typeof(OrderStatus), element.Element("Status")!.Value),
|
||||
DateCreate = Convert.ToDateTime(element.Element("DateCreate")!.Value),
|
||||
DateImplement = Convert.ToDateTime(element.Element("DateCreate")!.Value)
|
||||
Sum = Convert.ToDouble(element.Element("Sum")!.Value),
|
||||
DateCreate = DateTime.ParseExact(element.Element("DateCreate")!.Value, "G", null),
|
||||
};
|
||||
DateTime.TryParse(element.Element("DateImplement")!.Value, out DateTime dateImpl);
|
||||
order.DateImplement = dateImpl;
|
||||
|
||||
if (!Enum.TryParse(element.Element("Status")!.Value, out OrderStatus status))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
order.Status = status;
|
||||
return order;
|
||||
}
|
||||
|
||||
public static Order? Create(OrderBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new Order
|
||||
{
|
||||
Id = model.Id,
|
||||
DishId = model.DishId,
|
||||
Count = model.Count,
|
||||
Sum = model.Sum,
|
||||
Status = model.Status,
|
||||
DateCreate = model.DateCreate,
|
||||
DateImplement = model.DateImplement,
|
||||
};
|
||||
}
|
||||
public void Update(OrderBindingModel? model)
|
||||
|
Loading…
x
Reference in New Issue
Block a user