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

78 lines
2.7 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)
{
using var context = new RepairsShopDatabase();
return context.Orders
2024-04-06 16:29:29 +04:00
.Where(x => ((!model.Id.HasValue || x.Id == model.Id) &&
(!model.DateFrom.HasValue || x.DateCreate >= model.DateFrom) &&
(!model.DateTo.HasValue || x.DateCreate <= model.DateTo)))
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();
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();
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;
}
}
}