Lab3 + новый sln

This commit is contained in:
Леонид Малафеев 2024-03-23 18:19:44 +04:00
parent cd38b2626b
commit c67ee5f392
22 changed files with 1139 additions and 1 deletions

View File

@ -6,4 +6,11 @@
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.27">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project> </Project>

View File

@ -17,6 +17,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JewerlyStoreView", "..\Jewe
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JewerlyStoreFileImplement", "..\JewerlyStoreFileImplement\JewerlyStoreFileImplement.csproj", "{CBCAFAE3-4CF6-43BB-A053-97FA6FD7A90B}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JewerlyStoreFileImplement", "..\JewerlyStoreFileImplement\JewerlyStoreFileImplement.csproj", "{CBCAFAE3-4CF6-43BB-A053-97FA6FD7A90B}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JewelryStoreDatabaseImplement", "..\JewelryStoreDatabaseImplement\JewelryStoreDatabaseImplement.csproj", "{B60549DE-BD6D-412E-8318-30220C43B061}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -51,6 +53,10 @@ Global
{CBCAFAE3-4CF6-43BB-A053-97FA6FD7A90B}.Debug|Any CPU.Build.0 = Debug|Any CPU {CBCAFAE3-4CF6-43BB-A053-97FA6FD7A90B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CBCAFAE3-4CF6-43BB-A053-97FA6FD7A90B}.Release|Any CPU.ActiveCfg = Release|Any CPU {CBCAFAE3-4CF6-43BB-A053-97FA6FD7A90B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CBCAFAE3-4CF6-43BB-A053-97FA6FD7A90B}.Release|Any CPU.Build.0 = Release|Any CPU {CBCAFAE3-4CF6-43BB-A053-97FA6FD7A90B}.Release|Any CPU.Build.0 = Release|Any CPU
{B60549DE-BD6D-412E-8318-30220C43B061}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B60549DE-BD6D-412E-8318-30220C43B061}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B60549DE-BD6D-412E-8318-30220C43B061}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B60549DE-BD6D-412E-8318-30220C43B061}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

View File

@ -7,6 +7,10 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.27">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" /> <PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
</ItemGroup> </ItemGroup>

View File

@ -6,6 +6,13 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.27">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\JewerlyStoreDataModels\JewerlyStoreDataModels.csproj" /> <ProjectReference Include="..\JewerlyStoreDataModels\JewerlyStoreDataModels.csproj" />
</ItemGroup> </ItemGroup>

View File

