ISEbd-22_Alimova_M.S._Confe.../Confectionery/ConfectioneryFileImplement/OrderStorage.cs

133 lines
3.8 KiB
C#
Raw Normal View History

using ConfectioneryContracts.BindingModels;
using ConfectioneryContracts.SearchModels;
using ConfectioneryContracts.StoragesContracts;
using ConfectioneryContracts.ViewModels;
2024-03-15 09:13:25 +04:00
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;
public OrderStorage()
{
source = DataFileSingleton.GetInstance();
}
public List<OrderViewModel> GetFullList() => source.Orders.Select(x => AttachPastryName(x.GetViewModel)).ToList();
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (model.DateFrom.HasValue)
{
return source.Orders.Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo)
.Select(x => GetViewModel(x)).ToList();
}
if (model.ClientId.HasValue && !model.Id.HasValue)
{
return source.Orders.Where(x => x.ClientId == model.ClientId).Select(x => x.GetViewModel).ToList();
}
if (!model.ImplementerId.HasValue && !model.Id.HasValue)
{
return source.Orders.Where(x => x.ImplementerId == model.ImplementerId).Select(x => x.GetViewModel).ToList();
}
if (model.Id.HasValue)
{
return source.Orders.Where(x => x.Id.Equals(model.Id)).Select(x => GetViewModel(x)).ToList();
}
return new();
}
private OrderViewModel GetViewModel(Order order)
{
var viewModel = order.GetViewModel;
var Pastry = source.Pastrys.FirstOrDefault(x => x.Id == order.PastryId);
var client = source.Clients.FirstOrDefault(x => x.Id == order.ClientId);
if (Pastry != null)
{
viewModel.PastryName = Pastry.PastryName;
}
if (client != null)
{
viewModel.ClientFIO = client.ClientFIO;
}
return viewModel;
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (model.ImplementerId.HasValue)
{
return source.Orders.FirstOrDefault(x => x.ImplementerId == model.ImplementerId)?.GetViewModel;
}
if (!model.Id.HasValue)
{
return new();
}
return AttachPastryName(source.Orders.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel);
}
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();
return AttachPastryName(newOrder.GetViewModel);
}
public OrderViewModel? Update(OrderBindingModel model)
{
var order = source.Orders.FirstOrDefault(x => x.Id == model.Id);
if (order == null)
{
return null;
}
order.Update(model);
source.SaveOrders();
return AttachPastryName(order.GetViewModel);
}
public OrderViewModel? Delete(OrderBindingModel model)
{
var order = source.Orders.FirstOrDefault(x => x.Id == model.Id);
if (order != null)
{
source.Orders.Remove(order);
source.SaveOrders();
return AttachPastryName(order.GetViewModel);
}
return null;
}
private OrderViewModel? AttachPastryName(OrderViewModel? model)
{
if (model == null)
{
return null;
}
2024-03-15 10:52:30 +04:00
var pastry = source.Pastrys.FirstOrDefault(x => x.Id == model.PastryId);
if (pastry != null)
{
2024-03-15 10:52:30 +04:00
model.PastryName = pastry.PastryName;
}
return model;
}
}
}