lab3-hard-to-lab4-hard #8

Closed
Salikh wants to merge 14 commits from lab3-hard into laba4-hard
5 changed files with 69 additions and 70 deletions
Showing only changes of commit 1cb89e24d7 - Show all commits

View File

@ -13,8 +13,10 @@ namespace MotorPlantDatabaseImplement.Implements
public List<OrderViewModel> GetFullList()
{
using var context = new MotorPlantDatabase();
return context.Orders.Include(x => x.Engine).Select(x => x.GetViewModel).ToList();
}
return context.Orders
.Select(x => AccessEngineStorage(x.GetViewModel))
.ToList();
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (!model.Id.HasValue)
@ -22,67 +24,70 @@ namespace MotorPlantDatabaseImplement.Implements
return new();
}
using var context = new MotorPlantDatabase();
return context.Orders
.Include(x => x.Engine)
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
}
return context.Orders.Where(x => x.Id == model.Id).Select(x => AccessEngineStorage(x.GetViewModel)).ToList();
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new MotorPlantDatabase();
return context.Orders.Include(x => x.Engine)
.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
}
if (!model.Id.HasValue)
{
return null;
}
using var context = new MotorPlantDatabase();
return AccessEngineStorage(context.Orders.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel);
}
public OrderViewModel? Insert(OrderBindingModel model)
{
var newOrder = Order.Create(model);
if (newOrder == null)
{
return null;
}
using var context = new MotorPlantDatabase();
context.Orders.Add(newOrder);
context.SaveChanges();
return context.Orders
.Include(x => x.Engine)
.FirstOrDefault(x => x.Id == newOrder.Id)
?.GetViewModel;
}
var newOrder = Order.Create(model);
if (newOrder == null)
{
return null;
}
using var context = new MotorPlantDatabase();
context.Orders.Add(newOrder);
context.SaveChanges();
return AccessEngineStorage(newOrder.GetViewModel);
}
public OrderViewModel? Update(OrderBindingModel model)
{
using var context = new MotorPlantDatabase();
var order = context.Orders.FirstOrDefault(x => x.Id == model.Id);
if (order == null)
{
return null;
}
order.Update(model);
context.SaveChanges();
return context.Orders
.Include(x => x.Engine)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
}
public OrderViewModel? Delete(OrderBindingModel model)
{
using var context = new MotorPlantDatabase();
var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
var deletedElement = context.Orders
.Include(x => x.Engine)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
context.Orders.Remove(element);
context.SaveChanges();
return deletedElement;
}
return null;
}
}
using var context = new MotorPlantDatabase();
var order = context.Orders.FirstOrDefault(x => x.Id ==
model.Id);
if (order == null)
{
return null;
}
order.Update(model);
context.SaveChanges();
return AccessEngineStorage(order.GetViewModel);
}
public OrderViewModel? Delete(OrderBindingModel model)
{
using var context = new MotorPlantDatabase();
var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Orders.Remove(element);
context.SaveChanges();
return AccessEngineStorage(element.GetViewModel);
}
return null;
}
public static OrderViewModel AccessEngineStorage(OrderViewModel model)
{
if (model == null)
return null;
using var context = new MotorPlantDatabase();
foreach (var Engine in context.Engines)
{
if (Engine.Id == model.EngineId)
{
model.EngineName = Engine.EngineName;
break;
}
}
return model;
}
}
}

View File

@ -12,8 +12,8 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
namespace MotorPlantDatabaseImplement.Migrations
{
[DbContext(typeof(MotorPlantDatabase))]
[Migration("20240411122818_InitHardMigration")]
partial class InitHardMigration
[Migration("20240411140319_NewMig")]
partial class NewMig
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
@ -198,13 +198,11 @@ namespace MotorPlantDatabaseImplement.Migrations
modelBuilder.Entity("MotorPlantDatabaseImplement.Models.Order", b =>
{
b.HasOne("MotorPlantDatabaseImplement.Models.Engine", "Engine")
b.HasOne("MotorPlantDatabaseImplement.Models.Engine", null)
.WithMany("Orders")
.HasForeignKey("EngineId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Engine");
});
modelBuilder.Entity("MotorPlantDatabaseImplement.Models.ShopEngine", b =>

View File

@ -7,7 +7,7 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
namespace MotorPlantDatabaseImplement.Migrations
{
/// <inheritdoc />
public partial class InitHardMigration : Migration
public partial class NewMig : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)

View File

@ -195,13 +195,11 @@ namespace MotorPlantDatabaseImplement.Migrations
modelBuilder.Entity("MotorPlantDatabaseImplement.Models.Order", b =>
{
b.HasOne("MotorPlantDatabaseImplement.Models.Engine", "Engine")
b.HasOne("MotorPlantDatabaseImplement.Models.Engine", null)
.WithMany("Orders")
.HasForeignKey("EngineId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Engine");
});
modelBuilder.Entity("MotorPlantDatabaseImplement.Models.ShopEngine", b =>

View File

@ -27,8 +27,6 @@ namespace MotorPlantDatabaseImplement.Models
public DateTime? DateImplement { get; private set; }
public virtual Engine Engine { get; set; }
public static Order? Create(OrderBindingModel? model)
{
if (model == null)