121 lines
4.0 KiB
C#
121 lines
4.0 KiB
C#
using GiftShopContracts.BindingModels;
|
|
using GiftShopContracts.SearchModels;
|
|
using GiftShopContracts.StoragesContracts;
|
|
using GiftShopContracts.ViewModels;
|
|
using GiftShopDatabaseImplement.Models;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
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();
|
|
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;
|
|
}
|
|
|
|
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
|
|
{
|
|
if (model is null)
|
|
{
|
|
return new();
|
|
}
|
|
using var context = new GiftShopDatabase();
|
|
if (model.DateTo != null && model.DateFrom != null && model.ClientId.HasValue)
|
|
{
|
|
return context.Orders
|
|
.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)
|
|
return context.Orders
|
|
.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();
|
|
}
|
|
|
|
public List<OrderViewModel> GetFullList()
|
|
{
|
|
using var context = new GiftShopDatabase();
|
|
return context.Orders.Include(x => x.Gift)
|
|
.Include(x => x.Client).Include(x => x.Implementer).Select(x => x.GetViewModel).ToList();
|
|
}
|
|
|
|
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();
|
|
|
|
return context.Orders
|
|
.Include(x => x.Gift)
|
|
.Include(x => x.Client)
|
|
.Include(x => x.Implementer)
|
|
.FirstOrDefault(x => x.Id == newOrder.Id)
|
|
?.GetViewModel;
|
|
}
|
|
|
|
public OrderViewModel? Update(OrderBindingModel model)
|
|
{
|
|
using var context = new GiftShopDatabase();
|
|
var order = context.Orders.Include(x => x.Gift)
|
|
.Include(x => x.Client).Include(x => x.Implementer).FirstOrDefault(x => x.Id == model.Id);
|
|
if (order == null)
|
|
{
|
|
return null;
|
|
}
|
|
order.Update(model);
|
|
context.SaveChanges();
|
|
return order.GetViewModel;
|
|
}
|
|
}
|
|
}
|