This commit is contained in:
DeerElk 2024-04-10 11:44:57 +04:00
parent c30c2d570f
commit 3560693de7
15 changed files with 1016 additions and 1 deletions

View File

@ -6,6 +6,7 @@ MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfectioneryView", "Confectionery\ConfectioneryView.csproj", "{E19664A2-BC0D-451E-B9E1-2A5546873A14}"
ProjectSection(ProjectDependencies) = postProject
{511A9EBD-14A4-48E9-9101-41A5FB8D3C17} = {511A9EBD-14A4-48E9-9101-41A5FB8D3C17}
{E8F1F27D-0DD1-47DA-9BDF-8419D0B3EF58} = {E8F1F27D-0DD1-47DA-9BDF-8419D0B3EF58}
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfectioneryDataModels", "ConfectioneryDataModels\ConfectioneryDataModels.csproj", "{92EBD50D-79CE-46FC-B261-43098E98B78F}"
@ -18,6 +19,12 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfectioneryListImplement"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfectioneryFileImplement", "ConfectioneryFileImplement\ConfectioneryFileImplement.csproj", "{511A9EBD-14A4-48E9-9101-41A5FB8D3C17}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfectioneryDatabaseImplement", "ConfectioneryDatabaseImplement\ConfectioneryDatabaseImplement.csproj", "{E8F1F27D-0DD1-47DA-9BDF-8419D0B3EF58}"
ProjectSection(ProjectDependencies) = postProject
{181AF2FE-AE82-4767-B58B-36C0F22B56CD} = {181AF2FE-AE82-4767-B58B-36C0F22B56CD}
{92EBD50D-79CE-46FC-B261-43098E98B78F} = {92EBD50D-79CE-46FC-B261-43098E98B78F}
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -48,6 +55,10 @@ Global
{511A9EBD-14A4-48E9-9101-41A5FB8D3C17}.Debug|Any CPU.Build.0 = Debug|Any CPU
{511A9EBD-14A4-48E9-9101-41A5FB8D3C17}.Release|Any CPU.ActiveCfg = Release|Any CPU
{511A9EBD-14A4-48E9-9101-41A5FB8D3C17}.Release|Any CPU.Build.0 = Release|Any CPU
{E8F1F27D-0DD1-47DA-9BDF-8419D0B3EF58}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E8F1F27D-0DD1-47DA-9BDF-8419D0B3EF58}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E8F1F27D-0DD1-47DA-9BDF-8419D0B3EF58}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E8F1F27D-0DD1-47DA-9BDF-8419D0B3EF58}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -9,6 +9,10 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Logging.ApplicationInsights" Version="2.22.0" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.8" />
</ItemGroup>
@ -17,6 +21,7 @@
<ProjectReference Include="..\ConfectioneryBusinessLogic\ConfectioneryBusinessLogic.csproj" />
<ProjectReference Include="..\ConfectioneryListImplement\ConfectioneryListImplement.csproj" />
<ProjectReference Include="..\ConfectioneryFileImplement\ConfectioneryFileImplement.csproj" />
<ProjectReference Include="..\ConfectioneryDatabaseImplement\ConfectioneryDatabaseImplement.csproj" />
</ItemGroup>
</Project>

View File

@ -1,7 +1,7 @@
using ConfectioneryBusinessLogic.BusinessLogics;
using ConfectioneryContracts.BusinessLogicsContracts;
using ConfectioneryContracts.StoragesContracts;
using ConfectioneryFileImplement.Implements;
using ConfectioneryDatabaseImplement.Implements;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;

View File

@ -0,0 +1,21 @@
using ConfectioneryDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace ConfectioneryDatabaseImplement
{
public class ConfectioneryDatabase : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder
optionsBuilder)
{
if (optionsBuilder.IsConfigured == false)
{
optionsBuilder.UseSqlServer(@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=ConfectioneryDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
}
base.OnConfiguring(optionsBuilder);
}
public virtual DbSet<Component> Components { set; get; }
public virtual DbSet<Pastry> Pastries { set; get; }
public virtual DbSet<PastryComponent> PastryComponents { set; get; }
public virtual DbSet<Order> Orders { set; get; }
}
}

View File

@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\ConfectioneryContracts\ConfectioneryContracts.csproj" />
<ProjectReference Include="..\ConfectioneryDataModels\ConfectioneryDataModels.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>

View File

