diff --git a/TypographyShopDatabaseImplements/Implements/ShopStorage.cs b/TypographyShopDatabaseImplements/Implements/ShopStorage.cs new file mode 100644 index 0000000..d59c844 --- /dev/null +++ b/TypographyShopDatabaseImplements/Implements/ShopStorage.cs @@ -0,0 +1,144 @@ +using TypographyContracts.BindingModels; +using TypographyContracts.SearchModels; +using TypographyContracts.StoragesContracts; +using TypographyContracts.ViewModels; +using TypographyDatabaseImplement.Models; +using TypographyDataModels.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TypographyDatabaseImplements; + +namespace TypographyDatabaseImplement.Implements +{ + public class ShopStorage : IShopStorage + { + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue) + { + return new(); + } + using var context = new TypographyDatabase(); + return context.Shops.Include(x => x.Printeds).ThenInclude(x => x.Printed).FirstOrDefault(x => + (!string.IsNullOrEmpty(model.ShopName) && x.ShopName == model.ShopName) || + (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName)) + { + return new(); + } + using var context = new TypographyDatabase(); + return context.Shops.Include(x => x.Printeds).ThenInclude(x => x.Printed).Where(x => x.ShopName.Contains(model.ShopName)).ToList().Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() + { + using var context = new TypographyDatabase(); + return context.Shops.Include(x => x.Printeds).ThenInclude(x => x.Printed).ToList().Select(x => x.GetViewModel).ToList(); + } + + public ShopViewModel? Insert(ShopBindingModel model) + { + using var context = new TypographyDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var newShop = Shop.Create(context, model); + if (newShop == null) + { + return null; + } + if (context.Shops.Any(x => x.ShopName == newShop.ShopName)) + { + throw new Exception("Название магазина уже занято"); + } + + context.Shops.Add(newShop); + context.SaveChanges(); + transaction.Commit(); + return newShop.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public ShopViewModel? Update(ShopBindingModel model) + { + using var context = new TypographyDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var shop = context.Shops.Include(x => x.Printeds).FirstOrDefault(x => x.Id == model.Id); + if (shop == null) + { + return null; + } + shop.Update(model); + context.SaveChanges(); + if (model.ShopPrinteds.Count > 0) + { + shop.UpdatePrinteds(context, model); + } + transaction.Commit(); + return shop.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public ShopViewModel? Delete(ShopBindingModel model) + { + using var context = new TypographyDatabase(); + var shop = context.Shops.Include(x => x.Printeds).FirstOrDefault(x => x.Id == model.Id); + if (shop != null) + { + context.Shops.Remove(shop); + context.SaveChanges(); + return shop.GetViewModel; + } + return null; + } + + public bool SellPrinted(IPrintedModel model, int count) + { + using var context = new TypographyDatabase(); + using var transaction = context.Database.BeginTransaction(); + + foreach (var shopPrinteds in context.ShopPrinteds.Where(x => x.PrintedId == model.Id)) + { + var min = Math.Min(count, shopPrinteds.Count); + shopPrinteds.Count -= min; + count -= min; + if (count <= 0) + { + break; + } + } + + if (count == 0) + { + context.SaveChanges(); + transaction.Commit(); + } + else + transaction.Rollback(); + + if (count > 0) + return false; + + return true; + } + } +} \ No newline at end of file diff --git a/TypographyShopDatabaseImplements/Migrations/20240310133628_InitialCreate.Designer.cs b/TypographyShopDatabaseImplements/Migrations/20240422081714_InitialCreate.Designer.cs similarity index 68% rename from TypographyShopDatabaseImplements/Migrations/20240310133628_InitialCreate.Designer.cs rename to TypographyShopDatabaseImplements/Migrations/20240422081714_InitialCreate.Designer.cs index 4ec9ade..2fe648a 100644 --- a/TypographyShopDatabaseImplements/Migrations/20240310133628_InitialCreate.Designer.cs +++ b/TypographyShopDatabaseImplements/Migrations/20240422081714_InitialCreate.Designer.cs @@ -12,7 +12,7 @@ using TypographyDatabaseImplements; namespace TypographyDatabaseImplements.Migrations { [DbContext(typeof(TypographyDatabase))] - [Migration("20240310133628_InitialCreate")] + [Migration("20240422081714_InitialCreate")] partial class InitialCreate { /// @@ -78,6 +78,59 @@ namespace TypographyDatabaseImplements.Migrations b.ToTable("Orders"); }); + modelBuilder.Entity("TypographyDatabaseImplement.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("MaxCountPrinteds") + .HasColumnType("int"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("TypographyDatabaseImplement.Models.ShopPrinted", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("PrintedId") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("PrintedId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopPrinteds"); + }); + modelBuilder.Entity("TypographyDatabaseImplements.Models.Printed", b => { b.Property("Id") @@ -135,6 +188,25 @@ namespace TypographyDatabaseImplements.Migrations b.Navigation("Printed"); }); + modelBuilder.Entity("TypographyDatabaseImplement.Models.ShopPrinted", b => + { + b.HasOne("TypographyDatabaseImplements.Models.Printed", "Printed") + .WithMany() + .HasForeignKey("PrintedId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("TypographyDatabaseImplement.Models.Shop", "Shop") + .WithMany("Printeds") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Printed"); + + b.Navigation("Shop"); + }); + modelBuilder.Entity("TypographyDatabaseImplements.Models.PrintedComponent", b => { b.HasOne("TypographyDatabaseImplement.Models.Component", "Component") @@ -159,6 +231,11 @@ namespace TypographyDatabaseImplements.Migrations b.Navigation("PrintedComponents"); }); + modelBuilder.Entity("TypographyDatabaseImplement.Models.Shop", b => + { + b.Navigation("Printeds"); + }); + modelBuilder.Entity("TypographyDatabaseImplements.Models.Printed", b => { b.Navigation("Components"); diff --git a/TypographyShopDatabaseImplements/Migrations/20240310133628_InitialCreate.cs b/TypographyShopDatabaseImplements/Migrations/20240422081714_InitialCreate.cs similarity index 66% rename from TypographyShopDatabaseImplements/Migrations/20240310133628_InitialCreate.cs rename to TypographyShopDatabaseImplements/Migrations/20240422081714_InitialCreate.cs index 7a9d247..2026db1 100644 --- a/TypographyShopDatabaseImplements/Migrations/20240310133628_InitialCreate.cs +++ b/TypographyShopDatabaseImplements/Migrations/20240422081714_InitialCreate.cs @@ -39,6 +39,22 @@ namespace TypographyDatabaseImplements.Migrations table.PrimaryKey("PK_Printeds", x => x.Id); }); + migrationBuilder.CreateTable( + name: "Shops", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ShopName = table.Column(type: "nvarchar(max)", nullable: false), + Address = table.Column(type: "nvarchar(max)", nullable: false), + DateOpening = table.Column(type: "datetime2", nullable: false), + MaxCountPrinteds = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Shops", x => x.Id); + }); + migrationBuilder.CreateTable( name: "Orders", columns: table => new @@ -90,6 +106,33 @@ namespace TypographyDatabaseImplements.Migrations onDelete: ReferentialAction.Cascade); }); + migrationBuilder.CreateTable( + name: "ShopPrinteds", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + PrintedId = 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_ShopPrinteds", x => x.Id); + table.ForeignKey( + name: "FK_ShopPrinteds_Printeds_PrintedId", + column: x => x.PrintedId, + principalTable: "Printeds", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_ShopPrinteds_Shops_ShopId", + column: x => x.ShopId, + principalTable: "Shops", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + migrationBuilder.CreateIndex( name: "IX_Orders_PrintedId", table: "Orders", @@ -104,6 +147,16 @@ namespace TypographyDatabaseImplements.Migrations name: "IX_PrintedComponents_PrintedId", table: "PrintedComponents", column: "PrintedId"); + + migrationBuilder.CreateIndex( + name: "IX_ShopPrinteds_PrintedId", + table: "ShopPrinteds", + column: "PrintedId"); + + migrationBuilder.CreateIndex( + name: "IX_ShopPrinteds_ShopId", + table: "ShopPrinteds", + column: "ShopId"); } /// @@ -115,11 +168,17 @@ namespace TypographyDatabaseImplements.Migrations migrationBuilder.DropTable( name: "PrintedComponents"); + migrationBuilder.DropTable( + name: "ShopPrinteds"); + migrationBuilder.DropTable( name: "Components"); migrationBuilder.DropTable( name: "Printeds"); + + migrationBuilder.DropTable( + name: "Shops"); } } } diff --git a/TypographyShopDatabaseImplements/Migrations/TypographyDatabaseModelSnapshot.cs b/TypographyShopDatabaseImplements/Migrations/TypographyDatabaseModelSnapshot.cs index 5b61152..f8e4974 100644 --- a/TypographyShopDatabaseImplements/Migrations/TypographyDatabaseModelSnapshot.cs +++ b/TypographyShopDatabaseImplements/Migrations/TypographyDatabaseModelSnapshot.cs @@ -75,6 +75,59 @@ namespace TypographyDatabaseImplements.Migrations b.ToTable("Orders"); }); + modelBuilder.Entity("TypographyDatabaseImplement.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("MaxCountPrinteds") + .HasColumnType("int"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("TypographyDatabaseImplement.Models.ShopPrinted", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("PrintedId") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("PrintedId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopPrinteds"); + }); + modelBuilder.Entity("TypographyDatabaseImplements.Models.Printed", b => { b.Property("Id") @@ -132,6 +185,25 @@ namespace TypographyDatabaseImplements.Migrations b.Navigation("Printed"); }); + modelBuilder.Entity("TypographyDatabaseImplement.Models.ShopPrinted", b => + { + b.HasOne("TypographyDatabaseImplements.Models.Printed", "Printed") + .WithMany() + .HasForeignKey("PrintedId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("TypographyDatabaseImplement.Models.Shop", "Shop") + .WithMany("Printeds") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Printed"); + + b.Navigation("Shop"); + }); + modelBuilder.Entity("TypographyDatabaseImplements.Models.PrintedComponent", b => { b.HasOne("TypographyDatabaseImplement.Models.Component", "Component") @@ -156,6 +228,11 @@ namespace TypographyDatabaseImplements.Migrations b.Navigation("PrintedComponents"); }); + modelBuilder.Entity("TypographyDatabaseImplement.Models.Shop", b => + { + b.Navigation("Printeds"); + }); + modelBuilder.Entity("TypographyDatabaseImplements.Models.Printed", b => { b.Navigation("Components"); diff --git a/TypographyShopDatabaseImplements/Models/Shop.cs b/TypographyShopDatabaseImplements/Models/Shop.cs new file mode 100644 index 0000000..9c7da05 --- /dev/null +++ b/TypographyShopDatabaseImplements/Models/Shop.cs @@ -0,0 +1,115 @@ +using TypographyContracts.BindingModels; +using TypographyContracts.ViewModels; +using TypographyDataModels.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 TypographyDatabaseImplements; + +namespace TypographyDatabaseImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; set; } + + [Required] + public string ShopName { get; set; } = string.Empty; + + [Required] + public string Address { get; set; } = string.Empty; + + [Required] + public DateTime DateOpening { get; set; } + + [ForeignKey("ShopId")] + public List Printeds { get; set; } = new(); + + private Dictionary? _shopPrinteds = null; + + [NotMapped] + public Dictionary ShopPrinteds + { + get + { + if (_shopPrinteds == null) + { + _shopPrinteds = Printeds.ToDictionary(recPC => recPC.PrintedId, recPC => (recPC.Printed as IPrintedModel, recPC.Count)); + } + return _shopPrinteds; + } + } + + [Required] + public int MaxCountPrinteds { get; set; } + + public static Shop Create(TypographyDatabase context, ShopBindingModel model) + { + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + Address = model.Address, + DateOpening = model.DateOpening, + Printeds = model.ShopPrinteds.Select(x => new ShopPrinted + { + Printed = context.Printeds.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList(), + MaxCountPrinteds = model.MaxCountPrinteds + }; + } + + public void Update(ShopBindingModel model) + { + ShopName = model.ShopName; + Address = model.Address; + DateOpening = model.DateOpening; + MaxCountPrinteds = model.MaxCountPrinteds; + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Address = Address, + DateOpening = DateOpening, + ShopPrinteds = ShopPrinteds, + MaxCountPrinteds = MaxCountPrinteds + }; + + public void UpdatePrinteds(TypographyDatabase context, ShopBindingModel model) + { + var ShopPrinteds = context.ShopPrinteds.Where(rec => rec.ShopId == model.Id).ToList(); + if (ShopPrinteds != null && ShopPrinteds.Count > 0) + { + // удалили те, которых нет в модели + context.ShopPrinteds.RemoveRange(ShopPrinteds.Where(rec => !model.ShopPrinteds.ContainsKey(rec.PrintedId))); + context.SaveChanges(); + ShopPrinteds = context.ShopPrinteds.Where(rec => rec.ShopId == model.Id).ToList(); + // обновили количество у существующих записей + foreach (var updatePrinted in ShopPrinteds) + { + updatePrinted.Count = model.ShopPrinteds[updatePrinted.PrintedId].Item2; + model.ShopPrinteds.Remove(updatePrinted.PrintedId); + } + context.SaveChanges(); + } + var shop = context.Shops.First(x => x.Id == Id); + foreach (var elem in model.ShopPrinteds) + { + context.ShopPrinteds.Add(new ShopPrinted + { + Shop = shop, + Printed = context.Printeds.First(x => x.Id == elem.Key), + Count = elem.Value.Item2 + }); + context.SaveChanges(); + } + _shopPrinteds = null; + } + } +} \ No newline at end of file diff --git a/TypographyShopDatabaseImplements/Models/ShopPrinteds.cs b/TypographyShopDatabaseImplements/Models/ShopPrinteds.cs new file mode 100644 index 0000000..8bd362b --- /dev/null +++ b/TypographyShopDatabaseImplements/Models/ShopPrinteds.cs @@ -0,0 +1,23 @@ +using System.ComponentModel.DataAnnotations; +using TypographyDatabaseImplements.Models; + +namespace TypographyDatabaseImplement.Models +{ + public class ShopPrinted + { + public int Id { get; set; } + + [Required] + public int PrintedId { get; set; } + + [Required] + public int ShopId { get; set; } + + [Required] + public int Count { get; set; } + + public virtual Shop Shop { get; set; } = new(); + + public virtual Printed Printed { get; set; } = new(); + } +} \ No newline at end of file diff --git a/TypographyShopDatabaseImplements/TypographyDatabase.cs b/TypographyShopDatabaseImplements/TypographyDatabase.cs index ad7d1b1..1bbf5c0 100644 --- a/TypographyShopDatabaseImplements/TypographyDatabase.cs +++ b/TypographyShopDatabaseImplements/TypographyDatabase.cs @@ -16,7 +16,7 @@ namespace TypographyDatabaseImplements if (optionsBuilder.IsConfigured == false) { optionsBuilder.UseSqlServer(@"Data Source = .\SQLEXPRESS; - Initial Catalog=TypographyDatabaseFull; + Initial Catalog=TypographyDatabaseHardFull; Integrated Security=True;MultipleActiveResultSets=True;; TrustServerCertificate=True"); } @@ -26,5 +26,7 @@ namespace TypographyDatabaseImplements public virtual DbSet Printeds { set; get; } public virtual DbSet PrintedComponents { set; get; } public virtual DbSet Orders { set; get; } + public virtual DbSet Shops { set; get; } + public virtual DbSet ShopPrinteds { set; get; } } } diff --git a/TypographyView/FormShopSell.Designer.cs b/TypographyView/FormShopSell.Designer.cs index 44dc7ae..f1ac671 100644 --- a/TypographyView/FormShopSell.Designer.cs +++ b/TypographyView/FormShopSell.Designer.cs @@ -57,18 +57,18 @@ // comboBoxPrinted // comboBoxPrinted.FormattingEnabled = true; - comboBoxPrinted.Location = new Point(70, 8); + comboBoxPrinted.Location = new Point(106, 8); comboBoxPrinted.Margin = new Padding(3, 4, 3, 4); comboBoxPrinted.Name = "comboBoxPrinted"; - comboBoxPrinted.Size = new Size(370, 28); + comboBoxPrinted.Size = new Size(334, 28); comboBoxPrinted.TabIndex = 2; // // textBoxCount // - textBoxCount.Location = new Point(70, 47); + textBoxCount.Location = new Point(106, 47); textBoxCount.Margin = new Padding(3, 4, 3, 4); textBoxCount.Name = "textBoxCount"; - textBoxCount.Size = new Size(370, 27); + textBoxCount.Size = new Size(334, 27); textBoxCount.TabIndex = 3; // // buttonCancel @@ -78,7 +78,7 @@ buttonCancel.Name = "buttonCancel"; buttonCancel.Size = new Size(86, 31); buttonCancel.TabIndex = 4; - buttonCancel.Text = "Cancel"; + buttonCancel.Text = "Отмена"; buttonCancel.UseVisualStyleBackColor = true; buttonCancel.Click += buttonCancel_Click; // @@ -89,7 +89,7 @@ buttonSell.Name = "buttonSell"; buttonSell.Size = new Size(86, 31); buttonSell.TabIndex = 5; - buttonSell.Text = "Sell"; + buttonSell.Text = "Продать"; buttonSell.UseVisualStyleBackColor = true; buttonSell.Click += buttonSell_Click; // @@ -106,7 +106,7 @@ Controls.Add(labelPrinted); Margin = new Padding(3, 4, 3, 4); Name = "FormShopSell"; - Text = "Sell"; + Text = "Продажа"; Load += FormShopSell_Load; ResumeLayout(false); PerformLayout();