diff --git a/LawFirm/AbstractLawFirmDatabaseImplement/AbstractLawFirmDatabase.cs b/LawFirm/AbstractLawFirmDatabaseImplement/AbstractLawFirmDatabase.cs index 910677e..ae25c5a 100644 --- a/LawFirm/AbstractLawFirmDatabaseImplement/AbstractLawFirmDatabase.cs +++ b/LawFirm/AbstractLawFirmDatabaseImplement/AbstractLawFirmDatabase.cs @@ -23,5 +23,7 @@ namespace AbstractLawFirmDatabaseImplement public virtual DbSet Documents { set; get; } public virtual DbSet DocumentComponents { set; get; } public virtual DbSet Orders { set; get; } + public virtual DbSet Shops { set; get; } + public virtual DbSet ShopDocuments { set; get; } } } diff --git a/LawFirm/AbstractLawFirmDatabaseImplement/Implements/ShopStorage.cs b/LawFirm/AbstractLawFirmDatabaseImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..a36c5a7 --- /dev/null +++ b/LawFirm/AbstractLawFirmDatabaseImplement/Implements/ShopStorage.cs @@ -0,0 +1,138 @@ +using AbstractLawFirmContracts.BindingModels; +using AbstractLawFirmContracts.SearchModels; +using AbstractLawFirmContracts.StoragesContracts; +using AbstractLawFirmContracts.ViewModels; +using AbstractLawFirmDataModels.Models; +using AbstractLawFirmDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AbstractLawFirmDatabaseImplement.Implements +{ + public class ShopStorage : IShopStorage + { + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue) + { + return new(); + } + using var context = new AbstractLawFirmDatabase(); + return context.Shops.Include(x => x.Documents).ThenInclude(x => x.Document).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 AbstractLawFirmDatabase(); + return context.Shops.Include(x => x.Documents).ThenInclude(x => x.Document).Where(x => x.ShopName.Contains(model.ShopName)).ToList().Select(x => x.GetViewModel).ToList(); + } + public List GetFullList() + { + using var context = new AbstractLawFirmDatabase(); + return context.Shops.Include(x => x.Documents).ThenInclude(x => x.Document).ToList().Select(x => x.GetViewModel).ToList(); + } + public ShopViewModel? Insert(ShopBindingModel model) + { + using var context = new AbstractLawFirmDatabase(); + 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 AbstractLawFirmDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var shop = context.Shops.Include(x => x.Documents).FirstOrDefault(x => x.Id == model.Id); + if (shop == null) + { + return null; + } + shop.Update(model); + context.SaveChanges(); + if (model.ShopDocuments.Count > 0) + { + shop.UpdateDocuments(context, model); + } + transaction.Commit(); + return shop.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public ShopViewModel? Delete(ShopBindingModel model) + { + using var context = new AbstractLawFirmDatabase(); + var shop = context.Shops.Include(x => x.Documents).FirstOrDefault(x => x.Id == model.Id); + if (shop != null) + { + context.Shops.Remove(shop); + context.SaveChanges(); + return shop.GetViewModel; + } + return null; + } + public bool SellDocument(IDocumentModel model, int count) + { + using var context = new AbstractLawFirmDatabase(); + using var transaction = context.Database.BeginTransaction(); + + foreach (var shopDocuments in context.ShopDocuments.Where(x => x.DocumentId == model.Id)) + { + var min = Math.Min(count, shopDocuments.Count); + shopDocuments.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; + } + } +} diff --git a/LawFirm/AbstractLawFirmDatabaseImplement/Migrations/20240324181609_Migr_for_lab3hard.Designer.cs b/LawFirm/AbstractLawFirmDatabaseImplement/Migrations/20240324181609_Migr_for_lab3hard.Designer.cs new file mode 100644 index 0000000..e79f2c6 --- /dev/null +++ b/LawFirm/AbstractLawFirmDatabaseImplement/Migrations/20240324181609_Migr_for_lab3hard.Designer.cs @@ -0,0 +1,248 @@ +// +using System; +using AbstractLawFirmDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace AbstractLawFirmDatabaseImplement.Migrations +{ + [DbContext(typeof(AbstractLawFirmDatabase))] + [Migration("20240324181609_Migr_for_lab3hard")] + partial class Migr_for_lab3hard + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.16") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("AbstractLawFirmDatabaseImplement.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("AbstractLawFirmDatabaseImplement.Models.Document", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("DocumentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Documents"); + }); + + modelBuilder.Entity("AbstractLawFirmDatabaseImplement.Models.DocumentComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DocumentId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("DocumentId"); + + b.ToTable("DocumentComponents"); + }); + + modelBuilder.Entity("AbstractLawFirmDatabaseImplement.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("DocumentId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("DocumentId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("AbstractLawFirmDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("MaxCountDocuments") + .HasColumnType("int"); + + b.Property("OpeningDate") + .HasColumnType("datetime2"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("AbstractLawFirmDatabaseImplement.Models.ShopDocument", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DocumentId") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("DocumentId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopDocuments"); + }); + + modelBuilder.Entity("AbstractLawFirmDatabaseImplement.Models.DocumentComponent", b => + { + b.HasOne("AbstractLawFirmDatabaseImplement.Models.Component", "Component") + .WithMany("DocumentComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("AbstractLawFirmDatabaseImplement.Models.Document", "Document") + .WithMany("Components") + .HasForeignKey("DocumentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Document"); + }); + + modelBuilder.Entity("AbstractLawFirmDatabaseImplement.Models.Order", b => + { + b.HasOne("AbstractLawFirmDatabaseImplement.Models.Document", "Document") + .WithMany("Orders") + .HasForeignKey("DocumentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Document"); + }); + + modelBuilder.Entity("AbstractLawFirmDatabaseImplement.Models.ShopDocument", b => + { + b.HasOne("AbstractLawFirmDatabaseImplement.Models.Document", "Document") + .WithMany() + .HasForeignKey("DocumentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("AbstractLawFirmDatabaseImplement.Models.Shop", "Shop") + .WithMany("Documents") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Document"); + + b.Navigation("Shop"); + }); + + modelBuilder.Entity("AbstractLawFirmDatabaseImplement.Models.Component", b => + { + b.Navigation("DocumentComponents"); + }); + + modelBuilder.Entity("AbstractLawFirmDatabaseImplement.Models.Document", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); + + modelBuilder.Entity("AbstractLawFirmDatabaseImplement.Models.Shop", b => + { + b.Navigation("Documents"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/LawFirm/AbstractLawFirmDatabaseImplement/Migrations/20240324181609_Migr_for_lab3hard.cs b/LawFirm/AbstractLawFirmDatabaseImplement/Migrations/20240324181609_Migr_for_lab3hard.cs new file mode 100644 index 0000000..434bebc --- /dev/null +++ b/LawFirm/AbstractLawFirmDatabaseImplement/Migrations/20240324181609_Migr_for_lab3hard.cs @@ -0,0 +1,78 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace AbstractLawFirmDatabaseImplement.Migrations +{ + /// + public partial class Migr_for_lab3hard : 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"), + ShopName = table.Column(type: "nvarchar(max)", nullable: false), + Address = table.Column(type: "nvarchar(max)", nullable: false), + OpeningDate = table.Column(type: "datetime2", nullable: false), + MaxCountDocuments = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Shops", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "ShopDocuments", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + DocumentId = 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_ShopDocuments", x => x.Id); + table.ForeignKey( + name: "FK_ShopDocuments_Documents_DocumentId", + column: x => x.DocumentId, + principalTable: "Documents", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_ShopDocuments_Shops_ShopId", + column: x => x.ShopId, + principalTable: "Shops", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_ShopDocuments_DocumentId", + table: "ShopDocuments", + column: "DocumentId"); + + migrationBuilder.CreateIndex( + name: "IX_ShopDocuments_ShopId", + table: "ShopDocuments", + column: "ShopId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "ShopDocuments"); + + migrationBuilder.DropTable( + name: "Shops"); + } + } +} diff --git a/LawFirm/AbstractLawFirmDatabaseImplement/Migrations/AbstractLawFirmDatabaseModelSnapshot.cs b/LawFirm/AbstractLawFirmDatabaseImplement/Migrations/AbstractLawFirmDatabaseModelSnapshot.cs index 095b76d..df59b20 100644 --- a/LawFirm/AbstractLawFirmDatabaseImplement/Migrations/AbstractLawFirmDatabaseModelSnapshot.cs +++ b/LawFirm/AbstractLawFirmDatabaseImplement/Migrations/AbstractLawFirmDatabaseModelSnapshot.cs @@ -121,6 +121,59 @@ namespace AbstractLawFirmDatabaseImplement.Migrations b.ToTable("Orders"); }); + modelBuilder.Entity("AbstractLawFirmDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("MaxCountDocuments") + .HasColumnType("int"); + + b.Property("OpeningDate") + .HasColumnType("datetime2"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("AbstractLawFirmDatabaseImplement.Models.ShopDocument", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DocumentId") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("DocumentId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopDocuments"); + }); + modelBuilder.Entity("AbstractLawFirmDatabaseImplement.Models.DocumentComponent", b => { b.HasOne("AbstractLawFirmDatabaseImplement.Models.Component", "Component") @@ -151,6 +204,25 @@ namespace AbstractLawFirmDatabaseImplement.Migrations b.Navigation("Document"); }); + modelBuilder.Entity("AbstractLawFirmDatabaseImplement.Models.ShopDocument", b => + { + b.HasOne("AbstractLawFirmDatabaseImplement.Models.Document", "Document") + .WithMany() + .HasForeignKey("DocumentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("AbstractLawFirmDatabaseImplement.Models.Shop", "Shop") + .WithMany("Documents") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Document"); + + b.Navigation("Shop"); + }); + modelBuilder.Entity("AbstractLawFirmDatabaseImplement.Models.Component", b => { b.Navigation("DocumentComponents"); @@ -162,6 +234,11 @@ namespace AbstractLawFirmDatabaseImplement.Migrations b.Navigation("Orders"); }); + + modelBuilder.Entity("AbstractLawFirmDatabaseImplement.Models.Shop", b => + { + b.Navigation("Documents"); + }); #pragma warning restore 612, 618 } } diff --git a/LawFirm/AbstractLawFirmDatabaseImplement/Models/Shop.cs b/LawFirm/AbstractLawFirmDatabaseImplement/Models/Shop.cs new file mode 100644 index 0000000..b8168c2 --- /dev/null +++ b/LawFirm/AbstractLawFirmDatabaseImplement/Models/Shop.cs @@ -0,0 +1,114 @@ +using AbstractLawFirmContracts.BindingModels; +using AbstractLawFirmContracts.ViewModels; +using AbstractLawFirmDataModels.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; + +namespace AbstractLawFirmDatabaseImplement.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 OpeningDate { get; set; } + + [ForeignKey("ShopId")] + public List Documents { get; set; } = new(); + + private Dictionary? _shopDocuments = null; + + [NotMapped] + public Dictionary ShopDocuments + { + get + { + if (_shopDocuments == null) + { + _shopDocuments = Documents.ToDictionary(recPC => recPC.DocumentId, recPC => (recPC.Document as IDocumentModel, recPC.Count)); + } + return _shopDocuments; + } + } + + [Required] + public int MaxCountDocuments { get; set; } + + public static Shop Create(AbstractLawFirmDatabase context, ShopBindingModel model) + { + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + Address = model.Address, + OpeningDate = model.OpeningDate, + Documents = model.ShopDocuments.Select(x => new ShopDocument + { + Document = context.Documents.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList(), + MaxCountDocuments = model.MaxCountDocuments + }; + } + + public void Update(ShopBindingModel model) + { + ShopName = model.ShopName; + Address = model.Address; + OpeningDate = model.OpeningDate; + MaxCountDocuments = model.MaxCountDocuments; + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Address = Address, + OpeningDate = OpeningDate, + ShopDocuments = ShopDocuments, + MaxCountDocuments = MaxCountDocuments + }; + + public void UpdateDocuments(AbstractLawFirmDatabase context, ShopBindingModel model) + { + var ShopDocuments = context.ShopDocuments.Where(rec => rec.ShopId == model.Id).ToList(); + if (ShopDocuments != null && ShopDocuments.Count > 0) + { + // удалили те, которых нет в модели + context.ShopDocuments.RemoveRange(ShopDocuments.Where(rec => !model.ShopDocuments.ContainsKey(rec.DocumentId))); + context.SaveChanges(); + ShopDocuments = context.ShopDocuments.Where(rec => rec.ShopId == model.Id).ToList(); + // обновили количество у существующих записей + foreach (var updateDocument in ShopDocuments) + { + updateDocument.Count = model.ShopDocuments[updateDocument.DocumentId].Item2; + model.ShopDocuments.Remove(updateDocument.DocumentId); + } + context.SaveChanges(); + } + var shop = context.Shops.First(x => x.Id == Id); + foreach (var elem in model.ShopDocuments) + { + context.ShopDocuments.Add(new ShopDocument + { + Shop = shop, + Document = context.Documents.First(x => x.Id == elem.Key), + Count = elem.Value.Item2 + }); + context.SaveChanges(); + } + _shopDocuments = null; + } + } +} diff --git a/LawFirm/AbstractLawFirmDatabaseImplement/Models/ShopDocument.cs b/LawFirm/AbstractLawFirmDatabaseImplement/Models/ShopDocument.cs new file mode 100644 index 0000000..731539e --- /dev/null +++ b/LawFirm/AbstractLawFirmDatabaseImplement/Models/ShopDocument.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Runtime.ConstrainedExecution; +using System.Text; +using System.Threading.Tasks; + +namespace AbstractLawFirmDatabaseImplement.Models +{ + public class ShopDocument + { + public int Id { get; set; } + + [Required] + public int DocumentId { get; set; } + + [Required] + public int ShopId { get; set; } + + [Required] + public int Count { get; set; } + + public virtual Shop Shop { get; set; } = new(); + + public virtual Document Document { get; set; } = new(); + } +}