Исправления.

This commit is contained in:
Андрей Байгулов 2024-03-17 19:36:45 +04:00
parent ffe75c5f1c
commit a55f771ecf
2 changed files with 82 additions and 89 deletions

View File

@ -11,76 +11,77 @@ using System.Threading.Tasks;
namespace SushiBarFileImplement.Implements
{
public class SushiStorage : ISushiStorage
public class OrderStorage : IOrderStorage
{
private readonly DataFileSingleton source;
public SushiStorage()
public OrderStorage()
{
source = DataFileSingleton.GetInstance();
}
public List<SushiViewModel> GetFullList()
{
return source.Sushis.Select(x => x.GetViewModel).ToList();
}
public List<OrderViewModel> GetFullList() => source.Orders.Select(x => AttachSushiName(x.GetViewModel)).ToList();
public List<SushiViewModel> GetFilteredList(SushiSearchModel model)
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (string.IsNullOrEmpty(model.SushiName))
if (!model.Id.HasValue)
{
return new();
}
return source.Sushis.Where(x => x.SushiName.Contains(model.SushiName)).Select(x => x.GetViewModel).ToList();
return source.Orders.Where(x => x.Id == model.Id).Select(x => AttachSushiName(x.GetViewModel)).ToList();
}
public SushiViewModel? GetElement(SushiSearchModel model)
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (string.IsNullOrEmpty(model.SushiName) && !model.Id.HasValue)
if (!model.Id.HasValue)
{
return new();
}
return AttachSushiName(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;
}
return source.Sushis.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.SushiName) && x.SushiName == model.SushiName) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
source.Orders.Add(newOrder);
source.SaveOrders();
return AttachSushiName(newOrder.GetViewModel);
}
public SushiViewModel? Insert(SushiBindingModel model)
public OrderViewModel? Update(OrderBindingModel model)
{
model.Id = source.Sushis.Count > 0 ? source.Sushis.Max(x => x.Id) + 1 : 1;
var newSushi = Sushi.Create(model);
if (newSushi == null)
var order = source.Orders.FirstOrDefault(x => x.Id == model.Id);
if (order == null)
{
return null;
}
source.Sushis.Add(newSushi);
source.SaveSushis();
return newSushi.GetViewModel;
order.Update(model);
source.SaveOrders();
return AttachSushiName(order.GetViewModel);
}
public SushiViewModel? Update(SushiBindingModel model)
public OrderViewModel? Delete(OrderBindingModel model)
{
var Sushi = source.Sushis.FirstOrDefault(x => x.Id == model.Id);
if (Sushi == null)
var order = source.Orders.FirstOrDefault(x => x.Id == model.Id);
if (order != null)
{
return null;
}
Sushi.Update(model);
source.SaveSushis();
return Sushi.GetViewModel;
}
public SushiViewModel? Delete(SushiBindingModel model)
{
var Sushi = source.Sushis.FirstOrDefault(x => x.Id == model.Id);
if (Sushi != null)
{
source.Sushis.Remove(Sushi);
source.SaveSushis();
return Sushi.GetViewModel;
source.Orders.Remove(order);
source.SaveOrders();
return AttachSushiName(order.GetViewModel);
}
return null;
}
private OrderViewModel? AttachSushiName(OrderViewModel? model)
{
var sushi = source.Sushis.FirstOrDefault(x => x.Id == model?.SushiId);
model?.GetType().GetProperty("SushiName")?.SetValue(model, sushi?.SushiName);
return model;
}
}
}

View File

@ -11,84 +11,76 @@ using System.Threading.Tasks;
namespace SushiBarFileImplement.Implements
{
public class OrderStorage : IOrderStorage
public class SushiStorage : ISushiStorage
{
private readonly DataFileSingleton source;
public OrderStorage()
public SushiStorage()
{
source = DataFileSingleton.GetInstance();
}
public List<OrderViewModel> GetFullList() => source.Orders.Select(x => AttachSushiName(x.GetViewModel)).ToList();
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
public List<SushiViewModel> GetFullList()
{
if (!model.Id.HasValue)
return source.Sushis.Select(x => x.GetViewModel).ToList();
}
public List<SushiViewModel> GetFilteredList(SushiSearchModel model)
{
if (string.IsNullOrEmpty(model.SushiName))
{
return new();
}
return source.Orders.Where(x => x.Id == model.Id).Select(x => AttachSushiName(x.GetViewModel)).ToList();
return source.Sushis.Where(x => x.SushiName.Contains(model.SushiName)).Select(x => x.GetViewModel).ToList();
}
public OrderViewModel? GetElement(OrderSearchModel model)
public SushiViewModel? GetElement(SushiSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
return AttachSushiName(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)
if (string.IsNullOrEmpty(model.SushiName) && !model.Id.HasValue)
{
return null;
}
source.Orders.Add(newOrder);
source.SaveOrders();
return AttachSushiName(newOrder.GetViewModel);
return source.Sushis.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.SushiName) && x.SushiName == model.SushiName) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public OrderViewModel? Update(OrderBindingModel model)
public SushiViewModel? Insert(SushiBindingModel model)
{
var order = source.Orders.FirstOrDefault(x => x.Id == model.Id);
if (order == null)
model.Id = source.Sushis.Count > 0 ? source.Sushis.Max(x => x.Id) + 1 : 1;
var newSushi = Sushi.Create(model);
if (newSushi == null)
{
return null;
}
order.Update(model);
source.SaveOrders();
return AttachSushiName(order.GetViewModel);
source.Sushis.Add(newSushi);
source.SaveSushis();
return newSushi.GetViewModel;
}
public OrderViewModel? Delete(OrderBindingModel model)
public SushiViewModel? Update(SushiBindingModel model)
{
var order = source.Orders.FirstOrDefault(x => x.Id == model.Id);
if (order != null)
{
source.Orders.Remove(order);
source.SaveOrders();
return AttachSushiName(order.GetViewModel);
}
return null;
}
private OrderViewModel? AttachSushiName(OrderViewModel? model)
{
if (model == null)
var Sushi = source.Sushis.FirstOrDefault(x => x.Id == model.Id);
if (Sushi == null)
{
return null;
}
var sushi = source.Sushis.FirstOrDefault(x => x.Id == model.SushiId);
if (sushi != null)
Sushi.Update(model);
source.SaveSushis();
return Sushi.GetViewModel;
}
public SushiViewModel? Delete(SushiBindingModel model)
{
model.SushiName = sushi.SushiName;
var Sushi = source.Sushis.FirstOrDefault(x => x.Id == model.Id);
if (Sushi != null)
{
source.Sushis.Remove(Sushi);
source.SaveSushis();
return Sushi.GetViewModel;
}
return model;
return null;
}
}
}