@ -0,0 +1,80 @@
using JewelryStoreContracts.BindingModels;
using JewelryStoreContracts.SearchModels;
using JewelryStoreContracts.StoragesContracts;
using JewelryStoreContracts.ViewModels;
using JewelryStoreDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JewelryStoreDatabaseImplement.Implements
{
public class ComponentStorage : IComponentStorage
{
public List<ComponentViewModel> GetFullList()
{
using var context = new JewelryStoreDatabase();
return context.Components.Select(x => x.GetViewModel).ToList();
}
public List<ComponentViewModel> GetFilteredList(ComponentSearchModel model)
{
if (string.IsNullOrEmpty(model.ComponentName))
{
return new();
}
using var context = new JewelryStoreDatabase();
return context.Components.Where(x => x.ComponentName.Contains(model.ComponentName)).Select(x => x.GetViewModel).ToList();
}
public ComponentViewModel? GetElement(ComponentSearchModel model)
{
if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue)
{
return null;
}
using var context = new JewelryStoreDatabase();
return context.Components.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == model.ComponentName) ||
(model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public ComponentViewModel? Insert(ComponentBindingModel model)
{
var newComponent = Component.Create(model);
if (newComponent == null)
{
return null;
}
using var context = new JewelryStoreDatabase();
context.Components.Add(newComponent);
context.SaveChanges();
return newComponent.GetViewModel;
}
public ComponentViewModel? Update(ComponentBindingModel model)
{
using var context = new JewelryStoreDatabase();
var component = context.Components.FirstOrDefault(x => x.Id == model.Id);
if (component == null)
{
return null;
}
component.Update(model);
context.SaveChanges();
return component.GetViewModel;
}
public ComponentViewModel? Delete(ComponentBindingModel model)
{
using var context = new JewelryStoreDatabase();
var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Components.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,94 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JewelryStoreContracts.BindingModels;
using JewelryStoreContracts.SearchModels;
using JewelryStoreContracts.StoragesContracts;
using JewelryStoreContracts.ViewModels;
using JewelryStoreDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace JewelryStoreDatabaseImplement.Implements
{
public class JewelStorage : IJewelStorage
{
public List<JewelViewModel> GetFullList()
{
using var context = new JewelryStoreDatabase();
return context.Jewels.Include(x => x.Components).ThenInclude(x => x.Component).ToList().Select(x => x.GetViewModel).ToList();
}
public List<JewelViewModel> GetFilteredList(JewelSearchModel model)
{
if (string.IsNullOrEmpty(model.JewelName))
{
return new();
}
using var context = new JewelryStoreDatabase();
return context.Jewels.Include(x => x.Components).ThenInclude(x => x.Component)
.Where(x => x.JewelName.Contains(model.JewelName)).ToList().Select(x => x.GetViewModel).ToList();
}
public JewelViewModel? GetElement(JewelSearchModel model)
{
if (string.IsNullOrEmpty(model.JewelName) && !model.Id.HasValue)
{
return null;
}
using var context = new JewelryStoreDatabase();
return context.Jewels.Include(x => x.Components).ThenInclude(x => x.Component).FirstOrDefault(x => (!string.IsNullOrEmpty(model.JewelName) &&
x.JewelName == model.JewelName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public JewelViewModel? Insert(JewelBindingModel model)
{
using var context = new JewelryStoreDatabase();
var newProduct = Jewel.Create(context, model);
if (newProduct == null)
{
return null;
}
context.Jewels.Add(newProduct);
context.SaveChanges();
return newProduct.GetViewModel;
}
public JewelViewModel? Update(JewelBindingModel model)
{
using var context = new JewelryStoreDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var product = context.Jewels.FirstOrDefault(rec =>
rec.Id == model.Id);
if (product == null)
{
return null;
}
product.Update(model);
context.SaveChanges();
product.UpdateComponents(context, model);
transaction.Commit();
return product.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public JewelViewModel? Delete(JewelBindingModel model)
{
using var context = new JewelryStoreDatabase();
var element = context.Jewels
.Include(x => x.Components)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Jewels.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,94 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JewelryStoreContracts.BindingModels;
using JewelryStoreContracts.SearchModels;
using JewelryStoreContracts.StoragesContracts;
using JewelryStoreContracts.ViewModels;
using JewelryStoreDatabaseImplement.Models;
namespace JewelryStoreDatabaseImplement.Implements
{
public class OrderStorage : IOrderStorage
{
public List<OrderViewModel> GetFullList()
{
using var context = new JewelryStoreDatabase();
return context.Orders
.Select(x => AccessJewelStorage(x.GetViewModel, context))
.ToList();
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
using var context = new JewelryStoreDatabase();
return context.Orders
.Where(x => x.Id == model.Id)
.Select(x => AccessJewelStorage(x.GetViewModel, context))
.ToList();
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
using var context = new JewelryStoreDatabase();
return AccessJewelStorage(context.Orders.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel, context);
}
public OrderViewModel? Insert(OrderBindingModel model)
{
var newOrder = Order.Create(model);
if (newOrder == null)
{
return null;
}
using var context = new JewelryStoreDatabase();
context.Orders.Add(newOrder);
context.SaveChanges();
return AccessJewelStorage(newOrder.GetViewModel, context);
}
public OrderViewModel? Update(OrderBindingModel model)
{
using var context = new JewelryStoreDatabase();
var order = context.Orders.FirstOrDefault(x => x.Id == model.Id);
if (order == null)
{
return null;
}
order.Update(model);
context.SaveChanges();
return AccessJewelStorage(order.GetViewModel, context);
}
public OrderViewModel? Delete(OrderBindingModel model)
{
using var context = new JewelryStoreDatabase();
var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Orders.Remove(element);
context.SaveChanges();
return AccessJewelStorage(element.GetViewModel, context);
}
return null;
}
static OrderViewModel AccessJewelStorage(OrderViewModel model, JewelryStoreDatabase context)
{
if (model == null)
{
return null;
}
var jewel = context.Jewels.FirstOrDefault(x => x.Id == model.JewelId);
if (jewel == null) {
return null;
}
model.JewelName = jewel.JewelName;
return model;
}
}
}

View File

@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using JewelryStoreDatabaseImplement.Models;
namespace JewelryStoreDatabaseImplement
{
public class JewelryStoreDatabase : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (optionsBuilder.IsConfigured == false)
{
optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=JewelryStoreDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
}
base.OnConfiguring(optionsBuilder);
}
public virtual DbSet<Component> Components { set; get; }
public virtual DbSet<Jewel> Jewels { set; get; }
public virtual DbSet<JewelComponent> JewelComponents { set; get; }
public virtual DbSet<Order> Orders { set; get; }
}
}

View File

@ -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="6.0.27" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.27" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.27">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\JewelryStoreContracts\JewelryStoreContracts.csproj" />
<ProjectReference Include="..\JewerlyStoreDataModels\JewerlyStoreDataModels.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,170 @@
// <auto-generated />
using System;
using JewelryStoreDatabaseImplement;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace JewelryStoreDatabaseImplement.Migrations
{
[DbContext(typeof(JewelryStoreDatabase))]
[Migration("20240310110723_InitialCreate")]
partial class InitialCreate
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "6.0.27")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1);
modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Component", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<string>("ComponentName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Cost")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("Components");
});
modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Jewel", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<string>("JewelName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Price")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("Jewels");
});
modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.JewelComponent", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<int>("ComponentId")
.HasColumnType("int");
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("JewelId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ComponentId");
b.HasIndex("JewelId");
b.ToTable("JewelComponents");
});
modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Order", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<int>("Count")
.HasColumnType("int");
b.Property<DateTime>("DateCreate")
.HasColumnType("datetime2");
b.Property<DateTime?>("DateImplement")
.HasColumnType("datetime2");
b.Property<int>("JewelId")
.HasColumnType("int");
b.Property<int>("Status")
.HasColumnType("int");
b.Property<double>("Sum")
.HasColumnType("float");
b.HasKey("Id");
b.HasIndex("JewelId");
b.ToTable("Orders");
});
modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.JewelComponent", b =>
{
b.HasOne("JewelryStoreDatabaseImplement.Models.Component", "Component")
.WithMany("JewelComponents")
.HasForeignKey("ComponentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("JewelryStoreDatabaseImplement.Models.Jewel", "Jewel")
.WithMany("Components")
.HasForeignKey("JewelId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Component");
b.Navigation("Jewel");
});
modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Order", b =>
{
b.HasOne("JewelryStoreDatabaseImplement.Models.Jewel", "Jewel")
.WithMany("Orders")
.HasForeignKey("JewelId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Jewel");
});
modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Component", b =>
{
b.Navigation("JewelComponents");
});
modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Jewel", b =>
{
b.Navigation("Components");
b.Navigation("Orders");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,122 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace JewelryStoreDatabaseImplement.Migrations
{
public partial class InitialCreate : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Components",
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_Components", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Jewels",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
JewelName = table.Column<string>(type: "nvarchar(max)", nullable: false),
Price = table.Column<double>(type: "float", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Jewels", x => x.Id);
});
migrationBuilder.CreateTable(
name: "JewelComponents",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
JewelId = table.Column<int>(type: "int", nullable: false),
ComponentId = table.Column<int>(type: "int", nullable: false),
Count = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_JewelComponents", x => x.Id);
table.ForeignKey(
name: "FK_JewelComponents_Components_ComponentId",
column: x => x.ComponentId,
principalTable: "Components",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_JewelComponents_Jewels_JewelId",
column: x => x.JewelId,
principalTable: "Jewels",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "Orders",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
JewelId = 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_Jewels_JewelId",
column: x => x.JewelId,
principalTable: "Jewels",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_JewelComponents_ComponentId",
table: "JewelComponents",
column: "ComponentId");
migrationBuilder.CreateIndex(
name: "IX_JewelComponents_JewelId",
table: "JewelComponents",
column: "JewelId");
migrationBuilder.CreateIndex(
name: "IX_Orders_JewelId",
table: "Orders",
column: "JewelId");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "JewelComponents");
migrationBuilder.DropTable(
name: "Orders");
migrationBuilder.DropTable(
name: "Components");
migrationBuilder.DropTable(
name: "Jewels");
}
}
}

