From 4702f666e102104cd947d0467b2191afbafbf76d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Sun, 19 Feb 2023 22:48:53 +0400 Subject: [PATCH 01/11] =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=B1=D0=B0=D0=B7=D0=B0=20=D0=B4=D0=B0=D0=BD=D0=BD?= =?UTF-8?q?=D1=8B=D1=85=20=D0=BF=D1=80=D0=BE=D0=B5=D0=BA=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Confectionery.sln | 8 +- Confectionery/ConfectioneryView.csproj | 5 + ConfectioneryDatabaseImplement/Component.cs | 58 ++++++ .../ConfectioneryDatabase.cs | 27 +++ .../ConfectioneryDatabaseImplement.csproj | 23 +++ .../20230219142123_InitialCreate.Designer.cs | 171 ++++++++++++++++++ .../20230219142123_InitialCreate.cs | 125 +++++++++++++ .../ConfectioneryDatabaseModelSnapshot.cs | 168 +++++++++++++++++ ConfectioneryDatabaseImplement/Order.cs | 83 +++++++++ ConfectioneryDatabaseImplement/Pastry.cs | 103 +++++++++++ .../PastryComponent.cs | 18 ++ 11 files changed, 788 insertions(+), 1 deletion(-) create mode 100644 ConfectioneryDatabaseImplement/Component.cs create mode 100644 ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs create mode 100644 ConfectioneryDatabaseImplement/ConfectioneryDatabaseImplement.csproj create mode 100644 ConfectioneryDatabaseImplement/Migrations/20230219142123_InitialCreate.Designer.cs create mode 100644 ConfectioneryDatabaseImplement/Migrations/20230219142123_InitialCreate.cs create mode 100644 ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs create mode 100644 ConfectioneryDatabaseImplement/Order.cs create mode 100644 ConfectioneryDatabaseImplement/Pastry.cs create mode 100644 ConfectioneryDatabaseImplement/PastryComponent.cs diff --git a/Confectionery.sln b/Confectionery.sln index 108d8a2..cb0512c 100644 --- a/Confectionery.sln +++ b/Confectionery.sln @@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfectioneryBusinessLogic" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfectioneryListImplement", "ConfectionaryListImplement\ConfectioneryListImplement.csproj", "{0A7B118B-9B7C-4796-9846-66026159723E}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfectioneryFileImplement", "ConfectionaryFileImplement\ConfectioneryFileImplement.csproj", "{47A2EA59-4443-487E-85F4-AC49C04B7211}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfectioneryFileImplement", "ConfectionaryFileImplement\ConfectioneryFileImplement.csproj", "{47A2EA59-4443-487E-85F4-AC49C04B7211}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfectioneryDatabaseImplement", "ConfectioneryDatabaseImplement\ConfectioneryDatabaseImplement.csproj", "{2BB59FBD-AA9F-4005-8B0B-6E5176EED6A1}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -45,6 +47,10 @@ Global {47A2EA59-4443-487E-85F4-AC49C04B7211}.Debug|Any CPU.Build.0 = Debug|Any CPU {47A2EA59-4443-487E-85F4-AC49C04B7211}.Release|Any CPU.ActiveCfg = Release|Any CPU {47A2EA59-4443-487E-85F4-AC49C04B7211}.Release|Any CPU.Build.0 = Release|Any CPU + {2BB59FBD-AA9F-4005-8B0B-6E5176EED6A1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2BB59FBD-AA9F-4005-8B0B-6E5176EED6A1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2BB59FBD-AA9F-4005-8B0B-6E5176EED6A1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2BB59FBD-AA9F-4005-8B0B-6E5176EED6A1}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Confectionery/ConfectioneryView.csproj b/Confectionery/ConfectioneryView.csproj index 4947544..eaec16f 100644 --- a/Confectionery/ConfectioneryView.csproj +++ b/Confectionery/ConfectioneryView.csproj @@ -9,6 +9,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -18,6 +22,7 @@ + \ No newline at end of file diff --git a/ConfectioneryDatabaseImplement/Component.cs b/ConfectioneryDatabaseImplement/Component.cs new file mode 100644 index 0000000..e12c8f9 --- /dev/null +++ b/ConfectioneryDatabaseImplement/Component.cs @@ -0,0 +1,58 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryDataModels.Models; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using ConfectioneryContracts.ViewModels; + +namespace ConfectioneryDatabaseImplement.Models +{ + public class Component : IComponentModel + { + public int Id { get; private set; } + [Required] + public string ComponentName { get; private set; } = string.Empty; + [Required] + public double Cost { get; set; } + + [ForeignKey("ComponentId")] + public virtual List PastryComponents { get; set; } = new(); + + public static Component? Create(ComponentBindingModel model) + { + if (model == null) + { + return null; + } + return new Component() + { + Id = model.Id, + ComponentName = model.ComponentName, + Cost = model.Cost + }; + } + public static Component Create(ComponentViewModel model) + { + return new Component + { + Id = model.Id, + ComponentName = model.ComponentName, + Cost = model.Cost + }; + } + public void Update(ComponentBindingModel model) + { + if (model == null) + { + return; + } + ComponentName = model.ComponentName; + Cost = model.Cost; + } + public ComponentViewModel GetViewModel => new() + { + Id = Id, + ComponentName = ComponentName, + Cost = Cost + }; + } +} diff --git a/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs b/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs new file mode 100644 index 0000000..66c820b --- /dev/null +++ b/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs @@ -0,0 +1,27 @@ +using ConfectioneryDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System.Diagnostics.Metrics; + +namespace ConfectioneryDatabaseImplement +{ + public class ConfectioneryDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@" + Server = localhost\SQLEXPRESS; + Initial Catalog = ConfectioneryDatabaseFull; + Integrated Security = True; + MultipleActiveResultSets = True; + TrustServerCertificate = True"); + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Components { set; get; } + public virtual DbSet Pastries { set; get; } + public virtual DbSet PastryComponents { set; get; } + public virtual DbSet Orders { set; get; } + } +} diff --git a/ConfectioneryDatabaseImplement/ConfectioneryDatabaseImplement.csproj b/ConfectioneryDatabaseImplement/ConfectioneryDatabaseImplement.csproj new file mode 100644 index 0000000..6c6f63a --- /dev/null +++ b/ConfectioneryDatabaseImplement/ConfectioneryDatabaseImplement.csproj @@ -0,0 +1,23 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/ConfectioneryDatabaseImplement/Migrations/20230219142123_InitialCreate.Designer.cs b/ConfectioneryDatabaseImplement/Migrations/20230219142123_InitialCreate.Designer.cs new file mode 100644 index 0000000..4922144 --- /dev/null +++ b/ConfectioneryDatabaseImplement/Migrations/20230219142123_InitialCreate.Designer.cs @@ -0,0 +1,171 @@ +// +using System; +using ConfectioneryDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace ConfectioneryDatabaseImplement.Migrations +{ + [DbContext(typeof(ConfectioneryDatabase))] + [Migration("20230219142123_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.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("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("PastryId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("PastryId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Pastry", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("PastryName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Pastries"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.PastryComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("PastryId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("PastryId"); + + b.ToTable("PastryComponents"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Order", b => + { + b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", "Pastry") + .WithMany("Orders") + .HasForeignKey("PastryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Pastry"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.PastryComponent", b => + { + b.HasOne("ConfectioneryDatabaseImplement.Models.Component", "Component") + .WithMany("PastryComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", "Pastry") + .WithMany("Components") + .HasForeignKey("PastryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Pastry"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Component", b => + { + b.Navigation("PastryComponents"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Pastry", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/ConfectioneryDatabaseImplement/Migrations/20230219142123_InitialCreate.cs b/ConfectioneryDatabaseImplement/Migrations/20230219142123_InitialCreate.cs new file mode 100644 index 0000000..d633f68 --- /dev/null +++ b/ConfectioneryDatabaseImplement/Migrations/20230219142123_InitialCreate.cs @@ -0,0 +1,125 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace ConfectioneryDatabaseImplement.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: "Pastries", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + PastryName = table.Column(type: "nvarchar(max)", nullable: false), + Price = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Pastries", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + PastryId = 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_Pastries_PastryId", + column: x => x.PastryId, + principalTable: "Pastries", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "PastryComponents", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + PastryId = 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_PastryComponents", x => x.Id); + table.ForeignKey( + name: "FK_PastryComponents_Components_ComponentId", + column: x => x.ComponentId, + principalTable: "Components", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_PastryComponents_Pastries_PastryId", + column: x => x.PastryId, + principalTable: "Pastries", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_Orders_PastryId", + table: "Orders", + column: "PastryId"); + + migrationBuilder.CreateIndex( + name: "IX_PastryComponents_ComponentId", + table: "PastryComponents", + column: "ComponentId"); + + migrationBuilder.CreateIndex( + name: "IX_PastryComponents_PastryId", + table: "PastryComponents", + column: "PastryId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "PastryComponents"); + + migrationBuilder.DropTable( + name: "Components"); + + migrationBuilder.DropTable( + name: "Pastries"); + } + } +} diff --git a/ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs b/ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs new file mode 100644 index 0000000..a67eb4a --- /dev/null +++ b/ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs @@ -0,0 +1,168 @@ +// +using System; +using ConfectioneryDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace ConfectioneryDatabaseImplement.Migrations +{ + [DbContext(typeof(ConfectioneryDatabase))] + partial class ConfectioneryDatabaseModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.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("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("PastryId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("PastryId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Pastry", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("PastryName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Pastries"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.PastryComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("PastryId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("PastryId"); + + b.ToTable("PastryComponents"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Order", b => + { + b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", "Pastry") + .WithMany("Orders") + .HasForeignKey("PastryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Pastry"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.PastryComponent", b => + { + b.HasOne("ConfectioneryDatabaseImplement.Models.Component", "Component") + .WithMany("PastryComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", "Pastry") + .WithMany("Components") + .HasForeignKey("PastryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Pastry"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Component", b => + { + b.Navigation("PastryComponents"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Pastry", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/ConfectioneryDatabaseImplement/Order.cs b/ConfectioneryDatabaseImplement/Order.cs new file mode 100644 index 0000000..6770bf7 --- /dev/null +++ b/ConfectioneryDatabaseImplement/Order.cs @@ -0,0 +1,83 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Enums; +using ConfectioneryDataModels.Models; +using Microsoft.EntityFrameworkCore; +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; +using System.Xml.Linq; + +namespace ConfectioneryDatabaseImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + + [Required] + public int PastryId { 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; } + + public Pastry Pastry { get; private set; } + + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order() + { + PastryId = model.PastryId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement, + Id = model.Id, + }; + } + + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + PastryId = model.PastryId; + Count = model.Count; + Sum = model.Sum; + Status = model.Status; + DateCreate = model.DateCreate; + DateImplement = model.DateImplement; + Id = model.Id; + } + public OrderViewModel GetViewModel => new() + { + PastryId = PastryId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + Id = Id, + }; + } +} diff --git a/ConfectioneryDatabaseImplement/Pastry.cs b/ConfectioneryDatabaseImplement/Pastry.cs new file mode 100644 index 0000000..cac6c89 --- /dev/null +++ b/ConfectioneryDatabaseImplement/Pastry.cs @@ -0,0 +1,103 @@ +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; +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; + +namespace ConfectioneryDatabaseImplement.Models +{ + public class Pastry : IPastryModel + { + public int Id { get; set; } + [Required] + public string PastryName { get; set; } = string.Empty; + [Required] + public double Price { get; set; } + private Dictionary? _pastryComponents = null; + + [NotMapped] + public Dictionary PastryComponents + { + get + { + if (_pastryComponents == null) + { + _pastryComponents = Components + .ToDictionary(recPC => recPC.ComponentId, recPC => + (recPC.Component as IComponentModel, recPC.Count)); + } + return _pastryComponents; + } + } + + [ForeignKey("PastryId")] + public virtual List Components { get; set; } = new(); + [ForeignKey("PastryId")] + public virtual List Orders { get; set; } = new(); + + public static Pastry Create(ConfectioneryDatabase context, PastryBindingModel model) + { + return new Pastry() + { + Id = model.Id, + PastryName = model.PastryName, + Price = model.Price, + Components = model.PastryComponents.Select(x => new PastryComponent + { + Component = context.Components.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(PastryBindingModel model) + { + PastryName = model.PastryName; + Price = model.Price; + } + public PastryViewModel GetViewModel => new() + { + Id = Id, + PastryName = PastryName, + Price = Price, + PastryComponents = PastryComponents + }; + public void UpdateComponents(ConfectioneryDatabase context, PastryBindingModel model) + { + var pastryComponents = context.PastryComponents + .Where(rec => rec.PastryId == model.Id) + .ToList(); + if (pastryComponents != null && pastryComponents.Count > 0) + { // удалили те, которых нет в модели + context.PastryComponents + .RemoveRange(pastryComponents + .Where(rec => !model.PastryComponents + .ContainsKey(rec.ComponentId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateComponent in pastryComponents) + { + updateComponent.Count = model.PastryComponents[updateComponent.ComponentId].Item2; + model.PastryComponents.Remove(updateComponent.ComponentId); + } + context.SaveChanges(); + } + var pastry = context.Pastries.First(x => x.Id == Id); + foreach (var pc in model.PastryComponents) + { + context.PastryComponents.Add(new PastryComponent + { + Pastry = pastry, + Component = context.Components.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _pastryComponents = null; + } + } +} diff --git a/ConfectioneryDatabaseImplement/PastryComponent.cs b/ConfectioneryDatabaseImplement/PastryComponent.cs new file mode 100644 index 0000000..d7ca43d --- /dev/null +++ b/ConfectioneryDatabaseImplement/PastryComponent.cs @@ -0,0 +1,18 @@ +using System.ComponentModel.DataAnnotations; + +namespace ConfectioneryDatabaseImplement.Models +{ + public class PastryComponent + { + public int Id { get; set; } + [Required] + public int PastryId { get; set; } + [Required] + public int ComponentId { get; set; } + [Required] + public int Count { get; set; } + + public virtual Component Component { get; set; } = new(); + public virtual Pastry Pastry { get; set; } = new(); + } +} -- 2.25.1 From 1a8bfc323e557330ef75d6ed9091bd3074b46e5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Mon, 20 Feb 2023 01:35:50 +0400 Subject: [PATCH 02/11] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D0=BD=D1=8B=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81?= =?UTF-8?q?=D1=8B=20=D1=85=D1=80=D0=B0=D0=BD=D0=B8=D0=BB=D0=B8=D1=89=D0=B0?= =?UTF-8?q?=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D1=8E=D1=89=D0=B8=D0=B5?= =?UTF-8?q?=20=D1=81=20=D0=B1=D0=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Confectionery/Program.cs | 4 +- .../ComponentStorage.cs | 80 +++++++++++++++++ .../ConfectioneryDatabase.cs | 1 + .../OrderStorage.cs | 74 +++++++++++++++ .../PastryStorage.cs | 89 +++++++++++++++++++ 5 files changed, 246 insertions(+), 2 deletions(-) create mode 100644 ConfectioneryDatabaseImplement/ComponentStorage.cs create mode 100644 ConfectioneryDatabaseImplement/OrderStorage.cs create mode 100644 ConfectioneryDatabaseImplement/PastryStorage.cs diff --git a/Confectionery/Program.cs b/Confectionery/Program.cs index bd6722d..0ccb4c2 100644 --- a/Confectionery/Program.cs +++ b/Confectionery/Program.cs @@ -1,8 +1,8 @@ -using ConfectioneryFileImplement.Implements; +using ConfectioneryDatabaseImplement.Implements; +using ConfectioneryDatabaseImplement; using ConfectioneryBusinessLogic.BusinessLogics; using ConfectioneryContracts.BusinessLogicsContracts; using ConfectioneryContracts.StoragesContract; -using ConfectioneryFileImplement; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; diff --git a/ConfectioneryDatabaseImplement/ComponentStorage.cs b/ConfectioneryDatabaseImplement/ComponentStorage.cs new file mode 100644 index 0000000..495ecf7 --- /dev/null +++ b/ConfectioneryDatabaseImplement/ComponentStorage.cs @@ -0,0 +1,80 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContract; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDatabaseImplement.Models; + +namespace ConfectioneryDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new ConfectioneryDatabase(); + return context.Components + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + using var context = new ConfectioneryDatabase(); + return context.Components + .Where(x => x.ComponentName.Contains(model.ComponentName)) + .Select(x => x.GetViewModel) + .ToList(); + } + public ComponentViewModel? GetElement(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue) + { + return null; + } + using var context = new ConfectioneryDatabase(); + return context.Components + .FirstOrDefault(x => + (!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == model.ComponentName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + public ComponentViewModel? Insert(ComponentBindingModel model) + { + var newComponent = Component.Create(model); + if (newComponent == null) + { + return null; + } + using var context = new ConfectioneryDatabase(); + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var component = context.Components.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + context.SaveChanges(); + return component.GetViewModel; + } + public ComponentViewModel? Delete(ComponentBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Components.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs b/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs index 66c820b..08095db 100644 --- a/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs +++ b/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs @@ -8,6 +8,7 @@ namespace ConfectioneryDatabaseImplement { protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { + // Перед работой необходимо установить SQL Express if (optionsBuilder.IsConfigured == false) { optionsBuilder.UseSqlServer(@" diff --git a/ConfectioneryDatabaseImplement/OrderStorage.cs b/ConfectioneryDatabaseImplement/OrderStorage.cs new file mode 100644 index 0000000..4e87161 --- /dev/null +++ b/ConfectioneryDatabaseImplement/OrderStorage.cs @@ -0,0 +1,74 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContract; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDatabaseImplement.Models; + +namespace ConfectioneryDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var element = context.Orders.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + using var context = new ConfectioneryDatabase(); + if (!model.Id.HasValue) + { + return null; + } + return context.Orders.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel; + } + + public List GetFilteredList(OrderSearchModel model) + { + var result = GetElement(model); + return result != null ? new() { result } : new(); + } + + public List GetFullList() + { + using var context = new ConfectioneryDatabase(); + return context.Orders + .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 newOrder.GetViewModel; + } + + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + context.SaveChanges(); + return order.GetViewModel; + } + } +} diff --git a/ConfectioneryDatabaseImplement/PastryStorage.cs b/ConfectioneryDatabaseImplement/PastryStorage.cs new file mode 100644 index 0000000..84af032 --- /dev/null +++ b/ConfectioneryDatabaseImplement/PastryStorage.cs @@ -0,0 +1,89 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContract; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDatabaseImplement; +using ConfectioneryDatabaseImplement.Models; + +namespace ConfectioneryDatabaseImplement.Implements +{ + public class PastryStorage : IPastryStorage + { + public PastryViewModel? Delete(PastryBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var element = context.Pastries.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + context.Pastries.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public PastryViewModel? GetElement(PastrySearchModel model) + { + if (string.IsNullOrEmpty(model.PastryName) && !model.Id.HasValue) + { + return null; + } + using var context = new ConfectioneryDatabase(); + return context.Pastries.FirstOrDefault + (x => (!string.IsNullOrEmpty(model.PastryName) && x.PastryName == model.PastryName) || + (model.Id.HasValue && x.Id == model.Id) + )?.GetViewModel; + } + + public List GetFilteredList(PastrySearchModel model) + { + if (string.IsNullOrEmpty(model.PastryName)) + { + return new(); + } + using var context = new ConfectioneryDatabase(); + return context.Pastries + .Select(x => x.GetViewModel) + .Where(x => x.PastryName.Contains(model.PastryName)) + .ToList(); + } + + public List GetFullList() + { + using var context = new ConfectioneryDatabase(); + return context.Pastries + .Select(x => x.GetViewModel) + .ToList(); + } + + public PastryViewModel? Insert(PastryBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var newPastry = Pastry.Create(context, model); + if (newPastry == null) + { + return null; + } + newPastry = context.Pastries.Add(newPastry).Entity; + context.SaveChanges(); + model.Id = newPastry.Id; + newPastry.UpdateComponents(context, model); + return newPastry.GetViewModel; + } + + public PastryViewModel? Update(PastryBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var pastry = context.Pastries.FirstOrDefault(x => x.Id == model.Id); + if (pastry == null) + { + return null; + } + pastry.Update(model); + pastry.UpdateComponents(context, model); + context.Update(pastry); + context.SaveChanges(); + return pastry.GetViewModel; + } + } +} -- 2.25.1 From b18e9a142d79b7526dfda1cf9fd6613de58d4329 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Tue, 28 Feb 2023 22:14:49 +0400 Subject: [PATCH 03/11] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=BE=20=D0=B2=D1=8B=D1=87=D0=B8=D1=81=D0=BB=D1=8F?= =?UTF-8?q?=D0=B5=D0=BC=D0=BE=D0=B5=20=D0=BF=D0=BE=D0=BB=D0=B5=20PastryNam?= =?UTF-8?q?e=20=D0=B2=20dbImplement?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConfectioneryDatabaseImplement/Order.cs | 28 ++++++++++++++++--------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/ConfectioneryDatabaseImplement/Order.cs b/ConfectioneryDatabaseImplement/Order.cs index 6770bf7..59cbfb0 100644 --- a/ConfectioneryDatabaseImplement/Order.cs +++ b/ConfectioneryDatabaseImplement/Order.cs @@ -69,15 +69,23 @@ namespace ConfectioneryDatabaseImplement.Models DateImplement = model.DateImplement; Id = model.Id; } - public OrderViewModel GetViewModel => new() - { - PastryId = PastryId, - Count = Count, - Sum = Sum, - Status = Status, - DateCreate = DateCreate, - DateImplement = DateImplement, - Id = Id, - }; + public OrderViewModel GetViewModel + { + get + { + var context = new ConfectioneryDatabase(); + return new() + { + PastryName = context.Pastries.FirstOrDefault(x => x.Id == PastryId)?.PastryName ?? string.Empty, + PastryId = PastryId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + Id = Id, + }; + } + } } } -- 2.25.1 From f2ca847c6b93bf145ea4b03acacc52803e08b455 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Tue, 28 Feb 2023 22:19:14 +0400 Subject: [PATCH 04/11] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D0=B2=D0=BE=D0=B7=D0=BC=D0=BE=D0=B6=D0=BD?= =?UTF-8?q?=D0=BE=D1=81=D1=82=D1=8C=20=D0=BF=D0=BE=D0=BB=D1=83=D1=87=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F=20=D1=81=D0=BF=D0=B8=D1=81=D0=BA=D0=B0=20?= =?UTF-8?q?=D0=BA=D0=BE=D0=BC=D0=BF=D0=BE=D0=BD=D0=B5=D0=BD=D1=82=20=D0=B8?= =?UTF-8?q?=D0=B7=20=D0=B1=D0=B4=20=D0=B2=D0=BC=D0=B5=D1=81=D1=82=D0=B5=20?= =?UTF-8?q?=D1=81=20=D0=B8=D0=B7=D0=B4=D0=B5=D0=BB=D0=B8=D0=B5=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConfectioneryDatabaseImplement/PastryStorage.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ConfectioneryDatabaseImplement/PastryStorage.cs b/ConfectioneryDatabaseImplement/PastryStorage.cs index 84af032..aed74f0 100644 --- a/ConfectioneryDatabaseImplement/PastryStorage.cs +++ b/ConfectioneryDatabaseImplement/PastryStorage.cs @@ -4,6 +4,7 @@ using ConfectioneryContracts.StoragesContract; using ConfectioneryContracts.ViewModels; using ConfectioneryDatabaseImplement; using ConfectioneryDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; namespace ConfectioneryDatabaseImplement.Implements { @@ -29,7 +30,10 @@ namespace ConfectioneryDatabaseImplement.Implements return null; } using var context = new ConfectioneryDatabase(); - return context.Pastries.FirstOrDefault + return context.Pastries + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .FirstOrDefault (x => (!string.IsNullOrEmpty(model.PastryName) && x.PastryName == model.PastryName) || (model.Id.HasValue && x.Id == model.Id) )?.GetViewModel; -- 2.25.1 From 6ca67882ecde9d51682dbd2ef38d4e5089a48fb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Tue, 28 Feb 2023 22:47:06 +0400 Subject: [PATCH 05/11] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=86=D0=B5=D0=BB=D1=8C=D0=BD=D0=B0=D1=8F?= =?UTF-8?q?=20=D1=82=D1=80=D0=B0=D0=BD=D0=B7=D0=B0=D0=BA=D1=86=D0=B8=D1=8F?= =?UTF-8?q?=20=D0=BF=D1=80=D0=B8=20=D0=BE=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B8=20=D0=B8=D0=B7=D0=B4=D0=B5=D0=BB=D0=B8?= =?UTF-8?q?=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../PastryStorage.cs | 30 ++++++++++++++----- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/ConfectioneryDatabaseImplement/PastryStorage.cs b/ConfectioneryDatabaseImplement/PastryStorage.cs index aed74f0..3ea0953 100644 --- a/ConfectioneryDatabaseImplement/PastryStorage.cs +++ b/ConfectioneryDatabaseImplement/PastryStorage.cs @@ -47,6 +47,8 @@ namespace ConfectioneryDatabaseImplement.Implements } using var context = new ConfectioneryDatabase(); return context.Pastries + .Include(x => x.Components) + .ThenInclude(x => x.Component) .Select(x => x.GetViewModel) .Where(x => x.PastryName.Contains(model.PastryName)) .ToList(); @@ -56,6 +58,8 @@ namespace ConfectioneryDatabaseImplement.Implements { using var context = new ConfectioneryDatabase(); return context.Pastries + .Include(x => x.Components) + .ThenInclude(x => x.Component) .Select(x => x.GetViewModel) .ToList(); } @@ -78,16 +82,26 @@ namespace ConfectioneryDatabaseImplement.Implements public PastryViewModel? Update(PastryBindingModel model) { using var context = new ConfectioneryDatabase(); - var pastry = context.Pastries.FirstOrDefault(x => x.Id == model.Id); - if (pastry == null) + using var transaction = context.Database.BeginTransaction(); + try { - return null; + var pastry = context.Pastries.FirstOrDefault(x => x.Id == model.Id); + if (pastry == null) + { + return null; + } + pastry.Update(model); + pastry.UpdateComponents(context, model); + context.Update(pastry); + context.SaveChanges(); + transaction.Commit(); + return pastry.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; } - pastry.Update(model); - pastry.UpdateComponents(context, model); - context.Update(pastry); - context.SaveChanges(); - return pastry.GetViewModel; } } } -- 2.25.1 From b2eb054e2286c7847e6e7e6ca3593807bce5ca4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Tue, 28 Feb 2023 22:47:06 +0400 Subject: [PATCH 06/11] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D1=86=D0=B5=D0=BB=D1=8C=D0=BD=D0=B0=D1=8F?= =?UTF-8?q?=20=D1=82=D1=80=D0=B0=D0=BD=D0=B7=D0=B0=D0=BA=D1=86=D0=B8=D1=8F?= =?UTF-8?q?=20=D0=BF=D1=80=D0=B8=20=D0=BE=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B8=20=D0=B8=D0=B7=D0=B4=D0=B5=D0=BB=D0=B8?= =?UTF-8?q?=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../PastryStorage.cs | 32 +++++++++++++------ 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/ConfectioneryDatabaseImplement/PastryStorage.cs b/ConfectioneryDatabaseImplement/PastryStorage.cs index aed74f0..45eafbb 100644 --- a/ConfectioneryDatabaseImplement/PastryStorage.cs +++ b/ConfectioneryDatabaseImplement/PastryStorage.cs @@ -47,6 +47,8 @@ namespace ConfectioneryDatabaseImplement.Implements } using var context = new ConfectioneryDatabase(); return context.Pastries + .Include(x => x.Components) + .ThenInclude(x => x.Component) .Select(x => x.GetViewModel) .Where(x => x.PastryName.Contains(model.PastryName)) .ToList(); @@ -56,6 +58,8 @@ namespace ConfectioneryDatabaseImplement.Implements { using var context = new ConfectioneryDatabase(); return context.Pastries + .Include(x => x.Components) + .ThenInclude(x => x.Component) .Select(x => x.GetViewModel) .ToList(); } @@ -71,23 +75,33 @@ namespace ConfectioneryDatabaseImplement.Implements newPastry = context.Pastries.Add(newPastry).Entity; context.SaveChanges(); model.Id = newPastry.Id; - newPastry.UpdateComponents(context, model); + // newPastry.UpdateComponents(context, model); return newPastry.GetViewModel; } public PastryViewModel? Update(PastryBindingModel model) { using var context = new ConfectioneryDatabase(); - var pastry = context.Pastries.FirstOrDefault(x => x.Id == model.Id); - if (pastry == null) + using var transaction = context.Database.BeginTransaction(); + try { - return null; + var pastry = context.Pastries.FirstOrDefault(x => x.Id == model.Id); + if (pastry == null) + { + return null; + } + pastry.Update(model); + pastry.UpdateComponents(context, model); + context.Update(pastry); + context.SaveChanges(); + transaction.Commit(); + return pastry.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; } - pastry.Update(model); - pastry.UpdateComponents(context, model); - context.Update(pastry); - context.SaveChanges(); - return pastry.GetViewModel; } } } -- 2.25.1 From 8860c632eef5eb5568a2b3364b8e4464d2d116eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Tue, 28 Feb 2023 23:10:17 +0400 Subject: [PATCH 07/11] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B0=20=D0=B4=D0=B0=D1=83=D0=BD=D1=81=D0=BA?= =?UTF-8?q?=D0=B0=D1=8F=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B0=20=D1=8D?= =?UTF-8?q?=D0=B3=D0=BE=D0=B2=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConfectioneryDatabaseImplement/Pastry.cs | 2 +- ConfectioneryDatabaseImplement/PastryStorage.cs | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/ConfectioneryDatabaseImplement/Pastry.cs b/ConfectioneryDatabaseImplement/Pastry.cs index cac6c89..8f69a58 100644 --- a/ConfectioneryDatabaseImplement/Pastry.cs +++ b/ConfectioneryDatabaseImplement/Pastry.cs @@ -79,7 +79,7 @@ namespace ConfectioneryDatabaseImplement.Models .ContainsKey(rec.ComponentId))); context.SaveChanges(); // обновили количество у существующих записей - foreach (var updateComponent in pastryComponents) + foreach (var updateComponent in pastryComponents.Where(x => model.PastryComponents.ContainsKey(x.ComponentId))) { updateComponent.Count = model.PastryComponents[updateComponent.ComponentId].Item2; model.PastryComponents.Remove(updateComponent.ComponentId); diff --git a/ConfectioneryDatabaseImplement/PastryStorage.cs b/ConfectioneryDatabaseImplement/PastryStorage.cs index 45eafbb..90ce3b6 100644 --- a/ConfectioneryDatabaseImplement/PastryStorage.cs +++ b/ConfectioneryDatabaseImplement/PastryStorage.cs @@ -72,10 +72,8 @@ namespace ConfectioneryDatabaseImplement.Implements { return null; } - newPastry = context.Pastries.Add(newPastry).Entity; + context.Pastries.Add(newPastry); context.SaveChanges(); - model.Id = newPastry.Id; - // newPastry.UpdateComponents(context, model); return newPastry.GetViewModel; } @@ -91,9 +89,9 @@ namespace ConfectioneryDatabaseImplement.Implements return null; } pastry.Update(model); - pastry.UpdateComponents(context, model); context.Update(pastry); context.SaveChanges(); + pastry.UpdateComponents(context, model); transaction.Commit(); return pastry.GetViewModel; } -- 2.25.1 From c892d141587d2dd5957f26bf845efab42a9db67e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Tue, 28 Feb 2023 23:19:51 +0400 Subject: [PATCH 08/11] =?UTF-8?q?=D1=83=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD=20?= =?UTF-8?q?=D0=BD=D0=B5=D0=BD=D1=83=D0=B6=D0=BD=D1=8B=D0=B9=20update?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConfectioneryDatabaseImplement/PastryStorage.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/ConfectioneryDatabaseImplement/PastryStorage.cs b/ConfectioneryDatabaseImplement/PastryStorage.cs index 90ce3b6..b5a60b0 100644 --- a/ConfectioneryDatabaseImplement/PastryStorage.cs +++ b/ConfectioneryDatabaseImplement/PastryStorage.cs @@ -89,7 +89,6 @@ namespace ConfectioneryDatabaseImplement.Implements return null; } pastry.Update(model); - context.Update(pastry); context.SaveChanges(); pastry.UpdateComponents(context, model); transaction.Commit(); -- 2.25.1 From c346aa79df11bd786e0e3c4213b3d575f3e25054 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Wed, 1 Mar 2023 00:04:15 +0400 Subject: [PATCH 09/11] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D1=81=D1=83=D1=89=D0=BD=D0=BE=D1=81=D1=82?= =?UTF-8?q?=D1=8C=20=D0=BC=D0=B0=D0=B3=D0=B0=D0=B7=D0=B8=D0=BD=20=D0=B8=20?= =?UTF-8?q?=D0=B5=D0=B5=20=D1=81=D0=B2=D1=8F=D0=B7=D1=8C=20=D1=81=20=D0=B8?= =?UTF-8?q?=D0=B7=D0=B4=D0=B5=D0=BB=D0=B8=D0=B5=D0=BC=20=D0=BC=D0=BD=D0=BE?= =?UTF-8?q?=D0=B3=D0=B8=D0=B5=20=D0=BA=D0=BE=20=D0=BC=D0=BD=D0=BE=D0=B3?= =?UTF-8?q?=D0=B8=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ConfectioneryDatabase.cs | 1 + .../20230228200344_create_shop.Designer.cs | 262 ++++++++++++++++++ .../Migrations/20230228200344_create_shop.cs | 89 ++++++ .../ConfectioneryDatabaseModelSnapshot.cs | 91 ++++++ ConfectioneryDatabaseImplement/Pastry.cs | 2 + ConfectioneryDatabaseImplement/Shop.cs | 84 ++++++ ConfectioneryDatabaseImplement/ShopPastry.cs | 24 ++ ConfectioneryDatabaseImplement/ShopStorage.cs | 116 ++++++++ 8 files changed, 669 insertions(+) create mode 100644 ConfectioneryDatabaseImplement/Migrations/20230228200344_create_shop.Designer.cs create mode 100644 ConfectioneryDatabaseImplement/Migrations/20230228200344_create_shop.cs create mode 100644 ConfectioneryDatabaseImplement/Shop.cs create mode 100644 ConfectioneryDatabaseImplement/ShopPastry.cs create mode 100644 ConfectioneryDatabaseImplement/ShopStorage.cs diff --git a/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs b/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs index 08095db..7164d50 100644 --- a/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs +++ b/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs @@ -24,5 +24,6 @@ namespace ConfectioneryDatabaseImplement public virtual DbSet Pastries { set; get; } public virtual DbSet PastryComponents { set; get; } public virtual DbSet Orders { set; get; } + public virtual DbSet Shops { set; get; } } } diff --git a/ConfectioneryDatabaseImplement/Migrations/20230228200344_create_shop.Designer.cs b/ConfectioneryDatabaseImplement/Migrations/20230228200344_create_shop.Designer.cs new file mode 100644 index 0000000..256b50a --- /dev/null +++ b/ConfectioneryDatabaseImplement/Migrations/20230228200344_create_shop.Designer.cs @@ -0,0 +1,262 @@ +// +using System; +using ConfectioneryDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace ConfectioneryDatabaseImplement.Migrations +{ + [DbContext(typeof(ConfectioneryDatabase))] + [Migration("20230228200344_create_shop")] + partial class create_shop + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.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("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("PastryId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("PastryId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Pastry", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("PastryName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Pastries"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.PastryComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("PastryId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("PastryId"); + + b.ToTable("PastryComponents"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("DateOpening") + .HasColumnType("datetime2"); + + b.Property("MaxCountPastries") + .HasColumnType("int"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("PastryId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("PastryId"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.ShopPastry", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("PastryId") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("PastryId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopPastry"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Order", b => + { + b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", "Pastry") + .WithMany("Orders") + .HasForeignKey("PastryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Pastry"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.PastryComponent", b => + { + b.HasOne("ConfectioneryDatabaseImplement.Models.Component", "Component") + .WithMany("PastryComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", "Pastry") + .WithMany("Components") + .HasForeignKey("PastryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Pastry"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Shop", b => + { + b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", null) + .WithMany("Shops") + .HasForeignKey("PastryId"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.ShopPastry", b => + { + b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", "Pastry") + .WithMany() + .HasForeignKey("PastryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ConfectioneryDatabaseImplement.Models.Shop", "Shop") + .WithMany("ShopPastries") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Pastry"); + + b.Navigation("Shop"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Component", b => + { + b.Navigation("PastryComponents"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Pastry", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + + b.Navigation("Shops"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Shop", b => + { + b.Navigation("ShopPastries"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/ConfectioneryDatabaseImplement/Migrations/20230228200344_create_shop.cs b/ConfectioneryDatabaseImplement/Migrations/20230228200344_create_shop.cs new file mode 100644 index 0000000..1546c98 --- /dev/null +++ b/ConfectioneryDatabaseImplement/Migrations/20230228200344_create_shop.cs @@ -0,0 +1,89 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace ConfectioneryDatabaseImplement.Migrations +{ + /// + public partial class create_shop : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Shops", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Name = table.Column(type: "nvarchar(max)", nullable: false), + Address = table.Column(type: "nvarchar(max)", nullable: false), + MaxCountPastries = table.Column(type: "int", nullable: false), + DateOpening = table.Column(type: "datetime2", nullable: false), + PastryId = table.Column(type: "int", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Shops", x => x.Id); + table.ForeignKey( + name: "FK_Shops_Pastries_PastryId", + column: x => x.PastryId, + principalTable: "Pastries", + principalColumn: "Id"); + }); + + migrationBuilder.CreateTable( + name: "ShopPastry", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + PastryId = table.Column(type: "int", nullable: false), + ShopId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_ShopPastry", x => x.Id); + table.ForeignKey( + name: "FK_ShopPastry_Pastries_PastryId", + column: x => x.PastryId, + principalTable: "Pastries", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_ShopPastry_Shops_ShopId", + column: x => x.ShopId, + principalTable: "Shops", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_ShopPastry_PastryId", + table: "ShopPastry", + column: "PastryId"); + + migrationBuilder.CreateIndex( + name: "IX_ShopPastry_ShopId", + table: "ShopPastry", + column: "ShopId"); + + migrationBuilder.CreateIndex( + name: "IX_Shops_PastryId", + table: "Shops", + column: "PastryId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "ShopPastry"); + + migrationBuilder.DropTable( + name: "Shops"); + } + } +} diff --git a/ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs b/ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs index a67eb4a..609d1d8 100644 --- a/ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs +++ b/ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs @@ -121,6 +121,64 @@ namespace ConfectioneryDatabaseImplement.Migrations b.ToTable("PastryComponents"); }); + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("DateOpening") + .HasColumnType("datetime2"); + + b.Property("MaxCountPastries") + .HasColumnType("int"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("PastryId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("PastryId"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.ShopPastry", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("PastryId") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("PastryId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopPastry"); + }); + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Order", b => { b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", "Pastry") @@ -151,6 +209,32 @@ namespace ConfectioneryDatabaseImplement.Migrations b.Navigation("Pastry"); }); + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Shop", b => + { + b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", null) + .WithMany("Shops") + .HasForeignKey("PastryId"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.ShopPastry", b => + { + b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", "Pastry") + .WithMany() + .HasForeignKey("PastryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ConfectioneryDatabaseImplement.Models.Shop", "Shop") + .WithMany("ShopPastries") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Pastry"); + + b.Navigation("Shop"); + }); + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Component", b => { b.Navigation("PastryComponents"); @@ -161,6 +245,13 @@ namespace ConfectioneryDatabaseImplement.Migrations b.Navigation("Components"); b.Navigation("Orders"); + + b.Navigation("Shops"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Shop", b => + { + b.Navigation("ShopPastries"); }); #pragma warning restore 612, 618 } diff --git a/ConfectioneryDatabaseImplement/Pastry.cs b/ConfectioneryDatabaseImplement/Pastry.cs index 8f69a58..a9c7fdf 100644 --- a/ConfectioneryDatabaseImplement/Pastry.cs +++ b/ConfectioneryDatabaseImplement/Pastry.cs @@ -39,6 +39,8 @@ namespace ConfectioneryDatabaseImplement.Models public virtual List Components { get; set; } = new(); [ForeignKey("PastryId")] public virtual List Orders { get; set; } = new(); + [ForeignKey("PastryId")] + public virtual List Shops { get; set; } = new(); public static Pastry Create(ConfectioneryDatabase context, PastryBindingModel model) { diff --git a/ConfectioneryDatabaseImplement/Shop.cs b/ConfectioneryDatabaseImplement/Shop.cs new file mode 100644 index 0000000..3f1c726 --- /dev/null +++ b/ConfectioneryDatabaseImplement/Shop.cs @@ -0,0 +1,84 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDatabaseImplement; +using ConfectioneryDataModels; +using ConfectioneryDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Xml.Linq; + +namespace ConfectioneryDatabaseImplement.Models +{ + public class Shop : IShopModel + { + [Required] + public string Name { get; private set; } = string.Empty; + + [Required] + public string Address { get; private set; } = string.Empty; + + [Required] + public int MaxCountPastries { get; private set; } + + public DateTime DateOpening { get; private set; } + + private Dictionary? _cachedPastries = null; + [NotMapped] + public Dictionary Pastries + { + get + { + if (_cachedPastries == null) + { + using var context = new ConfectioneryDatabase(); + _cachedPastries = ShopPastries + .ToDictionary(x => x.Id, x => (context.Pastries + .FirstOrDefault(y => y.Id == x.Id)! as IPastryModel, x.Count)); + } + return _cachedPastries; + } + } + + public int Id { get; private set; } + + [ForeignKey("ShopId")] + public virtual List ShopPastries { get; set; } = new(); + + public static Shop? Create(ShopBindingModel? model) + { + if (model == null) + { + return null; + } + return new Shop() + { + Id = model.Id, + Name = model.Name, + Address = model.Address, + DateOpening = model.DateOpening, + MaxCountPastries = model.MaxCountPastries, + }; + } + public void Update(ShopBindingModel? model) + { + if (model == null) + { + return; + } + Name = model.Name; + Address = model.Address; + DateOpening = model.DateOpening; + // TODO update pastries + _cachedPastries = null; + } + public ShopViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Address = Address, + Pastries = Pastries, + DateOpening = DateOpening, + MaxCountPastries = MaxCountPastries, + }; + } +} diff --git a/ConfectioneryDatabaseImplement/ShopPastry.cs b/ConfectioneryDatabaseImplement/ShopPastry.cs new file mode 100644 index 0000000..90a4475 --- /dev/null +++ b/ConfectioneryDatabaseImplement/ShopPastry.cs @@ -0,0 +1,24 @@ +using ConfectioneryDatabaseImplement.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 ShopPastry + { + public int Id { get; set; } + [Required] + public int PastryId { get; set; } + [Required] + public int ShopId { get; set; } + [Required] + public int Count { get; set; } + + public virtual Shop Shop { get; set; } = new(); + public virtual Pastry Pastry { get; set; } = new(); + } +} diff --git a/ConfectioneryDatabaseImplement/ShopStorage.cs b/ConfectioneryDatabaseImplement/ShopStorage.cs new file mode 100644 index 0000000..1b7609d --- /dev/null +++ b/ConfectioneryDatabaseImplement/ShopStorage.cs @@ -0,0 +1,116 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContract; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDatabaseImplement.Models; +using ConfectioneryDataModels; +using ConfectioneryDataModels.Models; + +namespace ConfectioneryDatabaseImplement +{ + public class ShopStorage : IShopStorage + { + + public ShopViewModel? Delete(ShopBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var element = context.Shops.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + context.Shops.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new ConfectioneryDatabase(); + return context.Shops.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel; + } + + public List GetFilteredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.Name)) + { + return new(); + } + using var context = new ConfectioneryDatabase(); + return context.Shops + .Select(x => x.GetViewModel) + .Where(x => x.Name.Contains(model.Name ?? string.Empty)) + .ToList(); + } + + public List GetFullList() + { + using var context = new ConfectioneryDatabase(); + return context.Shops + .Select(shop => shop.GetViewModel) + .ToList(); + } + + public ShopViewModel? Insert(ShopBindingModel model) + { + var newShop = Shop.Create(model); + if (newShop == null) + { + return null; + } + using var context = new ConfectioneryDatabase(); + context.Shops.Add(newShop); + context.SaveChanges(); + return newShop.GetViewModel; + } + + public ShopViewModel? Update(ShopBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var shop = context.Shops.FirstOrDefault(x => x.Id == model.Id); + if (shop == null) + { + return null; + } + shop.Update(model); + context.SaveChanges(); + return shop.GetViewModel; + } + + public bool HasNeedPastries(IPastryModel pastry, int needCount) + { + using var context = new ConfectioneryDatabase(); + var resultCount = context.Shops + .Select(shop => shop.Pastries + .FirstOrDefault(x => x.Key == pastry.Id).Value.Item2) + .Sum(); + return resultCount >= needCount; + } + + public bool SellPastries(IPastryModel pastry, int needCount) + { + if (!HasNeedPastries(pastry, needCount)) + { + return false; + } + using var context = new ConfectioneryDatabase(); + foreach (var shop in context.Shops.Where(shop => shop.Pastries.ContainsKey(pastry.Id))) + { + var tuple = shop.Pastries[pastry.Id]; + var diff = Math.Min(tuple.Item2, needCount); + shop.Pastries[pastry.Id] = (tuple.Item1, tuple.Item2 - diff); + + needCount -= diff; + if (needCount <= 0) + { + return true; + } + } + return true; + } + } +} -- 2.25.1 From 369025f7a8d8bb355785517f6ba0fbc35c45324d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Wed, 1 Mar 2023 01:51:24 +0400 Subject: [PATCH 10/11] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D0=BD=D0=BE=20=D0=BE=D0=B1=D0=BD=D0=BE=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BC=D0=B0=D0=B3=D0=B0=D0=B7?= =?UTF-8?q?=D0=B8=D0=BD=D0=B0=20=D0=B2=D0=BC=D0=B5=D1=81=D1=82=D0=B5=20?= =?UTF-8?q?=D1=81=D0=BE=20=D1=81=D0=B2=D1=8F=D0=B7=D0=B0=D0=BD=D0=BD=D1=8B?= =?UTF-8?q?=D0=BC=D0=B8=20=D0=B8=D0=B7=D0=B4=D0=B5=D0=BB=D0=B8=D1=8F=D0=BC?= =?UTF-8?q?=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ConfectioneryDatabase.cs | 2 + .../20230228200344_create_shop.Designer.cs | 4 +- .../20230228204422_create_shop1.Designer.cs | 262 ++++++++++++++++++ .../Migrations/20230228204422_create_shop1.cs | 112 ++++++++ .../ConfectioneryDatabaseModelSnapshot.cs | 2 +- ConfectioneryDatabaseImplement/Shop.cs | 44 ++- ConfectioneryDatabaseImplement/ShopStorage.cs | 42 ++- 7 files changed, 451 insertions(+), 17 deletions(-) create mode 100644 ConfectioneryDatabaseImplement/Migrations/20230228204422_create_shop1.Designer.cs create mode 100644 ConfectioneryDatabaseImplement/Migrations/20230228204422_create_shop1.cs diff --git a/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs b/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs index 7164d50..d1ce8be 100644 --- a/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs +++ b/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs @@ -25,5 +25,7 @@ namespace ConfectioneryDatabaseImplement public virtual DbSet PastryComponents { set; get; } public virtual DbSet Orders { set; get; } public virtual DbSet Shops { set; get; } + public virtual DbSet ShopPastries { set; get; } + } } diff --git a/ConfectioneryDatabaseImplement/Migrations/20230228200344_create_shop.Designer.cs b/ConfectioneryDatabaseImplement/Migrations/20230228200344_create_shop.Designer.cs index 256b50a..ab30b13 100644 --- a/ConfectioneryDatabaseImplement/Migrations/20230228200344_create_shop.Designer.cs +++ b/ConfectioneryDatabaseImplement/Migrations/20230228200344_create_shop.Designer.cs @@ -228,7 +228,7 @@ namespace ConfectioneryDatabaseImplement.Migrations .IsRequired(); b.HasOne("ConfectioneryDatabaseImplement.Models.Shop", "Shop") - .WithMany("ShopPastries") + .WithMany("ShopPastry") .HasForeignKey("ShopId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); @@ -254,7 +254,7 @@ namespace ConfectioneryDatabaseImplement.Migrations modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Shop", b => { - b.Navigation("ShopPastries"); + b.Navigation("ShopPastry"); }); #pragma warning restore 612, 618 } diff --git a/ConfectioneryDatabaseImplement/Migrations/20230228204422_create_shop1.Designer.cs b/ConfectioneryDatabaseImplement/Migrations/20230228204422_create_shop1.Designer.cs new file mode 100644 index 0000000..d47f4b1 --- /dev/null +++ b/ConfectioneryDatabaseImplement/Migrations/20230228204422_create_shop1.Designer.cs @@ -0,0 +1,262 @@ +// +using System; +using ConfectioneryDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace ConfectioneryDatabaseImplement.Migrations +{ + [DbContext(typeof(ConfectioneryDatabase))] + [Migration("20230228204422_create_shop1")] + partial class create_shop1 + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.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("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("PastryId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("PastryId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Pastry", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("PastryName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Pastries"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.PastryComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("PastryId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("PastryId"); + + b.ToTable("PastryComponents"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("DateOpening") + .HasColumnType("datetime2"); + + b.Property("MaxCountPastries") + .HasColumnType("int"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("PastryId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("PastryId"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.ShopPastry", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("PastryId") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("PastryId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopPastries"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Order", b => + { + b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", "Pastry") + .WithMany("Orders") + .HasForeignKey("PastryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Pastry"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.PastryComponent", b => + { + b.HasOne("ConfectioneryDatabaseImplement.Models.Component", "Component") + .WithMany("PastryComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", "Pastry") + .WithMany("Components") + .HasForeignKey("PastryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Pastry"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Shop", b => + { + b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", null) + .WithMany("Shops") + .HasForeignKey("PastryId"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.ShopPastry", b => + { + b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", "Pastry") + .WithMany() + .HasForeignKey("PastryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ConfectioneryDatabaseImplement.Models.Shop", "Shop") + .WithMany("ShopPastries") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Pastry"); + + b.Navigation("Shop"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Component", b => + { + b.Navigation("PastryComponents"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Pastry", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + + b.Navigation("Shops"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Shop", b => + { + b.Navigation("ShopPastries"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/ConfectioneryDatabaseImplement/Migrations/20230228204422_create_shop1.cs b/ConfectioneryDatabaseImplement/Migrations/20230228204422_create_shop1.cs new file mode 100644 index 0000000..107508b --- /dev/null +++ b/ConfectioneryDatabaseImplement/Migrations/20230228204422_create_shop1.cs @@ -0,0 +1,112 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace ConfectioneryDatabaseImplement.Migrations +{ + /// + public partial class create_shop1 : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_ShopPastry_Pastries_PastryId", + table: "ShopPastry"); + + migrationBuilder.DropForeignKey( + name: "FK_ShopPastry_Shops_ShopId", + table: "ShopPastry"); + + migrationBuilder.DropPrimaryKey( + name: "PK_ShopPastry", + table: "ShopPastry"); + + migrationBuilder.RenameTable( + name: "ShopPastry", + newName: "ShopPastries"); + + migrationBuilder.RenameIndex( + name: "IX_ShopPastry_ShopId", + table: "ShopPastries", + newName: "IX_ShopPastries_ShopId"); + + migrationBuilder.RenameIndex( + name: "IX_ShopPastry_PastryId", + table: "ShopPastries", + newName: "IX_ShopPastries_PastryId"); + + migrationBuilder.AddPrimaryKey( + name: "PK_ShopPastries", + table: "ShopPastries", + column: "Id"); + + migrationBuilder.AddForeignKey( + name: "FK_ShopPastries_Pastries_PastryId", + table: "ShopPastries", + column: "PastryId", + principalTable: "Pastries", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + + migrationBuilder.AddForeignKey( + name: "FK_ShopPastries_Shops_ShopId", + table: "ShopPastries", + column: "ShopId", + principalTable: "Shops", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_ShopPastries_Pastries_PastryId", + table: "ShopPastries"); + + migrationBuilder.DropForeignKey( + name: "FK_ShopPastries_Shops_ShopId", + table: "ShopPastries"); + + migrationBuilder.DropPrimaryKey( + name: "PK_ShopPastries", + table: "ShopPastries"); + + migrationBuilder.RenameTable( + name: "ShopPastries", + newName: "ShopPastry"); + + migrationBuilder.RenameIndex( + name: "IX_ShopPastries_ShopId", + table: "ShopPastry", + newName: "IX_ShopPastry_ShopId"); + + migrationBuilder.RenameIndex( + name: "IX_ShopPastries_PastryId", + table: "ShopPastry", + newName: "IX_ShopPastry_PastryId"); + + migrationBuilder.AddPrimaryKey( + name: "PK_ShopPastry", + table: "ShopPastry", + column: "Id"); + + migrationBuilder.AddForeignKey( + name: "FK_ShopPastry_Pastries_PastryId", + table: "ShopPastry", + column: "PastryId", + principalTable: "Pastries", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + + migrationBuilder.AddForeignKey( + name: "FK_ShopPastry_Shops_ShopId", + table: "ShopPastry", + column: "ShopId", + principalTable: "Shops", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + } + } +} diff --git a/ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs b/ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs index 609d1d8..d006e0b 100644 --- a/ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs +++ b/ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs @@ -176,7 +176,7 @@ namespace ConfectioneryDatabaseImplement.Migrations b.HasIndex("ShopId"); - b.ToTable("ShopPastry"); + b.ToTable("ShopPastries"); }); modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Order", b => diff --git a/ConfectioneryDatabaseImplement/Shop.cs b/ConfectioneryDatabaseImplement/Shop.cs index 3f1c726..190e58d 100644 --- a/ConfectioneryDatabaseImplement/Shop.cs +++ b/ConfectioneryDatabaseImplement/Shop.cs @@ -3,6 +3,8 @@ using ConfectioneryContracts.ViewModels; using ConfectioneryDatabaseImplement; using ConfectioneryDataModels; using ConfectioneryDataModels.Models; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging.Abstractions; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Xml.Linq; @@ -32,8 +34,8 @@ namespace ConfectioneryDatabaseImplement.Models { using var context = new ConfectioneryDatabase(); _cachedPastries = ShopPastries - .ToDictionary(x => x.Id, x => (context.Pastries - .FirstOrDefault(y => y.Id == x.Id)! as IPastryModel, x.Count)); + .ToDictionary(x => x.PastryId, x => (context.Pastries + .FirstOrDefault(y => y.Id == x.PastryId)! as IPastryModel, x.Count)); } return _cachedPastries; } @@ -44,7 +46,7 @@ namespace ConfectioneryDatabaseImplement.Models [ForeignKey("ShopId")] public virtual List ShopPastries { get; set; } = new(); - public static Shop? Create(ShopBindingModel? model) + public static Shop? Create(ConfectioneryDatabase context, ShopBindingModel? model) { if (model == null) { @@ -57,6 +59,11 @@ namespace ConfectioneryDatabaseImplement.Models Address = model.Address, DateOpening = model.DateOpening, MaxCountPastries = model.MaxCountPastries, + ShopPastries = model.Pastries.Select(x => new ShopPastry + { + Pastry = context.Pastries.FirstOrDefault(y => y.Id == x.Key)!, + Count = x.Value.Item2, + }).ToList() }; } public void Update(ShopBindingModel? model) @@ -68,8 +75,6 @@ namespace ConfectioneryDatabaseImplement.Models Name = model.Name; Address = model.Address; DateOpening = model.DateOpening; - // TODO update pastries - _cachedPastries = null; } public ShopViewModel GetViewModel => new() { @@ -80,5 +85,34 @@ namespace ConfectioneryDatabaseImplement.Models DateOpening = DateOpening, MaxCountPastries = MaxCountPastries, }; + + public void UpdatePastries(ConfectioneryDatabase context, ShopBindingModel model) + { + var shopPastries = context.ShopPastries + .Where(rec => rec.ShopId == model.Id) + .ToList(); + // удалили те, которых нет в модели + if (shopPastries != null && shopPastries.Count > 0) + { + context.ShopPastries + .RemoveRange(shopPastries + .Where(rec => !model.Pastries + .ContainsKey(rec.PastryId))); + // обновили количество у существующих записей + foreach (var updatePastry in shopPastries.Where(x => model.Pastries.ContainsKey(x.PastryId))) + { + updatePastry.Count = model.Pastries[updatePastry.PastryId].Item2; + model.Pastries.Remove(updatePastry.PastryId); + } + } + var shop = context.Shops.First(x => x.Id == model.Id); + shop.ShopPastries.AddRange(model.Pastries.Select(x => new ShopPastry + { + Pastry = context.Pastries.First(y => y.Id == x.Key), + Count = x.Value.Item2, + }).Except(shopPastries ?? new())); + context.SaveChanges(); + _cachedPastries = null; + } } } diff --git a/ConfectioneryDatabaseImplement/ShopStorage.cs b/ConfectioneryDatabaseImplement/ShopStorage.cs index 1b7609d..d541ceb 100644 --- a/ConfectioneryDatabaseImplement/ShopStorage.cs +++ b/ConfectioneryDatabaseImplement/ShopStorage.cs @@ -5,6 +5,7 @@ using ConfectioneryContracts.ViewModels; using ConfectioneryDatabaseImplement.Models; using ConfectioneryDataModels; using ConfectioneryDataModels.Models; +using Microsoft.EntityFrameworkCore; namespace ConfectioneryDatabaseImplement { @@ -31,7 +32,11 @@ namespace ConfectioneryDatabaseImplement return null; } using var context = new ConfectioneryDatabase(); - return context.Shops.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel; + return context.Shops + .Include(x => x.ShopPastries) + .ThenInclude(x => x.Pastry) + .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) + ?.GetViewModel; } public List GetFilteredList(ShopSearchModel model) @@ -42,6 +47,8 @@ namespace ConfectioneryDatabaseImplement } using var context = new ConfectioneryDatabase(); return context.Shops + .Include(x => x.ShopPastries) + .ThenInclude(x => x.Pastry) .Select(x => x.GetViewModel) .Where(x => x.Name.Contains(model.Name ?? string.Empty)) .ToList(); @@ -51,21 +58,37 @@ namespace ConfectioneryDatabaseImplement { using var context = new ConfectioneryDatabase(); return context.Shops + .Include(x => x.ShopPastries) + .ThenInclude(x => x.Pastry) .Select(shop => shop.GetViewModel) .ToList(); } public ShopViewModel? Insert(ShopBindingModel model) { - var newShop = Shop.Create(model); - if (newShop == null) - { - return null; - } using var context = new ConfectioneryDatabase(); - context.Shops.Add(newShop); - context.SaveChanges(); - return newShop.GetViewModel; + using var transaction = context.Database.BeginTransaction(); + try + { + var newShop = Shop.Create(context, model); + if (newShop == null) + { + return null; + } + if (context.Shops.Any(x => x.Name == newShop.Name)) + { + throw new Exception("Не должно быть два магазина с одним названием"); + } + context.Shops.Add(newShop); + context.SaveChanges(); + transaction.Commit(); + return newShop.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } } public ShopViewModel? Update(ShopBindingModel model) @@ -77,6 +100,7 @@ namespace ConfectioneryDatabaseImplement return null; } shop.Update(model); + shop.UpdatePastries(context, model); context.SaveChanges(); return shop.GetViewModel; } -- 2.25.1 From ce944a6803102092d654ea44d53e6f3a4318e8b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Wed, 1 Mar 2023 02:38:30 +0400 Subject: [PATCH 11/11] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D0=BD=D0=B0=20=D0=BB=D0=BE=D0=B3=D0=B8=D0=BA?= =?UTF-8?q?=D0=B0=20=D0=BF=D1=80=D0=BE=D0=B4=D0=B0=D0=B6=D0=B8=20=D0=B8?= =?UTF-8?q?=D0=B7=D0=B4=D0=B5=D0=BB=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConfectioneryDatabaseImplement/ShopStorage.cs | 41 ++++++++++--------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/ConfectioneryDatabaseImplement/ShopStorage.cs b/ConfectioneryDatabaseImplement/ShopStorage.cs index d541ceb..133c0e4 100644 --- a/ConfectioneryDatabaseImplement/ShopStorage.cs +++ b/ConfectioneryDatabaseImplement/ShopStorage.cs @@ -107,34 +107,37 @@ namespace ConfectioneryDatabaseImplement public bool HasNeedPastries(IPastryModel pastry, int needCount) { - using var context = new ConfectioneryDatabase(); - var resultCount = context.Shops - .Select(shop => shop.Pastries - .FirstOrDefault(x => x.Key == pastry.Id).Value.Item2) - .Sum(); - return resultCount >= needCount; + throw new NotImplementedException(); } public bool SellPastries(IPastryModel pastry, int needCount) { - if (!HasNeedPastries(pastry, needCount)) - { - return false; - } using var context = new ConfectioneryDatabase(); - foreach (var shop in context.Shops.Where(shop => shop.Pastries.ContainsKey(pastry.Id))) + using var transaction = context.Database.BeginTransaction(); + foreach (var sp in context.ShopPastries.Where(x => x.PastryId == pastry.Id)) { - var tuple = shop.Pastries[pastry.Id]; - var diff = Math.Min(tuple.Item2, needCount); - shop.Pastries[pastry.Id] = (tuple.Item1, tuple.Item2 - diff); - - needCount -= diff; - if (needCount <= 0) + var res = Math.Min(needCount, sp.Count); + sp.Count -= res; + needCount -= res; + if (sp.Count == 0) // Изделия больше нет в магазине, значит удаляем его { - return true; + context.ShopPastries.Remove(sp); + } + if (needCount == 0) // Нельзя коммитить изменения в цикле, что использует контекст, поэтому выходим + { + break; } } - return true; + if (needCount == 0) + { + context.SaveChanges(); + transaction.Commit(); + } + else + { + transaction.Rollback(); + } + return needCount == 0; } } } -- 2.25.1