осталось сделать Implemtnts
This commit is contained in:
parent
3e0e0f4f63
commit
ea077a3fa4
@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputersShopListImplement"
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputersShopFileImplement", "ComputersShopFileImplement\ComputersShopFileImplement.csproj", "{E40EB0BF-429F-4D39-BD09-E7E2F28F9086}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputersShopFileImplement", "ComputersShopFileImplement\ComputersShopFileImplement.csproj", "{E40EB0BF-429F-4D39-BD09-E7E2F28F9086}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputersShopDatabaseImplement", "ComputersShopDatabaseImplement\ComputersShopDatabaseImplement.csproj", "{3CAD2BDD-9EB7-4215-A8CD-32D0455E7859}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
@ -45,6 +47,10 @@ Global
|
|||||||
{E40EB0BF-429F-4D39-BD09-E7E2F28F9086}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{E40EB0BF-429F-4D39-BD09-E7E2F28F9086}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{E40EB0BF-429F-4D39-BD09-E7E2F28F9086}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{E40EB0BF-429F-4D39-BD09-E7E2F28F9086}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{E40EB0BF-429F-4D39-BD09-E7E2F28F9086}.Release|Any CPU.Build.0 = Release|Any CPU
|
{E40EB0BF-429F-4D39-BD09-E7E2F28F9086}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{3CAD2BDD-9EB7-4215-A8CD-32D0455E7859}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{3CAD2BDD-9EB7-4215-A8CD-32D0455E7859}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{3CAD2BDD-9EB7-4215-A8CD-32D0455E7859}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{3CAD2BDD-9EB7-4215-A8CD-32D0455E7859}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
@ -19,6 +19,10 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.4">
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
</PackageReference>
|
||||||
<PackageReference Include="NLog" Version="5.1.2" />
|
<PackageReference Include="NLog" Version="5.1.2" />
|
||||||
<PackageReference Include="NLog.Extensions.Logging" Version="5.2.2" />
|
<PackageReference Include="NLog.Extensions.Logging" Version="5.2.2" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
@ -26,6 +30,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\ComputersShopBusinessLogic\ComputersShopBusinessLogic.csproj" />
|
<ProjectReference Include="..\ComputersShopBusinessLogic\ComputersShopBusinessLogic.csproj" />
|
||||||
<ProjectReference Include="..\ComputersShopContracts\ComputersShopContracts.csproj" />
|
<ProjectReference Include="..\ComputersShopContracts\ComputersShopContracts.csproj" />
|
||||||
|
<ProjectReference Include="..\ComputersShopDatabaseImplement\ComputersShopDatabaseImplement.csproj" />
|
||||||
<ProjectReference Include="..\ComputersShopFileImplement\ComputersShopFileImplement.csproj" />
|
<ProjectReference Include="..\ComputersShopFileImplement\ComputersShopFileImplement.csproj" />
|
||||||
<ProjectReference Include="..\ComputersShopImplement\ComputersShopListImplement.csproj" />
|
<ProjectReference Include="..\ComputersShopImplement\ComputersShopListImplement.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
@ -0,0 +1,29 @@
|
|||||||
|
using ComputersShopDatabaseImplement.Models;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace ComputersShopDatabaseImplement
|
||||||
|
{
|
||||||
|
public class ComputersShopDatabase : DbContext
|
||||||
|
{
|
||||||
|
protected override void OnConfiguring(DbContextOptionsBuilder
|
||||||
|
optionsBuilder)
|
||||||
|
{
|
||||||
|
if (optionsBuilder.IsConfigured == false)
|
||||||
|
{
|
||||||
|
optionsBuilder.UseSqlServer
|
||||||
|
(
|
||||||
|
@"Data Source=DESKTOP-SJOCT17;
|
||||||
|
Initial Catalog=ComputersShopDatabase;
|
||||||
|
Integrated Security=True;
|
||||||
|
MultipleActiveResultSets=True;
|
||||||
|
TrustServerCertificate=True"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
base.OnConfiguring(optionsBuilder);
|
||||||
|
}
|
||||||
|
public virtual DbSet<Component> Components { set; get; }
|
||||||
|
public virtual DbSet<Computer> Computers { set; get; }
|
||||||
|
public virtual DbSet<ComputerComponent> ComputerComponents { set; get; }
|
||||||
|
public virtual DbSet<Order> Orders { set; get; }
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
<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.4" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.4">
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
</PackageReference>
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.4" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.4">
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
</PackageReference>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\ComputersShopBusinessLogic\ComputersShopBusinessLogic.csproj" />
|
||||||
|
<ProjectReference Include="..\ComputersShopContracts\ComputersShopContracts.csproj" />
|
||||||
|
<ProjectReference Include="..\ComputersShopDataModels\ComputersShopDataModels.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
@ -0,0 +1,85 @@
|
|||||||
|
using ComputersShopContracts.BindingModels;
|
||||||
|
using ComputersShopContracts.SearchModels;
|
||||||
|
using ComputersShopContracts.StoragesContracts;
|
||||||
|
using ComputersShopContracts.ViewModels;
|
||||||
|
using ComputersShopDatabaseImplement.Models;
|
||||||
|
|
||||||
|
namespace ComputersShopDatabaseImplement.Implements
|
||||||
|
{
|
||||||
|
internal class ComponentStorage : IComponentStorage
|
||||||
|
{
|
||||||
|
public List<ComponentViewModel> GetFullList()
|
||||||
|
{
|
||||||
|
using var context = new ComputersShopDatabase();
|
||||||
|
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 ComputersShopDatabase();
|
||||||
|
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 ComputersShopDatabase();
|
||||||
|
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 ComputersShopDatabase();
|
||||||
|
context.Components.Add(newComponent);
|
||||||
|
context.SaveChanges();
|
||||||
|
return newComponent.GetViewModel;
|
||||||
|
}
|
||||||
|
public ComponentViewModel? Update(ComponentBindingModel model)
|
||||||
|
{
|
||||||
|
using var context = new ComputersShopDatabase();
|
||||||
|
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 ComputersShopDatabase();
|
||||||
|
var element = context.Components.FirstOrDefault(rec => rec.Id ==
|
||||||
|
model.Id);
|
||||||
|
if (element != null)
|
||||||
|
{
|
||||||
|
context.Components.Remove(element);
|
||||||
|
context.SaveChanges();
|
||||||
|
return element.GetViewModel;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,12 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace ComputersShopDatabaseImplement.Implements
|
||||||
|
{
|
||||||
|
internal class ComputerStorage
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace ComputersShopDatabaseImplement.Implements
|
||||||
|
{
|
||||||
|
internal class OrderStorage
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
169
ComputersShop/ComputersShopDatabaseImplement/Migrations/20230411104535_init-migration.Designer.cs
generated
Normal file
169
ComputersShop/ComputersShopDatabaseImplement/Migrations/20230411104535_init-migration.Designer.cs
generated
Normal file
@ -0,0 +1,169 @@
|
|||||||
|
// <auto-generated />
|
||||||
|
using System;
|
||||||
|
using ComputersShopDatabaseImplement;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||||
|
using Microsoft.EntityFrameworkCore.Metadata;
|
||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace ComputersShopDatabaseImplement.Migrations
|
||||||
|
{
|
||||||
|
[DbContext(typeof(ComputersShopDatabase))]
|
||||||
|
[Migration("20230411104535_init-migration")]
|
||||||
|
partial class initmigration
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||||
|
{
|
||||||
|
#pragma warning disable 612, 618
|
||||||
|
modelBuilder
|
||||||
|
.HasAnnotation("ProductVersion", "7.0.4")
|
||||||
|
.HasAnnotation("Relational:MaxIdentifierLength", 128);
|
||||||
|
|
||||||
|
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
|
||||||
|
|
||||||
|
modelBuilder.Entity("ComputersShopDatabaseImplement.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("ComputersShopDatabaseImplement.Models.Computer", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||||
|
|
||||||
|
b.Property<string>("ComputerName")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.Property<double>("Price")
|
||||||
|
.HasColumnType("float");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.ToTable("Computers");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("ComputersShopDatabaseImplement.Models.ComputerComponent", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||||
|
|
||||||
|
b.Property<int>("ComponentId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<int>("ComputerId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<int>("Count")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex("ComponentId");
|
||||||
|
|
||||||
|
b.HasIndex("ComputerId");
|
||||||
|
|
||||||
|
b.ToTable("ComputerComponents");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("ComputersShopDatabaseImplement.Models.Order", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||||
|
|
||||||
|
b.Property<int>("ComputerId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<int>("Count")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<DateTime>("DateCreate")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<DateTime?>("DateImplement")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int>("Status")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<double>("Sum")
|
||||||
|
.HasColumnType("float");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex("ComputerId");
|
||||||
|
|
||||||
|
b.ToTable("Orders");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("ComputersShopDatabaseImplement.Models.ComputerComponent", b =>
|
||||||
|
{
|
||||||
|
b.HasOne("ComputersShopDatabaseImplement.Models.Component", "Component")
|
||||||
|
.WithMany("ComputerComponents")
|
||||||
|
.HasForeignKey("ComponentId")
|
||||||
|
.OnDelete(DeleteBehavior.Cascade)
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
|
b.HasOne("ComputersShopDatabaseImplement.Models.Computer", "Computer")
|
||||||
|
.WithMany("Components")
|
||||||
|
.HasForeignKey("ComputerId")
|
||||||
|
.OnDelete(DeleteBehavior.Cascade)
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
|
b.Navigation("Component");
|
||||||
|
|
||||||
|
b.Navigation("Computer");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("ComputersShopDatabaseImplement.Models.Order", b =>
|
||||||
|
{
|
||||||
|
b.HasOne("ComputersShopDatabaseImplement.Models.Computer", null)
|
||||||
|
.WithMany("Orders")
|
||||||
|
.HasForeignKey("ComputerId")
|
||||||
|
.OnDelete(DeleteBehavior.Cascade)
|
||||||
|
.IsRequired();
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("ComputersShopDatabaseImplement.Models.Component", b =>
|
||||||
|
{
|
||||||
|
b.Navigation("ComputerComponents");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("ComputersShopDatabaseImplement.Models.Computer", b =>
|
||||||
|
{
|
||||||
|
b.Navigation("Components");
|
||||||
|
|
||||||
|
b.Navigation("Orders");
|
||||||
|
});
|
||||||
|
#pragma warning restore 612, 618
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,125 @@
|
|||||||
|
using System;
|
||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace ComputersShopDatabaseImplement.Migrations
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
public partial class initmigration : Migration
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
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: "Computers",
|
||||||
|
columns: table => new
|
||||||
|
{
|
||||||
|
Id = table.Column<int>(type: "int", nullable: false)
|
||||||
|
.Annotation("SqlServer:Identity", "1, 1"),
|
||||||
|
ComputerName = table.Column<string>(type: "nvarchar(max)", nullable: false),
|
||||||
|
Price = table.Column<double>(type: "float", nullable: false)
|
||||||
|
},
|
||||||
|
constraints: table =>
|
||||||
|
{
|
||||||
|
table.PrimaryKey("PK_Computers", x => x.Id);
|
||||||
|
});
|
||||||
|
|
||||||
|
migrationBuilder.CreateTable(
|
||||||
|
name: "ComputerComponents",
|
||||||
|
columns: table => new
|
||||||
|
{
|
||||||
|
Id = table.Column<int>(type: "int", nullable: false)
|
||||||
|
.Annotation("SqlServer:Identity", "1, 1"),
|
||||||
|
ComputerId = 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_ComputerComponents", x => x.Id);
|
||||||
|
table.ForeignKey(
|
||||||
|
name: "FK_ComputerComponents_Components_ComponentId",
|
||||||
|
column: x => x.ComponentId,
|
||||||
|
principalTable: "Components",
|
||||||
|
principalColumn: "Id",
|
||||||
|
onDelete: ReferentialAction.Cascade);
|
||||||
|
table.ForeignKey(
|
||||||
|
name: "FK_ComputerComponents_Computers_ComputerId",
|
||||||
|
column: x => x.ComputerId,
|
||||||
|
principalTable: "Computers",
|
||||||
|
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"),
|
||||||
|
ComputerId = 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_Computers_ComputerId",
|
||||||
|
column: x => x.ComputerId,
|
||||||
|
principalTable: "Computers",
|
||||||
|
principalColumn: "Id",
|
||||||
|
onDelete: ReferentialAction.Cascade);
|
||||||
|
});
|
||||||
|
|
||||||
|
migrationBuilder.CreateIndex(
|
||||||
|
name: "IX_ComputerComponents_ComponentId",
|
||||||
|
table: "ComputerComponents",
|
||||||
|
column: "ComponentId");
|
||||||
|
|
||||||
|
migrationBuilder.CreateIndex(
|
||||||
|
name: "IX_ComputerComponents_ComputerId",
|
||||||
|
table: "ComputerComponents",
|
||||||
|
column: "ComputerId");
|
||||||
|
|
||||||
|
migrationBuilder.CreateIndex(
|
||||||
|
name: "IX_Orders_ComputerId",
|
||||||
|
table: "Orders",
|
||||||
|
column: "ComputerId");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Down(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.DropTable(
|
||||||
|
name: "ComputerComponents");
|
||||||
|
|
||||||
|
migrationBuilder.DropTable(
|
||||||
|
name: "Orders");
|
||||||
|
|
||||||
|
migrationBuilder.DropTable(
|
||||||
|
name: "Components");
|
||||||
|
|
||||||
|
migrationBuilder.DropTable(
|
||||||
|
name: "Computers");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,166 @@
|
|||||||
|
// <auto-generated />
|
||||||
|
using System;
|
||||||
|
using ComputersShopDatabaseImplement;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||||
|
using Microsoft.EntityFrameworkCore.Metadata;
|
||||||
|
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace ComputersShopDatabaseImplement.Migrations
|
||||||
|
{
|
||||||
|
[DbContext(typeof(ComputersShopDatabase))]
|
||||||
|
partial class ComputersShopDatabaseModelSnapshot : ModelSnapshot
|
||||||
|
{
|
||||||
|
protected override void BuildModel(ModelBuilder modelBuilder)
|
||||||
|
{
|
||||||
|
#pragma warning disable 612, 618
|
||||||
|
modelBuilder
|
||||||
|
.HasAnnotation("ProductVersion", "7.0.4")
|
||||||
|
.HasAnnotation("Relational:MaxIdentifierLength", 128);
|
||||||
|
|
||||||
|
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
|
||||||
|
|
||||||
|
modelBuilder.Entity("ComputersShopDatabaseImplement.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("ComputersShopDatabaseImplement.Models.Computer", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||||
|
|
||||||
|
b.Property<string>("ComputerName")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.Property<double>("Price")
|
||||||
|
.HasColumnType("float");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.ToTable("Computers");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("ComputersShopDatabaseImplement.Models.ComputerComponent", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||||
|
|
||||||
|
b.Property<int>("ComponentId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<int>("ComputerId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<int>("Count")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex("ComponentId");
|
||||||
|
|
||||||
|
b.HasIndex("ComputerId");
|
||||||
|
|
||||||
|
b.ToTable("ComputerComponents");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("ComputersShopDatabaseImplement.Models.Order", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||||
|
|
||||||
|
b.Property<int>("ComputerId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<int>("Count")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<DateTime>("DateCreate")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<DateTime?>("DateImplement")
|
||||||
|
.HasColumnType("datetime2");
|
||||||
|
|
||||||
|
b.Property<int>("Status")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<double>("Sum")
|
||||||
|
.HasColumnType("float");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex("ComputerId");
|
||||||
|
|
||||||
|
b.ToTable("Orders");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("ComputersShopDatabaseImplement.Models.ComputerComponent", b =>
|
||||||
|
{
|
||||||
|
b.HasOne("ComputersShopDatabaseImplement.Models.Component", "Component")
|
||||||
|
.WithMany("ComputerComponents")
|
||||||
|
.HasForeignKey("ComponentId")
|
||||||
|
.OnDelete(DeleteBehavior.Cascade)
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
|
b.HasOne("ComputersShopDatabaseImplement.Models.Computer", "Computer")
|
||||||
|
.WithMany("Components")
|
||||||
|
.HasForeignKey("ComputerId")
|
||||||
|
.OnDelete(DeleteBehavior.Cascade)
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
|
b.Navigation("Component");
|
||||||
|
|
||||||
|
b.Navigation("Computer");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("ComputersShopDatabaseImplement.Models.Order", b =>
|
||||||
|
{
|
||||||
|
b.HasOne("ComputersShopDatabaseImplement.Models.Computer", null)
|
||||||
|
.WithMany("Orders")
|
||||||
|
.HasForeignKey("ComputerId")
|
||||||
|
.OnDelete(DeleteBehavior.Cascade)
|
||||||
|
.IsRequired();
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("ComputersShopDatabaseImplement.Models.Component", b =>
|
||||||
|
{
|
||||||
|
b.Navigation("ComputerComponents");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("ComputersShopDatabaseImplement.Models.Computer", b =>
|
||||||
|
{
|
||||||
|
b.Navigation("Components");
|
||||||
|
|
||||||
|
b.Navigation("Orders");
|
||||||
|
});
|
||||||
|
#pragma warning restore 612, 618
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,56 @@
|
|||||||
|
using System.ComponentModel.DataAnnotations.Schema;
|
||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using ComputersShopContracts.BindingModels;
|
||||||
|
using ComputersShopContracts.ViewModels;
|
||||||
|
using ComputersShopDataModels.Models;
|
||||||
|
|
||||||
|
namespace ComputersShopDatabaseImplement.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<ComputerComponent> ComputerComponents { 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
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,90 @@
|
|||||||
|
using ComputersShopContracts.BindingModels;
|
||||||
|
using ComputersShopContracts.ViewModels;
|
||||||
|
using ComputersShopDataModels.Models;
|
||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using System.ComponentModel.DataAnnotations.Schema;
|
||||||
|
|
||||||
|
namespace ComputersShopDatabaseImplement.Models
|
||||||
|
{
|
||||||
|
public class Computer : IComputerModel
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
[Required]
|
||||||
|
public string ComputerName { get; set; } = string.Empty;
|
||||||
|
[Required]
|
||||||
|
public double Price { get; set; }
|
||||||
|
private Dictionary<int, (IComponentModel, int)>? _computerComponents = null;
|
||||||
|
[NotMapped]
|
||||||
|
public Dictionary<int, (IComponentModel, int)> ComputerComponents
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if (_computerComponents == null)
|
||||||
|
{
|
||||||
|
_computerComponents = Components
|
||||||
|
.ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count));
|
||||||
|
}
|
||||||
|
return _computerComponents;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
[ForeignKey("ComputerId")]
|
||||||
|
public virtual List<ComputerComponent> Components { get; set; } = new();
|
||||||
|
[ForeignKey("ComputerId")]
|
||||||
|
public virtual List<Order> Orders { get; set; } = new();
|
||||||
|
public static Computer Create(ComputersShopDatabase context, ComputerBindingModel model)
|
||||||
|
{
|
||||||
|
return new Computer()
|
||||||
|
{
|
||||||
|
Id = model.Id,
|
||||||
|
ComputerName = model.ComputerName,
|
||||||
|
Price = model.Price,
|
||||||
|
Components = model.ComputerComponents.Select(x => new ComputerComponent
|
||||||
|
{
|
||||||
|
Component = context.Components.First(y => y.Id == x.Key),
|
||||||
|
Count = x.Value.Item2
|
||||||
|
}).ToList()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
public void Update(ComputerBindingModel model)
|
||||||
|
{
|
||||||
|
ComputerName = model.ComputerName;
|
||||||
|
Price = model.Price;
|
||||||
|
}
|
||||||
|
public ComputerViewModel GetViewModel => new()
|
||||||
|
{
|
||||||
|
Id = Id,
|
||||||
|
ComputerName = ComputerName,
|
||||||
|
Price = Price,
|
||||||
|
ComputerComponents = ComputerComponents
|
||||||
|
};
|
||||||
|
public void UpdateComponents(ComputersShopDatabase context, ComputerBindingModel model)
|
||||||
|
{
|
||||||
|
var computerComponents = context.ComputerComponents.Where(rec => rec.ComputerId == model.Id).ToList();
|
||||||
|
if (computerComponents != null && computerComponents.Count > 0)
|
||||||
|
{ // удалили те, которых нет в модели
|
||||||
|
context.ComputerComponents.RemoveRange(computerComponents.Where(rec
|
||||||
|
=> !model.ComputerComponents.ContainsKey(rec.ComponentId)));
|
||||||
|
context.SaveChanges();
|
||||||
|
// обновили количество у существующих записей
|
||||||
|
foreach (var updateComponent in computerComponents)
|
||||||
|
{
|
||||||
|
updateComponent.Count = model.ComputerComponents[updateComponent.ComponentId].Item2;
|
||||||
|
model.ComputerComponents.Remove(updateComponent.ComponentId);
|
||||||
|
}
|
||||||
|
context.SaveChanges();
|
||||||
|
}
|
||||||
|
var computer = context.Computers.First(x => x.Id == Id);
|
||||||
|
foreach (var pc in model.ComputerComponents)
|
||||||
|
{
|
||||||
|
context.ComputerComponents.Add(new ComputerComponent
|
||||||
|
{
|
||||||
|
Computer = computer,
|
||||||
|
Component = context.Components.First(x => x.Id == pc.Key),
|
||||||
|
Count = pc.Value.Item2
|
||||||
|
});
|
||||||
|
context.SaveChanges();
|
||||||
|
}
|
||||||
|
_computerComponents = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
|
namespace ComputersShopDatabaseImplement.Models
|
||||||
|
{
|
||||||
|
public class ComputerComponent
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
[Required]
|
||||||
|
public int ComputerId { get; set; }
|
||||||
|
[Required]
|
||||||
|
public int ComponentId { get; set; }
|
||||||
|
[Required]
|
||||||
|
public int Count { get; set; }
|
||||||
|
public virtual Component Component { get; set; } = new();
|
||||||
|
public virtual Computer Computer { get; set; } = new();
|
||||||
|
}
|
||||||
|
}
|
76
ComputersShop/ComputersShopDatabaseImplement/Models/Order.cs
Normal file
76
ComputersShop/ComputersShopDatabaseImplement/Models/Order.cs
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
using ComputersShopContracts.BindingModels;
|
||||||
|
using ComputersShopContracts.ViewModels;
|
||||||
|
using ComputersShopDataModels.Enums;
|
||||||
|
using ComputersShopDataModels.Models;
|
||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
|
namespace ComputersShopDatabaseImplement.Models
|
||||||
|
{
|
||||||
|
public class Order : IOrderModel
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
[Required]
|
||||||
|
public int ComputerId { get; set; }
|
||||||
|
[Required]
|
||||||
|
public int Count { get; private set; }
|
||||||
|
[Required]
|
||||||
|
public double Sum { get; private set; }
|
||||||
|
[Required]
|
||||||
|
public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен;
|
||||||
|
[Required]
|
||||||
|
public DateTime DateCreate { get; private set; } = DateTime.Now;
|
||||||
|
public DateTime? DateImplement { get; private set; }
|
||||||
|
public static Order? Create(OrderBindingModel model)
|
||||||
|
{
|
||||||
|
if (model == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return new Order()
|
||||||
|
{
|
||||||
|
Id = model.Id,
|
||||||
|
ComputerId = model.ComputerId,
|
||||||
|
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,
|
||||||
|
ComputerId = model.ComputerId,
|
||||||
|
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 != null)
|
||||||
|
{
|
||||||
|
DateImplement = model.DateImplement;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public OrderViewModel GetViewModel => new()
|
||||||
|
{
|
||||||
|
Id = Id,
|
||||||
|
ComputerId = ComputerId,
|
||||||
|
Count = Count,
|
||||||
|
Sum = Sum,
|
||||||
|
Status = Status,
|
||||||
|
DateCreate = DateCreate,
|
||||||
|
DateImplement = DateImplement
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user