2024-04-03 18:35:23 +04:00
|
|
|
|
using ConfectioneryContracts.BindingModels;
|
|
|
|
|
using ConfectioneryContracts.SearchModels;
|
|
|
|
|
using ConfectioneryContracts.StoragesContracts;
|
|
|
|
|
using ConfectioneryContracts.ViewModels;
|
|
|
|
|
using ConfectioneryFileImplement.Models;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace ConfectioneryFileImplement.Implements
|
|
|
|
|
{
|
|
|
|
|
public class OrderStorage : IOrderStorage
|
|
|
|
|
{
|
|
|
|
|
private readonly DataFileSingleton source;
|
2024-05-02 17:46:55 +04:00
|
|
|
|
|
2024-04-03 18:35:23 +04:00
|
|
|
|
public OrderStorage()
|
|
|
|
|
{
|
|
|
|
|
source = DataFileSingleton.GetInstance();
|
|
|
|
|
}
|
2024-05-02 17:46:55 +04:00
|
|
|
|
|
2024-04-03 18:35:23 +04:00
|
|
|
|
public List<OrderViewModel> GetFullList()
|
|
|
|
|
{
|
2024-05-02 17:46:55 +04:00
|
|
|
|
return source.Orders
|
|
|
|
|
.Select(x => AddInfo(x.GetViewModel))
|
|
|
|
|
.ToList();
|
2024-04-03 18:35:23 +04:00
|
|
|
|
}
|
2024-05-02 17:46:55 +04:00
|
|
|
|
|
2024-04-03 18:35:23 +04:00
|
|
|
|
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
|
|
|
|
|
{
|
2024-05-02 17:46:55 +04:00
|
|
|
|
if (model.Id.HasValue)
|
2024-04-03 18:35:23 +04:00
|
|
|
|
{
|
2024-05-02 17:46:55 +04:00
|
|
|
|
return source.Orders
|
|
|
|
|
.Where(x => x.Id == model.Id)
|
|
|
|
|
.Select(x => AddInfo(x.GetViewModel))
|
|
|
|
|
.ToList();
|
2024-04-03 18:35:23 +04:00
|
|
|
|
}
|
2024-05-02 17:46:55 +04:00
|
|
|
|
else if (model.DateFrom.HasValue && model.DateTo.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return source.Orders
|
|
|
|
|
.Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo)
|
|
|
|
|
.Select(x => AddInfo(x.GetViewModel))
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
else if (model.ClientId.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return source.Orders
|
|
|
|
|
.Where(x => x.ClientId == model.ClientId)
|
|
|
|
|
.Select(x => AddInfo(x.GetViewModel))
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
return new();
|
2024-04-03 18:35:23 +04:00
|
|
|
|
}
|
2024-05-02 17:46:55 +04:00
|
|
|
|
|
2024-04-03 18:35:23 +04:00
|
|
|
|
public OrderViewModel? GetElement(OrderSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (!model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
var order = source.Orders.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
if (order == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2024-05-02 17:46:55 +04:00
|
|
|
|
return AddInfo(order.GetViewModel);
|
2024-04-03 18:35:23 +04:00
|
|
|
|
}
|
2024-05-02 17:46:55 +04:00
|
|
|
|
|
2024-04-03 18:35:23 +04:00
|
|
|
|
public OrderViewModel? Insert(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
source.SaveOrders();
|
2024-05-02 17:46:55 +04:00
|
|
|
|
return AddInfo(newOrder.GetViewModel);
|
2024-04-03 18:35:23 +04:00
|
|
|
|
}
|
2024-05-02 17:46:55 +04:00
|
|
|
|
|
|
|
|
|
public OrderViewModel? Update(OrderBindingModel model)
|
2024-04-03 18:35:23 +04:00
|
|
|
|
{
|
|
|
|
|
var order = source.Orders.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
if (order == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2024-05-02 17:46:55 +04:00
|
|
|
|
order.Update(model);
|
2024-04-03 18:35:23 +04:00
|
|
|
|
source.SaveOrders();
|
2024-05-02 17:46:55 +04:00
|
|
|
|
return AddInfo(order.GetViewModel);
|
2024-04-03 18:35:23 +04:00
|
|
|
|
}
|
2024-05-02 17:46:55 +04:00
|
|
|
|
|
|
|
|
|
public OrderViewModel? Delete(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
var element = source.Orders.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
if (element != null)
|
|
|
|
|
{
|
|
|
|
|
source.Orders.Remove(element);
|
|
|
|
|
source.SaveOrders();
|
|
|
|
|
return AddInfo(element.GetViewModel);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private OrderViewModel AddInfo(OrderViewModel model)
|
2024-04-03 18:35:23 +04:00
|
|
|
|
{
|
2024-05-02 17:46:55 +04:00
|
|
|
|
var selectedPastry = source.Pastries.FirstOrDefault(x => x.Id == model.PastryId);
|
|
|
|
|
model.PastryName = selectedPastry?.PastryName ?? string.Empty;
|
|
|
|
|
var selectedClient = source.Clients.FirstOrDefault(x => x.Id == model.ClientId);
|
|
|
|
|
model.ClientFIO = selectedClient?.ClientFIO ?? string.Empty;
|
|
|
|
|
return model;
|
2024-04-03 18:35:23 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|