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; }