View File

@ -0,0 +1,168 @@
// <auto-generated />
using System;
using JewelryStoreDatabaseImplement;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace JewelryStoreDatabaseImplement.Migrations
{
[DbContext(typeof(JewelryStoreDatabase))]
partial class JewelryStoreDatabaseModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "6.0.27")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1);
modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Component", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<string>("ComponentName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Cost")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("Components");
});
modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Jewel", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<string>("JewelName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Price")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("Jewels");
});
modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.JewelComponent", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<int>("ComponentId")
.HasColumnType("int");
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("JewelId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ComponentId");
b.HasIndex("JewelId");
b.ToTable("JewelComponents");
});
modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Order", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<int>("Count")
.HasColumnType("int");
b.Property<DateTime>("DateCreate")
.HasColumnType("datetime2");
b.Property<DateTime?>("DateImplement")
.HasColumnType("datetime2");
b.Property<int>("JewelId")
.HasColumnType("int");
b.Property<int>("Status")
.HasColumnType("int");
b.Property<double>("Sum")
.HasColumnType("float");
b.HasKey("Id");
b.HasIndex("JewelId");
b.ToTable("Orders");
});
modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.JewelComponent", b =>
{
b.HasOne("JewelryStoreDatabaseImplement.Models.Component", "Component")
.WithMany("JewelComponents")
.HasForeignKey("ComponentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("JewelryStoreDatabaseImplement.Models.Jewel", "Jewel")
.WithMany("Components")
.HasForeignKey("JewelId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Component");
b.Navigation("Jewel");
});
modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Order", b =>
{
b.HasOne("JewelryStoreDatabaseImplement.Models.Jewel", "Jewel")
.WithMany("Orders")
.HasForeignKey("JewelId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Jewel");
});
modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Component", b =>
{
b.Navigation("JewelComponents");
});
modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Jewel", b =>
{
b.Navigation("Components");
b.Navigation("Orders");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,61 @@
using JewelryStoreContracts.BindingModels;
using JewelryStoreContracts.ViewModels;
using JewerlyStoreDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JewelryStoreDatabaseImplement.Models
{
public class Component : IComponentModel
{
public int Id { get; private set; }
[Required]
public string ComponentName { get; private set; } = string.Empty;
[Required]
public double Cost { get; set; }
[ForeignKey("ComponentId")]
public virtual List<JewelComponent> JewelComponents { get; set; } = new();
public static Component? Create(ComponentBindingModel model)
{
if (model == null)
{
return null;
}
return new Component()
{
Id = model.Id,
ComponentName = model.ComponentName,
Cost = model.Cost
};
}
public static Component Create(ComponentViewModel model)
{
return new Component
{
Id = model.Id,
ComponentName = model.ComponentName,
Cost = model.Cost
};
}
public void Update(ComponentBindingModel model)
{
if (model == null)
{
return;
}
ComponentName = model.ComponentName;
Cost = model.Cost;
}
public ComponentViewModel GetViewModel => new()
{
Id = Id,
ComponentName = ComponentName,
Cost = Cost
};
}
}

View File

@ -0,0 +1,97 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using JewelryStoreContracts.BindingModels;
using JewelryStoreContracts.ViewModels;
using JewerlyStoreDataModels.Models;
namespace JewelryStoreDatabaseImplement.Models
{
public class Jewel : IJewelModel
{
public int Id { get; set; }
[Required]
public string JewelName { get; set; } = string.Empty;
[Required]
public double Price { get; set; }
private Dictionary<int, (IComponentModel, int)>? _jewelComponents = null;
[NotMapped]
public Dictionary<int, (IComponentModel, int)> JewelComponents
{
get
{
if (_jewelComponents == null)
{
_jewelComponents = Components.ToDictionary(recJC => recJC.ComponentId, recJC => (recJC.Component as IComponentModel, recJC.Count));
}
return _jewelComponents;
}
}
[ForeignKey("JewelId")]
public virtual List<JewelComponent> Components { get; set; } = new();
[ForeignKey("JewelId")]
public virtual List<Order> Orders { get; set; } = new();
public static Jewel Create(JewelryStoreDatabase context, JewelBindingModel model)
{
return new Jewel()
{
Id = model.Id,
JewelName = model.JewelName,
Price = model.Price,
Components = model.JewelComponents.Select(x => new JewelComponent
{
Component = context.Components.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(JewelBindingModel model)
{
JewelName = model.JewelName;
Price = model.Price;
}
public JewelViewModel GetViewModel => new()
{
Id = Id,
JewelName = JewelName,
Price = Price,
JewelComponents = JewelComponents
};
public void UpdateComponents(JewelryStoreDatabase context, JewelBindingModel model)
{
var jewelComponents = context.JewelComponents.Where(rec => rec.JewelId == model.Id).ToList();
if (jewelComponents != null && jewelComponents.Count > 0)
{ // удалили те, которых нет в модели
context.JewelComponents.RemoveRange(jewelComponents.Where(rec => !model.JewelComponents.ContainsKey(rec.ComponentId)));
context.SaveChanges();
// обновили количество у существующих записей
foreach (var updateComponent in jewelComponents)
{
updateComponent.Count = model.JewelComponents[updateComponent.ComponentId].Item2;
model.JewelComponents.Remove(updateComponent.ComponentId);
}
context.SaveChanges();
}
var jewel = context.Jewels.First(x => x.Id == Id);
foreach (var jc in model.JewelComponents)
{
context.JewelComponents.Add(new JewelComponent
{
Jewel = jewel,
Component = context.Components.First(x => x.Id == jc.Key),
Count = jc.Value.Item2
});
context.SaveChanges();
}
_jewelComponents = null;
}
}
}

View File

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
namespace JewelryStoreDatabaseImplement.Models
{
public class JewelComponent
{
public int Id { get; set; }
[Required]
public int JewelId { get; set; }
[Required]
public int ComponentId { get; set; }
[Required]
public int Count { get; set; }
public virtual Component Component { get; set; } = new();
public virtual Jewel Jewel { get; set; } = new();
}
}

View File

@ -0,0 +1,68 @@
using JewelryStoreContracts.BindingModels;
using JewelryStoreContracts.ViewModels;
using JewerlyStoreDataModels.Enums;
using JewerlyStoreDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JewelryStoreDatabaseImplement.Models
{
public class Order : IOrderModel
{
public int Id { get; private set; }
[Required]
public int JewelId { get; set; }
[Required]
public int Count { get; set; }
[Required]
public double Sum { get; set; }
[Required]
public OrderStatus Status { get; set; }
[Required]
public DateTime DateCreate { get; set; }
public DateTime? DateImplement { get; set; }
public Jewel Jewel { get; set; }
public static Order? Create(OrderBindingModel? model)
{
if (model == null)
{
return null;
}
return new Order()
{
Id = model.Id,
JewelId = model.JewelId,
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;
if (model.DateImplement.HasValue) DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel => new()
{
Id = Id,
JewelId = JewelId,
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement
};
}
}

View File

@ -6,6 +6,13 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.27">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\JewelryStoreContracts\JewelryStoreContracts.csproj" /> <ProjectReference Include="..\JewelryStoreContracts\JewelryStoreContracts.csproj" />
<ProjectReference Include="..\JewerlyStoreDataModels\JewerlyStoreDataModels.csproj" /> <ProjectReference Include="..\JewerlyStoreDataModels\JewerlyStoreDataModels.csproj" />

View File

@ -6,4 +6,11 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.27">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project> </Project>

View File

@ -6,6 +6,13 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.27">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\JewelryStoreContracts\JewelryStoreContracts.csproj" /> <ProjectReference Include="..\JewelryStoreContracts\JewelryStoreContracts.csproj" />
<ProjectReference Include="..\JewerlyStoreDataModels\JewerlyStoreDataModels.csproj" /> <ProjectReference Include="..\JewerlyStoreDataModels\JewerlyStoreDataModels.csproj" />

View File

@ -9,6 +9,15 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.27">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.27" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.27">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" /> <PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.8" /> <PackageReference Include="NLog.Extensions.Logging" Version="5.3.8" />
</ItemGroup> </ItemGroup>
@ -16,6 +25,7 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\JewelryStoreBusinessLogic\JewelryStoreBusinessLogic.csproj" /> <ProjectReference Include="..\JewelryStoreBusinessLogic\JewelryStoreBusinessLogic.csproj" />
<ProjectReference Include="..\JewelryStoreContracts\JewelryStoreContracts.csproj" /> <ProjectReference Include="..\JewelryStoreContracts\JewelryStoreContracts.csproj" />
<ProjectReference Include="..\JewelryStoreDatabaseImplement\JewelryStoreDatabaseImplement.csproj" />
<ProjectReference Include="..\JewelryStoreListImplement\JewelryStoreListImplement.csproj" /> <ProjectReference Include="..\JewelryStoreListImplement\JewelryStoreListImplement.csproj" />
<ProjectReference Include="..\JewerlyStoreDataModels\JewerlyStoreDataModels.csproj" /> <ProjectReference Include="..\JewerlyStoreDataModels\JewerlyStoreDataModels.csproj" />
<ProjectReference Include="..\JewerlyStoreFileImplement\JewerlyStoreFileImplement.csproj" /> <ProjectReference Include="..\JewerlyStoreFileImplement\JewerlyStoreFileImplement.csproj" />

View File

@ -0,0 +1,58 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JewelryStoreBusinessLogic", "..\JewelryStoreBusinessLogic\JewelryStoreBusinessLogic.csproj", "{25D087CD-29D6-42C3-AB7D-0CF881DA7DAC}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JewelryStoreContracts", "..\JewelryStoreContracts\JewelryStoreContracts.csproj", "{F057F327-02A5-4407-996A-61BEA106E6BD}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JewelryStoreDatabaseImplement", "..\JewelryStoreDatabaseImplement\JewelryStoreDatabaseImplement.csproj", "{07B59EAD-064C-4CC2-8300-526EEAD7DF40}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JewelryStoreListImplement", "..\JewelryStoreListImplement\JewelryStoreListImplement.csproj", "{8EE8B191-F9D3-4F61-B1BF-0F1200B26766}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JewerlyStoreDataModels", "..\JewerlyStoreDataModels\JewerlyStoreDataModels.csproj", "{F8D0CC7E-1525-4837-B328-9FA606D6FF2D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JewerlyStoreFileImplement", "..\JewerlyStoreFileImplement\JewerlyStoreFileImplement.csproj", "{AAFF546C-1A7B-435E-88E1-9EFFCB4DA7A1}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JewerlyStoreView", "JewerlyStoreView.csproj", "{2A784D25-F667-471A-A320-0EF04F57953B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{25D087CD-29D6-42C3-AB7D-0CF881DA7DAC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{25D087CD-29D6-42C3-AB7D-0CF881DA7DAC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{25D087CD-29D6-42C3-AB7D-0CF881DA7DAC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{25D087CD-29D6-42C3-AB7D-0CF881DA7DAC}.Release|Any CPU.Build.0 = Release|Any CPU
{F057F327-02A5-4407-996A-61BEA106E6BD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F057F327-02A5-4407-996A-61BEA106E6BD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F057F327-02A5-4407-996A-61BEA106E6BD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F057F327-02A5-4407-996A-61BEA106E6BD}.Release|Any CPU.Build.0 = Release|Any CPU
{07B59EAD-064C-4CC2-8300-526EEAD7DF40}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{07B59EAD-064C-4CC2-8300-526EEAD7DF40}.Debug|Any CPU.Build.0 = Debug|Any CPU
{07B59EAD-064C-4CC2-8300-526EEAD7DF40}.Release|Any CPU.ActiveCfg = Release|Any CPU
{07B59EAD-064C-4CC2-8300-526EEAD7DF40}.Release|Any CPU.Build.0 = Release|Any CPU
{8EE8B191-F9D3-4F61-B1BF-0F1200B26766}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8EE8B191-F9D3-4F61-B1BF-0F1200B26766}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8EE8B191-F9D3-4F61-B1BF-0F1200B26766}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8EE8B191-F9D3-4F61-B1BF-0F1200B26766}.Release|Any CPU.Build.0 = Release|Any CPU
{F8D0CC7E-1525-4837-B328-9FA606D6FF2D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F8D0CC7E-1525-4837-B328-9FA606D6FF2D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F8D0CC7E-1525-4837-B328-9FA606D6FF2D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F8D0CC7E-1525-4837-B328-9FA606D6FF2D}.Release|Any CPU.Build.0 = Release|Any CPU
{AAFF546C-1A7B-435E-88E1-9EFFCB4DA7A1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AAFF546C-1A7B-435E-88E1-9EFFCB4DA7A1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AAFF546C-1A7B-435E-88E1-9EFFCB4DA7A1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AAFF546C-1A7B-435E-88E1-9EFFCB4DA7A1}.Release|Any CPU.Build.0 = Release|Any CPU
{2A784D25-F667-471A-A320-0EF04F57953B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2A784D25-F667-471A-A320-0EF04F57953B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2A784D25-F667-471A-A320-0EF04F57953B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2A784D25-F667-471A-A320-0EF04F57953B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal

View File

@ -1,6 +1,6 @@
using JewelryStoreContracts.BusinessLogicsContracts; using JewelryStoreContracts.BusinessLogicsContracts;
using JewelryStoreContracts.StoragesContracts; using JewelryStoreContracts.StoragesContracts;
using JewerlyStoreFileImplement.Implements; using JewelryStoreDatabaseImplement.Implements;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging; using NLog.Extensions.Logging;