From 22dfcdeeb86a4d49e039a322641d854375026c06 Mon Sep 17 00:00:00 2001 From: AnnaLioness Date: Sun, 10 Mar 2024 17:15:08 +0400 Subject: [PATCH 1/2] =?UTF-8?q?=D0=BB=D0=B0=D0=B13=20=D1=80=D0=B0=D0=B1?= =?UTF-8?q?=D0=BE=D1=82=D0=B0=D0=B5=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AbstractLawFirmDatabase.cs | 27 +++ .../AbstractLawFirmDatabaseImplement.csproj | 23 +++ .../Implements/ComponentStorage.cs | 90 +++++++++ .../Implements/DocumentStorage.cs | 106 +++++++++++ .../Implements/OrderStorage.cs | 85 +++++++++ .../20240310114902_InitialCreate.Designer.cs | 171 ++++++++++++++++++ .../20240310114902_InitialCreate.cs | 125 +++++++++++++ .../AbstractLawFirmDatabaseModelSnapshot.cs | 168 +++++++++++++++++ .../Models/Component.cs | 63 +++++++ .../Models/Document.cs | 101 +++++++++++ .../Models/DocumentComponent.cs | 22 +++ .../Models/Order.cs | 71 ++++++++ LawFirm/LawFirm.sln | 6 + LawFirm/LawFirmView/LawFirmView.csproj | 5 + LawFirm/LawFirmView/Program.cs | 2 +- 15 files changed, 1064 insertions(+), 1 deletion(-) create mode 100644 LawFirm/AbstractLawFirmDatabaseImplement/AbstractLawFirmDatabase.cs create mode 100644 LawFirm/AbstractLawFirmDatabaseImplement/AbstractLawFirmDatabaseImplement.csproj create mode 100644 LawFirm/AbstractLawFirmDatabaseImplement/Implements/ComponentStorage.cs create mode 100644 LawFirm/AbstractLawFirmDatabaseImplement/Implements/DocumentStorage.cs create mode 100644 LawFirm/AbstractLawFirmDatabaseImplement/Implements/OrderStorage.cs create mode 100644 LawFirm/AbstractLawFirmDatabaseImplement/Migrations/20240310114902_InitialCreate.Designer.cs create mode 100644 LawFirm/AbstractLawFirmDatabaseImplement/Migrations/20240310114902_InitialCreate.cs create mode 100644 LawFirm/AbstractLawFirmDatabaseImplement/Migrations/AbstractLawFirmDatabaseModelSnapshot.cs create mode 100644 LawFirm/AbstractLawFirmDatabaseImplement/Models/Component.cs create mode 100644 LawFirm/AbstractLawFirmDatabaseImplement/Models/Document.cs create mode 100644 LawFirm/AbstractLawFirmDatabaseImplement/Models/DocumentComponent.cs create mode 100644 LawFirm/AbstractLawFirmDatabaseImplement/Models/Order.cs diff --git a/LawFirm/AbstractLawFirmDatabaseImplement/AbstractLawFirmDatabase.cs b/LawFirm/AbstractLawFirmDatabaseImplement/AbstractLawFirmDatabase.cs new file mode 100644 index 0000000..910677e --- /dev/null +++ b/LawFirm/AbstractLawFirmDatabaseImplement/AbstractLawFirmDatabase.cs @@ -0,0 +1,27 @@ +using Microsoft.EntityFrameworkCore; +using AbstractLawFirmDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Numerics; +using System.Text; +using System.Threading.Tasks; + +namespace AbstractLawFirmDatabaseImplement +{ + public class AbstractLawFirmDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=AbstractLawFirmDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Components { set; get; } + public virtual DbSet Documents { set; get; } + public virtual DbSet DocumentComponents { set; get; } + public virtual DbSet Orders { set; get; } + } +} diff --git a/LawFirm/AbstractLawFirmDatabaseImplement/AbstractLawFirmDatabaseImplement.csproj b/LawFirm/AbstractLawFirmDatabaseImplement/AbstractLawFirmDatabaseImplement.csproj new file mode 100644 index 0000000..c49152a --- /dev/null +++ b/LawFirm/AbstractLawFirmDatabaseImplement/AbstractLawFirmDatabaseImplement.csproj @@ -0,0 +1,23 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/LawFirm/AbstractLawFirmDatabaseImplement/Implements/ComponentStorage.cs b/LawFirm/AbstractLawFirmDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..dc8cc85 --- /dev/null +++ b/LawFirm/AbstractLawFirmDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,90 @@ +using AbstractLawFirmContracts.BindingModels.BindingModels; +using AbstractLawFirmContracts.SearchModels; +using AbstractLawFirmContracts.StoragesContracts; +using AbstractLawFirmContracts.ViewModels; +using AbstractLawFirmDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AbstractLawFirmDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new AbstractLawFirmDatabase(); + return context.Components + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(ComponentSearchModel + model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + using var context = new AbstractLawFirmDatabase(); + return context.Components + .Where(x => x.ComponentName.Contains(model.ComponentName)) + .Select(x => x.GetViewModel) + .ToList(); + } + public ComponentViewModel? GetElement(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue) + { + return null; + } + using var context = new AbstractLawFirmDatabase(); + return context.Components + .FirstOrDefault(x => + (!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == + model.ComponentName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + public ComponentViewModel? Insert(ComponentBindingModel model) + { + var newComponent = Component.Create(model); + if (newComponent == null) + { + return null; + } + using var context = new AbstractLawFirmDatabase(); + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new AbstractLawFirmDatabase(); + var component = context.Components.FirstOrDefault(x => x.Id == + model.Id); + if (component == null) + { + return null; + } + component.Update(model); + context.SaveChanges(); + return component.GetViewModel; + } + public ComponentViewModel? Delete(ComponentBindingModel model) + { + using var context = new AbstractLawFirmDatabase(); + var element = context.Components.FirstOrDefault(rec => rec.Id == + model.Id); + if (element != null) + { + context.Components.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + } +} diff --git a/LawFirm/AbstractLawFirmDatabaseImplement/Implements/DocumentStorage.cs b/LawFirm/AbstractLawFirmDatabaseImplement/Implements/DocumentStorage.cs new file mode 100644 index 0000000..5bacc9e --- /dev/null +++ b/LawFirm/AbstractLawFirmDatabaseImplement/Implements/DocumentStorage.cs @@ -0,0 +1,106 @@ +using AbstractLawFirmContracts.BindingModels; +using AbstractLawFirmContracts.SearchModels; +using AbstractLawFirmContracts.StoragesContracts; +using AbstractLawFirmContracts.ViewModels; +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 DocumentStorage : IDocumentStorage + { + public List GetFullList() + { + using var context = new AbstractLawFirmDatabase(); + return context.Documents + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(DocumentSearchModel model) + { + if (string.IsNullOrEmpty(model.DocumentName)) + { + return new(); + } + using var context = new AbstractLawFirmDatabase(); + return context.Documents + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .Where(x => x.DocumentName.Contains(model.DocumentName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public DocumentViewModel? GetElement(DocumentSearchModel model) + { + if (string.IsNullOrEmpty(model.DocumentName) && + !model.Id.HasValue) + { + return null; + } + using var context = new AbstractLawFirmDatabase(); + return context.Documents.Include(x => x.Components).ThenInclude(x => x.Component) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.DocumentName) && + x.DocumentName == model.DocumentName) || + (model.Id.HasValue && x.Id == + model.Id)) + ?.GetViewModel; + } + public DocumentViewModel? Insert(DocumentBindingModel model) + { + using var context = new AbstractLawFirmDatabase(); + var newDocument = Document.Create(context, model); + if (newDocument == null) + { + return null; + } + context.Documents.Add(newDocument); + context.SaveChanges(); + return newDocument.GetViewModel; + } + public DocumentViewModel? Update(DocumentBindingModel model) + { + using var context = new AbstractLawFirmDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var document = context.Documents.FirstOrDefault(rec => + rec.Id == model.Id); + if (document == null) + { + return null; + } + document.Update(model); + context.SaveChanges(); + document.UpdateComponents(context, model); + transaction.Commit(); + return document.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public DocumentViewModel? Delete(DocumentBindingModel model) + { + using var context = new AbstractLawFirmDatabase(); + var element = context.Documents.Include(x => x.Components).FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Documents.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/LawFirm/AbstractLawFirmDatabaseImplement/Implements/OrderStorage.cs b/LawFirm/AbstractLawFirmDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..29e72b2 --- /dev/null +++ b/LawFirm/AbstractLawFirmDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,85 @@ +using AbstractLawFirmContracts.BindingModels; +using AbstractLawFirmContracts.SearchModels; +using AbstractLawFirmContracts.StoragesContracts; +using AbstractLawFirmContracts.ViewModels; +using Microsoft.EntityFrameworkCore; +using AbstractLawFirmDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AbstractLawFirmDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new AbstractLawFirmDatabase(); + return context.Orders.Include(x => x.Document).FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new AbstractLawFirmDatabase(); + return context.Orders + .Where(x => x.Id == model.Id) + .Include(x => x.Document) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new AbstractLawFirmDatabase(); + return context.Orders.Include(x => x.Document).Select(x => x.GetViewModel).ToList(); + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + using var context = new AbstractLawFirmDatabase(); + context.Orders.Add(newOrder); + context.SaveChanges(); + return context.Orders.Include(x => x.Document).FirstOrDefault(x => x.Id == newOrder.Id)?.GetViewModel; + } + + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new AbstractLawFirmDatabase(); + var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + context.SaveChanges(); + return context.Orders.Include(x => x.Document).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + } + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new AbstractLawFirmDatabase(); + var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/LawFirm/AbstractLawFirmDatabaseImplement/Migrations/20240310114902_InitialCreate.Designer.cs b/LawFirm/AbstractLawFirmDatabaseImplement/Migrations/20240310114902_InitialCreate.Designer.cs new file mode 100644 index 0000000..72441aa --- /dev/null +++ b/LawFirm/AbstractLawFirmDatabaseImplement/Migrations/20240310114902_InitialCreate.Designer.cs @@ -0,0 +1,171 @@ +// +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("20240310114902_InitialCreate")] + partial class InitialCreate + { + /// + 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.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.Component", b => + { + b.Navigation("DocumentComponents"); + }); + + modelBuilder.Entity("AbstractLawFirmDatabaseImplement.Models.Document", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/LawFirm/AbstractLawFirmDatabaseImplement/Migrations/20240310114902_InitialCreate.cs b/LawFirm/AbstractLawFirmDatabaseImplement/Migrations/20240310114902_InitialCreate.cs new file mode 100644 index 0000000..2f36f3f --- /dev/null +++ b/LawFirm/AbstractLawFirmDatabaseImplement/Migrations/20240310114902_InitialCreate.cs @@ -0,0 +1,125 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace AbstractLawFirmDatabaseImplement.Migrations +{ + /// + public partial class InitialCreate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Components", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ComponentName = table.Column(type: "nvarchar(max)", nullable: false), + Cost = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Components", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Documents", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + DocumentName = table.Column(type: "nvarchar(max)", nullable: false), + Price = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Documents", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "DocumentComponents", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + DocumentId = table.Column(type: "int", nullable: false), + ComponentId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_DocumentComponents", x => x.Id); + table.ForeignKey( + name: "FK_DocumentComponents_Components_ComponentId", + column: x => x.ComponentId, + principalTable: "Components", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_DocumentComponents_Documents_DocumentId", + column: x => x.DocumentId, + principalTable: "Documents", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + DocumentId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false), + Sum = table.Column(type: "float", nullable: false), + Status = table.Column(type: "int", nullable: false), + DateCreate = table.Column(type: "datetime2", nullable: false), + DateImplement = table.Column(type: "datetime2", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Orders", x => x.Id); + table.ForeignKey( + name: "FK_Orders_Documents_DocumentId", + column: x => x.DocumentId, + principalTable: "Documents", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_DocumentComponents_ComponentId", + table: "DocumentComponents", + column: "ComponentId"); + + migrationBuilder.CreateIndex( + name: "IX_DocumentComponents_DocumentId", + table: "DocumentComponents", + column: "DocumentId"); + + migrationBuilder.CreateIndex( + name: "IX_Orders_DocumentId", + table: "Orders", + column: "DocumentId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "DocumentComponents"); + + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "Components"); + + migrationBuilder.DropTable( + name: "Documents"); + } + } +} diff --git a/LawFirm/AbstractLawFirmDatabaseImplement/Migrations/AbstractLawFirmDatabaseModelSnapshot.cs b/LawFirm/AbstractLawFirmDatabaseImplement/Migrations/AbstractLawFirmDatabaseModelSnapshot.cs new file mode 100644 index 0000000..095b76d --- /dev/null +++ b/LawFirm/AbstractLawFirmDatabaseImplement/Migrations/AbstractLawFirmDatabaseModelSnapshot.cs @@ -0,0 +1,168 @@ +// +using System; +using AbstractLawFirmDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace AbstractLawFirmDatabaseImplement.Migrations +{ + [DbContext(typeof(AbstractLawFirmDatabase))] + partial class AbstractLawFirmDatabaseModelSnapshot : ModelSnapshot + { + protected override void BuildModel(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.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.Component", b => + { + b.Navigation("DocumentComponents"); + }); + + modelBuilder.Entity("AbstractLawFirmDatabaseImplement.Models.Document", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/LawFirm/AbstractLawFirmDatabaseImplement/Models/Component.cs b/LawFirm/AbstractLawFirmDatabaseImplement/Models/Component.cs new file mode 100644 index 0000000..0501143 --- /dev/null +++ b/LawFirm/AbstractLawFirmDatabaseImplement/Models/Component.cs @@ -0,0 +1,63 @@ +using AbstractLawFirmContracts.BindingModels.BindingModels; +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 Component : IComponentModel + { + public int Id { get; private set; } + [Required] + public string ComponentName { get; private set; } = string.Empty; + [Required] + public double Cost { get; set; } + [ForeignKey("ComponentId")] + public virtual List DocumentComponents { get; set; } = new(); + public static Component? Create(ComponentBindingModel model) + { + if (model == null) + { + return null; + } + return new Component() + { + Id = model.Id, + ComponentName = model.ComponentName, + Cost = model.Cost + }; + } + public static Component Create(ComponentViewModel model) + { + return new Component + { + Id = model.Id, + ComponentName = model.ComponentName, + Cost = model.Cost + }; + } + public void Update(ComponentBindingModel model) + { + if (model == null) + { + return; + } + ComponentName = model.ComponentName; + Cost = model.Cost; + } + public ComponentViewModel GetViewModel => new() + { + Id = Id, + ComponentName = ComponentName, + Cost = Cost + }; + + } +} diff --git a/LawFirm/AbstractLawFirmDatabaseImplement/Models/Document.cs b/LawFirm/AbstractLawFirmDatabaseImplement/Models/Document.cs new file mode 100644 index 0000000..184430e --- /dev/null +++ b/LawFirm/AbstractLawFirmDatabaseImplement/Models/Document.cs @@ -0,0 +1,101 @@ +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; +using AbstractLawFirmContracts.BindingModels; +using AbstractLawFirmContracts.ViewModels; + +namespace AbstractLawFirmDatabaseImplement.Models +{ + public class Document : IDocumentModel + { + public int Id { get; set; } + [Required] + public string DocumentName { get; set; } = string.Empty; + [Required] + public double Price { get; set; } + private Dictionary? _documentComponents = + null; + [NotMapped] + public Dictionary DocumentComponents + { + get + { + if (_documentComponents == null) + { + _documentComponents = Components + .ToDictionary(recPC => recPC.ComponentId, recPC => + (recPC.Component as IComponentModel, recPC.Count)); + } + return _documentComponents; + } + } + [ForeignKey("DocumentId")] + public virtual List Components { get; set; } = new(); + [ForeignKey("DocumentId")] + public virtual List Orders { get; set; } = new(); + public static Document Create(AbstractLawFirmDatabase context, + DocumentBindingModel model) + { + return new Document() + { + Id = model.Id, + DocumentName = model.DocumentName, + Price = model.Price, + Components = model.DocumentComponents.Select(x => new + DocumentComponent + { + Component = context.Components.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(DocumentBindingModel model) + { + DocumentName = model.DocumentName; + Price = model.Price; + } + public DocumentViewModel GetViewModel => new() + { + Id = Id, + DocumentName = DocumentName, + Price = Price, + DocumentComponents = DocumentComponents + }; + public void UpdateComponents(AbstractLawFirmDatabase context, + DocumentBindingModel model) + { + var documentComponents = context.DocumentComponents.Where(rec => rec.DocumentId == model.Id).ToList(); + if (documentComponents != null && documentComponents.Count > 0) + { // удалили те, которых нет в модели + context.DocumentComponents.RemoveRange(documentComponents.Where(rec + => !model.DocumentComponents.ContainsKey(rec.ComponentId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateComponent in documentComponents) + { + updateComponent.Count = model.DocumentComponents[updateComponent.ComponentId].Item2; + model.DocumentComponents.Remove(updateComponent.ComponentId); + } + context.SaveChanges(); + } + var document = context.Documents.First(x => x.Id == Id); + foreach (var pc in model.DocumentComponents) + { + context.DocumentComponents.Add(new DocumentComponent + { + Document = document, + Component = context.Components.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _documentComponents = null; + } + + } +} diff --git a/LawFirm/AbstractLawFirmDatabaseImplement/Models/DocumentComponent.cs b/LawFirm/AbstractLawFirmDatabaseImplement/Models/DocumentComponent.cs new file mode 100644 index 0000000..58c471e --- /dev/null +++ b/LawFirm/AbstractLawFirmDatabaseImplement/Models/DocumentComponent.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AbstractLawFirmDatabaseImplement.Models +{ + public class DocumentComponent + { + public int Id { get; set; } + [Required] + public int DocumentId { get; set; } + [Required] + public int ComponentId { get; set; } + [Required] + public int Count { get; set; } + public virtual Component Component { get; set; } = new(); + public virtual Document Document { get; set; } = new(); + } +} diff --git a/LawFirm/AbstractLawFirmDatabaseImplement/Models/Order.cs b/LawFirm/AbstractLawFirmDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..bff7c2f --- /dev/null +++ b/LawFirm/AbstractLawFirmDatabaseImplement/Models/Order.cs @@ -0,0 +1,71 @@ +using AbstractLawFirmContracts.BindingModels; +using AbstractLawFirmContracts.ViewModels; +using AbstractLawFirmDataModels.Enums; +using AbstractLawFirmDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AbstractLawFirmDatabaseImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + [Required] + public int DocumentId { get; private set; } + [Required] + public int Count { get; private set; } + [Required] + public double Sum { get; private set; } + [Required] + public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; + [Required] + public DateTime DateCreate { get; private set; } = DateTime.Now; + + public DateTime? DateImplement { get; private set; } + public virtual Document Document { get; set; } + + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order + { + DocumentId = model.DocumentId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement, + Id = model.Id, + }; + } + + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + Status = model.Status; + DateImplement = model.DateImplement; + } + + public OrderViewModel GetViewModel => new() + { + DocumentId = DocumentId, + Count = Count, + Sum = Sum, + DateCreate = DateCreate, + DateImplement = DateImplement, + Id = Id, + Status = Status, + DocumentName = Document.DocumentName + }; + } +} diff --git a/LawFirm/LawFirm.sln b/LawFirm/LawFirm.sln index f8713d1..ddd4948 100644 --- a/LawFirm/LawFirm.sln +++ b/LawFirm/LawFirm.sln @@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractLawFirmListImplemen EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbstractLawFirmFileImplement", "AbstractLawFirmFileImplement\AbstractLawFirmFileImplement.csproj", "{5E8949F4-7CFD-49C4-9F43-ADFAF14F1682}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbstractLawFirmDatabaseImplement", "AbstractLawFirmDatabaseImplement\AbstractLawFirmDatabaseImplement.csproj", "{BAC5C4E1-6768-4D55-A760-57D8B5BEDBA2}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -45,6 +47,10 @@ Global {5E8949F4-7CFD-49C4-9F43-ADFAF14F1682}.Debug|Any CPU.Build.0 = Debug|Any CPU {5E8949F4-7CFD-49C4-9F43-ADFAF14F1682}.Release|Any CPU.ActiveCfg = Release|Any CPU {5E8949F4-7CFD-49C4-9F43-ADFAF14F1682}.Release|Any CPU.Build.0 = Release|Any CPU + {BAC5C4E1-6768-4D55-A760-57D8B5BEDBA2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BAC5C4E1-6768-4D55-A760-57D8B5BEDBA2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BAC5C4E1-6768-4D55-A760-57D8B5BEDBA2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BAC5C4E1-6768-4D55-A760-57D8B5BEDBA2}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/LawFirm/LawFirmView/LawFirmView.csproj b/LawFirm/LawFirmView/LawFirmView.csproj index 7ff567d..d6ddd06 100644 --- a/LawFirm/LawFirmView/LawFirmView.csproj +++ b/LawFirm/LawFirmView/LawFirmView.csproj @@ -9,12 +9,17 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + diff --git a/LawFirm/LawFirmView/Program.cs b/LawFirm/LawFirmView/Program.cs index cffdc50..1986fb9 100644 --- a/LawFirm/LawFirmView/Program.cs +++ b/LawFirm/LawFirmView/Program.cs @@ -1,7 +1,7 @@ using AbstractLawFirmBusinessLogic.BusinessLogic; using AbstractLawFirmContracts.BusinessLogicsContracts; using AbstractLawFirmContracts.StoragesContracts; -using AbstractLawFirmFileImplement.Implements; +using AbstractLawFirmDatabaseImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; -- 2.25.1 From f4a72b68f49ec65a513c069c8b779dd5b83d221d Mon Sep 17 00:00:00 2001 From: AnnaLioness Date: Sun, 24 Mar 2024 22:31:39 +0400 Subject: [PATCH 2/2] =?UTF-8?q?3=20=D1=81=D0=BB=D0=BE=D0=B6=D0=BD=D0=B0?= =?UTF-8?q?=D1=8F=20=D0=B3=D0=BE=D1=82=D0=BE=D0=B2=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AbstractLawFirmDatabase.cs | 2 + .../Implements/ShopStorage.cs | 138 ++++++++++ ...240324181609_Migr_for_lab3hard.Designer.cs | 248 ++++++++++++++++++ .../20240324181609_Migr_for_lab3hard.cs | 78 ++++++ .../AbstractLawFirmDatabaseModelSnapshot.cs | 77 ++++++ .../Models/Shop.cs | 114 ++++++++ .../Models/ShopDocument.cs | 28 ++ 7 files changed, 685 insertions(+) create mode 100644 LawFirm/AbstractLawFirmDatabaseImplement/Implements/ShopStorage.cs create mode 100644 LawFirm/AbstractLawFirmDatabaseImplement/Migrations/20240324181609_Migr_for_lab3hard.Designer.cs create mode 100644 LawFirm/AbstractLawFirmDatabaseImplement/Migrations/20240324181609_Migr_for_lab3hard.cs create mode 100644 LawFirm/AbstractLawFirmDatabaseImplement/Models/Shop.cs create mode 100644 LawFirm/AbstractLawFirmDatabaseImplement/Models/ShopDocument.cs 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(); + } +} -- 2.25.1