Borschevskaya A.A. Lab work 3 #5

Closed
pgirl1 wants to merge 9 commits from lab3_base into lab2_base
5 changed files with 227 additions and 19 deletions
Showing only changes of commit 7649b1fa3b - Show all commits

View File

@ -3,6 +3,7 @@ using FurnitureAssemblyContracts.SearchModels;
using FurnitureAssemblyContracts.StoragesContracts;
using FurnitureAssemblyContracts.ViewModels;
using FurnitureAssemblyDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
@ -21,7 +22,7 @@ namespace FurnitureAssemblyDatabaseImplement.Implements
return new();
}
using var context = new FurnitureAssemblyDatabase();
return context.Orders.FirstOrDefault(x =>
return context.Orders.Include(x => x.Furniture).FirstOrDefault(x =>
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
@ -34,6 +35,7 @@ namespace FurnitureAssemblyDatabaseImplement.Implements
}
using var context = new FurnitureAssemblyDatabase();
return context.Orders
.Include(x => x.Furniture)
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
@ -43,7 +45,7 @@ namespace FurnitureAssemblyDatabaseImplement.Implements
{
using var context = new FurnitureAssemblyDatabase();
return context.Orders
.ToList()
.Include(x => x.Furniture)
.Select(x => x.GetViewModel)
.ToList();
}
@ -58,7 +60,7 @@ namespace FurnitureAssemblyDatabaseImplement.Implements
}
context.Orders.Add(newOrder);
context.SaveChanges();
return newOrder.GetViewModel;
return context.Orders.Include(x => x.Furniture).FirstOrDefault(x => x.Id == newOrder.Id)?.GetViewModel;
}
public OrderViewModel? Update(OrderBindingModel model)
@ -71,7 +73,7 @@ namespace FurnitureAssemblyDatabaseImplement.Implements
}
order.Update(model);
context.SaveChanges();
return order.GetViewModel;
return context.Orders.Include(x => x.Furniture).FirstOrDefault(x => x.Id == order.Id)?.GetViewModel;
}
public OrderViewModel? Delete(OrderBindingModel model)
@ -82,9 +84,19 @@ namespace FurnitureAssemblyDatabaseImplement.Implements
{
context.Orders.Remove(order);
context.SaveChanges();
return order.GetViewModel;
return context.Orders.Include(x => x.Furniture).FirstOrDefault(x => x.Id == order.Id)?.GetViewModel;
}
return null;
}
public OrderViewModel GetOrderViewModel(Order order)
{
var viewmodel = order.GetViewModel;
using var context = new FurnitureAssemblyDatabase();
var furniture = context.Furnitures.FirstOrDefault(x => x.Id == order.FurnitureId);
if (furniture != null)
viewmodel.FurnitureName = furniture.FurnitureName;
return viewmodel;
}
}
}

View File

@ -0,0 +1,174 @@
// <auto-generated />
using System;
using FurnitureAssemblyDatabaseImplement;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace FurnitureAssemblyDatabaseImplement.Migrations
{
[DbContext(typeof(FurnitureAssemblyDatabase))]
[Migration("20230311052240_SecondMigration")]
partial class SecondMigration
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.3")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.Component", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("ComponentName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Cost")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("Components");
});
modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.Furniture", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("FurnitureName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Price")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("Furnitures");
});
modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.FurnitureComponent", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("ComponentId")
.HasColumnType("int");
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("FurnitureId")
.HasColumnType("int");
b.Property<int>("ProductId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ComponentId");
b.HasIndex("FurnitureId");
b.ToTable("FurnitureComponents");
});
modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.Order", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("Count")
.HasColumnType("int");
b.Property<DateTime>("DateCreate")
.HasColumnType("datetime2");
b.Property<DateTime?>("DateImplement")
.HasColumnType("datetime2");
b.Property<int>("FurnitureId")
.HasColumnType("int");
b.Property<int>("Status")
.HasColumnType("int");
b.Property<double>("Sum")
.HasColumnType("float");
b.HasKey("Id");
b.HasIndex("FurnitureId");
b.ToTable("Orders");
});
modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.FurnitureComponent", b =>
{
b.HasOne("FurnitureAssemblyDatabaseImplement.Models.Component", "Component")
.WithMany("FurnitureComponents")
.HasForeignKey("ComponentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("FurnitureAssemblyDatabaseImplement.Models.Furniture", "Furniture")
.WithMany("Components")
.HasForeignKey("FurnitureId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Component");
b.Navigation("Furniture");
});
modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.Order", b =>
{
b.HasOne("FurnitureAssemblyDatabaseImplement.Models.Furniture", "Furniture")
.WithMany("Orders")
.HasForeignKey("FurnitureId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Furniture");
});
modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.Component", b =>
{
b.Navigation("FurnitureComponents");
});
modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.Furniture", b =>
{
b.Navigation("Components");
b.Navigation("Orders");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,29 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace FurnitureAssemblyDatabaseImplement.Migrations
{
/// <inheritdoc />
public partial class SecondMigration : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "FurnitureName",
table: "Orders");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "FurnitureName",
table: "Orders",
type: "nvarchar(max)",
nullable: false,
defaultValue: "");
}
}
}

View File

@ -111,10 +111,6 @@ namespace FurnitureAssemblyDatabaseImplement.Migrations
b.Property<int>("FurnitureId")
.HasColumnType("int");
b.Property<string>("FurnitureName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<int>("Status")
.HasColumnType("int");
@ -149,11 +145,13 @@ namespace FurnitureAssemblyDatabaseImplement.Migrations
modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.Order", b =>
{
b.HasOne("FurnitureAssemblyDatabaseImplement.Models.Furniture", null)
b.HasOne("FurnitureAssemblyDatabaseImplement.Models.Furniture", "Furniture")
.WithMany("Orders")
.HasForeignKey("FurnitureId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Furniture");
});
modelBuilder.Entity("FurnitureAssemblyDatabaseImplement.Models.Component", b =>

View File

@ -18,7 +18,6 @@ namespace FurnitureAssemblyDatabaseImplement.Models
public int Id { get; private set; }
[Required]
public int FurnitureId { get; private set; }
public string FurnitureName { get; private set; } = string.Empty;
[Required]
public int Count { get; private set; }
[Required]
@ -30,6 +29,8 @@ namespace FurnitureAssemblyDatabaseImplement.Models
public DateTime? DateImplement { get; private set; }
public virtual Furniture Furniture { get; set; }
public static Order? Create(OrderBindingModel? model)
{
if (model == null)
@ -40,7 +41,6 @@ namespace FurnitureAssemblyDatabaseImplement.Models
{
Id = model.Id,
FurnitureId = model.FurnitureId,
FurnitureName = model.FurnitureName,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
@ -55,7 +55,6 @@ namespace FurnitureAssemblyDatabaseImplement.Models
{
Id = model.Id,
FurnitureId = model.FurnitureId,
FurnitureName = model.FurnitureName,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
@ -70,19 +69,15 @@ namespace FurnitureAssemblyDatabaseImplement.Models
{
return;
}
FurnitureId = model.FurnitureId;
FurnitureName = model.FurnitureName;
Count = model.Count;
Sum = model.Sum;
Status = model.Status;
DateCreate = model.DateCreate;
DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel => new()
{
Id = Id,
FurnitureId = FurnitureId,
FurnitureName = FurnitureName,
FurnitureName = Furniture.FurnitureName,
Count = Count,
Sum = Sum,
Status = Status,