Merge branch 'LabWork03_base' into LabWork03_hard

This commit is contained in:
Сергей Полевой 2023-04-21 02:17:44 +04:00
commit a52ff0cd2e
15 changed files with 1076 additions and 2 deletions

View File

@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FlowerShopBusinessLogic", "
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FlowerShopListImplement", "FlowerShopListImplement\FlowerShopListImplement.csproj", "{C58840E6-D68D-476E-AB3A-EB13F1740FC8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FlowerShopFileImplement", "FlowerShopFileImplement\FlowerShopFileImplement.csproj", "{87156689-2FDA-4065-90CC-749B2FE3C0D6}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FlowerShopFileImplement", "FlowerShopFileImplement\FlowerShopFileImplement.csproj", "{87156689-2FDA-4065-90CC-749B2FE3C0D6}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FlowerShopDatabaseImplement", "FlowerShopDatabaseImplement\FlowerShopDatabaseImplement.csproj", "{77B5E3FA-7A48-41AD-BB77-6EBF273550AC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -45,6 +47,10 @@ Global
{87156689-2FDA-4065-90CC-749B2FE3C0D6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{87156689-2FDA-4065-90CC-749B2FE3C0D6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{87156689-2FDA-4065-90CC-749B2FE3C0D6}.Release|Any CPU.Build.0 = Release|Any CPU
{77B5E3FA-7A48-41AD-BB77-6EBF273550AC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{77B5E3FA-7A48-41AD-BB77-6EBF273550AC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{77B5E3FA-7A48-41AD-BB77-6EBF273550AC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{77B5E3FA-7A48-41AD-BB77-6EBF273550AC}.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="7.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.2.1" />
</ItemGroup>
@ -16,6 +20,7 @@
<ItemGroup>
<ProjectReference Include="..\FlowerShopBusinessLogic\FlowerShopBusinessLogic.csproj" />
<ProjectReference Include="..\FlowerShopContracts\FlowerShopContracts.csproj" />
<ProjectReference Include="..\FlowerShopDatabaseImplement\FlowerShopDatabaseImplement.csproj" />
<ProjectReference Include="..\FlowerShopDataModels\FlowerShopDataModels.csproj" />
<ProjectReference Include="..\FlowerShopFileImplement\FlowerShopFileImplement.csproj" />
<ProjectReference Include="..\FlowerShopListImplement\FlowerShopListImplement.csproj" />

View File

@ -1,6 +1,6 @@
using FlowerShopContracts.StoragesContracts;
using Microsoft.Extensions.DependencyInjection;
using FlowerShopFileImplement.Implements;
using FlowerShopDatabaseImplement.Implements;
using FlowerShopBusinessLogic.BusinessLogics;
using FlowerShopContracts.BusinessLogicsContracts;
using NLog.Extensions.Logging;

View File

@ -0,0 +1,22 @@
using FlowerShopDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.Identity.Client;
namespace FlowerShopDatabaseImplement
{
public class FlowerShopDatabase : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (optionsBuilder.IsConfigured == false)
{
optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Initial Catalog=FlowerShopDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;TrustServerCertificate=True");
}
base.OnConfiguring(optionsBuilder);
}
public virtual DbSet<Component> Components { set; get; }
public virtual DbSet<Bouquet> Bouquets { set; get; }
public virtual DbSet<BouquetComponent> BouquetComponents { 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="7.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\FlowerShopContracts\FlowerShopContracts.csproj" />
<ProjectReference Include="..\FlowerShopDataModels\FlowerShopDataModels.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,123 @@
using FlowerShopContracts.BindingModels;
using FlowerShopContracts.SearchModels;
using FlowerShopContracts.StoragesContracts;
using FlowerShopContracts.ViewModels;
using FlowerShopDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace FlowerShopDatabaseImplement.Implements
{
public class BouquetStorage : IBouquetStorage
{
public BouquetViewModel? GetElement(BouquetSearchModel model)
{
if (string.IsNullOrEmpty(model.BouquetName) && !model.Id.HasValue)
{
return null;
}
using var context = new FlowerShopDatabase();
return context.Bouquets.Include(x => x.Components)
.ThenInclude(x => x.Component)
.FirstOrDefault(
x => (!string.IsNullOrEmpty(model.BouquetName) && x.BouquetName == model.BouquetName) || (model.Id.HasValue && x.Id == model.Id)
)?.GetViewModel;
}
public List<BouquetViewModel> GetFilteredList(BouquetSearchModel model)
{
if (string.IsNullOrEmpty(model.BouquetName))
{
return new();
}
using var context = new FlowerShopDatabase();
return context.Bouquets.Include(x => x.Components).ThenInclude(x => x.Component).Where(x => x.BouquetName.Contains(model.BouquetName)).Select(x => x.GetViewModel).ToList();
}
public List<BouquetViewModel> GetFullList()
{
using var context = new FlowerShopDatabase();
return context.Bouquets.Include(x => x.Components).ThenInclude(x => x.Component).Select(x => x.GetViewModel).ToList();
}
public BouquetViewModel? Insert(BouquetBindingModel model)
{
using var context = new FlowerShopDatabase();
using var transaction = context.Database.BeginTransaction();
{
try
{
var newBouquet = Bouquet.Create(context, model);
if (newBouquet == null)
{
transaction.Rollback();
return null;
}
context.Bouquets.Add(newBouquet);
context.SaveChanges();
context.Database.CommitTransaction();
return newBouquet.GetViewModel;
}
catch (Exception)
{
transaction.Rollback();
return null;
}
}
}
public BouquetViewModel? Update(BouquetBindingModel model)
{
using var context = new FlowerShopDatabase();
using var transaction = context.Database.BeginTransaction();
{
try
{
var bouquet = context.Bouquets.FirstOrDefault(x => x.Id == model.Id);
if (bouquet == null)
{
transaction.Rollback();
return null;
}
bouquet.Update(model);
bouquet.UpdateComponents(context, model);
context.SaveChanges();
context.Database.CommitTransaction();
return bouquet.GetViewModel;
}
catch (Exception)
{
transaction.Rollback();
return null;
}
}
}
public BouquetViewModel? Delete(BouquetBindingModel model)
{
using var context = new FlowerShopDatabase();
var element = context.Bouquets.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Bouquets.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,89 @@
using FlowerShopContracts.BindingModels;
using FlowerShopContracts.SearchModels;
using FlowerShopContracts.StoragesContracts;
using FlowerShopContracts.ViewModels;
using FlowerShopDatabaseImplement.Models;
namespace FlowerShopDatabaseImplement.Implements
{
public class ComponentStorage : IComponentStorage
{
public ComponentViewModel? GetElement(ComponentSearchModel model)
{
if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue)
{
return null;
}
using var context = new FlowerShopDatabase();
return context.Components.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == model.ComponentName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public List<ComponentViewModel> GetFilteredList(ComponentSearchModel model)
{
if (string.IsNullOrEmpty(model.ComponentName))
{
return new();
}
using var context = new FlowerShopDatabase();
return context.Components.Where(x => x.ComponentName.Contains(model.ComponentName)).Select(x => x.GetViewModel).ToList();
}
public List<ComponentViewModel> GetFullList()
{
using var context = new FlowerShopDatabase();
return context.Components.Select(x => x.GetViewModel).ToList();
}
public ComponentViewModel? Insert(ComponentBindingModel model)
{
var newComponent = Component.Create(model);
if (newComponent == null)
{
return null;
}
using var context = new FlowerShopDatabase();
context.Components.Add(newComponent);
context.SaveChanges();
return newComponent.GetViewModel;
}
public ComponentViewModel? Update(ComponentBindingModel model)
{
using var context = new FlowerShopDatabase();
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 FlowerShopDatabase();
var element = context.Components.FirstOrDefault(record => record.Id == model.Id);
if (element != null)
{
context.Components.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,89 @@
using FlowerShopContracts.BindingModels;
using FlowerShopContracts.SearchModels;
using FlowerShopContracts.StoragesContracts;
using FlowerShopContracts.ViewModels;
using FlowerShopDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace FlowerShopDatabaseImplement.Implements
{
public class OrderStorage : IOrderStorage
{
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new FlowerShopDatabase();
return context.Orders.Include(x => x.Bouquet).FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
using var context = new FlowerShopDatabase();
return context.Orders.Where(x => x.Id == model.Id).Include(x => x.Bouquet).Select(x => x.GetViewModel).ToList();
}
public List<OrderViewModel> GetFullList()
{
using var context = new FlowerShopDatabase();
return context.Orders.Include(x => x.Bouquet).Select(x => x.GetViewModel).ToList();
}
public OrderViewModel? Insert(OrderBindingModel model)
{
var newOrder = Order.Create(model);
if (newOrder == null)
{
return null;
}
using var context = new FlowerShopDatabase();
context.Orders.Add(newOrder);
context.SaveChanges();
return context.Orders.Include(x => x.Bouquet).FirstOrDefault(x => x.Id == newOrder.Id)?.GetViewModel;
}
public OrderViewModel? Update(OrderBindingModel model)
{
using var context = new FlowerShopDatabase();
var order = context.Orders.FirstOrDefault(x => x.Id == model.Id);
if (order == null)
{
return null;
}
order.Update(model);
context.SaveChanges();
return context.Orders.Include(x => x.Bouquet).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
}
public OrderViewModel? Delete(OrderBindingModel model)
{
using var context = new FlowerShopDatabase();
var element = context.Orders.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,171 @@
// <auto-generated />
using System;
using FlowerShopDatabaseImplement;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace FlowerShopDatabaseImplement.Migrations
{
[DbContext(typeof(FlowerShopDatabase))]
[Migration("20230312011037_InitialCreate")]
partial class InitialCreate
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.3")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Bouquet", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("BouquetName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Price")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("Bouquets");
});
modelBuilder.Entity("FlowerShopDatabaseImplement.Models.BouquetComponent", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("BouquetId")
.HasColumnType("int");
b.Property<int>("ComponentId")
.HasColumnType("int");
b.Property<int>("Count")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("BouquetId");
b.HasIndex("ComponentId");
b.ToTable("BouquetComponents");
});
modelBuilder.Entity("FlowerShopDatabaseImplement.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("FlowerShopDatabaseImplement.Models.Order", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("BouquetId")
.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("BouquetId");
b.ToTable("Orders");
});
modelBuilder.Entity("FlowerShopDatabaseImplement.Models.BouquetComponent", b =>
{
b.HasOne("FlowerShopDatabaseImplement.Models.Bouquet", "Bouquet")
.WithMany("Components")
.HasForeignKey("BouquetId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("FlowerShopDatabaseImplement.Models.Component", "Component")
.WithMany("BouquetComponents")
.HasForeignKey("ComponentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Bouquet");
b.Navigation("Component");
});
modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Order", b =>
{
b.HasOne("FlowerShopDatabaseImplement.Models.Bouquet", "Bouquet")
.WithMany("Orders")
.HasForeignKey("BouquetId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Bouquet");
});
modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Bouquet", b =>
{
b.Navigation("Components");
b.Navigation("Orders");
});
modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Component", b =>
{
b.Navigation("BouquetComponents");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,125 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace FlowerShopDatabaseImplement.Migrations
{
/// <inheritdoc />
public partial class InitialCreate : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Bouquets",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
BouquetName = table.Column<string>(type: "nvarchar(max)", nullable: false),
Price = table.Column<double>(type: "float", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Bouquets", x => x.Id);
});
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: "Orders",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
BouquetId = 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_Bouquets_BouquetId",
column: x => x.BouquetId,
principalTable: "Bouquets",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "BouquetComponents",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
BouquetId = 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_BouquetComponents", x => x.Id);
table.ForeignKey(
name: "FK_BouquetComponents_Bouquets_BouquetId",
column: x => x.BouquetId,
principalTable: "Bouquets",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_BouquetComponents_Components_ComponentId",
column: x => x.ComponentId,
principalTable: "Components",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_BouquetComponents_BouquetId",
table: "BouquetComponents",
column: "BouquetId");
migrationBuilder.CreateIndex(
name: "IX_BouquetComponents_ComponentId",
table: "BouquetComponents",
column: "ComponentId");
migrationBuilder.CreateIndex(
name: "IX_Orders_BouquetId",
table: "Orders",
column: "BouquetId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "BouquetComponents");
migrationBuilder.DropTable(
name: "Orders");
migrationBuilder.DropTable(
name: "Components");
migrationBuilder.DropTable(
name: "Bouquets");
}
}
}

View File

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

View File

@ -0,0 +1,100 @@
using FlowerShopContracts.BindingModels;
using FlowerShopContracts.ViewModels;
using FlowerShopDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace FlowerShopDatabaseImplement.Models
{
public class Bouquet : IBouquetModel
{
public int Id { get; private set; }
[Required]
public string BouquetName { get; private set; } = string.Empty;
[Required]
public double Price { get; private set; }
private Dictionary<int, (IComponentModel, int)>? _bouquetComponents = null;
[NotMapped]
public Dictionary<int, (IComponentModel, int)> BouquetComponents
{
get
{
if (_bouquetComponents == null)
{
_bouquetComponents = Components.ToDictionary(record => record.ComponentId, record => (record.Component as IComponentModel, record.Count));
}
return _bouquetComponents;
}
}
[ForeignKey("BouquetId")]
public virtual List<BouquetComponent> Components { get; set; } = new();
[ForeignKey("BouquetId")]
public virtual List<Order> Orders { get; set; } = new();
public static Bouquet? Create(FlowerShopDatabase context, BouquetBindingModel model)
{
return new Bouquet()
{
Id = model.Id,
BouquetName = model.BouquetName,
Price = model.Price,
Components = model.BouquetComponents.Select(x => new BouquetComponent
{
Component = context.Components.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(BouquetBindingModel model)
{
BouquetName = model.BouquetName;
Price = model.Price;
}
public BouquetViewModel GetViewModel => new()
{
Id = Id,
BouquetName = BouquetName,
Price = Price,
BouquetComponents = BouquetComponents
};
public void UpdateComponents(FlowerShopDatabase context, BouquetBindingModel model)
{
var bouquetComponents = context.BouquetComponents.Where(record => record.BouquetId == model.Id).ToList();
if (bouquetComponents != null && bouquetComponents.Count > 0)
{
context.BouquetComponents.RemoveRange(bouquetComponents.Where(record => !model.BouquetComponents.ContainsKey(record.ComponentId)));
context.SaveChanges();
foreach (var updateComponent in bouquetComponents)
{
updateComponent.Count = model.BouquetComponents[updateComponent.ComponentId].Item2;
model.BouquetComponents.Remove(updateComponent.ComponentId);
}
context.SaveChanges();
}
var bouquet = context.Bouquets.First(x => x.Id == Id);
foreach (var pc in model.BouquetComponents)
{
context.BouquetComponents.Add(new BouquetComponent
{
Bouquet = bouquet,
Component = context.Components.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
});
context.SaveChanges();
}
_bouquetComponents = null;
}
}
}

View File

@ -0,0 +1,22 @@
using System.ComponentModel.DataAnnotations;
namespace FlowerShopDatabaseImplement.Models
{
public class BouquetComponent
{
public int Id { get; set; }
[Required]
public int BouquetId { get; set; }
[Required]
public int ComponentId { get; set; }
[Required]
public int Count { get; set; }
public virtual Component Component { get; set; } = new();
public virtual Bouquet Bouquet { get; set; } = new();
}
}

View File

@ -0,0 +1,56 @@
using FlowerShopContracts.BindingModels;
using FlowerShopContracts.ViewModels;
using FlowerShopDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace FlowerShopDatabaseImplement.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<BouquetComponent> BouquetComponents { 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 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,75 @@
using FlowerShopContracts.BindingModels;
using FlowerShopContracts.ViewModels;
using FlowerShopDataModels.Enums;
using FlowerShopDataModels.Models;
using System.ComponentModel.DataAnnotations;
namespace FlowerShopDatabaseImplement.Models
{
public class Order : IOrderModel
{
public int Id { get; private set; }
[Required]
public int BouquetId { get; private set; }
[Required]
public int Count { get; private set; }
[Required]
public double Sum { get; private set; }
[Required]
public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен;
[Required]
public DateTime DateCreate { get; private set; } = DateTime.Now;
public DateTime? DateImplement { get; private set; }
public virtual Bouquet Bouquet { get; set; }
public static Order? Create(OrderBindingModel? model)
{
if (model == null)
{
return null;
}
return new Order
{
BouquetId = model.BouquetId,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement,
Id = model.Id
};
}
public void Update(OrderBindingModel? model)
{
if (model == null)
{
return;
}
Status = model.Status;
DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel => new()
{
BouquetId = BouquetId,
Count = Count,
Sum = Sum,
DateCreate = DateCreate,
DateImplement = DateImplement,
Id = Id,
Status = Status,
BouquetName = Bouquet.BouquetName
};
}
}