ISEbd-22_Baygulov_A.A._Sush.../SushiBar/SushiBarFileImplement/OrderStorage.cs

87 lines
2.7 KiB
C#
Raw Normal View History

2024-03-14 21:54:43 +04:00
using SushiBarContracts.BindingModels;
using SushiBarContracts.SearchModels;
using SushiBarContracts.StoragesContracts;
using SushiBarContracts.ViewModels;
using SushiBarFileImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SushiBarFileImplement.Implements
{
2024-03-17 19:36:45 +04:00
public class OrderStorage : IOrderStorage
2024-03-14 21:54:43 +04:00
{
private readonly DataFileSingleton source;
2024-03-17 19:36:45 +04:00
public OrderStorage()
2024-03-14 21:54:43 +04:00
{
source = DataFileSingleton.GetInstance();
}
2024-03-17 19:36:45 +04:00
public List<OrderViewModel> GetFullList() => source.Orders.Select(x => AttachSushiName(x.GetViewModel)).ToList();
2024-03-14 21:54:43 +04:00
2024-03-17 19:36:45 +04:00
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
2024-03-14 21:54:43 +04:00
{
2024-03-17 19:36:45 +04:00
if (!model.Id.HasValue)
2024-03-14 21:54:43 +04:00
{
return new();
}
2024-03-17 19:36:45 +04:00
return source.Orders.Where(x => x.Id == model.Id).Select(x => AttachSushiName(x.GetViewModel)).ToList();
2024-03-14 21:54:43 +04:00
}
2024-03-17 19:36:45 +04:00
public OrderViewModel? GetElement(OrderSearchModel model)
2024-03-14 21:54:43 +04:00
{
2024-03-17 19:36:45 +04:00
if (!model.Id.HasValue)
2024-03-14 21:54:43 +04:00
{
2024-03-17 19:36:45 +04:00
return new();
2024-03-14 21:54:43 +04:00
}
2024-03-17 19:36:45 +04:00
return AttachSushiName(source.Orders.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel);
2024-03-14 21:54:43 +04:00
}
2024-03-17 19:36:45 +04:00
public OrderViewModel? Insert(OrderBindingModel model)
2024-03-14 21:54:43 +04:00
{
2024-03-17 19:36:45 +04:00
model.Id = source.Orders.Count > 0 ? source.Orders.Max(x => x.Id) + 1 : 1;
var newOrder = Order.Create(model);
if (newOrder == null)
2024-03-14 21:54:43 +04:00
{
return null;
}
2024-03-17 19:36:45 +04:00
source.Orders.Add(newOrder);
source.SaveOrders();
return AttachSushiName(newOrder.GetViewModel);
2024-03-14 21:54:43 +04:00
}
2024-03-17 19:36:45 +04:00
public OrderViewModel? Update(OrderBindingModel model)
2024-03-14 21:54:43 +04:00
{
2024-03-17 19:36:45 +04:00
var order = source.Orders.FirstOrDefault(x => x.Id == model.Id);
if (order == null)
2024-03-14 21:54:43 +04:00
{
return null;
}
2024-03-17 19:36:45 +04:00
order.Update(model);
source.SaveOrders();
return AttachSushiName(order.GetViewModel);
2024-03-14 21:54:43 +04:00
}
2024-03-17 19:36:45 +04:00
public OrderViewModel? Delete(OrderBindingModel model)
2024-03-14 21:54:43 +04:00
{
2024-03-17 19:36:45 +04:00
var order = source.Orders.FirstOrDefault(x => x.Id == model.Id);
if (order != null)
2024-03-14 21:54:43 +04:00
{
2024-03-17 19:36:45 +04:00
source.Orders.Remove(order);
source.SaveOrders();
return AttachSushiName(order.GetViewModel);
2024-03-14 21:54:43 +04:00
}
return null;
}
2024-03-17 19:36:45 +04:00
private OrderViewModel? AttachSushiName(OrderViewModel? model)
{
2024-03-17 20:49:37 +04:00
var sushi = model?.SushiId != null ? source.Sushis.FirstOrDefault(x => x.Id == model.SushiId) : null;
model.SushiName = sushi?.SushiName;
2024-03-17 19:36:45 +04:00
return model;
}
2024-03-14 21:54:43 +04:00
}
}