From 1d0a220011c22329a3d6e690e979705889babe15 Mon Sep 17 00:00:00 2001 From: gg12 darfren Date: Mon, 4 Mar 2024 19:46:39 +0400 Subject: [PATCH 1/5] =?UTF-8?q?=D0=9D=D0=B0=D0=B2=D0=B5=D1=80=D0=BD=D0=BE?= =?UTF-8?q?=D0=B5=20=D0=BE=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- IceCreamShop/ClassLibrary1/Class1.cs | 7 + .../ClassLibrary1/ClassLibrary1.csproj | 9 + IceCreamShop/IceCreamShop.sln | 12 +- IceCreamShop/IceCreamShop/IceCreamShop.csproj | 5 + IceCreamShop/IceCreamShop/Program.cs | 2 +- .../IceCreamShopDataBase.cs | 29 +++ .../IceCreamShopDatabaseImplement.csproj | 23 +++ .../Implements/ComponentStorage.cs | 91 ++++++++++ .../Implements/IceCreamStorage.cs | 110 ++++++++++++ .../Implements/OrderStorage.cs | 99 ++++++++++ .../20240304143043_InitialCreate.Designer.cs | 169 ++++++++++++++++++ .../20240304143043_InitialCreate.cs | 125 +++++++++++++ .../IceCreamShopDataBaseModelSnapshot.cs | 166 +++++++++++++++++ .../Models/Component.cs | 63 +++++++ .../Models/IceCream.cs | 103 +++++++++++ .../Models/IceCreamComponent.cs | 22 +++ .../Models/Order.cs | 69 +++++++ 17 files changed, 1102 insertions(+), 2 deletions(-) create mode 100644 IceCreamShop/ClassLibrary1/Class1.cs create mode 100644 IceCreamShop/ClassLibrary1/ClassLibrary1.csproj create mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDataBase.cs create mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDatabaseImplement.csproj create mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Implements/ComponentStorage.cs create mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Implements/IceCreamStorage.cs create mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Implements/OrderStorage.cs create mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240304143043_InitialCreate.Designer.cs create mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240304143043_InitialCreate.cs create mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Migrations/IceCreamShopDataBaseModelSnapshot.cs create mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Models/Component.cs create mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Models/IceCream.cs create mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Models/IceCreamComponent.cs create mode 100644 IceCreamShop/IceCreamShopDatabaseImplement/Models/Order.cs diff --git a/IceCreamShop/ClassLibrary1/Class1.cs b/IceCreamShop/ClassLibrary1/Class1.cs new file mode 100644 index 0000000..ca3cd76 --- /dev/null +++ b/IceCreamShop/ClassLibrary1/Class1.cs @@ -0,0 +1,7 @@ +namespace ClassLibrary1 +{ + public class Class1 + { + + } +} diff --git a/IceCreamShop/ClassLibrary1/ClassLibrary1.csproj b/IceCreamShop/ClassLibrary1/ClassLibrary1.csproj new file mode 100644 index 0000000..fa71b7a --- /dev/null +++ b/IceCreamShop/ClassLibrary1/ClassLibrary1.csproj @@ -0,0 +1,9 @@ + + + + net8.0 + enable + enable + + + diff --git a/IceCreamShop/IceCreamShop.sln b/IceCreamShop/IceCreamShop.sln index fee3e3e..0bb6839 100644 --- a/IceCreamShop/IceCreamShop.sln +++ b/IceCreamShop/IceCreamShop.sln @@ -13,7 +13,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IceCreamShopBusinessLogic", EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IceCreamShopListImplement", "IceCreamShopListImplement\IceCreamShopListImplement.csproj", "{566BA4F5-7316-4747-A745-FCA934EBA9E0}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IceCreamShopFileImplement", "IceCreamShopFileImplement\IceCreamShopFileImplement.csproj", "{A689B62C-67DF-47D8-B265-6C453D1055DA}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IceCreamShopFileImplement", "IceCreamShopFileImplement\IceCreamShopFileImplement.csproj", "{A689B62C-67DF-47D8-B265-6C453D1055DA}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IceCreamShopDatabaseImplement", "IceCreamShopDatabaseImplement\IceCreamShopDatabaseImplement.csproj", "{CAF716C0-0F6F-43C2-B6F6-903AA9D96FC5}" + ProjectSection(ProjectDependencies) = postProject + {0979632B-BB70-441E-9109-C52DBA5A85B2} = {0979632B-BB70-441E-9109-C52DBA5A85B2} + {8D344A7A-951D-496B-BC8A-DF18A06B1DF0} = {8D344A7A-951D-496B-BC8A-DF18A06B1DF0} + EndProjectSection EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -45,6 +51,10 @@ Global {A689B62C-67DF-47D8-B265-6C453D1055DA}.Debug|Any CPU.Build.0 = Debug|Any CPU {A689B62C-67DF-47D8-B265-6C453D1055DA}.Release|Any CPU.ActiveCfg = Release|Any CPU {A689B62C-67DF-47D8-B265-6C453D1055DA}.Release|Any CPU.Build.0 = Release|Any CPU + {CAF716C0-0F6F-43C2-B6F6-903AA9D96FC5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CAF716C0-0F6F-43C2-B6F6-903AA9D96FC5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CAF716C0-0F6F-43C2-B6F6-903AA9D96FC5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CAF716C0-0F6F-43C2-B6F6-903AA9D96FC5}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/IceCreamShop/IceCreamShop/IceCreamShop.csproj b/IceCreamShop/IceCreamShop/IceCreamShop.csproj index 5aaeb5f..0ca01d7 100644 --- a/IceCreamShop/IceCreamShop/IceCreamShop.csproj +++ b/IceCreamShop/IceCreamShop/IceCreamShop.csproj @@ -9,6 +9,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -16,6 +20,7 @@ + diff --git a/IceCreamShop/IceCreamShop/Program.cs b/IceCreamShop/IceCreamShop/Program.cs index 27b419a..5469e49 100644 --- a/IceCreamShop/IceCreamShop/Program.cs +++ b/IceCreamShop/IceCreamShop/Program.cs @@ -1,7 +1,7 @@ using IceCreamShopBusinessLogic.BusinessLogic; using IceCreamShopContracts.BusinessLogicsContracts; using IceCreamShopContracts.StoragesContracts; -using IceCreamShopFileImplement.Implements; +using IceCreamShopDatabaseImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDataBase.cs b/IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDataBase.cs new file mode 100644 index 0000000..6b30208 --- /dev/null +++ b/IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDataBase.cs @@ -0,0 +1,29 @@ +using IceCreamShopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopDatabaseImplement +{ + public class IceCreamShopDataBase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder +optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=IceCreamShopDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Components { set; get; } + public virtual DbSet IceCreams { set; get; } + public virtual DbSet IceCreamComponents { set; get; } + public virtual DbSet Orders { set; get; } + + } +} diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDatabaseImplement.csproj b/IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDatabaseImplement.csproj new file mode 100644 index 0000000..ed37272 --- /dev/null +++ b/IceCreamShop/IceCreamShopDatabaseImplement/IceCreamShopDatabaseImplement.csproj @@ -0,0 +1,23 @@ + + + + net8.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Implements/ComponentStorage.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..a750bd2 --- /dev/null +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,91 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.SearchModels; +using IceCreamShopContracts.StoragesContracts; +using IceCreamShopContracts.ViewModels; +using IceCreamShopDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new IceCreamShopDataBase(); + return context.Components + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(ComponentSearchModel + model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + using var context = new IceCreamShopDataBase(); + 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 IceCreamShopDataBase(); + 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 IceCreamShopDataBase(); + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new IceCreamShopDataBase(); + 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 IceCreamShopDataBase(); + var element = context.Components.FirstOrDefault(rec => rec.Id == + model.Id); + if (element != null) + { + context.Components.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } + + +} diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Implements/IceCreamStorage.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Implements/IceCreamStorage.cs new file mode 100644 index 0000000..904cc18 --- /dev/null +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Implements/IceCreamStorage.cs @@ -0,0 +1,110 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.SearchModels; +using IceCreamShopContracts.StoragesContracts; +using IceCreamShopContracts.ViewModels; +using IceCreamShopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopDatabaseImplement.Implements +{ + public class IceCreamStorage : IIceCreamStorage + { + public List GetFullList() + { + using var context = new IceCreamShopDataBase(); + return context.IceCreams + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(IceCreamSearchModel model) + { + if (string.IsNullOrEmpty(model.IceCreamName)) + { + return new(); + } + using var context = new IceCreamShopDataBase(); + return context.IceCreams.Include(x => x.Components) + .ThenInclude(x => x.Component) + .Where(x => x.IceCreamName.Contains(model.IceCreamName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public IceCreamViewModel? GetElement(IceCreamSearchModel model) + { + if (string.IsNullOrEmpty(model.IceCreamName) && + !model.Id.HasValue) + { + return null; + } + using var context = new IceCreamShopDataBase(); + return context.IceCreams + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.IceCreamName) && + x.IceCreamName == model.IceCreamName) || + (model.Id.HasValue && x.Id == + model.Id)) + ?.GetViewModel; + } + public IceCreamViewModel? Insert(IceCreamBindingModel model) + { + using var context = new IceCreamShopDataBase(); + var newProduct = IceCream.Create(context, model); + if (newProduct == null) + { + return null; + } + context.IceCreams.Add(newProduct); + context.SaveChanges(); + return newProduct.GetViewModel; + } + public IceCreamViewModel? Update(IceCreamBindingModel model) + { + using var context = new IceCreamShopDataBase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var iceCream = context.IceCreams.FirstOrDefault(rec => + rec.Id == model.Id); + if (iceCream == null) + { + return null; + } + iceCream.Update(model); + context.SaveChanges(); + iceCream.UpdateComponents(context, model); + transaction.Commit(); + return iceCream.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public IceCreamViewModel? Delete(IceCreamBindingModel model) + { + using var context = new IceCreamShopDataBase(); + var element = context.IceCreams + .Include(x => x.Components) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.IceCreams.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} + diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Implements/OrderStorage.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..bb3371a --- /dev/null +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,99 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.SearchModels; +using IceCreamShopContracts.StoragesContracts; +using IceCreamShopContracts.ViewModels; +using IceCreamShopDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public List GetFullList() + { + using var context = new IceCreamShopDataBase(); + return context.Orders + .Select(x => AccessIceCreamStorage(x.GetViewModel)) + .ToList(); + } + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new IceCreamShopDataBase(); + return context.Orders + .Where(x => x.Id == model.Id) + .Select(x => AccessIceCreamStorage(x.GetViewModel)) + .ToList(); + } + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new IceCreamShopDataBase(); + return AccessIceCreamStorage(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 IceCreamShopDataBase(); + context.Orders.Add(newOrder); + context.SaveChanges(); + return AccessIceCreamStorage(newOrder.GetViewModel); + } + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new IceCreamShopDataBase(); + var order = context.Orders.FirstOrDefault(x => x.Id == + model.Id); + if (order == null) + { + return null; + } + order.Update(model); + context.SaveChanges(); + return AccessIceCreamStorage(order.GetViewModel); + } + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new IceCreamShopDataBase(); + var element = context.Orders.FirstOrDefault(rec => rec.Id == + model.Id); + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + return AccessIceCreamStorage(element.GetViewModel); + } + return null; + } + + public static OrderViewModel AccessIceCreamStorage(OrderViewModel model) + { + if (model == null) + return null; + using var context = new IceCreamShopDataBase(); + foreach (var iceCream in context.IceCreams) + { + if (iceCream.Id == model.IceCreamId) + { + model.IceCreamName = iceCream.IceCreamName; + break; + } + } + return model; + } + } +} diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240304143043_InitialCreate.Designer.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240304143043_InitialCreate.Designer.cs new file mode 100644 index 0000000..655fb59 --- /dev/null +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240304143043_InitialCreate.Designer.cs @@ -0,0 +1,169 @@ +// +using System; +using IceCreamShopDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace IceCreamShopDatabaseImplement.Migrations +{ + [DbContext(typeof(IceCreamShopDataBase))] + [Migration("20240304143043_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.2") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCream", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("IceCreamName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("IceCreams"); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCreamComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("IceCreamId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("IceCreamId"); + + b.ToTable("IceCreamComponents"); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.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("IceCreamId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("IceCreamId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCreamComponent", b => + { + b.HasOne("IceCreamShopDatabaseImplement.Models.Component", "Component") + .WithMany("IceCreamComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("IceCreamShopDatabaseImplement.Models.IceCream", "IceCream") + .WithMany("Components") + .HasForeignKey("IceCreamId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("IceCream"); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Order", b => + { + b.HasOne("IceCreamShopDatabaseImplement.Models.IceCream", null) + .WithMany("Orders") + .HasForeignKey("IceCreamId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Component", b => + { + b.Navigation("IceCreamComponents"); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCream", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240304143043_InitialCreate.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240304143043_InitialCreate.cs new file mode 100644 index 0000000..ae61479 --- /dev/null +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240304143043_InitialCreate.cs @@ -0,0 +1,125 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace IceCreamShopDatabaseImplement.Migrations +{ + /// + public partial class InitialCreate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Components", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ComponentName = table.Column(type: "nvarchar(max)", nullable: false), + Cost = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Components", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "IceCreams", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + IceCreamName = table.Column(type: "nvarchar(max)", nullable: false), + Price = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_IceCreams", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "IceCreamComponents", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + IceCreamId = table.Column(type: "int", nullable: false), + ComponentId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_IceCreamComponents", x => x.Id); + table.ForeignKey( + name: "FK_IceCreamComponents_Components_ComponentId", + column: x => x.ComponentId, + principalTable: "Components", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_IceCreamComponents_IceCreams_IceCreamId", + column: x => x.IceCreamId, + principalTable: "IceCreams", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + 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), + IceCreamId = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Orders", x => x.Id); + table.ForeignKey( + name: "FK_Orders_IceCreams_IceCreamId", + column: x => x.IceCreamId, + principalTable: "IceCreams", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_IceCreamComponents_ComponentId", + table: "IceCreamComponents", + column: "ComponentId"); + + migrationBuilder.CreateIndex( + name: "IX_IceCreamComponents_IceCreamId", + table: "IceCreamComponents", + column: "IceCreamId"); + + migrationBuilder.CreateIndex( + name: "IX_Orders_IceCreamId", + table: "Orders", + column: "IceCreamId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "IceCreamComponents"); + + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "Components"); + + migrationBuilder.DropTable( + name: "IceCreams"); + } + } +} diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/IceCreamShopDataBaseModelSnapshot.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/IceCreamShopDataBaseModelSnapshot.cs new file mode 100644 index 0000000..447b981 --- /dev/null +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/IceCreamShopDataBaseModelSnapshot.cs @@ -0,0 +1,166 @@ +// +using System; +using IceCreamShopDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace IceCreamShopDatabaseImplement.Migrations +{ + [DbContext(typeof(IceCreamShopDataBase))] + partial class IceCreamShopDataBaseModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.2") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCream", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("IceCreamName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("IceCreams"); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCreamComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("IceCreamId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("IceCreamId"); + + b.ToTable("IceCreamComponents"); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.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("IceCreamId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("IceCreamId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCreamComponent", b => + { + b.HasOne("IceCreamShopDatabaseImplement.Models.Component", "Component") + .WithMany("IceCreamComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("IceCreamShopDatabaseImplement.Models.IceCream", "IceCream") + .WithMany("Components") + .HasForeignKey("IceCreamId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("IceCream"); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Order", b => + { + b.HasOne("IceCreamShopDatabaseImplement.Models.IceCream", null) + .WithMany("Orders") + .HasForeignKey("IceCreamId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Component", b => + { + b.Navigation("IceCreamComponents"); + }); + + modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCream", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Models/Component.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Models/Component.cs new file mode 100644 index 0000000..90a7bad --- /dev/null +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Models/Component.cs @@ -0,0 +1,63 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.ViewModels; +using IceCreamShopDataModels.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 IceCreamShopDatabaseImplement.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 IceCreamComponents { 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 + }; + } +} + diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Models/IceCream.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Models/IceCream.cs new file mode 100644 index 0000000..f2f42c6 --- /dev/null +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Models/IceCream.cs @@ -0,0 +1,103 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.ViewModels; +using IceCreamShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopDatabaseImplement.Models +{ + public class IceCream : IIceCreamModel + { + public int Id { get; set; } + [Required] + public string IceCreamName { get; set; } = string.Empty; + [Required] + public double Price { get; set; } + private Dictionary? _iceCreamComponents = + null; + [NotMapped] + public Dictionary IceCreamComponents + { + get + { + if (_iceCreamComponents == null) + { + _iceCreamComponents = Components + .ToDictionary(recPC => recPC.ComponentId, recPC => + (recPC.Component as IComponentModel, recPC.Count)); + } + return _iceCreamComponents; + } + } + [ForeignKey("IceCreamId")] + public virtual List Components { get; set; } = new(); + [ForeignKey("IceCreamId")] + public virtual List Orders { get; set; } = new(); + public static IceCream Create(IceCreamShopDataBase context, + IceCreamBindingModel model) + { + return new IceCream() + { + Id = model.Id, + IceCreamName = model.IceCreamName, + Price = model.Price, + Components = model.IceCreamComponents.Select(x => new + IceCreamComponent + { + Component = context.Components.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(IceCreamBindingModel model) + { + IceCreamName = model.IceCreamName; + Price = model.Price; + } + public IceCreamViewModel GetViewModel => new() + { + Id = Id, + IceCreamName = IceCreamName, + Price = Price, + IceCreamComponents = IceCreamComponents + }; + public void UpdateComponents(IceCreamShopDataBase context, + IceCreamBindingModel model) + { + var iceCreamComponents = context.IceCreamComponents.Where(rec => + rec.IceCreamId == model.Id).ToList(); + if (iceCreamComponents != null && IceCreamComponents.Count > 0) + { + context.IceCreamComponents.RemoveRange(iceCreamComponents.Where(rec +=> !model.IceCreamComponents.ContainsKey(rec.ComponentId))); + + context.SaveChanges(); + foreach (var updateComponent in iceCreamComponents) + { + updateComponent.Count = + model.IceCreamComponents[updateComponent.ComponentId].Item2; + model.IceCreamComponents.Remove(updateComponent.ComponentId); + } + context.SaveChanges(); + } + var iceCream = context.IceCreams.First(x => x.Id == Id); + foreach (var pc in model.IceCreamComponents) + { + context.IceCreamComponents.Add(new IceCreamComponent + { + IceCream = iceCream, + Component = context.Components.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _iceCreamComponents = null; + } + } +} + diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Models/IceCreamComponent.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Models/IceCreamComponent.cs new file mode 100644 index 0000000..a8ff994 --- /dev/null +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Models/IceCreamComponent.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopDatabaseImplement.Models +{ + public class IceCreamComponent + { + public int Id { get; set; } + [Required] + public int IceCreamId { get; set; } + [Required] + public int ComponentId { get; set; } + [Required] + public int Count { get; set; } + public virtual Component Component { get; set; } = new(); + public virtual IceCream IceCream { get; set; } = new(); + } +} diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Models/Order.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..a80e766 --- /dev/null +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Models/Order.cs @@ -0,0 +1,69 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.ViewModels; +using IceCreamShopDataModels.Enums; +using IceCreamShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopDatabaseImplement.Models +{ + public class Order : IOrderModel + { + 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 IceCreamId { 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, + IceCreamId = model.IceCreamId, + }; + } + + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + Status = model.Status; + DateImplement = model.DateImplement; + } + + public OrderViewModel GetViewModel => new() + { + IceCreamId = IceCreamId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + Id = Id, + }; + + } +} -- 2.25.1 From a0165f3c17a0769833da4cdfc73d7d83c6509a34 Mon Sep 17 00:00:00 2001 From: gg12 darfren Date: Tue, 5 Mar 2024 12:45:28 +0400 Subject: [PATCH 2/5] =?UTF-8?q?=D0=A1=D0=B4=D0=B0=D0=BD=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- IceCreamShop/IceCreamShop.sln | 5 ++++- .../Implements/IceCreamStorage.cs | 8 ++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/IceCreamShop/IceCreamShop.sln b/IceCreamShop/IceCreamShop.sln index 0bb6839..e0b065d 100644 --- a/IceCreamShop/IceCreamShop.sln +++ b/IceCreamShop/IceCreamShop.sln @@ -4,6 +4,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00 VisualStudioVersion = 17.8.34309.116 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IceCreamShop", "IceCreamShop\IceCreamShop.csproj", "{CD111C92-FD71-4F7E-A7E7-03C273B634E6}" + ProjectSection(ProjectDependencies) = postProject + {CAF716C0-0F6F-43C2-B6F6-903AA9D96FC5} = {CAF716C0-0F6F-43C2-B6F6-903AA9D96FC5} + EndProjectSection EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IceCreamShopDataModels", "IceCreamShopDataModels\IceCreamShopDataModels.csproj", "{0979632B-BB70-441E-9109-C52DBA5A85B2}" EndProject @@ -15,7 +18,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IceCreamShopListImplement", EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IceCreamShopFileImplement", "IceCreamShopFileImplement\IceCreamShopFileImplement.csproj", "{A689B62C-67DF-47D8-B265-6C453D1055DA}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IceCreamShopDatabaseImplement", "IceCreamShopDatabaseImplement\IceCreamShopDatabaseImplement.csproj", "{CAF716C0-0F6F-43C2-B6F6-903AA9D96FC5}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IceCreamShopDatabaseImplement", "IceCreamShopDatabaseImplement\IceCreamShopDatabaseImplement.csproj", "{CAF716C0-0F6F-43C2-B6F6-903AA9D96FC5}" ProjectSection(ProjectDependencies) = postProject {0979632B-BB70-441E-9109-C52DBA5A85B2} = {0979632B-BB70-441E-9109-C52DBA5A85B2} {8D344A7A-951D-496B-BC8A-DF18A06B1DF0} = {8D344A7A-951D-496B-BC8A-DF18A06B1DF0} diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Implements/IceCreamStorage.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Implements/IceCreamStorage.cs index 904cc18..ce66090 100644 --- a/IceCreamShop/IceCreamShopDatabaseImplement/Implements/IceCreamStorage.cs +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Implements/IceCreamStorage.cs @@ -58,14 +58,14 @@ namespace IceCreamShopDatabaseImplement.Implements public IceCreamViewModel? Insert(IceCreamBindingModel model) { using var context = new IceCreamShopDataBase(); - var newProduct = IceCream.Create(context, model); - if (newProduct == null) + var newIceCream = IceCream.Create(context, model); + if (newIceCream == null) { return null; } - context.IceCreams.Add(newProduct); + context.IceCreams.Add(newIceCream); context.SaveChanges(); - return newProduct.GetViewModel; + return newIceCream.GetViewModel; } public IceCreamViewModel? Update(IceCreamBindingModel model) { -- 2.25.1 From 0ef1212a8ad7f7f809a38c4a831a2d66aae17115 Mon Sep 17 00:00:00 2001 From: gg12 darfren Date: Tue, 5 Mar 2024 13:14:25 +0400 Subject: [PATCH 3/5] =?UTF-8?q?=D0=AF=20=D0=BD=D0=B5=20=D0=B7=D0=BD=D0=B0?= =?UTF-8?q?=D1=8E=20=D0=BA=D0=B0=D0=BA=20=D1=8D=D1=82=D0=BE=20=D0=BF=D1=80?= =?UTF-8?q?=D0=BE=D0=B8=D0=B7=D0=BE=D1=88=D0=BB=D0=BE=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- IceCreamShop/ClassLibrary1/Class1.cs | 7 ------- IceCreamShop/ClassLibrary1/ClassLibrary1.csproj | 9 --------- 2 files changed, 16 deletions(-) delete mode 100644 IceCreamShop/ClassLibrary1/Class1.cs delete mode 100644 IceCreamShop/ClassLibrary1/ClassLibrary1.csproj diff --git a/IceCreamShop/ClassLibrary1/Class1.cs b/IceCreamShop/ClassLibrary1/Class1.cs deleted file mode 100644 index ca3cd76..0000000 --- a/IceCreamShop/ClassLibrary1/Class1.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace ClassLibrary1 -{ - public class Class1 - { - - } -} diff --git a/IceCreamShop/ClassLibrary1/ClassLibrary1.csproj b/IceCreamShop/ClassLibrary1/ClassLibrary1.csproj deleted file mode 100644 index fa71b7a..0000000 --- a/IceCreamShop/ClassLibrary1/ClassLibrary1.csproj +++ /dev/null @@ -1,9 +0,0 @@ - - - - net8.0 - enable - enable - - - -- 2.25.1 From 4badbb156984517ea6e4bd87fff99d0ac0b1994a Mon Sep 17 00:00:00 2001 From: gg12 darfren Date: Sun, 17 Mar 2024 11:13:42 +0400 Subject: [PATCH 4/5] One more LINQ --- .../Implements/OrderStorage.cs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Implements/OrderStorage.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Implements/OrderStorage.cs index bb3371a..8a23f6d 100644 --- a/IceCreamShop/IceCreamShopDatabaseImplement/Implements/OrderStorage.cs +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Implements/OrderStorage.cs @@ -85,14 +85,10 @@ namespace IceCreamShopDatabaseImplement.Implements if (model == null) return null; using var context = new IceCreamShopDataBase(); - foreach (var iceCream in context.IceCreams) - { - if (iceCream.Id == model.IceCreamId) - { - model.IceCreamName = iceCream.IceCreamName; - break; - } - } + var iceCream = context.IceCreams.FirstOrDefault(x => (x.Id == model.IceCreamId)); + if (iceCream == null) + return model; + model.IceCreamName = iceCream.IceCreamName; return model; } } -- 2.25.1 From 7eb4e5cfb039cb8c6727c09478fd0a5ead59524b Mon Sep 17 00:00:00 2001 From: gg12 darfren Date: Mon, 18 Mar 2024 17:16:29 +0400 Subject: [PATCH 5/5] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D1=8F=D1=8E=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B8=20=D0=BC?= =?UTF-8?q?=D0=BE=D0=BB=D0=BE=D0=B4=D0=BE=D1=81=D1=82=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/OrderStorage.cs | 31 ++++++------------- ... 20240318125446_InitialCreate.Designer.cs} | 6 ++-- ...ate.cs => 20240318125446_InitialCreate.cs} | 0 .../IceCreamShopDataBaseModelSnapshot.cs | 4 ++- .../Models/Order.cs | 6 ++-- 5 files changed, 21 insertions(+), 26 deletions(-) rename IceCreamShop/IceCreamShopDatabaseImplement/Migrations/{20240304143043_InitialCreate.Designer.cs => 20240318125446_InitialCreate.Designer.cs} (97%) rename IceCreamShop/IceCreamShopDatabaseImplement/Migrations/{20240304143043_InitialCreate.cs => 20240318125446_InitialCreate.cs} (100%) diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Implements/OrderStorage.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Implements/OrderStorage.cs index 8a23f6d..450324f 100644 --- a/IceCreamShop/IceCreamShopDatabaseImplement/Implements/OrderStorage.cs +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Implements/OrderStorage.cs @@ -3,6 +3,7 @@ using IceCreamShopContracts.SearchModels; using IceCreamShopContracts.StoragesContracts; using IceCreamShopContracts.ViewModels; using IceCreamShopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; @@ -16,8 +17,8 @@ namespace IceCreamShopDatabaseImplement.Implements public List GetFullList() { using var context = new IceCreamShopDataBase(); - return context.Orders - .Select(x => AccessIceCreamStorage(x.GetViewModel)) + return context.Orders.Include(x => x.IceCream) + .Select(x => x.GetViewModel) .ToList(); } public List GetFilteredList(OrderSearchModel model) @@ -29,7 +30,7 @@ namespace IceCreamShopDatabaseImplement.Implements using var context = new IceCreamShopDataBase(); return context.Orders .Where(x => x.Id == model.Id) - .Select(x => AccessIceCreamStorage(x.GetViewModel)) + .Select(x => x.GetViewModel) .ToList(); } public OrderViewModel? GetElement(OrderSearchModel model) @@ -39,19 +40,19 @@ namespace IceCreamShopDatabaseImplement.Implements return null; } using var context = new IceCreamShopDataBase(); - return AccessIceCreamStorage(context.Orders.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel); + return context.Orders.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; } public OrderViewModel? Insert(OrderBindingModel model) { - var newOrder = Order.Create(model); + using var context = new IceCreamShopDataBase(); + var newOrder = Order.Create(context, model); if (newOrder == null) { return null; } - using var context = new IceCreamShopDataBase(); context.Orders.Add(newOrder); context.SaveChanges(); - return AccessIceCreamStorage(newOrder.GetViewModel); + return newOrder.GetViewModel; } public OrderViewModel? Update(OrderBindingModel model) { @@ -64,7 +65,7 @@ namespace IceCreamShopDatabaseImplement.Implements } order.Update(model); context.SaveChanges(); - return AccessIceCreamStorage(order.GetViewModel); + return order.GetViewModel; } public OrderViewModel? Delete(OrderBindingModel model) { @@ -75,21 +76,9 @@ namespace IceCreamShopDatabaseImplement.Implements { context.Orders.Remove(element); context.SaveChanges(); - return AccessIceCreamStorage(element.GetViewModel); + return element.GetViewModel; } return null; } - - public static OrderViewModel AccessIceCreamStorage(OrderViewModel model) - { - if (model == null) - return null; - using var context = new IceCreamShopDataBase(); - var iceCream = context.IceCreams.FirstOrDefault(x => (x.Id == model.IceCreamId)); - if (iceCream == null) - return model; - model.IceCreamName = iceCream.IceCreamName; - return model; - } } } diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240304143043_InitialCreate.Designer.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240318125446_InitialCreate.Designer.cs similarity index 97% rename from IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240304143043_InitialCreate.Designer.cs rename to IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240318125446_InitialCreate.Designer.cs index 655fb59..38becf5 100644 --- a/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240304143043_InitialCreate.Designer.cs +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240318125446_InitialCreate.Designer.cs @@ -12,7 +12,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace IceCreamShopDatabaseImplement.Migrations { [DbContext(typeof(IceCreamShopDataBase))] - [Migration("20240304143043_InitialCreate")] + [Migration("20240318125446_InitialCreate")] partial class InitialCreate { /// @@ -145,11 +145,13 @@ namespace IceCreamShopDatabaseImplement.Migrations modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Order", b => { - b.HasOne("IceCreamShopDatabaseImplement.Models.IceCream", null) + b.HasOne("IceCreamShopDatabaseImplement.Models.IceCream", "IceCream") .WithMany("Orders") .HasForeignKey("IceCreamId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); + + b.Navigation("IceCream"); }); modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Component", b => diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240304143043_InitialCreate.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240318125446_InitialCreate.cs similarity index 100% rename from IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240304143043_InitialCreate.cs rename to IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240318125446_InitialCreate.cs diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/IceCreamShopDataBaseModelSnapshot.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/IceCreamShopDataBaseModelSnapshot.cs index 447b981..8400a67 100644 --- a/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/IceCreamShopDataBaseModelSnapshot.cs +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Migrations/IceCreamShopDataBaseModelSnapshot.cs @@ -142,11 +142,13 @@ namespace IceCreamShopDatabaseImplement.Migrations modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Order", b => { - b.HasOne("IceCreamShopDatabaseImplement.Models.IceCream", null) + b.HasOne("IceCreamShopDatabaseImplement.Models.IceCream", "IceCream") .WithMany("Orders") .HasForeignKey("IceCreamId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); + + b.Navigation("IceCream"); }); modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Component", b => diff --git a/IceCreamShop/IceCreamShopDatabaseImplement/Models/Order.cs b/IceCreamShop/IceCreamShopDatabaseImplement/Models/Order.cs index a80e766..6bce4fd 100644 --- a/IceCreamShop/IceCreamShopDatabaseImplement/Models/Order.cs +++ b/IceCreamShop/IceCreamShopDatabaseImplement/Models/Order.cs @@ -25,8 +25,8 @@ namespace IceCreamShopDatabaseImplement.Models public DateTime? DateImplement { get; private set; } [Required] public int IceCreamId { get; private set; } - - public static Order? Create(OrderBindingModel model) + public virtual IceCream IceCream { get; private set; } + public static Order? Create(IceCreamShopDataBase context, OrderBindingModel model) { if (model == null) { @@ -41,6 +41,7 @@ namespace IceCreamShopDatabaseImplement.Models DateCreate = model.DateCreate, DateImplement = model.DateImplement, IceCreamId = model.IceCreamId, + IceCream = context.IceCreams.FirstOrDefault(x => x.Id == model.IceCreamId) }; } @@ -62,6 +63,7 @@ namespace IceCreamShopDatabaseImplement.Models Status = Status, DateCreate = DateCreate, DateImplement = DateImplement, + IceCreamName = IceCream.IceCreamName, Id = Id, }; -- 2.25.1