@ -0,0 +1,82 @@
using ConfectioneryContracts.BindingModels;
using ConfectioneryContracts.SearchModels;
using ConfectioneryContracts.StoragesContracts;
using ConfectioneryContracts.ViewModels;
using ConfectioneryDatabaseImplement.Models;
namespace ConfectioneryDatabaseImplement.Implements
{
public class ComponentStorage : IComponentStorage
{
public List<ComponentViewModel> GetFullList()
{
using var context = new ConfectioneryDatabase();
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 ConfectioneryDatabase();
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 ConfectioneryDatabase();
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 ConfectioneryDatabase();
context.Components.Add(newComponent);
context.SaveChanges();
return newComponent.GetViewModel;
}
public ComponentViewModel? Update(ComponentBindingModel model)
{
using var context = new ConfectioneryDatabase();
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 ConfectioneryDatabase();
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,79 @@
using ConfectioneryContracts.BindingModels;
using ConfectioneryContracts.SearchModels;
using ConfectioneryContracts.StoragesContracts;
using ConfectioneryContracts.ViewModels;
using ConfectioneryDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace ConfectioneryDatabaseImplement.Implements
{
public class OrderStorage : IOrderStorage
{
public List<OrderViewModel> GetFullList()
{
using var context = new ConfectioneryDatabase();
return context.Orders.Include(x => x.Pastries)
.Select(x => x.GetViewModel)
.ToList();
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
using var context = new ConfectioneryDatabase();
return context.Orders.Include(x => x.Pastries)
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new ConfectioneryDatabase();
return context.Orders.Include(x => x.Pastries)
.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
}
public OrderViewModel? Insert(OrderBindingModel model)
{
var newOrder = Order.Create(model);
if (newOrder == null)
{
return null;
}
using var context = new ConfectioneryDatabase();
context.Orders.Add(newOrder);
context.SaveChanges();
return newOrder.GetViewModel;
}
public OrderViewModel? Update(OrderBindingModel model)
{
using var context = new ConfectioneryDatabase();
var order = context.Orders.Include(x => x.Pastries)
.FirstOrDefault(x => x.Id == model.Id);
if (order == null)
{
return null;
}
order.Update(model);
context.SaveChanges();
return order.GetViewModel;
}
public OrderViewModel? Delete(OrderBindingModel model)
{
using var context = new ConfectioneryDatabase();
var element = context.Orders.Include(x => x.Pastries)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Orders.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,101 @@
using ConfectioneryContracts.BindingModels;
using ConfectioneryContracts.SearchModels;
using ConfectioneryContracts.StoragesContracts;
using ConfectioneryContracts.ViewModels;
using ConfectioneryDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace ConfectioneryDatabaseImplement.Implements
{
public class PastryStorage : IPastryStorage
{
public List<PastryViewModel> GetFullList()
{
using var context = new ConfectioneryDatabase();
return context.Pastries
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<PastryViewModel> GetFilteredList(PastrySearchModel model)
{
if (string.IsNullOrEmpty(model.PastryName))
{
return new();
}
using var context = new ConfectioneryDatabase();
return context.Pastries
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.Where(x => x.PastryName.Contains(model.PastryName))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public PastryViewModel? GetElement(PastrySearchModel model)
{
if (string.IsNullOrEmpty(model.PastryName) && !model.Id.HasValue)
{
return null;
}
using var context = new ConfectioneryDatabase();
return context.Pastries
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.PastryName)
&& x.PastryName == model.PastryName) ||
(model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public PastryViewModel? Insert(PastryBindingModel model)
{
using var context = new ConfectioneryDatabase();
var newPastry = Pastry.Create(context, model);
if (newPastry == null)
{
return null;
}
context.Pastries.Add(newPastry);
context.SaveChanges();
return newPastry.GetViewModel;
}
public PastryViewModel? Update(PastryBindingModel model)
{
using var context = new ConfectioneryDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var pastry = context.Pastries.FirstOrDefault(rec =>
rec.Id == model.Id);
if (pastry == null)
{
return null;
}
pastry.Update(model);
context.SaveChanges();
pastry.UpdateComponents(context, model);
transaction.Commit();
return pastry.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public PastryViewModel? Delete(PastryBindingModel model)
{
using var context = new ConfectioneryDatabase();
var element = context.Pastries
.Include(x => x.Components)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Pastries.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,171 @@
// <auto-generated />
using System;
using ConfectioneryDatabaseImplement;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace ConfectioneryDatabaseImplement.Migrations
{
[DbContext(typeof(ConfectioneryDatabase))]
[Migration("20240410062130_InitialCreate")]
partial class InitialCreate
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "8.0.3")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("ConfectioneryDatabaseImplement.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("ConfectioneryDatabaseImplement.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>("PastryId")
.HasColumnType("int");
b.Property<int>("Status")
.HasColumnType("int");
b.Property<double>("Sum")
.HasColumnType("float");
b.HasKey("Id");
b.HasIndex("PastryId");
b.ToTable("Orders");
});
modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Pastry", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("PastryName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Price")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("Pastries");
});
modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.PastryComponent", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("ComponentId")
.HasColumnType("int");
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("PastryId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ComponentId");
b.HasIndex("PastryId");
b.ToTable("PastryComponents");
});
modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Order", b =>
{
b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", "Pastries")
.WithMany("Orders")
.HasForeignKey("PastryId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Pastries");
});
modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.PastryComponent", b =>
{
b.HasOne("ConfectioneryDatabaseImplement.Models.Component", "Component")
.WithMany("PastryComponents")
.HasForeignKey("ComponentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", "Pastry")
.WithMany("Components")
.HasForeignKey("PastryId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Component");
b.Navigation("Pastry");
});
modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Component", b =>
{
b.Navigation("PastryComponents");
});
modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Pastry", b =>
{
b.Navigation("Components");
b.Navigation("Orders");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,125 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace ConfectioneryDatabaseImplement.Migrations
{
/// <inheritdoc />
public partial class InitialCreate : 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: "Pastries",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
PastryName = table.Column<string>(type: "nvarchar(max)", nullable: false),
Price = table.Column<double>(type: "float", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Pastries", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Orders",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
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),
PastryId = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Orders", x => x.Id);
table.ForeignKey(
name: "FK_Orders_Pastries_PastryId",
column: x => x.PastryId,
principalTable: "Pastries",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "PastryComponents",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
PastryId = 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_PastryComponents", x => x.Id);
table.ForeignKey(
name: "FK_PastryComponents_Components_ComponentId",
column: x => x.ComponentId,
principalTable: "Components",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_PastryComponents_Pastries_PastryId",
column: x => x.PastryId,
principalTable: "Pastries",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_Orders_PastryId",
table: "Orders",
column: "PastryId");
migrationBuilder.CreateIndex(
name: "IX_PastryComponents_ComponentId",
table: "PastryComponents",
column: "ComponentId");
migrationBuilder.CreateIndex(
name: "IX_PastryComponents_PastryId",
table: "PastryComponents",
column: "PastryId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Orders");
migrationBuilder.DropTable(
name: "PastryComponents");
migrationBuilder.DropTable(
name: "Components");
migrationBuilder.DropTable(
name: "Pastries");
}
}
}

View File

@ -0,0 +1,168 @@
// <auto-generated />
using System;
using ConfectioneryDatabaseImplement;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace ConfectioneryDatabaseImplement.Migrations
{
[DbContext(typeof(ConfectioneryDatabase))]
partial class ConfectioneryDatabaseModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "8.0.3")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("ConfectioneryDatabaseImplement.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("ConfectioneryDatabaseImplement.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>("PastryId")
.HasColumnType("int");
b.Property<int>("Status")
.HasColumnType("int");
b.Property<double>("Sum")
.HasColumnType("float");
b.HasKey("Id");
b.HasIndex("PastryId");
b.ToTable("Orders");
});
modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Pastry", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("PastryName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Price")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("Pastries");
});
modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.PastryComponent", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("ComponentId")
.HasColumnType("int");
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("PastryId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ComponentId");
b.HasIndex("PastryId");
b.ToTable("PastryComponents");
});
modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Order", b =>
{
b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", "Pastries")
.WithMany("Orders")
.HasForeignKey("PastryId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Pastries");
});
modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.PastryComponent", b =>
{
b.HasOne("ConfectioneryDatabaseImplement.Models.Component", "Component")
.WithMany("PastryComponents")
.HasForeignKey("ComponentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", "Pastry")
.WithMany("Components")
.HasForeignKey("PastryId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Component");
b.Navigation("Pastry");
});
modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Component", b =>
{
b.Navigation("PastryComponents");
});
modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Pastry", b =>
{
b.Navigation("Components");
b.Navigation("Orders");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,56 @@
using ConfectioneryContracts.BindingModels;
using ConfectioneryContracts.ViewModels;
using ConfectioneryDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace ConfectioneryDatabaseImplement.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<PastryComponent> PastryComponents { 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,60 @@
using ConfectioneryContracts.BindingModels;
using ConfectioneryContracts.ViewModels;
using ConfectioneryDataModels.Enums;
using System.ComponentModel.DataAnnotations;
namespace ConfectioneryDatabaseImplement.Models
{
public class Order
{
public int Id { get; private set; }
[Required]
public int Count { get; private set; }
[Required]
public double Sum { get; private set; }
[Required]
public OrderStatus Status { get; private set; }
[Required]
public DateTime DateCreate { get; private set; }
public DateTime? DateImplement { get; private set; }
[Required]
public int PastryId { get; private set; }
public virtual Pastry? Pastries { get; private set; }
public static Order? Create(OrderBindingModel model)
{
if (model == null)
{
return null;
}
return new Order()
{
Id = model.Id,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement,
PastryId = model.PastryId,
};
}
public void Update(OrderBindingModel? model)
{
if (model == null)
{
return;
}
Status = model.Status;
DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel => new()
{
PastryId = PastryId,
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement,
Id = Id,
PastryName = Pastries?.PastryName ?? string.Empty
};
}
}

View File

@ -0,0 +1,96 @@
using ConfectioneryContracts.BindingModels;
using ConfectioneryContracts.ViewModels;
using ConfectioneryDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace ConfectioneryDatabaseImplement.Models
{
public class Pastry : IPastryModel
{
public int Id { get; set; }
[Required]
public string PastryName { get; set; } = string.Empty;
[Required]
public double Price { get; set; }
private Dictionary<int, (IComponentModel, int)>?
_pastryComponents = null;
[NotMapped]
public Dictionary<int, (IComponentModel, int)> PastryComponents
{
get
{
if (_pastryComponents == null)
{
_pastryComponents = Components
.ToDictionary(recPC => recPC.ComponentId, recPC =>
(recPC.Component as IComponentModel, recPC.Count));
}
return _pastryComponents;
}
}
[ForeignKey("PastryId")]
public virtual List<PastryComponent> Components { get; set; } = new();
[ForeignKey("PastryId")]
public virtual List<Order> Orders { get; set; } = new();
public static Pastry Create(ConfectioneryDatabase context,
PastryBindingModel model)
{
return new Pastry()
{
Id = model.Id,
PastryName = model.PastryName,
Price = model.Price,
Components = model.PastryComponents.Select(x =>
new PastryComponent
{
Component = context.Components.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(PastryBindingModel model)
{
PastryName = model.PastryName;
Price = model.Price;
}
public PastryViewModel GetViewModel => new()
{
Id = Id,
PastryName = PastryName,
Price = Price,
PastryComponents = PastryComponents
};
public void UpdateComponents(ConfectioneryDatabase context,
PastryBindingModel model)
{
var pastryComponents = context.PastryComponents
.Where(rec => rec.PastryId == model.Id).ToList();
if (pastryComponents != null && pastryComponents.Count > 0)
{
context.PastryComponents.RemoveRange(pastryComponents
.Where(rec => !model.PastryComponents
.ContainsKey(rec.ComponentId)));
context.SaveChanges();
foreach (var updateComponent in pastryComponents)
{
updateComponent.Count = model
.PastryComponents[updateComponent.ComponentId].Item2;
model.PastryComponents.Remove(updateComponent.ComponentId);
}
context.SaveChanges();
}
var pastry = context.Pastries.First(x => x.Id == Id);
foreach (var pc in model.PastryComponents)
{
context.PastryComponents.Add(new PastryComponent
{
Pastry = pastry,
Component = context.Components.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
});
context.SaveChanges();
}
_pastryComponents = null;
}
}
}

View File

@ -0,0 +1,16 @@
using System.ComponentModel.DataAnnotations;
namespace ConfectioneryDatabaseImplement.Models
{
public class PastryComponent
{
public int Id { get; set; }
[Required]
public int PastryId { get; set; }
[Required]
public int ComponentId { get; set; }
[Required]
public int Count { get; set; }
public virtual Component Component { get; set; } = new();
public virtual Pastry Pastry { get; set; } = new();
}
}