90 lines
2.8 KiB
C#
90 lines
2.8 KiB
C#
using ConfectioneryContracts.BindingModels;
|
|
using ConfectioneryContracts.SearchModels;
|
|
using ConfectioneryContracts.ViewModels;
|
|
using ConfectioneryFileImplement.Models;
|
|
using System.Xml.Linq;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using ConfectioneryContracts.StoragesContracts;
|
|
|
|
namespace ConfectioneryFileImplement.Implements
|
|
{
|
|
public class OrderStorage : IOrderStorage
|
|
{
|
|
private readonly DataFileSingleton _source;
|
|
|
|
public OrderStorage()
|
|
{
|
|
_source = DataFileSingleton.GetInstance();
|
|
}
|
|
|
|
public List<OrderViewModel> GetFullList()
|
|
{
|
|
return _source.Orders.Select(x => GetViewModel(x)).ToList();
|
|
}
|
|
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
|
|
{
|
|
if (model == null || !model.Id.HasValue)
|
|
{
|
|
return new();
|
|
}
|
|
return _source.Orders.Where(x => x.Id == model.Id).Select(x => GetViewModel(x)).ToList();
|
|
}
|
|
public OrderViewModel? GetElement(OrderSearchModel model)
|
|
{
|
|
if (!model.Id.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
return GetViewModel(_source.Orders.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id));
|
|
}
|
|
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 GetViewModel(newOrder);
|
|
}
|
|
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 GetViewModel(order);
|
|
}
|
|
public OrderViewModel? Delete(OrderBindingModel model)
|
|
{
|
|
var order = _source.Orders.FirstOrDefault(x => x.Id == model.Id);
|
|
if (order == null)
|
|
{
|
|
return null;
|
|
}
|
|
_source.Orders.Remove(order);
|
|
_source.SaveOrders();
|
|
return GetViewModel(order);
|
|
}
|
|
private OrderViewModel GetViewModel(Order order)
|
|
{
|
|
var viewModel = order.GetViewModel;
|
|
var pastry = _source.Pastries.FirstOrDefault(x => x.Id == order.PastryId);
|
|
if (pastry != null)
|
|
{
|
|
viewModel.PastryName = pastry?.PastryName;
|
|
}
|
|
return viewModel;
|
|
}
|
|
}
|
|
}
|