PIbd22_Kuznetsov_A.V._Sewin.../SewingDresses/SewingDressesDatabaseImplement/Implements/OrderStorage.cs

86 lines
3.3 KiB
C#
Raw Normal View History

2024-05-26 13:40:22 +04:00
using Microsoft.EntityFrameworkCore;
using SewingDressesContracts.BindingModels;
2024-05-04 19:44:42 +04:00
using SewingDressesContracts.SearchModels;
using SewingDressesContracts.StoragesContracts;
using SewingDressesContracts.ViewModels;
using SewingDressesDatabaseImplement.Models;
namespace SewingDressesDatabaseImplement.Implements
{
2024-05-26 13:34:14 +04:00
public class OrderStorage : IOrderStorage
{
2024-05-26 13:40:22 +04:00
public List<OrderViewModel> GetFullList()
2024-05-26 13:34:14 +04:00
{
using var context = new SewingDressesDatabase();
2024-05-26 13:40:22 +04:00
return context.Orders.Include(x => x.Dress).Include(x => x.Client).Include(x => x.Implement).Select(x => x.GetViewModel).ToList();
2024-05-26 13:34:14 +04:00
}
2024-05-26 13:40:22 +04:00
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
2024-05-26 13:34:14 +04:00
{
2024-05-26 13:40:22 +04:00
if (!model.Id.HasValue && !model.DateFrom.HasValue && !model.ClientId.HasValue && !model.Status.HasValue)
2024-05-26 13:34:14 +04:00
{
return new();
}
using var context = new SewingDressesDatabase();
if (model.Id.HasValue)
2024-05-26 13:40:22 +04:00
return context.Orders.Include(x => x.Dress).Include(x => x.Client).Include(x => x.Implement).Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList();
2024-05-26 13:34:14 +04:00
else if (model.ClientId.HasValue)
2024-05-26 13:40:22 +04:00
return context.Orders.Include(x => x.Dress).Include(x => x.Client).Include(x => x.Implement).Where(x => x.ClientId == model.ClientId).Select(x => x.GetViewModel).ToList();
else if (model.DateFrom != null)
return context.Orders.Include(x => x.Dress).Include(x => x.Client).Include(x => x.Implement).Where(x => x.DateCreate >= model.DateFrom).Where(x => x.DateCreate <= model.DateTo).Select(x => x.GetViewModel).ToList();
2024-05-26 13:34:14 +04:00
else
2024-05-26 13:40:22 +04:00
return context.Orders.Include(x => x.Dress).Include(x => x.Client).Include(x => x.Implement).Where(x => x.Status.Equals(model.Status)).Select(x => x.GetViewModel).ToList();
2024-05-26 13:34:14 +04:00
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
2024-05-26 13:40:22 +04:00
if (!model.Id.HasValue && (!model.ImplementId.HasValue || !model.Status.HasValue))
2024-05-26 13:34:14 +04:00
{
return null;
}
using var context = new SewingDressesDatabase();
2024-05-26 13:40:22 +04:00
if (model.Id.HasValue)
return context.Orders.Include(x => x.Dress).Include(x => x.Client).Include(x => x.Implement).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
return context.Orders.Include(x => x.Dress).Include(x => x.Client).Include(x => x.Implement).FirstOrDefault(x => x.ImplementId == model.ImplementId && x.Status == model.Status)?.GetViewModel;
2024-05-26 13:34:14 +04:00
}
public OrderViewModel? Insert(OrderBindingModel model)
{
2024-05-26 13:40:22 +04:00
using var context = new SewingDressesDatabase();
var order = Order.Create(model, context);
2024-05-26 13:34:14 +04:00
if (order == null)
{
return null;
}
context.Orders.Add(order);
context.SaveChanges();
2024-05-26 13:40:22 +04:00
return order.GetViewModel;
2024-05-04 19:44:42 +04:00
2024-05-26 13:34:14 +04:00
}
public OrderViewModel? Update(OrderBindingModel model)
{
2024-05-04 19:44:42 +04:00
2024-05-26 13:34:14 +04:00
using var context = new SewingDressesDatabase();
2024-05-26 13:40:22 +04:00
var order = context.Orders.Include(x => x.Dress).Include(x => x.Client).Include(x => x.Implement).FirstOrDefault(x => x.Id == model.Id);
2024-05-26 13:34:14 +04:00
if (order == null)
{
return null;
}
order.Update(model);
context.SaveChanges();
2024-05-26 13:40:22 +04:00
return order.GetViewModel;
2024-05-04 19:44:42 +04:00
2024-05-26 13:34:14 +04:00
}
public OrderViewModel? Delete(OrderBindingModel model)
{
using var context = new SewingDressesDatabase();
2024-05-26 13:40:22 +04:00
var element = context.Orders.Include(x => x.Dress).Include(x => x.Client).Include(x => x.Implement).FirstOrDefault(x => x.Id == model.Id);
2024-05-26 13:34:14 +04:00
if (element == null)
{
return null;
}
context.Orders.Remove(element);
context.SaveChanges();
2024-05-26 13:40:22 +04:00
return element.GetViewModel;
2024-05-26 13:34:14 +04:00
}
2024-05-04 19:44:42 +04:00
2024-05-26 13:34:14 +04:00
}
2024-05-04 19:44:42 +04:00
}