From bdbdba7f4d84fd24b98bd6594036883ada82aca1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D1=80=D1=8C=D1=8F=20=D0=90=D0=BD=D1=82=D0=BE?= =?UTF-8?q?=D0=BD=D0=BE=D0=B2=D0=B0?= Date: Mon, 22 May 2023 01:27:46 +0400 Subject: [PATCH] ThirdLaba --- Confectionery/Confectionery.sln | 18 +- .../ConfectioneryDatabase.cs | 30 ++++ .../ConfectioneryDatabaseImplement.csproj | 29 +++ .../Implements/IngredientStorage.cs | 88 +++++++++ .../Implements/OrderStorage.cs | 98 ++++++++++ .../Implements/SweetsStorage.cs | 112 ++++++++++++ .../20230313223812_InitialCreate.cs | 125 +++++++++++++ .../ConfectioneryDatabaseModelSnapshot.cs | 169 ++++++++++++++++++ .../Models/Ingredient.cs | 68 +++++++ .../Models/Order.cs | 69 +++++++ .../Models/Sweets.cs | 102 +++++++++++ .../Models/SweetsIngredient.cs | 27 +++ 12 files changed, 929 insertions(+), 6 deletions(-) create mode 100644 Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs create mode 100644 Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabaseImplement.csproj create mode 100644 Confectionery/ConfectioneryDatabaseImplement/Implements/IngredientStorage.cs create mode 100644 Confectionery/ConfectioneryDatabaseImplement/Implements/OrderStorage.cs create mode 100644 Confectionery/ConfectioneryDatabaseImplement/Implements/SweetsStorage.cs create mode 100644 Confectionery/ConfectioneryDatabaseImplement/Migrations/20230313223812_InitialCreate.cs create mode 100644 Confectionery/ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs create mode 100644 Confectionery/ConfectioneryDatabaseImplement/Models/Ingredient.cs create mode 100644 Confectionery/ConfectioneryDatabaseImplement/Models/Order.cs create mode 100644 Confectionery/ConfectioneryDatabaseImplement/Models/Sweets.cs create mode 100644 Confectionery/ConfectioneryDatabaseImplement/Models/SweetsIngredient.cs diff --git a/Confectionery/Confectionery.sln b/Confectionery/Confectionery.sln index 0a8fcea..76db81f 100644 --- a/Confectionery/Confectionery.sln +++ b/Confectionery/Confectionery.sln @@ -3,23 +3,25 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.3.32922.545 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfectioneryView", "ConfectioneryView\ConfectioneryView.csproj", "{9B75FC6A-4403-406F-BD78-70A656A9C38C}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfectioneryView", "ConfectioneryView\ConfectioneryView.csproj", "{9B75FC6A-4403-406F-BD78-70A656A9C38C}" ProjectSection(ProjectDependencies) = postProject {F40D4032-72B5-44F1-9657-940F5931C1C1} = {F40D4032-72B5-44F1-9657-940F5931C1C1} EndProjectSection EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfectioneryDataModels", "ConfectioneryDataModels\ConfectioneryDataModels.csproj", "{AE5AAF6C-4EA0-45F6-A433-50D1A347DD60}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfectioneryDataModels", "ConfectioneryDataModels\ConfectioneryDataModels.csproj", "{AE5AAF6C-4EA0-45F6-A433-50D1A347DD60}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfectioneryContracts", "ConfectioneryContracts\ConfectioneryContracts.csproj", "{138F5333-8A07-47A5-B6FB-5F5170E89D50}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfectioneryContracts", "ConfectioneryContracts\ConfectioneryContracts.csproj", "{138F5333-8A07-47A5-B6FB-5F5170E89D50}" ProjectSection(ProjectDependencies) = postProject {AE5AAF6C-4EA0-45F6-A433-50D1A347DD60} = {AE5AAF6C-4EA0-45F6-A433-50D1A347DD60} EndProjectSection EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfectioneryListImplement", "ConfectioneryListImplement\ConfectioneryListImplement.csproj", "{70FA9740-C8FF-48F1-939A-8B6AAB8CB6FD}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfectioneryListImplement", "ConfectioneryListImplement\ConfectioneryListImplement.csproj", "{70FA9740-C8FF-48F1-939A-8B6AAB8CB6FD}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfectioneryBusinessLogic", "ConfectioneryBusinessLogic\ConfectioneryBusinessLogic.csproj", "{FF4C1B6D-E988-4CE0-AF41-C4539744B9BE}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfectioneryBusinessLogic", "ConfectioneryBusinessLogic\ConfectioneryBusinessLogic.csproj", "{FF4C1B6D-E988-4CE0-AF41-C4539744B9BE}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfectioneryFileImplement", "ConfectioneryFileImplement\ConfectioneryFileImplement.csproj", "{F40D4032-72B5-44F1-9657-940F5931C1C1}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfectioneryFileImplement", "ConfectioneryFileImplement\ConfectioneryFileImplement.csproj", "{F40D4032-72B5-44F1-9657-940F5931C1C1}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfectioneryDatabaseImplement", "ConfectioneryDatabaseImplement\ConfectioneryDatabaseImplement.csproj", "{CDAAEEB5-96BD-4F0E-902D-C98435A6CD02}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -51,6 +53,10 @@ Global {F40D4032-72B5-44F1-9657-940F5931C1C1}.Debug|Any CPU.Build.0 = Debug|Any CPU {F40D4032-72B5-44F1-9657-940F5931C1C1}.Release|Any CPU.ActiveCfg = Release|Any CPU {F40D4032-72B5-44F1-9657-940F5931C1C1}.Release|Any CPU.Build.0 = Release|Any CPU + {CDAAEEB5-96BD-4F0E-902D-C98435A6CD02}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CDAAEEB5-96BD-4F0E-902D-C98435A6CD02}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CDAAEEB5-96BD-4F0E-902D-C98435A6CD02}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CDAAEEB5-96BD-4F0E-902D-C98435A6CD02}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs b/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs new file mode 100644 index 0000000..f9f9d25 --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs @@ -0,0 +1,30 @@ +using ConfectioneryDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryDatabaseImplement +{ + public class ConfectioneryDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-6JVHBUD\SQLEXPRESS;Initial Catalog=SweetsDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + + public virtual DbSet Ingredients { set; get; } + + public virtual DbSet ListSweets { set; get; } + + public virtual DbSet SweetsIngredients { set; get; } + + public virtual DbSet Orders { set; get; } + } +} diff --git a/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabaseImplement.csproj b/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabaseImplement.csproj new file mode 100644 index 0000000..696d68f --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabaseImplement.csproj @@ -0,0 +1,29 @@ + + + + net6.0 + enable + enable + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/Confectionery/ConfectioneryDatabaseImplement/Implements/IngredientStorage.cs b/Confectionery/ConfectioneryDatabaseImplement/Implements/IngredientStorage.cs new file mode 100644 index 0000000..b2bb384 --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Implements/IngredientStorage.cs @@ -0,0 +1,88 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContracts; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryDatabaseImplement.Implements +{ + public class IngredientStorage : IIngredientStorage + { + public List GetFullList() + { + using var context = new ConfectioneryDatabase(); + return context.Ingredients + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(IngredientSearchModel model) + { + if (string.IsNullOrEmpty(model.IngredientName)) + { + return new(); + } + using var context = new ConfectioneryDatabase(); + return context.Ingredients + .Where(x => x.IngredientName.Contains(model.IngredientName)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public IngredientViewModel? GetElement(IngredientSearchModel model) + { + if (string.IsNullOrEmpty(model.IngredientName) && !model.Id.HasValue) + { + return null; + } + using var context = new ConfectioneryDatabase(); + return context.Ingredients + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.IngredientName) && x.IngredientName == model.IngredientName) || + (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public IngredientViewModel? Insert(IngredientBindingModel model) + { + var newIngredient = Ingredient.Create(model); + if (newIngredient == null) + { + return null; + } + using var context = new ConfectioneryDatabase(); + context.Ingredients.Add(newIngredient); + context.SaveChanges(); + return newIngredient.GetViewModel; + } + + public IngredientViewModel? Update(IngredientBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var ingredient = context.Ingredients.FirstOrDefault(x => x.Id == model.Id); + if (ingredient == null) + { + return null; + } + ingredient.Update(model); + context.SaveChanges(); + return ingredient.GetViewModel; + } + + public IngredientViewModel? Delete(IngredientBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var element = context.Ingredients.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Ingredients.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/Confectionery/ConfectioneryDatabaseImplement/Implements/OrderStorage.cs b/Confectionery/ConfectioneryDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..5f4b99c --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,98 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContracts; +using ConfectioneryContracts.ViewModels; +using Microsoft.EntityFrameworkCore; +using ConfectioneryDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var element = context.Orders + .Include(x => x.Sweets) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new ConfectioneryDatabase(); + return context.Orders + .Include(x => x.Sweets) + .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) + ?.GetViewModel; + } + + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new ConfectioneryDatabase(); + return context.Orders + .Include(x => x.Sweets) + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFullList() + { + using var context = new ConfectioneryDatabase(); + return context.Orders + .Include(x => x.Sweets) + .Select(x => x.GetViewModel) + .ToList(); + } + + 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 context.Orders + .Include(x => x.Sweets) + .FirstOrDefault(x => x.Id == newOrder.Id) + ?.GetViewModel; + } + + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var order = context.Orders + .Include(x => x.Sweets) + .FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + context.SaveChanges(); + return order.GetViewModel; + } + } +} diff --git a/Confectionery/ConfectioneryDatabaseImplement/Implements/SweetsStorage.cs b/Confectionery/ConfectioneryDatabaseImplement/Implements/SweetsStorage.cs new file mode 100644 index 0000000..b9e5dfe --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Implements/SweetsStorage.cs @@ -0,0 +1,112 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContracts; +using ConfectioneryContracts.ViewModels; +using Microsoft.EntityFrameworkCore; +using ConfectioneryDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryDatabaseImplement.Implements +{ + public class SweetsStorage : ISweetsStorage + { + public List GetFullList() + { + using var context = new ConfectioneryDatabase(); + return context.ListSweets + .Include(x => x.Ingredients) + .ThenInclude(x => x.Ingredient) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(SweetsSearchModel model) + { + if (string.IsNullOrEmpty(model.SweetsName)) + { + return new(); + } + using var context = new ConfectioneryDatabase(); + return context.ListSweets + .Include(x => x.Ingredients) + .ThenInclude(x => x.Ingredient) + .Where(x => x.SweetsName.Contains(model.SweetsName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public SweetsViewModel? GetElement(SweetsSearchModel model) + { + if (string.IsNullOrEmpty(model.SweetsName) && !model.Id.HasValue) + { + return null; + } + using var context = new ConfectioneryDatabase(); + return context.ListSweets + .Include(x => x.Ingredients) + .ThenInclude(x => x.Ingredient) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.SweetsName) && x.SweetsName == model.SweetsName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public SweetsViewModel? Insert(SweetsBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var newSweets = Sweets.Create(context, model); + if (newSweets == null) + { + return null; + } + context.ListSweets.Add(newSweets); + context.SaveChanges(); + return newSweets.GetViewModel; + } + + public SweetsViewModel? Update(SweetsBindingModel model) + { + using var context = new ConfectioneryDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var sweets = context.ListSweets.FirstOrDefault(rec => rec.Id == model.Id); + if (sweets == null) + { + return null; + } + sweets.Update(model); + context.SaveChanges(); + sweets.UpdateIngredients(context, model); + transaction.Commit(); + return sweets.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public SweetsViewModel? Delete(SweetsBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var element = context.ListSweets + .Include(x => x.Ingredients) + .Include(x => x.Orders) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.ListSweets.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/Confectionery/ConfectioneryDatabaseImplement/Migrations/20230313223812_InitialCreate.cs b/Confectionery/ConfectioneryDatabaseImplement/Migrations/20230313223812_InitialCreate.cs new file mode 100644 index 0000000..9a04075 --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Migrations/20230313223812_InitialCreate.cs @@ -0,0 +1,125 @@ +using Microsoft.EntityFrameworkCore.Migrations; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryDatabaseImplement.Migrations +{ + public partial class InitialCreate : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Ingredients", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + IngredientName = table.Column(type: "nvarchar(max)", nullable: false), + Cost = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Ingredients", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "ListSweets", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + SweetsName = table.Column(type: "nvarchar(max)", nullable: false), + Price = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_ListSweets", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + SweetsId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false), + Sum = table.Column(type: "float", nullable: false), + Status = table.Column(type: "int", nullable: false), + DateCreate = table.Column(type: "datetime2", nullable: false), + DateImplement = table.Column(type: "datetime2", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Orders", x => x.Id); + table.ForeignKey( + name: "FK_Orders_ListSweets_SweetsId", + column: x => x.SweetsId, + principalTable: "ListSweets", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "SweetsIngredients", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + SweetsId = table.Column(type: "int", nullable: false), + IngredientId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_SweetsIngredients", x => x.Id); + table.ForeignKey( + name: "FK_SweetsIngredients_Ingredients_IngredientId", + column: x => x.IngredientId, + principalTable: "Ingredients", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_SweetsIngredients_ListSweets_SweetsId", + column: x => x.SweetsId, + principalTable: "ListSweets", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_Orders_SweetsId", + table: "Orders", + column: "SweetsId"); + + migrationBuilder.CreateIndex( + name: "IX_SweetsIngredients_IngredientId", + table: "SweetsIngredients", + column: "IngredientId"); + + migrationBuilder.CreateIndex( + name: "IX_SweetsIngredients_SweetsId", + table: "SweetsIngredients", + column: "SweetsId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "SweetsIngredients"); + + migrationBuilder.DropTable( + name: "Ingredients"); + + migrationBuilder.DropTable( + name: "ListSweets"); + } + } +} diff --git a/Confectionery/ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs b/Confectionery/ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs new file mode 100644 index 0000000..e94abad --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs @@ -0,0 +1,169 @@ +// +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +#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", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Ingredient", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Cost") + .HasColumnType("float"); + + b.Property("IngredientName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Ingredients"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.Property("SweetsId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("SweetsId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Sweets", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Price") + .HasColumnType("float"); + + b.Property("SweetsName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("ListSweets"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.SweetsIngredient", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("IngredientId") + .HasColumnType("int"); + + b.Property("SweetsId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("IngredientId"); + + b.HasIndex("SweetsId"); + + b.ToTable("SweetsIngredients"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Order", b => + { + b.HasOne("ConfectioneryDatabaseImplement.Models.Sweets", "Sweets") + .WithMany("Orders") + .HasForeignKey("SweetsId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Sweets"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.SweetsIngredient", b => + { + b.HasOne("ConfectioneryDatabaseImplement.Models.Ingredient", "Ingredient") + .WithMany("ConfectioneryIngredients") + .HasForeignKey("IngredientId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ConfectioneryDatabaseImplement.Models.Sweets", "Sweets") + .WithMany("Ingredients") + .HasForeignKey("SweetsId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Ingredient"); + + b.Navigation("Sweets"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Ingredient", b => + { + b.Navigation("ConfectioneryIngredients"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Sweets", b => + { + b.Navigation("Ingredients"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Confectionery/ConfectioneryDatabaseImplement/Models/Ingredient.cs b/Confectionery/ConfectioneryDatabaseImplement/Models/Ingredient.cs new file mode 100644 index 0000000..859ecb9 --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Models/Ingredient.cs @@ -0,0 +1,68 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Models; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryDatabaseImplement.Models +{ + public class Ingredient : IIngredientModel + { + public int Id { get; private set; } + + [Required] + public string IngredientName { get; private set; } = string.Empty; + + [Required] + public double Cost { get; set; } + + [ForeignKey("IngredientId")] + public virtual List ConfectioneryIngredients { get; set; } = new(); + + public static Ingredient? Create(IngredientBindingModel model) + { + if (model == null) + { + return null; + } + return new Ingredient() + { + Id = model.Id, + IngredientName = model.IngredientName, + Cost = model.Cost + }; + } + + public static Ingredient Create(IngredientViewModel model) + { + return new Ingredient + { + Id = model.Id, + IngredientName = model.IngredientName, + Cost = model.Cost + }; + } + + public void Update(IngredientBindingModel model) + { + if (model == null) + { + return; + } + IngredientName = model.IngredientName; + Cost = model.Cost; + } + + public IngredientViewModel GetViewModel => new() + { + Id = Id, + IngredientName = IngredientName, + Cost = Cost + }; + } +} diff --git a/Confectionery/ConfectioneryDatabaseImplement/Models/Order.cs b/Confectionery/ConfectioneryDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..13f217c --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Models/Order.cs @@ -0,0 +1,69 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Enums; +using ConfectioneryDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryDatabaseImplement.Models +{ + public class Order : IOrderModel + { + [Required] + public int SweetsId { get; set; } + [Required] + public int Count { get; set; } + [Required] + public double Sum { get; set; } + [Required] + public OrderStatus Status { get; set; } + [Required] + public DateTime DateCreate { get; set; } + public DateTime? DateImplement { get; set; } + public int Id { get; set; } + public Sweets Sweets { get; set; } + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + SweetsId = model.SweetsId, + 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() + { + SweetsId = SweetsId, + Count = Count, + Sum = Sum, + DateCreate = DateCreate, + DateImplement = DateImplement, + Id = Id, + Status = Status, + SweetsName = Sweets.SweetsName, + }; + } +} diff --git a/Confectionery/ConfectioneryDatabaseImplement/Models/Sweets.cs b/Confectionery/ConfectioneryDatabaseImplement/Models/Sweets.cs new file mode 100644 index 0000000..03e1d24 --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Models/Sweets.cs @@ -0,0 +1,102 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryDatabaseImplement.Models +{ + public class Sweets : ISweetsModel + { + public int Id { get; set; } + + [Required] + public string SweetsName { get; set; } = string.Empty; + + [Required] + public double Price { get; set; } + + private Dictionary? _sweetsIngredients = null; + + [NotMapped] + public Dictionary SweetsIngredients + { + get + { + if (_sweetsIngredients == null) + { + _sweetsIngredients = Ingredients + .ToDictionary(recPC => recPC.IngredientId, recPC => (recPC.Ingredient as IIngredientModel, recPC.Count)); + } + return _sweetsIngredients; + } + } + + [ForeignKey("SweetsId")] + public virtual List Ingredients { get; set; } = new(); + [ForeignKey("SweetsId")] + public virtual List Orders { get; set; } = new(); + + public static Sweets Create(ConfectioneryDatabase context, SweetsBindingModel model) + { + return new Sweets() + { + Id = model.Id, + SweetsName = model.SweetsName, + Price = model.Price, + Ingredients = model.SweetsIngredients.Select(x => new SweetsIngredient + { + Ingredient = context.Ingredients.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + + public void Update(SweetsBindingModel model) + { + SweetsName = model.SweetsName; + Price = model.Price; + } + + public SweetsViewModel GetViewModel => new() + { + Id = Id, + SweetsName = SweetsName, + Price = Price, + SweetsIngredients = SweetsIngredients + }; + + public void UpdateIngredients(ConfectioneryDatabase context, SweetsBindingModel model) + { + var sweetsIngredients = context.SweetsIngredients.Where(rec => rec.SweetsId == model.Id).ToList(); + if (sweetsIngredients != null && sweetsIngredients.Count > 0) + { + context.SweetsIngredients.RemoveRange(sweetsIngredients.Where(rec => !model.SweetsIngredients.ContainsKey(rec.IngredientId))); + context.SaveChanges(); + foreach (var updateIngredient in sweetsIngredients) + { + updateIngredient.Count = model.SweetsIngredients[updateIngredient.IngredientId].Item2; + model.SweetsIngredients.Remove(updateIngredient.IngredientId); + } + context.SaveChanges(); + } + var sweets = context.ListSweets.First(x => x.Id == Id); + foreach (var si in model.SweetsIngredients) + { + context.SweetsIngredients.Add(new SweetsIngredient + { + Sweets = sweets, + Ingredient = context.Ingredients.First(x => x.Id == si.Key), + Count = si.Value.Item2 + }); + context.SaveChanges(); + } + _sweetsIngredients = null; + } + } +} diff --git a/Confectionery/ConfectioneryDatabaseImplement/Models/SweetsIngredient.cs b/Confectionery/ConfectioneryDatabaseImplement/Models/SweetsIngredient.cs new file mode 100644 index 0000000..7e73c61 --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Models/SweetsIngredient.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryDatabaseImplement.Models +{ + public class SweetsIngredient + { + public int Id { get; set; } + + [Required] + public int SweetsId { get; set; } + + [Required] + public int IngredientId { get; set; } + + [Required] + public int Count { get; set; } + + public virtual Ingredient Ingredient { get; set; } = new(); + + public virtual Sweets Sweets { get; set; } = new(); + } +} -- 2.25.1