PIbd-23-Radaev-A.V.-GiftShop/GiftShop/GiftShopDatabaseImplement/Implements/OrderStorage.cs

95 lines
3.1 KiB
C#
Raw Normal View History

2024-04-07 10:15:40 +04:00
using GiftShopContracts.BindingModels;
using GiftShopContracts.SearchModels;
using GiftShopContracts.StoragesContracts;
using GiftShopContracts.ViewModels;
using GiftShopDatabaseImplement.Models;
2024-04-09 17:02:29 +04:00
using Microsoft.EntityFrameworkCore;
2024-04-07 10:15:40 +04:00
namespace GiftShopDatabaseImplement.Implements
{
public class OrderStorage : IOrderStorage
{
public OrderViewModel? Delete(OrderBindingModel model)
{
using var context = new GiftShopDatabase();
var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Orders.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
2024-05-15 15:49:43 +04:00
using var context = new GiftShopDatabase();
return context.Orders.Include(x => x.Gift).Include(x => x.Client).Include(x => x.Implementer).FirstOrDefault(
x => ((model.Id.HasValue && x.Id == model.Id) ||
(x.ImplementerId == model.ImplementerId && x.Status == model.Status)))?.GetViewModel;
}
2024-04-07 10:17:23 +04:00
2024-04-09 17:02:29 +04:00
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
2024-05-15 15:07:11 +04:00
if (model is null)
2024-04-09 17:02:29 +04:00
{
return new();
}
using var context = new GiftShopDatabase();
2024-05-15 15:49:43 +04:00
return context.Orders
.Include(x => x.Gift)
.Include(x => x.Client).Include(x => x.Implementer).Where(x => (
(!model.Id.HasValue || x.Id == model.Id) &&
(!model.DateFrom.HasValue || x.DateCreate >= model.DateFrom) &&
(!model.DateTo.HasValue || x.DateCreate <= model.DateTo)
&&
(!model.ClientId.HasValue || x.ClientId == model.ClientId)
)
)
.Select(x => x.GetViewModel)
.ToList();
}
2024-04-07 10:17:23 +04:00
2024-04-09 17:02:29 +04:00
public List<OrderViewModel> GetFullList()
2024-04-07 10:15:40 +04:00
{
using var context = new GiftShopDatabase();
2024-04-09 17:02:29 +04:00
return context.Orders.Include(x => x.Gift)
2024-05-15 15:07:11 +04:00
.Include(x => x.Client).Include(x => x.Implementer).Select(x => x.GetViewModel).ToList();
}
2024-04-07 10:15:40 +04:00
public OrderViewModel? Insert(OrderBindingModel model)
{
2024-05-15 15:49:43 +04:00
using var context = new GiftShopDatabase();
var newOrder = Order.Create(model, context);
2024-04-07 10:15:40 +04:00
if (newOrder == null)
{
return null;
}
context.Orders.Add(newOrder);
context.SaveChanges();
2024-04-09 17:02:29 +04:00
return context.Orders
.Include(x => x.Gift)
.Include(x => x.Client)
2024-05-15 15:07:11 +04:00
.Include(x => x.Implementer)
.FirstOrDefault(x => x.Id == newOrder.Id)
2024-04-09 17:02:29 +04:00
?.GetViewModel;
}
2024-04-07 10:15:40 +04:00
public OrderViewModel? Update(OrderBindingModel model)
{
using var context = new GiftShopDatabase();
2024-04-09 17:02:29 +04:00
var order = context.Orders.Include(x => x.Gift)
2024-05-15 15:07:11 +04:00
.Include(x => x.Client).Include(x => x.Implementer).FirstOrDefault(x => x.Id == model.Id);
if (order == null)
2024-04-07 10:15:40 +04:00
{
return null;
}
order.Update(model);
context.SaveChanges();
return order.GetViewModel;
}
}
}