diff --git a/LawFirm/LawFirm.sln b/LawFirm/LawFirm.sln index 85a406b..744d868 100644 --- a/LawFirm/LawFirm.sln +++ b/LawFirm/LawFirm.sln @@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LawFirmListImplement", "Law EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LawFirmBusinessLogic", "LawFirmBusinessLogic\LawFirmBusinessLogic.csproj", "{E09C46AA-9871-4B96-BC73-CCD93918AEE1}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LawFirmFileImplement", "LawFirmFileImplement\LawFirmFileImplement.csproj", "{E5961797-2975-425F-B5DD-FC92947C54E7}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LawFirmFileImplement", "LawFirmFileImplement\LawFirmFileImplement.csproj", "{E5961797-2975-425F-B5DD-FC92947C54E7}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LawFirmDatabaseImplement_", "LawFirmDatabaseImplement\LawFirmDatabaseImplement_.csproj", "{BEF83CF0-D549-4AE5-BBE4-FA3FDB47ACDC}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -45,6 +47,10 @@ Global {E5961797-2975-425F-B5DD-FC92947C54E7}.Debug|Any CPU.Build.0 = Debug|Any CPU {E5961797-2975-425F-B5DD-FC92947C54E7}.Release|Any CPU.ActiveCfg = Release|Any CPU {E5961797-2975-425F-B5DD-FC92947C54E7}.Release|Any CPU.Build.0 = Release|Any CPU + {BEF83CF0-D549-4AE5-BBE4-FA3FDB47ACDC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BEF83CF0-D549-4AE5-BBE4-FA3FDB47ACDC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BEF83CF0-D549-4AE5-BBE4-FA3FDB47ACDC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BEF83CF0-D549-4AE5-BBE4-FA3FDB47ACDC}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/LawFirm/LawFirmBusinessLogic/LawFirmBusinessLogic.csproj b/LawFirm/LawFirmBusinessLogic/LawFirmBusinessLogic.csproj index 426ab2e..3267956 100644 --- a/LawFirm/LawFirmBusinessLogic/LawFirmBusinessLogic.csproj +++ b/LawFirm/LawFirmBusinessLogic/LawFirmBusinessLogic.csproj @@ -7,6 +7,14 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/LawFirm/LawFirmContracts/LawFirmContracts.csproj b/LawFirm/LawFirmContracts/LawFirmContracts.csproj index 46ea406..1b8cd24 100644 --- a/LawFirm/LawFirmContracts/LawFirmContracts.csproj +++ b/LawFirm/LawFirmContracts/LawFirmContracts.csproj @@ -6,6 +6,17 @@ enable + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + diff --git a/LawFirm/LawFirmDataModels/LawFirmDataModels.csproj b/LawFirm/LawFirmDataModels/LawFirmDataModels.csproj index 132c02c..aed5a9c 100644 --- a/LawFirm/LawFirmDataModels/LawFirmDataModels.csproj +++ b/LawFirm/LawFirmDataModels/LawFirmDataModels.csproj @@ -6,4 +6,15 @@ enable + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + diff --git a/LawFirm/LawFirmDatabaseImplement/Implements/ComponentStorage.cs b/LawFirm/LawFirmDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..5851b2c --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,83 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.SearchModels; +using LawFirmContracts.StoragesContracts; +using LawFirmContracts.ViewModels; +using LawFirmDatabaseImplement_.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LawFirmDatabaseImplement_.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new LawFirmDatabase(); + return context.Components + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + using var context = new LawFirmDatabase(); + 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 LawFirmDatabase(); + 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 LawFirmDatabase(); + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new LawFirmDatabase(); + 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 LawFirmDatabase(); + 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/LawFirmDatabaseImplement/Implements/DocumentStorage.cs b/LawFirm/LawFirmDatabaseImplement/Implements/DocumentStorage.cs new file mode 100644 index 0000000..86a8da0 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Implements/DocumentStorage.cs @@ -0,0 +1,108 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.SearchModels; +using LawFirmContracts.StoragesContracts; +using LawFirmContracts.ViewModels; +using LawFirmDatabaseImplement_.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LawFirmDatabaseImplement_.Implements +{ + public class DocumentStorage : IDocumentStorage + { + public List GetFullList() + { + using var context = new LawFirmDatabase(); + 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 LawFirmDatabase(); + 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 LawFirmDatabase(); + 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 LawFirmDatabase(); + 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 LawFirmDatabase(); + 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 LawFirmDatabase(); + 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/LawFirmDatabaseImplement/Implements/OrderStorage.cs b/LawFirm/LawFirmDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..683cbfb --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,45 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.SearchModels; +using LawFirmContracts.StoragesContracts; +using LawFirmContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LawFirmDatabaseImplement_.Implements +{ + public class OrderStorage : IOrderStorage + { + public OrderViewModel? Delete(OrderBindingModel model) + { + throw new NotImplementedException(); + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + throw new NotImplementedException(); + } + + public List GetFilteredList(OrderSearchModel model) + { + throw new NotImplementedException(); + } + + public List GetFullList() + { + throw new NotImplementedException(); + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + throw new NotImplementedException(); + } + + public OrderViewModel? Update(OrderBindingModel model) + { + throw new NotImplementedException(); + } + } +} diff --git a/LawFirm/LawFirmDatabaseImplement/LawFirmDatabase.cs b/LawFirm/LawFirmDatabaseImplement/LawFirmDatabase.cs new file mode 100644 index 0000000..0bf7114 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/LawFirmDatabase.cs @@ -0,0 +1,26 @@ +using LawFirmDatabaseImplement_.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LawFirmDatabaseImplement_ +{ + public class LawFirmDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=CHESHIR\SQLEXPRESS;Initial Catalog=AbstractShopDatabase;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/LawFirmDatabaseImplement/LawFirmDatabaseImplement_.csproj b/LawFirm/LawFirmDatabaseImplement/LawFirmDatabaseImplement_.csproj new file mode 100644 index 0000000..495d68e --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/LawFirmDatabaseImplement_.csproj @@ -0,0 +1,27 @@ + + + + net6.0 + LawFirmDatabaseImplement_ + enable + enable + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + diff --git a/LawFirm/LawFirmDatabaseImplement/Migrations/20230509111454_InitMigration.Designer.cs b/LawFirm/LawFirmDatabaseImplement/Migrations/20230509111454_InitMigration.Designer.cs new file mode 100644 index 0000000..6f5cd50 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Migrations/20230509111454_InitMigration.Designer.cs @@ -0,0 +1,169 @@ +// +using System; +using LawFirmDatabaseImplement_; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace LawFirmDatabaseImplement_.Migrations +{ + [DbContext(typeof(LawFirmDatabase))] + [Migration("20230509111454_InitMigration")] + partial class InitMigration + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.5") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("LawFirmDatabaseImplement_.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("LawFirmDatabaseImplement_.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("LawFirmDatabaseImplement_.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("LawFirmDatabaseImplement_.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("LawFirmDatabaseImplement_.Models.DocumentComponent", b => + { + b.HasOne("LawFirmDatabaseImplement_.Models.Component", "Component") + .WithMany("DocumentComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("LawFirmDatabaseImplement_.Models.Document", "Document") + .WithMany("Components") + .HasForeignKey("DocumentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Document"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement_.Models.Order", b => + { + b.HasOne("LawFirmDatabaseImplement_.Models.Document", null) + .WithMany("Orders") + .HasForeignKey("DocumentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement_.Models.Component", b => + { + b.Navigation("DocumentComponents"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement_.Models.Document", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/LawFirm/LawFirmDatabaseImplement/Migrations/20230509111454_InitMigration.cs b/LawFirm/LawFirmDatabaseImplement/Migrations/20230509111454_InitMigration.cs new file mode 100644 index 0000000..1540f45 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Migrations/20230509111454_InitMigration.cs @@ -0,0 +1,125 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace LawFirmDatabaseImplement_.Migrations +{ + /// + public partial class InitMigration : 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/LawFirmDatabaseImplement/Migrations/LawFirmDatabaseModelSnapshot.cs b/LawFirm/LawFirmDatabaseImplement/Migrations/LawFirmDatabaseModelSnapshot.cs new file mode 100644 index 0000000..0dd2a7c --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Migrations/LawFirmDatabaseModelSnapshot.cs @@ -0,0 +1,166 @@ +// +using System; +using LawFirmDatabaseImplement_; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace LawFirmDatabaseImplement_.Migrations +{ + [DbContext(typeof(LawFirmDatabase))] + partial class LawFirmDatabaseModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.5") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("LawFirmDatabaseImplement_.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("LawFirmDatabaseImplement_.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("LawFirmDatabaseImplement_.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("LawFirmDatabaseImplement_.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("LawFirmDatabaseImplement_.Models.DocumentComponent", b => + { + b.HasOne("LawFirmDatabaseImplement_.Models.Component", "Component") + .WithMany("DocumentComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("LawFirmDatabaseImplement_.Models.Document", "Document") + .WithMany("Components") + .HasForeignKey("DocumentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Document"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement_.Models.Order", b => + { + b.HasOne("LawFirmDatabaseImplement_.Models.Document", null) + .WithMany("Orders") + .HasForeignKey("DocumentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement_.Models.Component", b => + { + b.Navigation("DocumentComponents"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement_.Models.Document", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/LawFirm/LawFirmDatabaseImplement/Models/Component.cs b/LawFirm/LawFirmDatabaseImplement/Models/Component.cs new file mode 100644 index 0000000..b6e1de3 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Models/Component.cs @@ -0,0 +1,69 @@ +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 LawFirmContracts.BindingModels; +using LawFirmContracts.ViewModels; +using LawFirmDataModels.Models; + +namespace LawFirmDatabaseImplement_.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/LawFirmDatabaseImplement/Models/Document.cs b/LawFirm/LawFirmDatabaseImplement/Models/Document.cs new file mode 100644 index 0000000..9453c9e --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Models/Document.cs @@ -0,0 +1,104 @@ +using LawFirmDataModels.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 LawFirmContracts.BindingModels; +using LawFirmContracts.ViewModels; + +namespace LawFirmDatabaseImplement_.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(LawFirmDatabase 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(LawFirmDatabase 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/LawFirmDatabaseImplement/Models/DocumentComponent.cs b/LawFirm/LawFirmDatabaseImplement/Models/DocumentComponent.cs new file mode 100644 index 0000000..5c67a66 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Models/DocumentComponent.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LawFirmDatabaseImplement_.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/LawFirmDatabaseImplement/Models/Order.cs b/LawFirm/LawFirmDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..04516a3 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Models/Order.cs @@ -0,0 +1,49 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.ViewModels; +using LawFirmDataModels.Enums; +using LawFirmDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace LawFirmDatabaseImplement_.Models +{ + public class Order : IOrderModel + { + public int DocumentId { get; set; } + + public int Count { get; set; } + + public double Sum { get; set; } + + public OrderStatus Status { get; set; } + + public DateTime DateCreate { get; set; } + + public DateTime? DateImplement { get; set; } + + public int Id { get; set; } + + public static Order? Create(XElement element) + { + return new Order(); + + } + + public void Update(OrderBindingModel? model) + { + + } + + public OrderViewModel GetViewModel => new() + { + + }; + + public XElement GetXElement => new("Order"); + + } +} diff --git a/LawFirm/LawFirmFileImplement/LawFirmFileImplement.csproj b/LawFirm/LawFirmFileImplement/LawFirmFileImplement.csproj index 77fe5b1..614e998 100644 --- a/LawFirm/LawFirmFileImplement/LawFirmFileImplement.csproj +++ b/LawFirm/LawFirmFileImplement/LawFirmFileImplement.csproj @@ -6,6 +6,17 @@ enable + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + diff --git a/LawFirm/LawFirmListImplement/LawFirmListImplement.csproj b/LawFirm/LawFirmListImplement/LawFirmListImplement.csproj index 77fe5b1..614e998 100644 --- a/LawFirm/LawFirmListImplement/LawFirmListImplement.csproj +++ b/LawFirm/LawFirmListImplement/LawFirmListImplement.csproj @@ -6,6 +6,17 @@ enable + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + diff --git a/LawFirm/LawFirmView/LawFirmView.csproj b/LawFirm/LawFirmView/LawFirmView.csproj index 0951ee9..dd89485 100644 --- a/LawFirm/LawFirmView/LawFirmView.csproj +++ b/LawFirm/LawFirmView/LawFirmView.csproj @@ -19,6 +19,14 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -28,6 +36,7 @@ + diff --git a/LawFirm/LawFirmView/Program.cs b/LawFirm/LawFirmView/Program.cs index 5b14d07..d40c93e 100644 --- a/LawFirm/LawFirmView/Program.cs +++ b/LawFirm/LawFirmView/Program.cs @@ -1,7 +1,7 @@ using LawFirmBusinessLogic.BusinessLogic; using LawFirmContracts.BusinessLogicsContracts; using LawFirmContracts.StoragesContracts; -using LawFirmFileImplement.Implements; +using LawFirmDatabaseImplement_.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging;