PIbd-21_Ihonkina_E.S._Preca.../PrecastConcretePlantDataBaseImplemet/Implements/OrderStorage.cs

152 lines
4.7 KiB
C#
Raw Normal View History

2023-03-18 19:33:11 +04:00
using Microsoft.EntityFrameworkCore;
using PrecastConcretePlantContracts.BindingModels;
using PrecastConcretePlantContracts.SearchModels;
using PrecastConcretePlantContracts.StoragesContracts;
using PrecastConcretePlantContracts.ViewModels;
using PrecastConcretePlantDatabaseImplement.Models;
using System;
2023-03-18 18:19:08 +04:00
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2023-03-18 19:33:11 +04:00
namespace PrecastConcretePlantDatabaseImplement.Implements
2023-03-18 18:19:08 +04:00
{
2023-03-18 19:33:11 +04:00
public class OrderStorage : IOrderStorage
2023-03-18 18:19:08 +04:00
{
2023-03-18 19:33:11 +04:00
public OrderViewModel? Delete(OrderBindingModel model)
{
using var context = new PrecastConcretePlantDatabase();
2023-04-19 23:48:32 +04:00
2023-03-18 19:33:11 +04:00
var element = context.Orders
2023-04-19 23:48:32 +04:00
.Include(x => x.Reinforced)
.Include(x => x.Client)
.Include(x => x.Implementer)
2023-03-18 19:33:11 +04:00
.FirstOrDefault(rec => rec.Id == model.Id);
2023-04-19 23:48:32 +04:00
2023-03-18 19:33:11 +04:00
if (element != null)
{
context.Orders.Remove(element);
context.SaveChanges();
2023-04-19 23:48:32 +04:00
2023-03-18 19:33:11 +04:00
return element.GetViewModel;
}
2023-04-19 23:48:32 +04:00
2023-03-18 19:33:11 +04:00
return null;
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
2023-04-19 23:48:32 +04:00
2023-03-18 19:33:11 +04:00
using var context = new PrecastConcretePlantDatabase();
2023-04-19 23:48:32 +04:00
return context.Orders
.Include(x => x.Client)
.Include(x => x.Implementer)
.FirstOrDefault(x =>
(model.Status == null || model.Status != null && model.Status.Contains(x.Status)) &&
model.ImplementerId.HasValue && x.ImplementerId == model.ImplementerId ||
model.Id.HasValue && x.Id == model.Id
)
?.GetViewModel;
2023-03-18 19:33:11 +04:00
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
2023-03-22 20:33:00 +04:00
if (model.Id.HasValue)
{
2023-04-19 23:48:32 +04:00
var result = GetElement(model);
return result != null ? new() { result } : new();
2023-03-22 20:33:00 +04:00
}
2023-04-19 23:48:32 +04:00
using var context = new PrecastConcretePlantDatabase();
IQueryable<Order>? queryWhere = null;
if (model.DateFrom.HasValue && model.DateTo.HasValue)
2023-03-22 20:33:00 +04:00
{
2023-04-19 23:48:32 +04:00
queryWhere = context.Orders
.Where(x => model.DateFrom <= x.DateCreate.Date &&
x.DateCreate.Date <= model.DateTo);
}
else if (model.Status != null)
{
queryWhere = context.Orders.Where(x => model.Status.Contains(x.Status));
2023-03-22 20:33:00 +04:00
}
2023-04-19 23:48:32 +04:00
2023-04-05 22:10:29 +04:00
else if (model.ClientId.HasValue)
{
2023-04-19 23:48:32 +04:00
queryWhere = context.Orders.Where(x => x.ClientId == model.ClientId);
2023-04-05 22:10:29 +04:00
}
2023-04-19 23:48:32 +04:00
2023-04-05 22:10:29 +04:00
else
{
return new();
}
2023-04-19 23:48:32 +04:00
return queryWhere
.Include(x => x.Client)
.Include(x => x.Implementer)
.Select(x => x.GetViewModel)
.ToList();
2023-03-18 19:33:11 +04:00
}
public List<OrderViewModel> GetFullList()
{
using var context = new PrecastConcretePlantDatabase();
2023-04-19 23:48:32 +04:00
2023-03-18 19:33:11 +04:00
return context.Orders
.Include(x => x.Reinforced)
2023-04-05 00:55:08 +04:00
.Include(x => x.Client)
2023-04-19 23:48:32 +04:00
.Include(x => x.Implementer)
.Select(x => x.GetViewModel).ToList();
2023-03-18 19:33:11 +04:00
}
public OrderViewModel? Insert(OrderBindingModel model)
{
var newOrder = Order.Create(model);
2023-04-19 23:48:32 +04:00
2023-03-18 19:33:11 +04:00
if (newOrder == null)
{
return null;
}
2023-04-19 23:48:32 +04:00
2023-03-18 19:33:11 +04:00
using var context = new PrecastConcretePlantDatabase();
2023-04-19 23:48:32 +04:00
2023-03-18 19:33:11 +04:00
context.Orders.Add(newOrder);
context.SaveChanges();
2023-04-19 23:48:32 +04:00
2023-03-18 19:33:11 +04:00
return context.Orders
2023-04-19 23:48:32 +04:00
.Include(x => x.Reinforced)
.Include(x => x.Client)
.Include(x => x.Implementer)
.FirstOrDefault(x => x.Id == newOrder.Id)
?.GetViewModel;
2023-03-18 19:33:11 +04:00
}
public OrderViewModel? Update(OrderBindingModel model)
{
using var context = new PrecastConcretePlantDatabase();
2023-04-19 23:48:32 +04:00
var order = context.Orders
.Include(x => x.Reinforced)
.Include(x => x.Client)
.Include(x => x.Implementer)
.FirstOrDefault(x => x.Id == model.Id);
2023-03-18 19:33:11 +04:00
if (order == null)
{
return null;
}
2023-04-19 23:48:32 +04:00
2023-03-18 19:33:11 +04:00
order.Update(model);
context.SaveChanges();
2023-04-19 23:48:32 +04:00
return order.GetViewModel;
2023-03-18 19:33:11 +04:00
}
2023-03-18 18:19:08 +04:00
}
2023-04-19 23:48:32 +04:00
}