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

79 lines
3.2 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-04-10 16:17:56 +04:00
return context.Orders.Include(x => x.Repair).Include(x => x.Client)
2024-04-22 22:29:10 +04:00
.Include(x => x.Implementer).Select(x => x.GetViewModel)
2024-03-14 18:53:30 +04:00
.ToList();
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
using var context = new RepairsShopDatabase();
2024-04-22 22:29:10 +04:00
return context.Orders.Include(x => x.Client).Include(x => x.Repair).Include(x => x.Implementer)
.Where(x => ((!model.Id.HasValue || x.Id == model.Id) &&
2024-04-06 16:29:29 +04:00
(!model.DateFrom.HasValue || x.DateCreate >= model.DateFrom) &&
2024-04-06 19:58:28 +04:00
(!model.DateTo.HasValue || x.DateCreate <= model.DateTo) &&
2024-04-22 22:29:10 +04:00
(!model.ClientId.HasValue || x.ClientId == model.ClientId) &&
(!model.Status.HasValue || x.Status == model.Status)))
.Select(x => x.GetViewModel)
2024-03-14 18:53:30 +04:00
.ToList();
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
using var context = new RepairsShopDatabase();
2024-04-22 22:29:10 +04:00
return context.Orders.Include(x => x.Repair).Include(x => x.Client).Include(x => x.Implementer).FirstOrDefault(
x => ((model.Id.HasValue && x.Id == model.Id) ||
(model.ImplementerId.HasValue && model.Status.HasValue &&
x.ImplementerId == model.ImplementerId && x.Status == model.Status)))?.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();
2024-03-25 20:17:43 +04:00
var newOrder = Order.Create(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();
2024-04-22 22:29:10 +04:00
var order = context.Orders.Include(x => x.Repair).Include(x => x.Client).Include(x => x.Implementer)
.FirstOrDefault(x => x.Id == model.Id);
2024-03-14 18:53:30 +04:00
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();
2024-04-22 22:29:10 +04:00
var element = context.Orders.Include(x => x.Repair).Include(x => x.Client).Include(x => x.Implementer).FirstOrDefault(rec => rec.Id ==
2024-03-14 18:53:30 +04:00
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;
}
}
}