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-04-09 17:02:29 +04:00
|
|
|
|
return context.Orders.Include(x => x.Gift)
|
|
|
|
|
.Include(x => x.Client).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)
|
|
|
|
|
{
|
|
|
|
|
if (!model.DateFrom.HasValue && !model.DateTo.HasValue && !model.ClientId.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return new();
|
|
|
|
|
}
|
|
|
|
|
using var context = new GiftShopDatabase();
|
|
|
|
|
if (model.DateFrom.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return context.Orders
|
|
|
|
|
.Include(x => x.Gift)
|
|
|
|
|
.Include(x => x.Client)
|
|
|
|
|
.Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
else if (model.ClientId.HasValue)
|
|
|
|
|
return context.Orders
|
|
|
|
|
.Include(x => x.Gift)
|
|
|
|
|
.Include(x => x.Client)
|
|
|
|
|
.Where(x => x.ClientId == model.ClientId)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
return context.Orders
|
|
|
|
|
.Include(x => x.Gift)
|
|
|
|
|
.Include(x => x.Client)
|
|
|
|
|
.Where(x => x.Id == model.Id)
|
|
|
|
|
.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)
|
|
|
|
|
.Include(x => x.Client).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)
|
|
|
|
|
.FirstOrDefault(x => x.Id == newOrder.Id)
|
|
|
|
|
?.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)
|
|
|
|
|
.Include(x => x.Client).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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|