merge lab3 with hard_lab3

This commit is contained in:
DavidMakarov 2024-04-19 10:21:48 +04:00
commit 3955a3c93f
15 changed files with 1018 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", "{8B6B6E6D-442B-46F0-8F60-E3D91E8D0AB5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FlowerShopFileImplement", "FlowerShopFileImplement\FlowerShopFileImplement.csproj", "{239BA98B-C7E4-4434-9A18-6EE4F40071E6}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FlowerShopFileImplement", "FlowerShopFileImplement\FlowerShopFileImplement.csproj", "{239BA98B-C7E4-4434-9A18-6EE4F40071E6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FlowerShopDatabaseImplement", "FlowerShopDatabaseImplement\FlowerShopDatabaseImplement.csproj", "{07E0542C-0ED4-4F69-B72D-B51EFF80C599}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -45,6 +47,10 @@ Global
{239BA98B-C7E4-4434-9A18-6EE4F40071E6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{239BA98B-C7E4-4434-9A18-6EE4F40071E6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{239BA98B-C7E4-4434-9A18-6EE4F40071E6}.Release|Any CPU.Build.0 = Release|Any CPU
{07E0542C-0ED4-4F69-B72D-B51EFF80C599}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{07E0542C-0ED4-4F69-B72D-B51EFF80C599}.Debug|Any CPU.Build.0 = Debug|Any CPU
{07E0542C-0ED4-4F69-B72D-B51EFF80C599}.Release|Any CPU.ActiveCfg = Release|Any CPU
{07E0542C-0ED4-4F69-B72D-B51EFF80C599}.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="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.Abstractions" Version="8.0.0" />
<PackageReference Include="NLog" Version="5.2.8" />
@ -17,6 +21,7 @@
<ItemGroup>
<ProjectReference Include="..\FlowerShopBusinessLogic\FlowerShopBusinessLogic.csproj" />
<ProjectReference Include="..\FlowerShopDatabaseImplement\FlowerShopDatabaseImplement.csproj" />
<ProjectReference Include="..\FlowerShopFileImplement\FlowerShopFileImplement.csproj" />
<ProjectReference Include="..\FlowerShopListImplement\FlowerShopListImplement.csproj" />
</ItemGroup>

View File

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

View File

@ -0,0 +1,21 @@
using FlowerShopDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace FlowerShopDatabaseImplement
{
public class FlowerShopDatabase : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (optionsBuilder.IsConfigured == false)
{
optionsBuilder.UseSqlServer(@"Data Source=WHITEBEAR\SQLEXPRESS;Initial Catalog=FlowerShopDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
}
base.OnConfiguring(optionsBuilder);
}
public virtual DbSet<Component> Components { set; get; }
public virtual DbSet<Flower> Flowers { set; get; }
public virtual DbSet<FlowerComponent> FlowerComponents { 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="..\FlowerShopContracts\FlowerShopContracts.csproj" />
<ProjectReference Include="..\FlowerShopDataModels\FlowerShopDataModels.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,80 @@
using FlowerShopContracts.BindingModels;
using FlowerShopContracts.SearchModels;
using FlowerShopContracts.StoragesContracts;
using FlowerShopContracts.ViewModels;
using FlowerShopDatabaseImplement.Models;
namespace FlowerShopDatabaseImplement.Implements
{
public class ComponentStorage : IComponentStorage
{
public List<ComponentViewModel> GetFullList()
{
using var context = new FlowerShopDatabase();
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 FlowerShopDatabase();
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 FlowerShopDatabase();
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 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(rec => rec.Id == model.Id);
if (element != null)
{
context.Components.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,105 @@
using FlowerShopContracts.BindingModels;
using FlowerShopContracts.SearchModels;
using FlowerShopContracts.StoragesContracts;
using FlowerShopContracts.ViewModels;
using FlowerShopDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace FlowerShopDatabaseImplement.Implements
{
public class FlowerStorage : IFlowerStorage
{
public List<FlowerViewModel> GetFullList()
{
using var context = new FlowerShopDatabase();
return context.Flowers
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<FlowerViewModel> GetFilteredList(FlowerSearchModel model)
{
if (string.IsNullOrEmpty(model.FlowerName))
{
return new();
}
using var context = new FlowerShopDatabase();
return context.Flowers
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.Where(x => x.FlowerName.Contains(model.FlowerName))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public FlowerViewModel? GetElement(FlowerSearchModel model)
{
if (string.IsNullOrEmpty(model.FlowerName) &&
!model.Id.HasValue)
{
return null;
}
using var context = new FlowerShopDatabase();
return context.Flowers
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.FlowerName) &&
x.FlowerName == model.FlowerName) ||
(model.Id.HasValue && x.Id ==
model.Id))
?.GetViewModel;
}
public FlowerViewModel? Insert(FlowerBindingModel model)
{
using var context = new FlowerShopDatabase();
var newFlower = Flower.Create(context, model);
if (newFlower == null)
{
return null;
}
context.Flowers.Add(newFlower);
context.SaveChanges();
return newFlower.GetViewModel;
}
public FlowerViewModel? Update(FlowerBindingModel model)
{
using var context = new FlowerShopDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var product = context.Flowers.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 FlowerViewModel? Delete(FlowerBindingModel model)
{
using var context = new FlowerShopDatabase();
var element = context.Flowers
.Include(x => x.Components)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Flowers.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,93 @@
using FlowerShopContracts.BindingModels;
using FlowerShopContracts.SearchModels;
using FlowerShopContracts.StoragesContracts;
using FlowerShopContracts.ViewModels;
using FlowerShopDatabaseImplement.Models;
namespace FlowerShopDatabaseImplement.Implements
{
public class OrderStorage : IOrderStorage
{
public List<OrderViewModel> GetFullList()
{
using var context = new FlowerShopDatabase();
return context.Orders
.Select(x => AccessFlowerStorage(x.GetViewModel))
.ToList();
}
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)
.Select(x => AccessFlowerStorage(x.GetViewModel))
.ToList();
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new FlowerShopDatabase();
return AccessFlowerStorage(context.Orders.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 FlowerShopDatabase();
context.Orders.Add(newOrder);
context.SaveChanges();
return AccessFlowerStorage(newOrder.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 AccessFlowerStorage(order.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 AccessFlowerStorage(element.GetViewModel);
}
return null;
}
public static OrderViewModel AccessFlowerStorage(OrderViewModel model)
{
if (model == null)
return null;
using var context = new FlowerShopDatabase();
foreach (var Flower in context.Flowers)
{
if (Flower.Id == model.FlowerId)
{
model.FlowerName = Flower.FlowerName;
break;
}
}
return model;
}
}
}

View File

@ -0,0 +1,169 @@
// <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("20240308215929_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("FlowerShopDatabaseImplement.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("FlowerShopDatabaseImplement.Models.Flower", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<string>("FlowerName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Price")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("Flowers");
});
modelBuilder.Entity("FlowerShopDatabaseImplement.Models.FlowerComponent", 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>("FlowerId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ComponentId");
b.HasIndex("FlowerId");
b.ToTable("FlowerComponents");
});
modelBuilder.Entity("FlowerShopDatabaseImplement.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")
.IsRequired()
.HasColumnType("datetime2");
b.Property<int>("FlowerId")
.HasColumnType("int");
b.Property<int>("Status")
.HasColumnType("int");
b.Property<double>("Sum")
.HasColumnType("float");
b.HasKey("Id");
b.HasIndex("FlowerId");
b.ToTable("Orders");
});
modelBuilder.Entity("FlowerShopDatabaseImplement.Models.FlowerComponent", b =>
{
b.HasOne("FlowerShopDatabaseImplement.Models.Component", "Component")
.WithMany("FlowerComponents")
.HasForeignKey("ComponentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("FlowerShopDatabaseImplement.Models.Flower", "Flower")
.WithMany("Components")
.HasForeignKey("FlowerId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Component");
b.Navigation("Flower");
});
modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Order", b =>
{
b.HasOne("FlowerShopDatabaseImplement.Models.Flower", null)
.WithMany("Orders")
.HasForeignKey("FlowerId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Component", b =>
{
b.Navigation("FlowerComponents");
});
modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Flower", 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 FlowerShopDatabaseImplement.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: "Flowers",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
FlowerName = table.Column<string>(type: "nvarchar(max)", nullable: false),
Price = table.Column<double>(type: "float", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Flowers", x => x.Id);
});
migrationBuilder.CreateTable(
name: "FlowerComponents",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
FlowerId = 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_FlowerComponents", x => x.Id);
table.ForeignKey(
name: "FK_FlowerComponents_Components_ComponentId",
column: x => x.ComponentId,
principalTable: "Components",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_FlowerComponents_Flowers_FlowerId",
column: x => x.FlowerId,
principalTable: "Flowers",
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"),
FlowerId = 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_Flowers_FlowerId",
column: x => x.FlowerId,
principalTable: "Flowers",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_FlowerComponents_ComponentId",
table: "FlowerComponents",
column: "ComponentId");
migrationBuilder.CreateIndex(
name: "IX_FlowerComponents_FlowerId",
table: "FlowerComponents",
column: "FlowerId");
migrationBuilder.CreateIndex(
name: "IX_Orders_FlowerId",
table: "Orders",
column: "FlowerId");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "FlowerComponents");
migrationBuilder.DropTable(
name: "Orders");
migrationBuilder.DropTable(
name: "Components");
migrationBuilder.DropTable(
name: "Flowers");
}
}
}

View File

@ -0,0 +1,167 @@
// <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", "6.0.27")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1);
modelBuilder.Entity("FlowerShopDatabaseImplement.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("FlowerShopDatabaseImplement.Models.Flower", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<string>("FlowerName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Price")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("Flowers");
});
modelBuilder.Entity("FlowerShopDatabaseImplement.Models.FlowerComponent", 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>("FlowerId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ComponentId");
b.HasIndex("FlowerId");
b.ToTable("FlowerComponents");
});
modelBuilder.Entity("FlowerShopDatabaseImplement.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")
.IsRequired()
.HasColumnType("datetime2");
b.Property<int>("FlowerId")
.HasColumnType("int");
b.Property<int>("Status")
.HasColumnType("int");
b.Property<double>("Sum")
.HasColumnType("float");
b.HasKey("Id");
b.HasIndex("FlowerId");
b.ToTable("Orders");
});
modelBuilder.Entity("FlowerShopDatabaseImplement.Models.FlowerComponent", b =>
{
b.HasOne("FlowerShopDatabaseImplement.Models.Component", "Component")
.WithMany("FlowerComponents")
.HasForeignKey("ComponentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("FlowerShopDatabaseImplement.Models.Flower", "Flower")
.WithMany("Components")
.HasForeignKey("FlowerId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Component");
b.Navigation("Flower");
});
modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Order", b =>
{
b.HasOne("FlowerShopDatabaseImplement.Models.Flower", null)
.WithMany("Orders")
.HasForeignKey("FlowerId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Component", b =>
{
b.Navigation("FlowerComponents");
});
modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Flower", b =>
{
b.Navigation("Components");
b.Navigation("Orders");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,57 @@
using FlowerShopContracts.BindingModels;
using FlowerShopContracts.ViewModels;
using FlowerShopDataModels.Models;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
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<FlowerComponent> FlowerComponents { 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,91 @@
using FlowerShopDataModels.Models;
using FlowerShopContracts.ViewModels;
using FlowerShopContracts.BindingModels;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
namespace FlowerShopDatabaseImplement.Models
{
public class Flower : IFlowerModel
{
public int Id { get; set; }
[Required]
public string FlowerName { get; set; } = string.Empty;
[Required]
public double Price { get; set; }
private Dictionary<int, (IComponentModel, int)>? _FlowerComponents = null;
[NotMapped]
public Dictionary<int, (IComponentModel, int)> FlowerComponents
{
get
{
if (_FlowerComponents == null)
{
_FlowerComponents = Components
.ToDictionary(recPC => recPC.ComponentId, recPC =>
(recPC.Component as IComponentModel, recPC.Count));
}
return _FlowerComponents;
}
}
[ForeignKey("FlowerId")]
public virtual List<FlowerComponent> Components { get; set; } = new();
[ForeignKey("FlowerId")]
public virtual List<Order> Orders { get; set; } = new();
public static Flower Create(FlowerShopDatabase context, FlowerBindingModel model)
{
return new Flower()
{
Id = model.Id,
FlowerName = model.FlowerName,
Price = model.Price,
Components = model.FlowerComponents.Select(x => new FlowerComponent
{
Component = context.Components.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(FlowerBindingModel model)
{
FlowerName = model.FlowerName;
Price = model.Price;
}
public FlowerViewModel GetViewModel => new()
{
Id = Id,
FlowerName = FlowerName,
Price = Price,
FlowerComponents = FlowerComponents
};
public void UpdateComponents(FlowerShopDatabase context, FlowerBindingModel model)
{
var FlowerComponents = context.FlowerComponents.Where(rec => rec.FlowerId == model.Id).ToList();
if (FlowerComponents != null && FlowerComponents.Count > 0)
{
context.FlowerComponents.RemoveRange(FlowerComponents.Where(rec
=> !model.FlowerComponents.ContainsKey(rec.ComponentId)));
context.SaveChanges();
foreach (var updateComponent in FlowerComponents)
{
updateComponent.Count = model.FlowerComponents[updateComponent.ComponentId].Item2;
model.FlowerComponents.Remove(updateComponent.ComponentId);
}
context.SaveChanges();
}
var Flower = context.Flowers.First(x => x.Id == Id);
foreach (var pc in model.FlowerComponents)
{
context.FlowerComponents.Add(new FlowerComponent
{
Flower = Flower,
Component = context.Components.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
});
context.SaveChanges();
}
_FlowerComponents = null;
}
}
}

View File

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

View File

@ -0,0 +1,59 @@
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; }
public int FlowerId { get; private set; }
[Required]
public int Count { get; 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; }
public static Order? Create(OrderBindingModel? model)
{
if (model == null)
{
return null;
}
return new Order()
{
Id = model.Id,
FlowerId = model.FlowerId,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement
};
}
public void Update(OrderBindingModel? model)
{
if (model == null)
{
return;
}
Status = model.Status;
DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel => new()
{
Id = Id,
FlowerId = FlowerId,
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement
};
}
}