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 16:54:29 +04:00
|
|
|
|
.Include(x => x.Ship).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) &&
|
|
|
|
|
(!model.DateTo.HasValue || x.DateCreate <= model.DateTo)
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
.Include(x => x.Ship)
|
|
|
|
|
.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 16:54:29 +04:00
|
|
|
|
return context.Orders.Include(x => x.Ship).FirstOrDefault(x => x.Id == model.Id)?.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();
|
|
|
|
|
var order = context.Orders.FirstOrDefault(x => x.Id ==
|
|
|
|
|
model.Id);
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|