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

121 lines
4.0 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)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new GiftShopDatabase();
2024-05-15 15:07:11 +04:00
if (model.ImplementerId.HasValue)
{
return context.Orders
.FirstOrDefault(x => x.ImplementerId == model.ImplementerId && x.Status.Equals(model.Status))
?.GetViewModel;
}
return context.Orders
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)
?.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:07:11 +04:00
if (model.DateTo != null && model.DateFrom != null && model.ClientId.HasValue)
2024-04-09 17:02:29 +04:00
{
return context.Orders
2024-05-15 15:07:11 +04:00
.Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo && x.ClientId == model.ClientId)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
else if (model.DateTo != null && model.DateFrom != null)
{
return context.Orders
.Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
else if (model.ClientId.HasValue)
2024-04-09 17:02:29 +04:00
return context.Orders
2024-05-15 15:07:11 +04:00
.Where(x => x.ClientId == model.ClientId)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
return context.Orders
.Where(x => x.Status == model.Status)
.ToList()
.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)
{
var newOrder = Order.Create(model);
if (newOrder == null)
{
return null;
}
using var context = new GiftShopDatabase();
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;
}
}
}