перетерпеть

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.StoragesContracts;
using AircraftPlantContracts.ViewModels; using AircraftPlantContracts.ViewModels;
using AircraftPlantDatabaseImplement.Models; using AircraftPlantDatabaseImplement.Models;
using System; using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AircraftPlantDatabaseImplement.Implements namespace AircraftPlantDatabaseImplement.Implements
{ {
@ -16,31 +12,39 @@ namespace AircraftPlantDatabaseImplement.Implements
public List<OrderViewModel> GetFullList() public List<OrderViewModel> GetFullList()
{ {
using var context = new AircraftPlantDatabase(); 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) public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{ {
if (!model.Id.HasValue) if (!model.Id.HasValue)
{ {
return new(); return new();
} }
using var context = new AircraftPlantDatabase(); using var context = new AircraftPlantDatabase();
return context.Orders return context.Orders
.Where(x => x.Id.Equals(model.Id)) .Include(x => x.Plant)
.Select(x => GetViewModel(x)) .Where(x => x.Id == model.Id)
.ToList(); .Select(x => x.GetViewModel)
.ToList();
} }
public OrderViewModel? GetElement(OrderSearchModel model) public OrderViewModel? GetElement(OrderSearchModel model)
{ {
if (!model.Id.HasValue) if (!model.Id.HasValue)
{ {
return null; return null;
} }
using var context = new AircraftPlantDatabase(); 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) public OrderViewModel? Insert(OrderBindingModel model)
{ {
var newOrder = Order.Create(model); var newOrder = Order.Create(model);
@ -48,12 +52,15 @@ namespace AircraftPlantDatabaseImplement.Implements
{ {
return null; return null;
} }
using var context = new AircraftPlantDatabase(); using var context = new AircraftPlantDatabase();
context.Orders.Add(newOrder); context.Orders.Add(newOrder);
context.SaveChanges(); context.SaveChanges();
return GetViewModel(newOrder); return context.Orders
.Include(x => x.Plant)
.FirstOrDefault(x => x.Id == newOrder.Id)
?.GetViewModel;
} }
public OrderViewModel? Update(OrderBindingModel model) public OrderViewModel? Update(OrderBindingModel model)
{ {
using var context = new AircraftPlantDatabase(); using var context = new AircraftPlantDatabase();
@ -62,33 +69,29 @@ namespace AircraftPlantDatabaseImplement.Implements
{ {
return null; return null;
} }
order.Update(model); order.Update(model);
context.SaveChanges(); context.SaveChanges();
return GetViewModel(order); return context.Orders
.Include(x => x.Plant)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
} }
public OrderViewModel? Delete(OrderBindingModel model) public OrderViewModel? Delete(OrderBindingModel model)
{ {
using var context = new AircraftPlantDatabase(); 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) if (element != null)
{ {
var deletedElement = context.Orders
.Include(x => x.Plant)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
context.Orders.Remove(element); context.Orders.Remove(element);
context.SaveChanges(); context.SaveChanges();
return GetViewModel(element); return deletedElement;
} }
return null; 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 => modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.Order", b =>
{ {
b.HasOne("AircraftPlantDatabaseImplement.Models.Plane", null) b.HasOne("AircraftPlantDatabaseImplement.Models.Plane", "Plane")
.WithMany("Orders") .WithMany("Orders")
.HasForeignKey("PlaneId") .HasForeignKey("PlaneId")
.OnDelete(DeleteBehavior.Cascade) .OnDelete(DeleteBehavior.Cascade)
.IsRequired(); .IsRequired();
b.Navigation("Plane");
}); });
modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.PlaneComponent", b => modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.PlaneComponent", b =>