PIbd-23-Yunusov-N.N.-CarRep.../CarRepairShop/CarRepairShopDatabaseImplement/Implements/OrderStorage.cs

80 lines
2.6 KiB
C#
Raw Normal View History

2024-03-14 18:53:30 +04:00
using CarRepairShopContracts.BindingModels;
using CarRepairShopContracts.SearchModels;
using CarRepairShopContracts.StoragesContracts;
using CarRepairShopContracts.ViewModels;
using CarRepairShopDatabaseImplement.Models;
2024-03-24 21:23:30 +04:00
using Microsoft.EntityFrameworkCore;
2024-03-14 18:53:30 +04:00
namespace CarRepairShopDatabaseImplement.Implements
{
public class OrderStorage : IOrderStorage
{
public List<OrderViewModel> GetFullList()
{
using var context = new RepairsShopDatabase();
2024-03-24 21:23:30 +04:00
return context.Orders.Include(x => x.Repair)
2024-03-24 20:29:38 +04:00
.Select(x => x.GetViewModel)
2024-03-14 18:53:30 +04:00
.ToList();
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
using var context = new RepairsShopDatabase();
return context.Orders
.Where(x => x.Id == model.Id)
2024-03-24 20:29:38 +04:00
.Select(x => x.GetViewModel)
2024-03-14 18:53:30 +04:00
.ToList();
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new RepairsShopDatabase();
2024-03-24 20:29:38 +04:00
return context.Orders.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
2024-03-14 18:53:30 +04:00
}
public OrderViewModel? Insert(OrderBindingModel model)
{
2024-03-24 20:29:38 +04:00
using var context = new RepairsShopDatabase();
var newOrder = Order.Create(context, model);
2024-03-14 18:53:30 +04:00
if (newOrder == null)
{
return null;
}
context.Orders.Add(newOrder);
context.SaveChanges();
2024-03-24 20:29:38 +04:00
return newOrder.GetViewModel;
2024-03-14 18:53:30 +04:00
}
public OrderViewModel? Update(OrderBindingModel model)
{
using var context = new RepairsShopDatabase();
var order = context.Orders.FirstOrDefault(x => x.Id ==
model.Id);
if (order == null)
{
return null;
}
order.Update(model);
context.SaveChanges();
2024-03-24 20:29:38 +04:00
return order.GetViewModel;
2024-03-14 18:53:30 +04:00
}
public OrderViewModel? Delete(OrderBindingModel model)
{
using var context = new RepairsShopDatabase();
var element = context.Orders.FirstOrDefault(rec => rec.Id ==
model.Id);
if (element != null)
{
context.Orders.Remove(element);
context.SaveChanges();
2024-03-24 20:29:38 +04:00
return element.GetViewModel;
2024-03-14 18:53:30 +04:00
}
return null;
}
}
}