SUBD/Sushi/SushiDatabaseImplement/Implement/OrderStorage.cs

112 lines
3.6 KiB
C#
Raw Permalink Normal View History

2024-04-04 03:47:27 +04:00
using SushiContracts.BindingModels;
using SushiContracts.SearchModels;
using SushiContracts.StoragesContracts;
using SushiContracts.ViewModels;
using SushiDatabaseImplement;
using SushiShopDatabaseImplement.Models;
namespace SushiShopDatabaseImplement.Implements
{
public class OrderStorage : IOrderStorage
{
public List<OrderViewModel> GetFullList()
{
using var context = new SushiDatabase();
return context.Orders
.Select(x => AccessSushisStorage(x.GetViewModel))
.ToList();
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
using var context = new SushiDatabase();
return context.Orders
.Where(x => x.Id == model.Id)
.Select(x => AccessSushisStorage(x.GetViewModel))
.ToList();
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new SushiDatabase();
return AccessSushisStorage(context.Orders.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel);
}
public OrderViewModel? Insert(OrderBindingModel model)
{
var newOrder = Order.Create(model);
if (newOrder == null)
{
return null;
}
using var context = new SushiDatabase();
context.Orders.Add(newOrder);
context.SaveChanges();
return AccessSushisStorage(newOrder.GetViewModel);
}
public OrderViewModel? Update(OrderBindingModel model)
{
using var context = new SushiDatabase();
var order = context.Orders.FirstOrDefault(x => x.Id ==
model.Id);
if (order == null)
{
return null;
}
order.Update(model);
context.SaveChanges();
return AccessSushisStorage(order.GetViewModel);
}
public OrderViewModel? Delete(OrderBindingModel model)
{
using var context = new SushiDatabase();
var element = context.Orders.FirstOrDefault(rec => rec.Id ==
model.Id);
if (element != null)
{
context.Orders.Remove(element);
context.SaveChanges();
return AccessSushisStorage(element.GetViewModel);
}
return null;
}
public static OrderViewModel AccessSushisStorage(OrderViewModel model)
{
if (model == null)
return null;
using var context = new SushiDatabase();
2024-04-18 11:16:56 +04:00
foreach (var Sushies in context.Sushies)
2024-04-04 03:47:27 +04:00
{
2024-04-18 11:16:56 +04:00
if (Sushies.Id == model.SushiId)
2024-04-04 03:47:27 +04:00
{
2024-04-18 11:16:56 +04:00
model.SushiName = Sushies.SushiName;
break;
}
}
foreach (var Clients in context.Clients)
{
if (Clients.Id == model.ClientId)
{
model.ClientName = Clients.ClientName;
break;
}
}
foreach (var Employees in context.Employees)
{
if (Employees.Id == model.EmployeeId)
{
model.EmployeeName = Employees.EmployeeName;
2024-04-04 03:47:27 +04:00
break;
}
}
return model;
}
}
}