Agliullov D. A. Lab Work 3 Hard #7

Closed
d.agliullov wants to merge 15 commits from Lab3_Hard into Lab2_Hard
7 changed files with 451 additions and 17 deletions
Showing only changes of commit 369025f7a8 - Show all commits

View File

@ -25,5 +25,7 @@ namespace ConfectioneryDatabaseImplement
public virtual DbSet<PastryComponent> PastryComponents { set; get; }
public virtual DbSet<Order> Orders { set; get; }
public virtual DbSet<Shop> Shops { set; get; }
public virtual DbSet<ShopPastry> ShopPastries { set; get; }
}
}

View File

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

View File

@ -0,0 +1,262 @@
// <auto-generated />
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
{
/// <inheritdoc />
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<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("ComponentName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Cost")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("Components");
});
modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Order", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("Count")
.HasColumnType("int");
b.Property<DateTime>("DateCreate")
.HasColumnType("datetime2");
b.Property<DateTime?>("DateImplement")
.HasColumnType("datetime2");
b.Property<int>("PastryId")
.HasColumnType("int");
b.Property<int>("Status")
.HasColumnType("int");
b.Property<double>("Sum")
.HasColumnType("float");
b.HasKey("Id");
b.HasIndex("PastryId");
b.ToTable("Orders");
});
modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Pastry", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("PastryName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Price")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("Pastries");
});
modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.PastryComponent", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("ComponentId")
.HasColumnType("int");
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("PastryId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ComponentId");
b.HasIndex("PastryId");
b.ToTable("PastryComponents");
});
modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Shop", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Address")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<DateTime>("DateOpening")
.HasColumnType("datetime2");
b.Property<int>("MaxCountPastries")
.HasColumnType("int");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<int?>("PastryId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("PastryId");
b.ToTable("Shops");
});
modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.ShopPastry", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("PastryId")
.HasColumnType("int");
b.Property<int>("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
}
}
}

View File

@ -0,0 +1,112 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace ConfectioneryDatabaseImplement.Migrations
{
/// <inheritdoc />
public partial class create_shop1 : Migration
{
/// <inheritdoc />
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);
}
/// <inheritdoc />
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);
}
}
}

View File

@ -176,7 +176,7 @@ namespace ConfectioneryDatabaseImplement.Migrations
b.HasIndex("ShopId");
b.ToTable("ShopPastry");
b.ToTable("ShopPastries");
});
modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Order", b =>

View File

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

View File

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