This commit is contained in:
Алексей Крюков 2024-04-22 12:36:19 +04:00
parent c659b227b7
commit 7a75216f0b
8 changed files with 506 additions and 9 deletions

View File

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

View File

@ -12,7 +12,7 @@ using TypographyDatabaseImplements;
namespace TypographyDatabaseImplements.Migrations
{
[DbContext(typeof(TypographyDatabase))]
[Migration("20240310133628_InitialCreate")]
[Migration("20240422081714_InitialCreate")]
partial class InitialCreate
{
/// <inheritdoc />
@ -78,6 +78,59 @@ namespace TypographyDatabaseImplements.Migrations
b.ToTable("Orders");
});
modelBuilder.Entity("TypographyDatabaseImplement.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>("MaxCountPrinteds")
.HasColumnType("int");
b.Property<string>("ShopName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Shops");
});
modelBuilder.Entity("TypographyDatabaseImplement.Models.ShopPrinted", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("PrintedId")
.HasColumnType("int");
b.Property<int>("ShopId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("PrintedId");
b.HasIndex("ShopId");
b.ToTable("ShopPrinteds");
});
modelBuilder.Entity("TypographyDatabaseImplements.Models.Printed", b =>
{
b.Property<int>("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");

View File

@ -39,6 +39,22 @@ namespace TypographyDatabaseImplements.Migrations
table.PrimaryKey("PK_Printeds", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Shops",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
ShopName = table.Column<string>(type: "nvarchar(max)", nullable: false),
Address = table.Column<string>(type: "nvarchar(max)", nullable: false),
DateOpening = table.Column<DateTime>(type: "datetime2", nullable: false),
MaxCountPrinteds = table.Column<int>(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<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
PrintedId = table.Column<int>(type: "int", nullable: false),
ShopId = table.Column<int>(type: "int", nullable: false),
Count = table.Column<int>(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");
}
/// <inheritdoc />
@ -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");
}
}
}

View File

@ -75,6 +75,59 @@ namespace TypographyDatabaseImplements.Migrations
b.ToTable("Orders");
});
modelBuilder.Entity("TypographyDatabaseImplement.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>("MaxCountPrinteds")
.HasColumnType("int");
b.Property<string>("ShopName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Shops");
});
modelBuilder.Entity("TypographyDatabaseImplement.Models.ShopPrinted", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("PrintedId")
.HasColumnType("int");
b.Property<int>("ShopId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("PrintedId");
b.HasIndex("ShopId");
b.ToTable("ShopPrinteds");
});
modelBuilder.Entity("TypographyDatabaseImplements.Models.Printed", b =>
{
b.Property<int>("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");

View File

@ -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<ShopPrinted> Printeds { get; set; } = new();
private Dictionary<int, (IPrintedModel, int)>? _shopPrinteds = null;
[NotMapped]
public Dictionary<int, (IPrintedModel, int)> 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;
}
}
}

View File

@ -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();
}
}

View File

@ -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<Printed> Printeds { set; get; }
public virtual DbSet<PrintedComponent> PrintedComponents { set; get; }
public virtual DbSet<Order> Orders { set; get; }
public virtual DbSet<Shop> Shops { set; get; }
public virtual DbSet<ShopPrinted> ShopPrinteds { set; get; }
}
}

View File

@ -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();