Lisov N.A LabWork3 #3
27
DressAtelierDatabaseImplement/DressAtelierDatabase.cs
Normal file
27
DressAtelierDatabaseImplement/DressAtelierDatabase.cs
Normal file
@ -0,0 +1,27 @@
|
||||
using DressAtelierDatabaseImplementation.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DressAtelierDatabaseImplementation
|
||||
{
|
||||
public class DressAtelierDatabase : DbContext
|
||||
{
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
if (optionsBuilder.IsConfigured == false)
|
||||
{
|
||||
optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-7QJ9UJE\SQLEXPRESS;Initial Catalog=DressAtelierDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
|
||||
}
|
||||
base.OnConfiguring(optionsBuilder);
|
||||
}
|
||||
public virtual DbSet<Material> Materials { set; get; }
|
||||
public virtual DbSet<Dress> Dresses { set; get; }
|
||||
public virtual DbSet<DressMaterial> DressMaterials { set; get; }
|
||||
public virtual DbSet<Order> Orders { set; get; }
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.3" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.3" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.3">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\DressAtelierContracts\DressAtelierContracts.csproj" />
|
||||
<ProjectReference Include="..\DressAtelierDataModels\DressAtelierDataModels.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
91
DressAtelierDatabaseImplement/Implements/DressStorage.cs
Normal file
91
DressAtelierDatabaseImplement/Implements/DressStorage.cs
Normal file
@ -0,0 +1,91 @@
|
||||
using DressAtelierContracts.BindingModels;
|
||||
using DressAtelierContracts.SearchModels;
|
||||
using DressAtelierContracts.StorageContracts;
|
||||
using DressAtelierContracts.ViewModels;
|
||||
using DressAtelierDatabaseImplementation.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DressAtelierDatabaseImplementation.Implements
|
||||
{
|
||||
public class DressStorage : IDressStorage
|
||||
{
|
||||
public List<DressViewModel> GetFullList()
|
||||
{
|
||||
using var context = new DressAtelierDatabase();
|
||||
return context.Dresses.Include(x => x.Materials).ThenInclude(x => x.Material).ToList().Select(x => x.GetViewModel).ToList();
|
||||
}
|
||||
|
||||
public List<DressViewModel> GetFilteredList(DressSearchModel model)
|
||||
{
|
||||
using var context = new DressAtelierDatabase();
|
||||
return context.Dresses.Include(x => x.Materials).ThenInclude(x => x.Material).Where(x => x.ID == model.ID).ToList().Select(x => x.GetViewModel).ToList();
|
||||
}
|
||||
|
||||
public DressViewModel? GetElement(DressSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.DressName) && !model.ID.HasValue)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new DressAtelierDatabase();
|
||||
return context.Dresses.Include(x => x.Materials).ThenInclude(x => x.Material).FirstOrDefault(x => !string.IsNullOrEmpty(model.DressName) ? x.DressName.Contains(model.DressName) : x.ID == model.ID).GetViewModel;
|
||||
}
|
||||
|
||||
public DressViewModel? Insert(DressBindingModel model)
|
||||
{
|
||||
using var context = new DressAtelierDatabase();
|
||||
var newDress = Dress.Create(context,model);
|
||||
if (newDress == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
context.Dresses.Add(newDress);
|
||||
context.SaveChanges();
|
||||
return newDress.GetViewModel;
|
||||
}
|
||||
|
||||
public DressViewModel? Update(DressBindingModel model)
|
||||
{
|
||||
using var context = new DressAtelierDatabase();
|
||||
using var transaction = context.Database.BeginTransaction();
|
||||
try
|
||||
{
|
||||
var dress = context.Dresses.FirstOrDefault(x => x.ID == model.ID);
|
||||
if(dress == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
dress.Update(model);
|
||||
context.SaveChanges();
|
||||
dress.UpdateComponents(context, model);
|
||||
transaction.Commit();
|
||||
return dress.GetViewModel;
|
||||
}
|
||||
catch
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public DressViewModel? Delete(DressBindingModel model)
|
||||
{
|
||||
using var context = new DressAtelierDatabase();
|
||||
var dress = context.Dresses.Include(x => x.Materials).FirstOrDefault(x => x.ID == model.ID);
|
||||
if(dress == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
context.Dresses.Remove(dress);
|
||||
context.SaveChanges();
|
||||
return dress.GetViewModel;
|
||||
}
|
||||
}
|
||||
}
|
85
DressAtelierDatabaseImplement/Implements/MaterialStorage.cs
Normal file
85
DressAtelierDatabaseImplement/Implements/MaterialStorage.cs
Normal file
@ -0,0 +1,85 @@
|
||||
using DressAtelierContracts.BindingModels;
|
||||
using DressAtelierContracts.SearchModels;
|
||||
using DressAtelierContracts.StorageContracts;
|
||||
using DressAtelierContracts.ViewModels;
|
||||
using DressAtelierDatabaseImplementation.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DressAtelierDatabaseImplementation.Implements
|
||||
{
|
||||
public class MaterialStorage : IMaterialStorage
|
||||
{
|
||||
|
||||
public List<MaterialViewModel> GetFullList()
|
||||
{
|
||||
using var context = new DressAtelierDatabase();
|
||||
return context.Materials.Select(x => x.GetViewModel).ToList();
|
||||
}
|
||||
public List<MaterialViewModel> GetFilteredList(MaterialSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.ComponentName))
|
||||
{
|
||||
return new();
|
||||
}
|
||||
|
||||
using var context = new DressAtelierDatabase();
|
||||
return context.Materials.Where(x => x.ComponentName.Contains(model.ComponentName)).Select(x => x.GetViewModel).ToList();
|
||||
}
|
||||
|
||||
public MaterialViewModel? GetElement(MaterialSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.ComponentName) && !model.ID.HasValue)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
using var context = new DressAtelierDatabase();
|
||||
return context.Materials.FirstOrDefault(x => !string.IsNullOrEmpty(model.ComponentName) ? x.ComponentName.Contains(model.ComponentName) : x.ID == model.ID).GetViewModel;
|
||||
}
|
||||
|
||||
public MaterialViewModel? Insert(MaterialBindingModel model)
|
||||
{
|
||||
using var context = new DressAtelierDatabase();
|
||||
var newComponent = Material.Create(model);
|
||||
if (newComponent == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
context.Materials.Add(newComponent);
|
||||
context.SaveChanges();
|
||||
return newComponent.GetViewModel;
|
||||
}
|
||||
|
||||
public MaterialViewModel? Update(MaterialBindingModel model)
|
||||
{
|
||||
using var context = new DressAtelierDatabase();
|
||||
var material = context.Materials.FirstOrDefault(x => x.ID == model.ID);
|
||||
if(material == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
material.Update(model);
|
||||
context.SaveChanges();
|
||||
return material.GetViewModel;
|
||||
}
|
||||
|
||||
public MaterialViewModel? Delete(MaterialBindingModel model)
|
||||
{
|
||||
using var context = new DressAtelierDatabase();
|
||||
var material = context.Materials.FirstOrDefault(x => x.ID == model.ID);
|
||||
if(material == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
context.Materials.Remove(material);
|
||||
context.SaveChanges();
|
||||
return material.GetViewModel;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
90
DressAtelierDatabaseImplement/Implements/OrderStorage.cs
Normal file
90
DressAtelierDatabaseImplement/Implements/OrderStorage.cs
Normal file
@ -0,0 +1,90 @@
|
||||
using DressAtelierContracts.BindingModels;
|
||||
using DressAtelierContracts.SearchModels;
|
||||
using DressAtelierContracts.StorageContracts;
|
||||
using DressAtelierContracts.ViewModels;
|
||||
using DressAtelierDatabaseImplementation.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DressAtelierDatabaseImplementation.Implements
|
||||
{
|
||||
public class OrderStorage : IOrderStorage
|
||||
{
|
||||
public List<OrderViewModel> GetFullList()
|
||||
{
|
||||
using var context = new DressAtelierDatabase();
|
||||
return context.Orders.Include(x => x.Dress).Select(x => x.GetViewModel).ToList();
|
||||
}
|
||||
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
|
||||
{
|
||||
using var context = new DressAtelierDatabase();
|
||||
return context.Orders.Include(x => x.Dress).Where(x => x.ID == model.ID).ToList().Select(x => x.GetViewModel).ToList();
|
||||
}
|
||||
|
||||
public OrderViewModel? GetElement(OrderSearchModel model)
|
||||
{
|
||||
if (!model.ID.HasValue)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
using var context = new DressAtelierDatabase();
|
||||
return context.Orders.Include(x => x.Dress).FirstOrDefault(x => x.ID == model.ID)?.GetViewModel;
|
||||
|
||||
}
|
||||
|
||||
public OrderViewModel? Insert(OrderBindingModel model)
|
||||
{
|
||||
using var context = new DressAtelierDatabase();
|
||||
var newOrder = Order.Create(model);
|
||||
if (newOrder == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
context.Orders.Add(newOrder);
|
||||
context.SaveChanges();
|
||||
return context.Orders.Include(x => x.Dress).FirstOrDefault(x => x.ID == model.ID)?.GetViewModel;
|
||||
}
|
||||
|
||||
public OrderViewModel? Update(OrderBindingModel model)
|
||||
{
|
||||
using var context = new DressAtelierDatabase();
|
||||
using var transaction = context.Database.BeginTransaction();
|
||||
try
|
||||
{
|
||||
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.Dress).FirstOrDefault(x => x.ID == model.ID)?.GetViewModel;
|
||||
}
|
||||
catch
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public OrderViewModel? Delete(OrderBindingModel model)
|
||||
{
|
||||
using var context = new DressAtelierDatabase();
|
||||
var order = context.Orders.FirstOrDefault(x => x.ID == model.ID);
|
||||
if(order == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
context.Orders.Remove(order);
|
||||
context.SaveChanges();
|
||||
return context.Orders.Include(x => x.Dress).FirstOrDefault(x => x.ID == model.ID)?.GetViewModel;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
171
DressAtelierDatabaseImplement/Migrations/20230312193501_InitMigration.Designer.cs
generated
Normal file
171
DressAtelierDatabaseImplement/Migrations/20230312193501_InitMigration.Designer.cs
generated
Normal file
@ -0,0 +1,171 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using DressAtelierDatabaseImplementation;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace DressAtelierDatabaseImplementation.Migrations
|
||||
{
|
||||
[DbContext(typeof(DressAtelierDatabase))]
|
||||
[Migration("20230312193501_InitMigration")]
|
||||
partial class InitMigration
|
||||
{
|
||||
/// <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("DressAtelierDatabaseImplementation.Models.Dress", b =>
|
||||
{
|
||||
b.Property<int>("ID")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("ID"));
|
||||
|
||||
b.Property<string>("DressName")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<double>("Price")
|
||||
.HasColumnType("float");
|
||||
|
||||
b.HasKey("ID");
|
||||
|
||||
b.ToTable("Dresses");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("DressAtelierDatabaseImplementation.Models.DressMaterial", b =>
|
||||
{
|
||||
b.Property<int>("ID")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("ID"));
|
||||
|
||||
b.Property<int>("Count")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("DressID")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("MaterialID")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("ID");
|
||||
|
||||
b.HasIndex("DressID");
|
||||
|
||||
b.HasIndex("MaterialID");
|
||||
|
||||
b.ToTable("DressMaterials");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("DressAtelierDatabaseImplementation.Models.Material", 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("Materials");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("DressAtelierDatabaseImplementation.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>("DressID")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("Status")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<double>("Sum")
|
||||
.HasColumnType("float");
|
||||
|
||||
b.HasKey("ID");
|
||||
|
||||
b.HasIndex("DressID");
|
||||
|
||||
b.ToTable("Orders");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("DressAtelierDatabaseImplementation.Models.DressMaterial", b =>
|
||||
{
|
||||
b.HasOne("DressAtelierDatabaseImplementation.Models.Dress", "Dress")
|
||||
.WithMany("Materials")
|
||||
.HasForeignKey("DressID")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("DressAtelierDatabaseImplementation.Models.Material", "Material")
|
||||
.WithMany("DressComponents")
|
||||
.HasForeignKey("MaterialID")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Dress");
|
||||
|
||||
b.Navigation("Material");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("DressAtelierDatabaseImplementation.Models.Order", b =>
|
||||
{
|
||||
b.HasOne("DressAtelierDatabaseImplementation.Models.Dress", "Dress")
|
||||
.WithMany("Orders")
|
||||
.HasForeignKey("DressID")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Dress");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("DressAtelierDatabaseImplementation.Models.Dress", b =>
|
||||
{
|
||||
b.Navigation("Materials");
|
||||
|
||||
b.Navigation("Orders");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("DressAtelierDatabaseImplementation.Models.Material", b =>
|
||||
{
|
||||
b.Navigation("DressComponents");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,125 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace DressAtelierDatabaseImplementation.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class InitMigration : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.CreateTable(
|
||||
name: "Dresses",
|
||||
columns: table => new
|
||||
{
|
||||
ID = table.Column<int>(type: "int", nullable: false)
|
||||
.Annotation("SqlServer:Identity", "1, 1"),
|
||||
DressName = table.Column<string>(type: "nvarchar(max)", nullable: false),
|
||||
Price = table.Column<double>(type: "float", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Dresses", x => x.ID);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "Materials",
|
||||
columns: table => new
|
||||
{
|
||||
ID = table.Column<int>(type: "int", nullable: false)
|
||||
.Annotation("SqlServer:Identity", "1, 1"),
|
||||
ComponentName = table.Column<string>(type: "nvarchar(max)", nullable: false),
|
||||
Cost = table.Column<double>(type: "float", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Materials", x => x.ID);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "Orders",
|
||||
columns: table => new
|
||||
{
|
||||
ID = table.Column<int>(type: "int", nullable: false)
|
||||
.Annotation("SqlServer:Identity", "1, 1"),
|
||||
DressID = table.Column<int>(type: "int", nullable: false),
|
||||
Count = table.Column<int>(type: "int", nullable: false),
|
||||
Sum = table.Column<double>(type: "float", nullable: false),
|
||||
Status = table.Column<int>(type: "int", nullable: false),
|
||||
DateCreate = table.Column<DateTime>(type: "datetime2", nullable: false),
|
||||
DateImplement = table.Column<DateTime>(type: "datetime2", nullable: true)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Orders", x => x.ID);
|
||||
table.ForeignKey(
|
||||
name: "FK_Orders_Dresses_DressID",
|
||||
column: x => x.DressID,
|
||||
principalTable: "Dresses",
|
||||
principalColumn: "ID",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "DressMaterials",
|
||||
columns: table => new
|
||||
{
|
||||
ID = table.Column<int>(type: "int", nullable: false)
|
||||
.Annotation("SqlServer:Identity", "1, 1"),
|
||||
DressID = table.Column<int>(type: "int", nullable: false),
|
||||
MaterialID = table.Column<int>(type: "int", nullable: false),
|
||||
Count = table.Column<int>(type: "int", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_DressMaterials", x => x.ID);
|
||||
table.ForeignKey(
|
||||
name: "FK_DressMaterials_Dresses_DressID",
|
||||
column: x => x.DressID,
|
||||
principalTable: "Dresses",
|
||||
principalColumn: "ID",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
table.ForeignKey(
|
||||
name: "FK_DressMaterials_Materials_MaterialID",
|
||||
column: x => x.MaterialID,
|
||||
principalTable: "Materials",
|
||||
principalColumn: "ID",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_DressMaterials_DressID",
|
||||
table: "DressMaterials",
|
||||
column: "DressID");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_DressMaterials_MaterialID",
|
||||
table: "DressMaterials",
|
||||
column: "MaterialID");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_Orders_DressID",
|
||||
table: "Orders",
|
||||
column: "DressID");
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "DressMaterials");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Orders");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Materials");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Dresses");
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,168 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using DressAtelierDatabaseImplementation;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace DressAtelierDatabaseImplementation.Migrations
|
||||
{
|
||||
[DbContext(typeof(DressAtelierDatabase))]
|
||||
partial class DressAtelierDatabaseModelSnapshot : ModelSnapshot
|
||||
{
|
||||
protected override void BuildModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "7.0.3")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 128);
|
||||
|
||||
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
|
||||
|
||||
modelBuilder.Entity("DressAtelierDatabaseImplementation.Models.Dress", b =>
|
||||
{
|
||||
b.Property<int>("ID")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("ID"));
|
||||
|
||||
b.Property<string>("DressName")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<double>("Price")
|
||||
.HasColumnType("float");
|
||||
|
||||
b.HasKey("ID");
|
||||
|
||||
b.ToTable("Dresses");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("DressAtelierDatabaseImplementation.Models.DressMaterial", b =>
|
||||
{
|
||||
b.Property<int>("ID")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("ID"));
|
||||
|
||||
b.Property<int>("Count")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("DressID")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("MaterialID")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("ID");
|
||||
|
||||
b.HasIndex("DressID");
|
||||
|
||||
b.HasIndex("MaterialID");
|
||||
|
||||
b.ToTable("DressMaterials");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("DressAtelierDatabaseImplementation.Models.Material", 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("Materials");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("DressAtelierDatabaseImplementation.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>("DressID")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("Status")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<double>("Sum")
|
||||
.HasColumnType("float");
|
||||
|
||||
b.HasKey("ID");
|
||||
|
||||
b.HasIndex("DressID");
|
||||
|
||||
b.ToTable("Orders");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("DressAtelierDatabaseImplementation.Models.DressMaterial", b =>
|
||||
{
|
||||
b.HasOne("DressAtelierDatabaseImplementation.Models.Dress", "Dress")
|
||||
.WithMany("Materials")
|
||||
.HasForeignKey("DressID")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("DressAtelierDatabaseImplementation.Models.Material", "Material")
|
||||
.WithMany("DressComponents")
|
||||
.HasForeignKey("MaterialID")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Dress");
|
||||
|
||||
b.Navigation("Material");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("DressAtelierDatabaseImplementation.Models.Order", b =>
|
||||
{
|
||||
b.HasOne("DressAtelierDatabaseImplementation.Models.Dress", "Dress")
|
||||
.WithMany("Orders")
|
||||
.HasForeignKey("DressID")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Dress");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("DressAtelierDatabaseImplementation.Models.Dress", b =>
|
||||
{
|
||||
b.Navigation("Materials");
|
||||
|
||||
b.Navigation("Orders");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("DressAtelierDatabaseImplementation.Models.Material", b =>
|
||||
{
|
||||
b.Navigation("DressComponents");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
103
DressAtelierDatabaseImplement/Models/Dress.cs
Normal file
103
DressAtelierDatabaseImplement/Models/Dress.cs
Normal file
@ -0,0 +1,103 @@
|
||||
using DressAtelierContracts.BindingModels;
|
||||
using DressAtelierContracts.ViewModels;
|
||||
using DressAtelierDataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DressAtelierDatabaseImplementation.Models
|
||||
{
|
||||
public class Dress : IDressModel
|
||||
{
|
||||
public int ID { get; private set; }
|
||||
[Required]
|
||||
public string DressName { get; private set; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
public double Price { get; private set; }
|
||||
|
||||
private Dictionary<int, (IMaterialModel, int)>? _dressMaterials = null;
|
||||
|
||||
[NotMapped]
|
||||
public Dictionary<int, (IMaterialModel, int)> DressComponents
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_dressMaterials == null)
|
||||
{
|
||||
_dressMaterials = Materials.ToDictionary(recPC => recPC.MaterialID, recPC =>(recPC.Material as IMaterialModel, recPC.Count));
|
||||
}
|
||||
return _dressMaterials;
|
||||
}
|
||||
}
|
||||
[ForeignKey("DressID")]
|
||||
public virtual List<DressMaterial> Materials { get; set; } = new();
|
||||
|
||||
[ForeignKey("DressID")]
|
||||
public virtual List<Order> Orders { get; set; } = new();
|
||||
|
||||
public static Dress Create(DressAtelierDatabase context, DressBindingModel model)
|
||||
{
|
||||
return new Dress()
|
||||
{
|
||||
ID = model.ID,
|
||||
DressName = model.DressName,
|
||||
Price = model.Price,
|
||||
Materials = model.DressComponents.Select(x => new DressMaterial
|
||||
{
|
||||
Material = context.Materials.First(y => y.ID == x.Key),
|
||||
Count = x.Value.Item2
|
||||
}).ToList()
|
||||
};
|
||||
}
|
||||
|
||||
public void Update(DressBindingModel model)
|
||||
{
|
||||
DressName = model.DressName;
|
||||
Price = model.Price;
|
||||
}
|
||||
|
||||
public DressViewModel GetViewModel => new()
|
||||
{
|
||||
ID = ID,
|
||||
DressName = DressName,
|
||||
Price = Price,
|
||||
DressComponents = DressComponents
|
||||
};
|
||||
|
||||
public void UpdateComponents(DressAtelierDatabase context, DressBindingModel model)
|
||||
{
|
||||
var dressMaterials = context.DressMaterials.Where(rec => rec.DressID == model.ID).ToList();
|
||||
if (dressMaterials != null && dressMaterials.Count > 0)
|
||||
{ // удалили те, которых нет в модели
|
||||
context.DressMaterials.RemoveRange(dressMaterials.Where(rec => !model.DressComponents.ContainsKey(rec.MaterialID)));
|
||||
context.SaveChanges();
|
||||
// обновили количество у существующих записей
|
||||
foreach (var updateComponent in dressMaterials)
|
||||
{
|
||||
updateComponent.Count = model.DressComponents[updateComponent.MaterialID].Item2;
|
||||
model.DressComponents.Remove(updateComponent.MaterialID);
|
||||
}
|
||||
context.SaveChanges();
|
||||
}
|
||||
var product = context.Dresses.First(x => x.ID == ID);
|
||||
foreach (var pc in model.DressComponents)
|
||||
{
|
||||
context.DressMaterials.Add(new DressMaterial
|
||||
{
|
||||
Dress = product,
|
||||
Material = context.Materials.First(x => x.ID == pc.Key),
|
||||
Count = pc.Value.Item2
|
||||
});
|
||||
context.SaveChanges();
|
||||
}
|
||||
_dressMaterials = null;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
25
DressAtelierDatabaseImplement/Models/DressMaterial.cs
Normal file
25
DressAtelierDatabaseImplement/Models/DressMaterial.cs
Normal file
@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DressAtelierDatabaseImplementation.Models
|
||||
{
|
||||
public class DressMaterial
|
||||
{
|
||||
public int ID { get; set; }
|
||||
|
||||
[Required]
|
||||
public int DressID { get; set; }
|
||||
[Required]
|
||||
public int MaterialID { get; set; }
|
||||
[Required]
|
||||
public int Count { get; set; }
|
||||
public virtual Material Material { get; set; } = new();
|
||||
public virtual Dress Dress { get; set; } = new();
|
||||
|
||||
}
|
||||
}
|
64
DressAtelierDatabaseImplement/Models/Material.cs
Normal file
64
DressAtelierDatabaseImplement/Models/Material.cs
Normal file
@ -0,0 +1,64 @@
|
||||
using DressAtelierContracts.BindingModels;
|
||||
using DressAtelierContracts.ViewModels;
|
||||
using DressAtelierDataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DressAtelierDatabaseImplementation.Models
|
||||
{
|
||||
public class Material : IMaterialModel
|
||||
{
|
||||
public string ComponentName { get; private set; } = string.Empty;
|
||||
|
||||
public double Cost { get; private set; }
|
||||
|
||||
public int ID { get; private set; }
|
||||
|
||||
[ForeignKey("MaterialID")]
|
||||
public virtual List<DressMaterial> DressComponents { get; set; } = new();
|
||||
|
||||
public static Material? Create(MaterialBindingModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new Material()
|
||||
{
|
||||
ID = model.ID,
|
||||
ComponentName = model.ComponentName,
|
||||
Cost = model.Cost
|
||||
};
|
||||
}
|
||||
|
||||
public static Material Create(MaterialViewModel model)
|
||||
{
|
||||
return new Material()
|
||||
{
|
||||
ID = model.ID,
|
||||
ComponentName = model.ComponentName,
|
||||
Cost = model.Cost
|
||||
};
|
||||
}
|
||||
|
||||
public void Update(MaterialBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
ComponentName = model.ComponentName;
|
||||
Cost = model.Cost;
|
||||
}
|
||||
public MaterialViewModel GetViewModel => new()
|
||||
{
|
||||
ID = ID,
|
||||
ComponentName = ComponentName,
|
||||
Cost = Cost
|
||||
};
|
||||
}
|
||||
}
|
95
DressAtelierDatabaseImplement/Models/Order.cs
Normal file
95
DressAtelierDatabaseImplement/Models/Order.cs
Normal file
@ -0,0 +1,95 @@
|
||||
using DressAtelierContracts.BindingModels;
|
||||
using DressAtelierContracts.ViewModels;
|
||||
using DressAtelierDataModels.Enums;
|
||||
using DressAtelierDataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace DressAtelierDatabaseImplementation.Models
|
||||
{
|
||||
public class Order : IOrderModel
|
||||
{
|
||||
public int ID { get; private set; }
|
||||
public int DressID { get; private set; }
|
||||
|
||||
[Required]
|
||||
public int Count { get; private set; }
|
||||
|
||||
[Required]
|
||||
public double Sum { get; private set; }
|
||||
|
||||
[Required]
|
||||
public OrderStatus Status { get; private set; } = OrderStatus.Unknown;
|
||||
|
||||
[Required]
|
||||
public DateTime DateCreate { get; private set; } = DateTime.Now;
|
||||
|
||||
public DateTime? DateImplement { get; private set; }
|
||||
|
||||
public virtual Dress Dress { get; set; }
|
||||
|
||||
public static Order? Create(OrderBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new Order()
|
||||
{
|
||||
ID = model.ID,
|
||||
DressID = model.DressID,
|
||||
Count = model.Count,
|
||||
Sum = model.Sum,
|
||||
Status = model.Status,
|
||||
DateCreate = model.DateCreate,
|
||||
DateImplement = model.DateImplement
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
public static Order Create(OrderViewModel model)
|
||||
{
|
||||
return new Order()
|
||||
{
|
||||
ID = model.ID,
|
||||
DressID = model.DressID,
|
||||
Count = model.Count,
|
||||
Sum = model.Sum,
|
||||
Status = model.Status,
|
||||
DateCreate = model.DateCreate,
|
||||
DateImplement = model.DateImplement
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
public void Update(OrderBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Status = model.Status;
|
||||
DateImplement = model.DateImplement;
|
||||
}
|
||||
|
||||
public OrderViewModel GetViewModel => new()
|
||||
|
||||
{
|
||||
ID = ID,
|
||||
DressID = DressID,
|
||||
Count = Count,
|
||||
Sum = Sum,
|
||||
Status = Status,
|
||||
DateCreate = DateCreate,
|
||||
DateImplement = DateImplement
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
using DressAtelierBusinessLogic.BusinessLogic;
|
||||
using DressAtelierContracts.BusinessLogicContracts;
|
||||
using DressAtelierContracts.StorageContracts;
|
||||
using DressAtelierFileImplement.Implements;
|
||||
using DressAtelierDatabaseImplementation.Implements;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NLog.Extensions.Logging;
|
||||
|
@ -25,6 +25,10 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.3">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
|
||||
<PackageReference Include="NLog.Extensions.Logging" Version="5.2.1" />
|
||||
@ -33,6 +37,7 @@
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\DressAtelierBusinessLogic\DressAtelierBusinessLogic.csproj" />
|
||||
<ProjectReference Include="..\DressAtelierContracts\DressAtelierContracts.csproj" />
|
||||
<ProjectReference Include="..\DressAtelierDatabaseImplement\DressAtelierDatabaseImplement.csproj" />
|
||||
<ProjectReference Include="..\DressAtelierDataModels\DressAtelierDataModels.csproj" />
|
||||
<ProjectReference Include="..\DressAtelierListImplement\DressAtelierListImplement.csproj" />
|
||||
<ProjectReference Include="..\DressAtelierFileImplement\DressAtelierFileImplement.csproj" />
|
||||
|
@ -13,12 +13,14 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DressAtelierBusinessLogic",
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DressAtelierListImplement", "..\DressAtelierListImplement\DressAtelierListImplement.csproj", "{1A88E277-E50A-45C5-89FA-677EBCD2EF63}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DressAtelierFileImplement", "..\DressAtelierFileImplement\DressAtelierFileImplement.csproj", "{C60D17B6-F4E1-4D5F-95EE-FCDB293E1BA5}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DressAtelierFileImplement", "..\DressAtelierFileImplement\DressAtelierFileImplement.csproj", "{C60D17B6-F4E1-4D5F-95EE-FCDB293E1BA5}"
|
||||
ProjectSection(ProjectDependencies) = postProject
|
||||
{2D48D801-8A00-4FFE-B995-8090A65D0A07} = {2D48D801-8A00-4FFE-B995-8090A65D0A07}
|
||||
{5500343A-5929-4C91-8509-D0E5F65D19BF} = {5500343A-5929-4C91-8509-D0E5F65D19BF}
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DressAtelierDatabaseImplement", "..\DressAtelierDatabaseImplement\DressAtelierDatabaseImplement.csproj", "{0C07823E-158E-4E02-9784-FB9705E28EE4}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@ -49,6 +51,10 @@ Global
|
||||
{C60D17B6-F4E1-4D5F-95EE-FCDB293E1BA5}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{C60D17B6-F4E1-4D5F-95EE-FCDB293E1BA5}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{C60D17B6-F4E1-4D5F-95EE-FCDB293E1BA5}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{0C07823E-158E-4E02-9784-FB9705E28EE4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{0C07823E-158E-4E02-9784-FB9705E28EE4}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{0C07823E-158E-4E02-9784-FB9705E28EE4}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{0C07823E-158E-4E02-9784-FB9705E28EE4}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
Loading…
x
Reference in New Issue
Block a user
Название изделия не заполняется