2024-06-02 16:54:29 +04:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using ShipyardContracts.BindingModels;
|
2024-04-27 19:15:39 +04:00
|
|
|
|
using ShipyardContracts.SearchModels;
|
|
|
|
|
using ShipyardContracts.StoragesContracts;
|
|
|
|
|
using ShipyardContracts.ViewModels;
|
|
|
|
|
using ShipyardDatabaseImplement.Models;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace ShipyardDatabaseImplement.Implements
|
|
|
|
|
{
|
|
|
|
|
public class OrderStorage : IOrderStorage
|
|
|
|
|
{
|
|
|
|
|
public List<OrderViewModel> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
using var context = new ShipyardDataBase();
|
|
|
|
|
return context.Orders
|
2024-06-02 18:07:29 +04:00
|
|
|
|
.Include(x => x.Ship).Include(x => x.Client).Include(x => x.Implementer).Select(x => x.GetViewModel)
|
2024-04-27 19:15:39 +04:00
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new ShipyardDataBase();
|
|
|
|
|
return context.Orders
|
2024-06-02 16:54:29 +04:00
|
|
|
|
.Where(x => (
|
|
|
|
|
(!model.Id.HasValue || x.Id == model.Id) &&
|
|
|
|
|
(!model.DateFrom.HasValue || x.DateCreate >= model.DateFrom) &&
|
2024-06-02 17:45:23 +04:00
|
|
|
|
(!model.DateTo.HasValue || x.DateCreate <= model.DateTo)&&
|
2024-06-02 18:07:29 +04:00
|
|
|
|
(!model.ClientId.HasValue || x.ClientId == model.ClientId)&&
|
|
|
|
|
(!model.Status.HasValue || x.Status == model.Status)
|
|
|
|
|
)
|
2024-06-02 16:54:29 +04:00
|
|
|
|
)
|
2024-06-02 18:07:29 +04:00
|
|
|
|
.Include(x => x.Implementer)
|
|
|
|
|
.Include(x => x.Ship)
|
2024-06-02 17:45:23 +04:00
|
|
|
|
.Include(x => x.Client)
|
2024-06-02 16:54:29 +04:00
|
|
|
|
.Select(x => x.GetViewModel)
|
2024-04-27 19:15:39 +04:00
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
public OrderViewModel? GetElement(OrderSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (!model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
using var context = new ShipyardDataBase();
|
2024-06-02 18:07:29 +04:00
|
|
|
|
return context.Orders.Include(x => x.Ship)
|
|
|
|
|
.Include(x => x.Client)
|
|
|
|
|
.Include(x => x.Implementer)
|
|
|
|
|
.FirstOrDefault(
|
|
|
|
|
x => x.Id == model.Id ||
|
|
|
|
|
(model.ImplementerId.HasValue && model.Status.HasValue &&
|
|
|
|
|
x.ImplementerId == model.ImplementerId && x.Status == model.Status))?
|
|
|
|
|
.GetViewModel;
|
2024-04-27 19:15:39 +04:00
|
|
|
|
}
|
|
|
|
|
public OrderViewModel? Insert(OrderBindingModel model)
|
|
|
|
|
{
|
2024-06-02 16:54:29 +04:00
|
|
|
|
using var context = new ShipyardDataBase();
|
|
|
|
|
var newOrder = Order.Create(model, context);
|
2024-04-27 19:15:39 +04:00
|
|
|
|
if (newOrder == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
context.Orders.Add(newOrder);
|
|
|
|
|
context.SaveChanges();
|
2024-06-02 16:54:29 +04:00
|
|
|
|
return newOrder.GetViewModel;
|
2024-04-27 19:15:39 +04:00
|
|
|
|
}
|
|
|
|
|
public OrderViewModel? Update(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new ShipyardDataBase();
|
2024-06-02 18:07:29 +04:00
|
|
|
|
var order = context.Orders
|
|
|
|
|
.Include(x => x.Implementer)
|
|
|
|
|
.FirstOrDefault(x => x.Id == model.Id);
|
2024-04-27 19:15:39 +04:00
|
|
|
|
if (order == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
order.Update(model);
|
|
|
|
|
context.SaveChanges();
|
2024-06-02 16:54:29 +04:00
|
|
|
|
return order.GetViewModel;
|
2024-04-27 19:15:39 +04:00
|
|
|
|
}
|
|
|
|
|
public OrderViewModel? Delete(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new ShipyardDataBase();
|
|
|
|
|
var element = context.Orders.FirstOrDefault(rec => rec.Id ==
|
|
|
|
|
model.Id);
|
|
|
|
|
if (element != null)
|
|
|
|
|
{
|
|
|
|
|
context.Orders.Remove(element);
|
|
|
|
|
context.SaveChanges();
|
2024-06-02 16:54:29 +04:00
|
|
|
|
return element.GetViewModel;
|
2024-04-27 19:15:39 +04:00
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|