96 lines
3.2 KiB
C#
Raw Normal View History

2023-02-12 14:58:15 +04:00
using FoodOrdersContracts.BindingModels;
using FoodOrdersContracts.SearchModels;
using FoodOrdersContracts.StoragesContracts;
using FoodOrdersContracts.ViewModels;
2023-02-12 15:28:46 +04:00
using FoodOrdersFileImplement.Models;
2023-03-12 10:57:32 +04:00
using System.Reflection;
using System.Security.Cryptography.X509Certificates;
2023-02-12 14:58:15 +04:00
2023-02-12 15:28:46 +04:00
namespace FoodOrdersFileImplement.Implements
2023-02-12 14:58:15 +04:00
{
public class OrderStorage : IOrderStorage
{
private readonly DataFileSingleton _source;
public OrderStorage()
{
_source = DataFileSingleton.GetInstance();
}
public List<OrderViewModel> GetFullList()
{
2023-02-12 15:34:57 +04:00
return _source.Orders.Select(x => GetViewModel(x)).ToList();
2023-02-12 14:58:15 +04:00
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
2023-03-25 20:38:11 +04:00
if (model.ClientId.HasValue)
{
return _source.Orders
.Where(x => x.ClientId == model.ClientId)
.Select(x => GetViewModel(x))
.ToList();
}
2023-03-10 17:49:43 +04:00
if (!model.Id.HasValue && model.DateFrom.HasValue && model.DateTo.HasValue)
2023-02-12 14:58:15 +04:00
{
return _source.Orders
.Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo)
2023-03-10 17:49:43 +04:00
.Select(x => GetViewModel(x))
.ToList();
2023-02-12 14:58:15 +04:00
}
2023-02-12 15:34:57 +04:00
return _source.Orders.Where(x => x.Id == model.Id).Select(x => GetViewModel(x)).ToList();
2023-02-12 14:58:15 +04:00
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
2023-03-12 10:35:52 +04:00
return _source.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
2023-02-12 14:58:15 +04:00
}
private OrderViewModel GetViewModel(Order order)
{
var viewModel = order.GetViewModel;
2023-03-12 10:57:32 +04:00
viewModel.DishName = _source.Dishes.FirstOrDefault(x => (x.Id == order.DishId))?.DishName;
2023-03-25 20:38:11 +04:00
viewModel.ClientFIO = _source.Clients.FirstOrDefault(x => (x.Id == order.ClientId))?.ClientFIO;
2023-02-12 14:58:15 +04:00
return viewModel;
}
public OrderViewModel? Delete(OrderBindingModel model)
{
2023-02-12 15:28:46 +04:00
var order = _source.Orders.FirstOrDefault(x => x.Id == model.Id);
if (order == null)
2023-02-12 14:58:15 +04:00
{
2023-02-12 15:28:46 +04:00
return null;
2023-02-12 14:58:15 +04:00
}
2023-02-12 15:28:46 +04:00
order.Update(model);
_source.SaveOrders();
return order.GetViewModel;
2023-02-12 14:58:15 +04:00
}
public OrderViewModel? Insert(OrderBindingModel model)
{
2023-02-12 15:28:46 +04:00
model.Id = _source.Orders.Count > 0 ? _source.Orders.Max(x => x.Id) + 1 : 1;
2023-02-12 14:58:15 +04:00
var newOrder = Order.Create(model);
if (newOrder == null)
{
return null;
}
_source.Orders.Add(newOrder);
2023-02-12 15:28:46 +04:00
_source.SaveOrders();
return newOrder.GetViewModel;
2023-02-12 14:58:15 +04:00
}
public OrderViewModel? Update(OrderBindingModel model)
{
2023-02-12 15:28:46 +04:00
var order = _source.Orders.FirstOrDefault(x => x.Id == model.Id);
if (order == null)
2023-02-12 14:58:15 +04:00
{
2023-02-12 15:28:46 +04:00
return null;
2023-02-12 14:58:15 +04:00
}
2023-02-12 15:28:46 +04:00
order.Update(model);
_source.SaveOrders();
return order.GetViewModel;
2023-02-12 14:58:15 +04:00
}
}
}