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] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D1=81=D1=83=D1=89=D0=BD=D0=BE=D1=81=D1=82=D1=8C?= =?UTF-8?q?=20=D0=BC=D0=B0=D0=B3=D0=B0=D0=B7=D0=B8=D0=BD=20=D0=B8=20=D0=B5?= =?UTF-8?q?=D0=B5=20=D1=81=D0=B2=D1=8F=D0=B7=D1=8C=20=D1=81=20=D0=B8=D0=B7?= =?UTF-8?q?=D0=B4=D0=B5=D0=BB=D0=B8=D0=B5=D0=BC=20=D0=BC=D0=BD=D0=BE=D0=B3?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=BA=D0=BE=20=D0=BC=D0=BD=D0=BE=D0=B3=D0=B8?= =?UTF-8?q?=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; + } + } +}