2024-04-29 17:59:45 +04:00
|
|
|
|
using ElectronicsShopContracts.BindingModels;
|
|
|
|
|
using ElectronicsShopContracts.SearchModels;
|
|
|
|
|
using ElectronicsShopContracts.StorageContracts;
|
|
|
|
|
using ElectronicsShopContracts.ViewModels;
|
|
|
|
|
using ElectronicsShopDataBaseImplement.Models;
|
2024-04-29 18:34:49 +04:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2024-06-01 00:42:46 +04:00
|
|
|
|
using System.Xml.Linq;
|
2024-05-25 13:06:38 +04:00
|
|
|
|
|
2024-04-29 17:59:45 +04:00
|
|
|
|
|
|
|
|
|
namespace ElectronicsShopDataBaseImplement.Implements
|
|
|
|
|
{
|
|
|
|
|
public class OrderStorage : IOrderStorage
|
|
|
|
|
{
|
2024-05-26 17:47:24 +04:00
|
|
|
|
public OrderViewModel? Insert(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new Database();
|
|
|
|
|
var newComponent = Order.Create(context,model);
|
|
|
|
|
if (newComponent == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
context.Orders.Add(newComponent);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return newComponent.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public OrderViewModel? Update(OrderBindingModel model) {
|
|
|
|
|
using var context = new Database();
|
|
|
|
|
using var transcation = context.Database.BeginTransaction();
|
2024-06-01 00:42:46 +04:00
|
|
|
|
|
|
|
|
|
var order = context.Orders.FirstOrDefault(rec => rec.ID == model.ID);
|
|
|
|
|
if (order == null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
order.UpdateProducts(context, model);
|
|
|
|
|
transcation.Commit();
|
|
|
|
|
return order.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public OrderViewModel? DeleteProduct(OrderBindingModel model) {
|
|
|
|
|
using var context = new Database();
|
|
|
|
|
using var transcation = context.Database.BeginTransaction();
|
|
|
|
|
|
|
|
|
|
var order = context.Orders.FirstOrDefault(rec => rec.ID == model.ID);
|
|
|
|
|
if (order == null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
order.DeleteProducts(context, model);
|
|
|
|
|
transcation.Commit();
|
|
|
|
|
return order.GetViewModel;
|
2024-05-26 17:47:24 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public OrderViewModel? Delete(OrderBindingModel model)
|
2024-04-29 17:59:45 +04:00
|
|
|
|
{
|
|
|
|
|
using var context = new Database();
|
|
|
|
|
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-30 23:49:53 +04:00
|
|
|
|
using var context = new Database();
|
|
|
|
|
if (model.ClientID.HasValue) {
|
|
|
|
|
return context.Orders
|
2024-07-24 13:02:15 +04:00
|
|
|
|
.Include(x => x.Payments)
|
2024-05-30 23:49:53 +04:00
|
|
|
|
.Include(x => x.Products)
|
|
|
|
|
.ThenInclude(x => x._product)
|
2024-05-31 16:05:43 +04:00
|
|
|
|
.OrderBy(x => x.ID)
|
|
|
|
|
.LastOrDefault(x => (model.ClientID.HasValue && x.ClientID == model.ClientID))?.GetViewModel;
|
2024-05-30 23:49:53 +04:00
|
|
|
|
}
|
|
|
|
|
if (model.ID.HasValue)
|
2024-04-29 17:59:45 +04:00
|
|
|
|
{
|
2024-05-30 23:49:53 +04:00
|
|
|
|
return context.Orders
|
2024-07-24 13:02:15 +04:00
|
|
|
|
.Include(x => x.Payments)
|
|
|
|
|
.Include(x => x.Products)
|
2024-05-30 23:49:53 +04:00
|
|
|
|
.ThenInclude(x => x._product)
|
|
|
|
|
.FirstOrDefault(x => (model.ID.HasValue && x.ID == model.ID))?.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
2024-04-29 17:59:45 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
|
|
|
|
|
{
|
2024-05-30 20:32:16 +04:00
|
|
|
|
using var context = new Database();
|
2024-05-31 16:05:43 +04:00
|
|
|
|
if (!model.ID.HasValue && model.DateFrom.HasValue && model.DateTo.HasValue)
|
2024-04-29 17:59:45 +04:00
|
|
|
|
{
|
|
|
|
|
return new();
|
|
|
|
|
}
|
2024-05-30 20:32:16 +04:00
|
|
|
|
else if (model.ClientID.HasValue) {
|
|
|
|
|
return context.Orders
|
|
|
|
|
.Include (x => x.Products)
|
|
|
|
|
.Where(x => x.ClientID == model.ClientID)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
2024-04-29 18:34:49 +04:00
|
|
|
|
return context.Orders
|
|
|
|
|
.Include(x => x.Products)
|
|
|
|
|
.ThenInclude(x => x._product)
|
|
|
|
|
.Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
2024-04-29 17:59:45 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<OrderViewModel> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
using var context = new Database();
|
2024-05-31 16:05:43 +04:00
|
|
|
|
return context.Orders
|
2024-05-26 17:47:24 +04:00
|
|
|
|
.Include(x => x.Products)
|
|
|
|
|
.ThenInclude(x => x._product).ToList()
|
|
|
|
|
.Select(x => x.GetViewModel).ToList();
|
2024-04-29 17:59:45 +04:00
|
|
|
|
}
|
2024-05-26 17:47:24 +04:00
|
|
|
|
}
|
2024-06-01 00:42:46 +04:00
|
|
|
|
}
|