перетерпеть

This commit is contained in:
Камилия Сафиулова 2024-03-26 19:43:48 +04:00
parent 5d694d2f12
commit 9354462a00
2 changed files with 36 additions and 31 deletions

View File

@ -3,11 +3,7 @@ using AircraftPlantContracts.SearchModels;
using AircraftPlantContracts.StoragesContracts;
using AircraftPlantContracts.ViewModels;
using AircraftPlantDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
namespace AircraftPlantDatabaseImplement.Implements
{
@ -16,31 +12,39 @@ namespace AircraftPlantDatabaseImplement.Implements
public List<OrderViewModel> GetFullList()
{
using var context = new AircraftPlantDatabase();
return context.Orders.Select(x => GetViewModel(x)).ToList();
return context.Orders
.Include(x => x.Plant)
.Select(x => x.GetViewModel)
.ToList();
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
using var context = new AircraftPlantDatabase();
return context.Orders
.Where(x => x.Id.Equals(model.Id))
.Select(x => GetViewModel(x))
.Include(x => x.Plant)
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new AircraftPlantDatabase();
return GetViewModel(context.Orders.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id));
return context.Orders
.Include(x => x.Plant)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
}
public OrderViewModel? Insert(OrderBindingModel model)
{
var newOrder = Order.Create(model);
@ -48,12 +52,15 @@ namespace AircraftPlantDatabaseImplement.Implements
{
return null;
}
using var context = new AircraftPlantDatabase();
context.Orders.Add(newOrder);
context.SaveChanges();
return GetViewModel(newOrder);
return context.Orders
.Include(x => x.Plant)
.FirstOrDefault(x => x.Id == newOrder.Id)
?.GetViewModel;
}
public OrderViewModel? Update(OrderBindingModel model)
{
using var context = new AircraftPlantDatabase();
@ -62,33 +69,29 @@ namespace AircraftPlantDatabaseImplement.Implements
{
return null;
}
order.Update(model);
context.SaveChanges();
return GetViewModel(order);
return context.Orders
.Include(x => x.Plant)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
}
public OrderViewModel? Delete(OrderBindingModel model)
{
using var context = new AircraftPlantDatabase();
var element = context.Orders.FirstOrDefault(x => x.Id == model.Id);
var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
var deletedElement = context.Orders
.Include(x => x.Plant)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
context.Orders.Remove(element);
context.SaveChanges();
return GetViewModel(element);
return deletedElement;
}
return null;
}
private static OrderViewModel GetViewModel(Order order)
{
using var context = new AircraftPlantDatabase();
var viewModel = order.GetViewModel;
var plane = context.Planes.FirstOrDefault(x => x.Id == order.PlaneId);
if (plane != null)
{
viewModel.PlaneName = plane.PlaneName;
}
return viewModel;
}
}
}

View File

@ -123,11 +123,13 @@ namespace AircraftPlantDatabaseImplement.Migrations
modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.Order", b =>
{
b.HasOne("AircraftPlantDatabaseImplement.Models.Plane", null)
b.HasOne("AircraftPlantDatabaseImplement.Models.Plane", "Plane")
.WithMany("Orders")
.HasForeignKey("PlaneId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Plane");
});
modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.PlaneComponent", b =>