From 9b28d6a5f9de954377dc007961380cd411be2067 Mon Sep 17 00:00:00 2001 From: Danil Markov Date: Mon, 13 Mar 2023 19:57:35 +0400 Subject: [PATCH 01/10] Lab3 --- LawFirm/LawFirm.sln | 6 + .../BusinessLogics/OrderLogic.cs | 2 +- .../BindingModels/OrderBindingModel.cs | 2 +- .../Implements/BlankStorage.cs | 84 +++++++++ .../Implements/DocumentStorage.cs | 107 +++++++++++ .../Implements/OrderStorage.cs | 101 +++++++++++ .../LawFirmDatabase.cs | 27 +++ .../LawFirmDatabaseImplement.csproj | 24 +++ .../20230313153615_InitialCreate.Designer.cs | 171 ++++++++++++++++++ .../20230313153615_InitialCreate.cs | 126 +++++++++++++ .../LawFirmDatabaseModelSnapshot.cs | 168 +++++++++++++++++ .../LawFirmDatabaseImplement/Models/Blank.cs | 56 ++++++ .../Models/Document.cs | 95 ++++++++++ .../Models/DocumentBlank.cs | 18 ++ .../LawFirmDatabaseImplement/Models/Order.cs | 67 +++++++ LawFirm/LawFirmView/LawFirmView.csproj | 5 + LawFirm/LawFirmView/Program.cs | 2 +- 17 files changed, 1058 insertions(+), 3 deletions(-) create mode 100644 LawFirm/LawFirmDatabaseImplement/Implements/BlankStorage.cs create mode 100644 LawFirm/LawFirmDatabaseImplement/Implements/DocumentStorage.cs create mode 100644 LawFirm/LawFirmDatabaseImplement/Implements/OrderStorage.cs create mode 100644 LawFirm/LawFirmDatabaseImplement/LawFirmDatabase.cs create mode 100644 LawFirm/LawFirmDatabaseImplement/LawFirmDatabaseImplement.csproj create mode 100644 LawFirm/LawFirmDatabaseImplement/Migrations/20230313153615_InitialCreate.Designer.cs create mode 100644 LawFirm/LawFirmDatabaseImplement/Migrations/20230313153615_InitialCreate.cs create mode 100644 LawFirm/LawFirmDatabaseImplement/Migrations/LawFirmDatabaseModelSnapshot.cs create mode 100644 LawFirm/LawFirmDatabaseImplement/Models/Blank.cs create mode 100644 LawFirm/LawFirmDatabaseImplement/Models/Document.cs create mode 100644 LawFirm/LawFirmDatabaseImplement/Models/DocumentBlank.cs create mode 100644 LawFirm/LawFirmDatabaseImplement/Models/Order.cs diff --git a/LawFirm/LawFirm.sln b/LawFirm/LawFirm.sln index c2716e6..b860d97 100644 --- a/LawFirm/LawFirm.sln +++ b/LawFirm/LawFirm.sln @@ -19,6 +19,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LawFirmFileImplement", "Law {6F47A7CF-F09A-4B49-8563-9A04CD34A12C} = {6F47A7CF-F09A-4B49-8563-9A04CD34A12C} EndProjectSection EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LawFirmDatabaseImplement", "LawFirmDatabaseImplement\LawFirmDatabaseImplement.csproj", "{9DCD1DFD-676E-469A-8919-A8E66CDA34B4}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -49,6 +51,10 @@ Global {2A2A140A-BD69-428C-955B-A062C67B7473}.Debug|Any CPU.Build.0 = Debug|Any CPU {2A2A140A-BD69-428C-955B-A062C67B7473}.Release|Any CPU.ActiveCfg = Release|Any CPU {2A2A140A-BD69-428C-955B-A062C67B7473}.Release|Any CPU.Build.0 = Release|Any CPU + {9DCD1DFD-676E-469A-8919-A8E66CDA34B4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9DCD1DFD-676E-469A-8919-A8E66CDA34B4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9DCD1DFD-676E-469A-8919-A8E66CDA34B4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9DCD1DFD-676E-469A-8919-A8E66CDA34B4}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/LawFirm/LawFirmBusinessLogic/BusinessLogics/OrderLogic.cs b/LawFirm/LawFirmBusinessLogic/BusinessLogics/OrderLogic.cs index a87b215..6a9989b 100644 --- a/LawFirm/LawFirmBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/LawFirm/LawFirmBusinessLogic/BusinessLogics/OrderLogic.cs @@ -47,7 +47,7 @@ namespace LawFirmBusinessLogic.BusinessLogics return false; } model.Status = newStatus; - if (model.Status == OrderStatus.Готов) model.DateImplement = DateTime.Now; + if (model.Status == OrderStatus.Готов) model.DateImplement = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc); else { model.DateImplement = viewModel.DateImplement; diff --git a/LawFirm/LawFirmContracts/BindingModels/OrderBindingModel.cs b/LawFirm/LawFirmContracts/BindingModels/OrderBindingModel.cs index 4b582fe..358159b 100644 --- a/LawFirm/LawFirmContracts/BindingModels/OrderBindingModel.cs +++ b/LawFirm/LawFirmContracts/BindingModels/OrderBindingModel.cs @@ -10,7 +10,7 @@ namespace LawFirmContracts.BindingModels public int Count { get; set; } public double Sum { get; set; } public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; - public DateTime DateCreate { get; set; } = DateTime.Now; + public DateTime DateCreate { get; set; } = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc); public DateTime? DateImplement { get; set; } } } diff --git a/LawFirm/LawFirmDatabaseImplement/Implements/BlankStorage.cs b/LawFirm/LawFirmDatabaseImplement/Implements/BlankStorage.cs new file mode 100644 index 0000000..9ab873f --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Implements/BlankStorage.cs @@ -0,0 +1,84 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.SearchModels; +using LawFirmContracts.StoragesContracts; +using LawFirmContracts.ViewModels; +using LawFirmDatabaseImplement.Models; + +namespace LawFirmDatabaseImplement.Implements +{ + public class BlankStorage : IBlankStorage + { + public List GetFullList() + { + using var context = new LawFirmDatabase(); + return context.Blanks + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(BlankSearchModel + model) + { + if (string.IsNullOrEmpty(model.BlankName)) + { + return new(); + } + using var context = new LawFirmDatabase(); + return context.Blanks + .Where(x => x.BlankName.Contains(model.BlankName)) + .Select(x => x.GetViewModel) + .ToList(); + } + public BlankViewModel? GetElement(BlankSearchModel model) + { + if (string.IsNullOrEmpty(model.BlankName) && !model.Id.HasValue) + { + return null; + } + using var context = new LawFirmDatabase(); + return context.Blanks + .FirstOrDefault(x => + (!string.IsNullOrEmpty(model.BlankName) && x.BlankName == + model.BlankName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + public BlankViewModel? Insert(BlankBindingModel model) + { + var newBlank = Blank.Create(model); + if (newBlank == null) + { + return null; + } + using var context = new LawFirmDatabase(); + context.Blanks.Add(newBlank); + context.SaveChanges(); + return newBlank.GetViewModel; + } + public BlankViewModel? Update(BlankBindingModel model) + { + using var context = new LawFirmDatabase(); + var component = context.Blanks.FirstOrDefault(x => x.Id == + model.Id); + if (component == null) + { + return null; + } + component.Update(model); + context.SaveChanges(); + return component.GetViewModel; + } + public BlankViewModel? Delete(BlankBindingModel model) + { + using var context = new LawFirmDatabase(); + var element = context.Blanks.FirstOrDefault(rec => rec.Id == + model.Id); + if (element != null) + { + context.Blanks.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..9b1ad9a --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Implements/DocumentStorage.cs @@ -0,0 +1,107 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.SearchModels; +using LawFirmContracts.StoragesContracts; +using LawFirmContracts.ViewModels; +using LawFirmDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + + +namespace LawFirmDatabaseImplement.Implements +{ + public class DocumentStorage : IDocumentStorage + { + public List GetFullList() + { + using var context = new LawFirmDatabase(); + return context.Documents + .Include(x => x.Blanks) + .ThenInclude(x => x.Blank) + .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.Blanks) + .ThenInclude(x => x.Blank) + .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.Blanks) + .ThenInclude(x => x.Blank) + .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 product = context.Documents.FirstOrDefault(rec => + rec.Id == model.Id); + if (product == null) + { + return null; + } + product.Update(model); + context.SaveChanges(); + product.UpdateBlanks(context, model); + transaction.Commit(); + return product.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public DocumentViewModel? Delete(DocumentBindingModel model) + { + using var context = new LawFirmDatabase(); + var element = context.Documents + .Include(x => x.Blanks) + .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..a7fb3e1 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,101 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.SearchModels; +using LawFirmContracts.StoragesContracts; +using LawFirmContracts.ViewModels; +using LawFirmDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace LawFirmDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new LawFirmDatabase(); + + var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + + return GetViewModel(element); + } + + return null; + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + + using var context = new LawFirmDatabase(); + + return GetViewModel(context.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))); + } + + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + + using var context = new LawFirmDatabase(); + + return context.Orders.Where(x => x.Id == model.Id).Select(x => GetViewModel(x)).ToList(); + } + + public List GetFullList() + { + using var context = new LawFirmDatabase(); + + return context.Orders.Select(x => GetViewModel(x)).ToList(); + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + + using var context = new LawFirmDatabase(); + + context.Orders.Add(newOrder); + context.SaveChanges(); + return GetViewModel(newOrder); + } + + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new LawFirmDatabase(); + + var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + + if (order == null) + { + return null; + } + + order.Update(model); + context.SaveChanges(); + + return GetViewModel(order); + } + + private static OrderViewModel GetViewModel(Order order) + { + var viewModel = order.GetViewModel; + using var context = new LawFirmDatabase(); + var element = context.Documents.FirstOrDefault(x => x.Id == order.DocumentId); + viewModel.DocumentName = element.DocumentName; + return viewModel; + } + } +} diff --git a/LawFirm/LawFirmDatabaseImplement/LawFirmDatabase.cs b/LawFirm/LawFirmDatabaseImplement/LawFirmDatabase.cs new file mode 100644 index 0000000..7a0e0a6 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/LawFirmDatabase.cs @@ -0,0 +1,27 @@ +using LawFirmDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace LawFirmDatabaseImplement +{ + public class LawFirmDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseNpgsql(@" + Host=localhost; + Port=5432; + Database=RPP; + Username=postgres; + Password=123"); + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Blanks { set; get; } + public virtual DbSet Documents { set; get; } + public virtual DbSet DocumentBlanks { 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..a972da9 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/LawFirmDatabaseImplement.csproj @@ -0,0 +1,24 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + diff --git a/LawFirm/LawFirmDatabaseImplement/Migrations/20230313153615_InitialCreate.Designer.cs b/LawFirm/LawFirmDatabaseImplement/Migrations/20230313153615_InitialCreate.Designer.cs new file mode 100644 index 0000000..b1a5f47 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Migrations/20230313153615_InitialCreate.Designer.cs @@ -0,0 +1,171 @@ +// +using System; +using LawFirmDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace LawFirmDatabaseImplement.Migrations +{ + [DbContext(typeof(LawFirmDatabase))] + [Migration("20230313153615_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Blank", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("BlankName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Price") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Blanks"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Document", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("DocumentName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Price") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Documents"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.DocumentBlank", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("BlankId") + .HasColumnType("integer"); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("DocumentId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("BlankId"); + + b.HasIndex("DocumentId"); + + b.ToTable("DocumentBlanks"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("DateCreate") + .HasColumnType("timestamp with time zone"); + + b.Property("DateImplement") + .HasColumnType("timestamp with time zone"); + + b.Property("DocumentId") + .HasColumnType("integer"); + + b.Property("Status") + .HasColumnType("integer"); + + b.Property("Sum") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.HasIndex("DocumentId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.DocumentBlank", b => + { + b.HasOne("LawFirmDatabaseImplement.Models.Blank", "Blank") + .WithMany("DocumentBlanks") + .HasForeignKey("BlankId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("LawFirmDatabaseImplement.Models.Document", "Document") + .WithMany("Blanks") + .HasForeignKey("DocumentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Blank"); + + b.Navigation("Document"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Order", b => + { + b.HasOne("LawFirmDatabaseImplement.Models.Document", "Document") + .WithMany("Orders") + .HasForeignKey("DocumentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Document"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Blank", b => + { + b.Navigation("DocumentBlanks"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Document", b => + { + b.Navigation("Blanks"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/LawFirm/LawFirmDatabaseImplement/Migrations/20230313153615_InitialCreate.cs b/LawFirm/LawFirmDatabaseImplement/Migrations/20230313153615_InitialCreate.cs new file mode 100644 index 0000000..36edd73 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Migrations/20230313153615_InitialCreate.cs @@ -0,0 +1,126 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace LawFirmDatabaseImplement.Migrations +{ + /// + public partial class InitialCreate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Blanks", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + BlankName = table.Column(type: "text", nullable: false), + Price = table.Column(type: "double precision", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Blanks", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Documents", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + DocumentName = table.Column(type: "text", nullable: false), + Price = table.Column(type: "double precision", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Documents", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "DocumentBlanks", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + DocumentId = table.Column(type: "integer", nullable: false), + BlankId = table.Column(type: "integer", nullable: false), + Count = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_DocumentBlanks", x => x.Id); + table.ForeignKey( + name: "FK_DocumentBlanks_Blanks_BlankId", + column: x => x.BlankId, + principalTable: "Blanks", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_DocumentBlanks_Documents_DocumentId", + column: x => x.DocumentId, + principalTable: "Documents", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + DocumentId = table.Column(type: "integer", nullable: false), + Count = table.Column(type: "integer", nullable: false), + Sum = table.Column(type: "double precision", nullable: false), + Status = table.Column(type: "integer", nullable: false), + DateCreate = table.Column(type: "timestamp with time zone", nullable: false), + DateImplement = table.Column(type: "timestamp with time zone", 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_DocumentBlanks_BlankId", + table: "DocumentBlanks", + column: "BlankId"); + + migrationBuilder.CreateIndex( + name: "IX_DocumentBlanks_DocumentId", + table: "DocumentBlanks", + column: "DocumentId"); + + migrationBuilder.CreateIndex( + name: "IX_Orders_DocumentId", + table: "Orders", + column: "DocumentId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "DocumentBlanks"); + + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "Blanks"); + + migrationBuilder.DropTable( + name: "Documents"); + } + } +} diff --git a/LawFirm/LawFirmDatabaseImplement/Migrations/LawFirmDatabaseModelSnapshot.cs b/LawFirm/LawFirmDatabaseImplement/Migrations/LawFirmDatabaseModelSnapshot.cs new file mode 100644 index 0000000..3a405da --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Migrations/LawFirmDatabaseModelSnapshot.cs @@ -0,0 +1,168 @@ +// +using System; +using LawFirmDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#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.3") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Blank", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("BlankName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Price") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Blanks"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Document", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("DocumentName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Price") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Documents"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.DocumentBlank", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("BlankId") + .HasColumnType("integer"); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("DocumentId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("BlankId"); + + b.HasIndex("DocumentId"); + + b.ToTable("DocumentBlanks"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("DateCreate") + .HasColumnType("timestamp with time zone"); + + b.Property("DateImplement") + .HasColumnType("timestamp with time zone"); + + b.Property("DocumentId") + .HasColumnType("integer"); + + b.Property("Status") + .HasColumnType("integer"); + + b.Property("Sum") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.HasIndex("DocumentId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.DocumentBlank", b => + { + b.HasOne("LawFirmDatabaseImplement.Models.Blank", "Blank") + .WithMany("DocumentBlanks") + .HasForeignKey("BlankId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("LawFirmDatabaseImplement.Models.Document", "Document") + .WithMany("Blanks") + .HasForeignKey("DocumentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Blank"); + + b.Navigation("Document"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Order", b => + { + b.HasOne("LawFirmDatabaseImplement.Models.Document", "Document") + .WithMany("Orders") + .HasForeignKey("DocumentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Document"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Blank", b => + { + b.Navigation("DocumentBlanks"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Document", b => + { + b.Navigation("Blanks"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/LawFirm/LawFirmDatabaseImplement/Models/Blank.cs b/LawFirm/LawFirmDatabaseImplement/Models/Blank.cs new file mode 100644 index 0000000..69c6f5b --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Models/Blank.cs @@ -0,0 +1,56 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.ViewModels; +using LawFirmDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace LawFirmDatabaseImplement.Models +{ + public class Blank : IBlankModel + { + public int Id { get; private set; } + [Required] + public string BlankName { get; private set; } = string.Empty; + [Required] + public double Price { get; set; } + [ForeignKey("BlankId")] + public virtual List DocumentBlanks { get; set; } = new(); + public static Blank? Create(BlankBindingModel model) + { + if (model == null) + { + return null; + } + return new Blank() + { + Id = model.Id, + BlankName = model.BlankName, + Price = model.Price + }; + } + public static Blank Create(BlankViewModel model) + { + return new Blank + { + Id = model.Id, + BlankName = model.BlankName, + Price = model.Price + }; + } + public void Update(BlankBindingModel model) + { + if (model == null) + { + return; + } + BlankName = model.BlankName; + Price = model.Price; + } + public BlankViewModel GetViewModel => new() + { + Id = Id, + BlankName = BlankName, + Price = Price + }; + } +} diff --git a/LawFirm/LawFirmDatabaseImplement/Models/Document.cs b/LawFirm/LawFirmDatabaseImplement/Models/Document.cs new file mode 100644 index 0000000..5e3db24 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Models/Document.cs @@ -0,0 +1,95 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.ViewModels; +using LawFirmDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +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? _productBlanks = null; + [NotMapped] + public Dictionary DocumentBlanks + { + get + { + if (_productBlanks == null) + { + _productBlanks = Blanks + .ToDictionary(recPC => recPC.BlankId, recPC => + (recPC.Blank as IBlankModel, recPC.Count)); + } + return _productBlanks; + } + } + [ForeignKey("DocumentId")] + public virtual List Blanks { 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, + Blanks = model.DocumentBlanks.Select(x => new DocumentBlank + { + Blank = context.Blanks.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, + DocumentBlanks = DocumentBlanks + }; + public void UpdateBlanks(LawFirmDatabase context, + DocumentBindingModel model) + { + var productBlanks = context.DocumentBlanks.Where(rec => + rec.DocumentId == model.Id).ToList(); + if (productBlanks != null && productBlanks.Count > 0) + { // удалили те, которых нет в модели + context.DocumentBlanks.RemoveRange(productBlanks.Where(rec + => !model.DocumentBlanks.ContainsKey(rec.BlankId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateBlank in productBlanks) + { + updateBlank.Count = + model.DocumentBlanks[updateBlank.BlankId].Item2; + model.DocumentBlanks.Remove(updateBlank.BlankId); + } + context.SaveChanges(); + } + var product = context.Documents.First(x => x.Id == Id); + foreach (var pc in model.DocumentBlanks) + { + context.DocumentBlanks.Add(new DocumentBlank + { + Document = product, + Blank = context.Blanks.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _productBlanks = null; + } + + } +} diff --git a/LawFirm/LawFirmDatabaseImplement/Models/DocumentBlank.cs b/LawFirm/LawFirmDatabaseImplement/Models/DocumentBlank.cs new file mode 100644 index 0000000..ec1a6a9 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Models/DocumentBlank.cs @@ -0,0 +1,18 @@ +using System.ComponentModel; +using System.ComponentModel.DataAnnotations; + +namespace LawFirmDatabaseImplement.Models +{ + public class DocumentBlank + { + public int Id { get; set; } + [Required] + public int DocumentId { get; set; } + [Required] + public int BlankId { get; set; } + [Required] + public int Count { get; set; } + public virtual Blank Blank { 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..1f72f68 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Models/Order.cs @@ -0,0 +1,67 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.ViewModels; +using LawFirmDataModels.Enums; +using LawFirmDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace LawFirmDatabaseImplement.Models +{ + public class Order : IOrderModel + { + [Required] + public int DocumentId { get; set; } + [Required] + public int Count { get; set; } + [Required] + public double Sum { get; set; } + [Required] + public OrderStatus Status { get; set; } + [Required] + public DateTime DateCreate { get; set; } + + public DateTime? DateImplement { get; set; } + + public int Id { get; set; } + public virtual Document Document { get; set; } + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + DocumentId = model.DocumentId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement + }; + } + + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + Status = model.Status; + DateImplement = model.DateImplement; + } + + public OrderViewModel GetViewModel => new() + { + Id = Id, + DocumentId = DocumentId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + DocumentName = Document.DocumentName + }; + } +} diff --git a/LawFirm/LawFirmView/LawFirmView.csproj b/LawFirm/LawFirmView/LawFirmView.csproj index 5a20cce..ae9cbfe 100644 --- a/LawFirm/LawFirmView/LawFirmView.csproj +++ b/LawFirm/LawFirmView/LawFirmView.csproj @@ -9,6 +9,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -16,6 +20,7 @@ + diff --git a/LawFirm/LawFirmView/Program.cs b/LawFirm/LawFirmView/Program.cs index e58d758..4caede4 100644 --- a/LawFirm/LawFirmView/Program.cs +++ b/LawFirm/LawFirmView/Program.cs @@ -1,7 +1,7 @@ using LawFirmBusinessLogic.BusinessLogics; using LawFirmContracts.BusinessLogicsContracts; using LawFirmContracts.StoragesContracts; -using LawFirmFileImplement.Implements; +using LawFirmDatabaseImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; -- 2.25.1 From 1779a803a989ad9e58e1a47874aceebedd113cb3 Mon Sep 17 00:00:00 2001 From: Danil Markov Date: Fri, 21 Apr 2023 13:44:19 +0400 Subject: [PATCH 02/10] fix --- .../LawFirmBusinessLogic/BusinessLogics/OrderLogic.cs | 2 +- LawFirm/LawFirmFileImplement/DataFileSingleton.cs | 9 ++++++--- .../LawFirmFileImplement/Implements/DocumentStorage.cs | 8 ++++---- LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs | 6 +++--- LawFirm/LawFirmFileImplement/Models/Document.cs | 3 +-- 5 files changed, 15 insertions(+), 13 deletions(-) diff --git a/LawFirm/LawFirmBusinessLogic/BusinessLogics/OrderLogic.cs b/LawFirm/LawFirmBusinessLogic/BusinessLogics/OrderLogic.cs index a87b215..07d30c1 100644 --- a/LawFirm/LawFirmBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/LawFirm/LawFirmBusinessLogic/BusinessLogics/OrderLogic.cs @@ -52,7 +52,7 @@ namespace LawFirmBusinessLogic.BusinessLogics { model.DateImplement = viewModel.DateImplement; } - CheckModel(model); + CheckModel(model, false); if (_orderStorage.Update(model) == null) { model.Status--; diff --git a/LawFirm/LawFirmFileImplement/DataFileSingleton.cs b/LawFirm/LawFirmFileImplement/DataFileSingleton.cs index dba4b10..a4880e5 100644 --- a/LawFirm/LawFirmFileImplement/DataFileSingleton.cs +++ b/LawFirm/LawFirmFileImplement/DataFileSingleton.cs @@ -27,15 +27,18 @@ namespace LawFirmFileImplement { Blanks = LoadData(BlankFileName, "Blank", x => Blank.Create(x)!)!; Documents = LoadData(DocumentFileName, "Document", x => Document.Create(x)!)!; - Orders = new List(); + Orders = LoadData(DocumentFileName, "Order", x => Document.Create(x)!)!; } private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) { if (File.Exists(filename)) { - return - XDocument.Load(filename)?.Root?.Elements(xmlNodeName)?.Select(selectFunction)?.ToList(); + return XDocument.Load(filename)?. + Root?. + Elements(xmlNodeName)?. + Select(selectFunction)?. + ToList(); } return new List(); } diff --git a/LawFirm/LawFirmFileImplement/Implements/DocumentStorage.cs b/LawFirm/LawFirmFileImplement/Implements/DocumentStorage.cs index 559f389..7a0d8da 100644 --- a/LawFirm/LawFirmFileImplement/Implements/DocumentStorage.cs +++ b/LawFirm/LawFirmFileImplement/Implements/DocumentStorage.cs @@ -50,14 +50,14 @@ namespace LawFirmFileImplement.Implements } public DocumentViewModel? Update(DocumentBindingModel model) { - var ship = source.Documents.FirstOrDefault(x => x.Id == model.Id); - if (ship == null) + var document = source.Documents.FirstOrDefault(x => x.Id == model.Id); + if (document == null) { return null; } - ship.Update(model); + document.Update(model); source.SaveDocuments(); - return ship.GetViewModel; + return document.GetViewModel; } public DocumentViewModel? Delete(DocumentBindingModel model) { diff --git a/LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs b/LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs index 783ef1a..0a958dc 100644 --- a/LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs +++ b/LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs @@ -38,10 +38,10 @@ namespace LawFirmFileImplement.Implements private OrderViewModel GetViewModel(Order order) { var viewModel = order.GetViewModel; - var ship = source.Documents.FirstOrDefault(x => x.Id == order.DocumentId); - if (ship != null) + var document = source.Documents.FirstOrDefault(x => x.Id == order.DocumentId); + if (document != null) { - viewModel.DocumentName = ship.DocumentName; + viewModel.DocumentName = document.DocumentName; } return viewModel; } diff --git a/LawFirm/LawFirmFileImplement/Models/Document.cs b/LawFirm/LawFirmFileImplement/Models/Document.cs index 9c2eca2..071a25d 100644 --- a/LawFirm/LawFirmFileImplement/Models/Document.cs +++ b/LawFirm/LawFirmFileImplement/Models/Document.cs @@ -11,8 +11,7 @@ namespace LawFirmFileImplement.Models public string DocumentName { get; private set; } = string.Empty; public double Price { get; private set; } public Dictionary Blanks { get; private set; } = new(); - private Dictionary? _productBlanks = - null; + private Dictionary? _productBlanks = null; public Dictionary DocumentBlanks { get -- 2.25.1 From 2419d6a9f764f25c461282663b5f6abe8e2a4085 Mon Sep 17 00:00:00 2001 From: Danil Markov Date: Fri, 21 Apr 2023 13:51:31 +0400 Subject: [PATCH 03/10] fix --- .../BusinessLogics/OrderLogic.cs | 2 +- .../Models/Document.cs | 22 +++++++++---------- .../Implements/DocumentStorage.cs | 8 +++---- .../Implements/OrderStorage.cs | 6 ++--- .../LawFirmFileImplement/Models/Document.cs | 10 ++++----- LawFirm/LawFirmView/FormCreateOrder.cs | 4 ++-- 6 files changed, 26 insertions(+), 26 deletions(-) diff --git a/LawFirm/LawFirmBusinessLogic/BusinessLogics/OrderLogic.cs b/LawFirm/LawFirmBusinessLogic/BusinessLogics/OrderLogic.cs index 6a9989b..60efcf0 100644 --- a/LawFirm/LawFirmBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/LawFirm/LawFirmBusinessLogic/BusinessLogics/OrderLogic.cs @@ -52,7 +52,7 @@ namespace LawFirmBusinessLogic.BusinessLogics { model.DateImplement = viewModel.DateImplement; } - CheckModel(model); + CheckModel(model, false); if (_orderStorage.Update(model) == null) { model.Status--; diff --git a/LawFirm/LawFirmDatabaseImplement/Models/Document.cs b/LawFirm/LawFirmDatabaseImplement/Models/Document.cs index 5e3db24..166110d 100644 --- a/LawFirm/LawFirmDatabaseImplement/Models/Document.cs +++ b/LawFirm/LawFirmDatabaseImplement/Models/Document.cs @@ -13,19 +13,19 @@ namespace LawFirmDatabaseImplement.Models public string DocumentName { get; set; } = string.Empty; [Required] public double Price { get; set; } - private Dictionary? _productBlanks = null; + private Dictionary? _documentBlanks = null; [NotMapped] public Dictionary DocumentBlanks { get { - if (_productBlanks == null) + if (_documentBlanks == null) { - _productBlanks = Blanks + _documentBlanks = Blanks .ToDictionary(recPC => recPC.BlankId, recPC => (recPC.Blank as IBlankModel, recPC.Count)); } - return _productBlanks; + return _documentBlanks; } } [ForeignKey("DocumentId")] @@ -61,15 +61,15 @@ namespace LawFirmDatabaseImplement.Models public void UpdateBlanks(LawFirmDatabase context, DocumentBindingModel model) { - var productBlanks = context.DocumentBlanks.Where(rec => + var documentBlanks = context.DocumentBlanks.Where(rec => rec.DocumentId == model.Id).ToList(); - if (productBlanks != null && productBlanks.Count > 0) + if (documentBlanks != null && documentBlanks.Count > 0) { // удалили те, которых нет в модели - context.DocumentBlanks.RemoveRange(productBlanks.Where(rec + context.DocumentBlanks.RemoveRange(documentBlanks.Where(rec => !model.DocumentBlanks.ContainsKey(rec.BlankId))); context.SaveChanges(); // обновили количество у существующих записей - foreach (var updateBlank in productBlanks) + foreach (var updateBlank in documentBlanks) { updateBlank.Count = model.DocumentBlanks[updateBlank.BlankId].Item2; @@ -77,18 +77,18 @@ namespace LawFirmDatabaseImplement.Models } context.SaveChanges(); } - var product = context.Documents.First(x => x.Id == Id); + var document = context.Documents.First(x => x.Id == Id); foreach (var pc in model.DocumentBlanks) { context.DocumentBlanks.Add(new DocumentBlank { - Document = product, + Document = document, Blank = context.Blanks.First(x => x.Id == pc.Key), Count = pc.Value.Item2 }); context.SaveChanges(); } - _productBlanks = null; + _documentBlanks = null; } } diff --git a/LawFirm/LawFirmFileImplement/Implements/DocumentStorage.cs b/LawFirm/LawFirmFileImplement/Implements/DocumentStorage.cs index 559f389..7a0d8da 100644 --- a/LawFirm/LawFirmFileImplement/Implements/DocumentStorage.cs +++ b/LawFirm/LawFirmFileImplement/Implements/DocumentStorage.cs @@ -50,14 +50,14 @@ namespace LawFirmFileImplement.Implements } public DocumentViewModel? Update(DocumentBindingModel model) { - var ship = source.Documents.FirstOrDefault(x => x.Id == model.Id); - if (ship == null) + var document = source.Documents.FirstOrDefault(x => x.Id == model.Id); + if (document == null) { return null; } - ship.Update(model); + document.Update(model); source.SaveDocuments(); - return ship.GetViewModel; + return document.GetViewModel; } public DocumentViewModel? Delete(DocumentBindingModel model) { diff --git a/LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs b/LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs index 783ef1a..0a958dc 100644 --- a/LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs +++ b/LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs @@ -38,10 +38,10 @@ namespace LawFirmFileImplement.Implements private OrderViewModel GetViewModel(Order order) { var viewModel = order.GetViewModel; - var ship = source.Documents.FirstOrDefault(x => x.Id == order.DocumentId); - if (ship != null) + var document = source.Documents.FirstOrDefault(x => x.Id == order.DocumentId); + if (document != null) { - viewModel.DocumentName = ship.DocumentName; + viewModel.DocumentName = document.DocumentName; } return viewModel; } diff --git a/LawFirm/LawFirmFileImplement/Models/Document.cs b/LawFirm/LawFirmFileImplement/Models/Document.cs index 9c2eca2..83afe20 100644 --- a/LawFirm/LawFirmFileImplement/Models/Document.cs +++ b/LawFirm/LawFirmFileImplement/Models/Document.cs @@ -11,20 +11,20 @@ namespace LawFirmFileImplement.Models public string DocumentName { get; private set; } = string.Empty; public double Price { get; private set; } public Dictionary Blanks { get; private set; } = new(); - private Dictionary? _productBlanks = + private Dictionary? _documentBlanks = null; public Dictionary DocumentBlanks { get { - if (_productBlanks == null) + if (_documentBlanks == null) { var source = DataFileSingleton.GetInstance(); - _productBlanks = Blanks.ToDictionary(x => x.Key, y => + _documentBlanks = Blanks.ToDictionary(x => x.Key, y => ((source.Blanks.FirstOrDefault(z => z.Id == y.Key) as IBlankModel)!, y.Value)); } - return _productBlanks; + return _documentBlanks; } } public static Document? Create(DocumentBindingModel model) @@ -70,7 +70,7 @@ namespace LawFirmFileImplement.Models Price = model.Price; Blanks = model.DocumentBlanks.ToDictionary(x => x.Key, x => x.Value.Item2); - _productBlanks = null; + _documentBlanks = null; } public DocumentViewModel GetViewModel => new() { diff --git a/LawFirm/LawFirmView/FormCreateOrder.cs b/LawFirm/LawFirmView/FormCreateOrder.cs index e971622..f6097f4 100644 --- a/LawFirm/LawFirmView/FormCreateOrder.cs +++ b/LawFirm/LawFirmView/FormCreateOrder.cs @@ -45,12 +45,12 @@ namespace LawFirmView try { int id = Convert.ToInt32(comboBoxDocument.SelectedValue); - var product = _logicD.ReadElement(new DocumentSearchModel + var document = _logicD.ReadElement(new DocumentSearchModel { Id = id }); int count = Convert.ToInt32(textBoxCount.Text); - textBoxSum.Text = Math.Round(count * (product?.Price ?? 0), 2).ToString(); + textBoxSum.Text = Math.Round(count * (document?.Price ?? 0), 2).ToString(); _logger.LogInformation("Расчет суммы заказа"); } catch (Exception ex) -- 2.25.1 From bca8b0373a9c933238c5ac63552b7e9476229b3d Mon Sep 17 00:00:00 2001 From: Danil Markov Date: Fri, 21 Apr 2023 13:55:02 +0400 Subject: [PATCH 04/10] fix --- LawFirm/LawFirmFileImplement/Models/Document.cs | 10 +++++----- LawFirm/LawFirmView/FormCreateOrder.cs | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/LawFirm/LawFirmFileImplement/Models/Document.cs b/LawFirm/LawFirmFileImplement/Models/Document.cs index 071a25d..117f779 100644 --- a/LawFirm/LawFirmFileImplement/Models/Document.cs +++ b/LawFirm/LawFirmFileImplement/Models/Document.cs @@ -11,19 +11,19 @@ namespace LawFirmFileImplement.Models public string DocumentName { get; private set; } = string.Empty; public double Price { get; private set; } public Dictionary Blanks { get; private set; } = new(); - private Dictionary? _productBlanks = null; + private Dictionary? _documentBlanks = null; public Dictionary DocumentBlanks { get { - if (_productBlanks == null) + if (_documentBlanks == null) { var source = DataFileSingleton.GetInstance(); - _productBlanks = Blanks.ToDictionary(x => x.Key, y => + _documentBlanks = Blanks.ToDictionary(x => x.Key, y => ((source.Blanks.FirstOrDefault(z => z.Id == y.Key) as IBlankModel)!, y.Value)); } - return _productBlanks; + return _documentBlanks; } } public static Document? Create(DocumentBindingModel model) @@ -69,7 +69,7 @@ namespace LawFirmFileImplement.Models Price = model.Price; Blanks = model.DocumentBlanks.ToDictionary(x => x.Key, x => x.Value.Item2); - _productBlanks = null; + _documentBlanks = null; } public DocumentViewModel GetViewModel => new() { diff --git a/LawFirm/LawFirmView/FormCreateOrder.cs b/LawFirm/LawFirmView/FormCreateOrder.cs index e971622..f6097f4 100644 --- a/LawFirm/LawFirmView/FormCreateOrder.cs +++ b/LawFirm/LawFirmView/FormCreateOrder.cs @@ -45,12 +45,12 @@ namespace LawFirmView try { int id = Convert.ToInt32(comboBoxDocument.SelectedValue); - var product = _logicD.ReadElement(new DocumentSearchModel + var document = _logicD.ReadElement(new DocumentSearchModel { Id = id }); int count = Convert.ToInt32(textBoxCount.Text); - textBoxSum.Text = Math.Round(count * (product?.Price ?? 0), 2).ToString(); + textBoxSum.Text = Math.Round(count * (document?.Price ?? 0), 2).ToString(); _logger.LogInformation("Расчет суммы заказа"); } catch (Exception ex) -- 2.25.1 From cb8fb410b36454c46d60ae34d8ec842b3ace2ac6 Mon Sep 17 00:00:00 2001 From: Danil Markov Date: Fri, 21 Apr 2023 13:56:13 +0400 Subject: [PATCH 05/10] fix --- LawFirm/LawFirmFileImplement/DataFileSingleton.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LawFirm/LawFirmFileImplement/DataFileSingleton.cs b/LawFirm/LawFirmFileImplement/DataFileSingleton.cs index dba4b10..6c0c8fd 100644 --- a/LawFirm/LawFirmFileImplement/DataFileSingleton.cs +++ b/LawFirm/LawFirmFileImplement/DataFileSingleton.cs @@ -27,7 +27,7 @@ namespace LawFirmFileImplement { Blanks = LoadData(BlankFileName, "Blank", x => Blank.Create(x)!)!; Documents = LoadData(DocumentFileName, "Document", x => Document.Create(x)!)!; - Orders = new List(); + Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; } private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) -- 2.25.1 From e7a14dbda638a21d2e4b33c9f4fbab5a3bc922f5 Mon Sep 17 00:00:00 2001 From: Danil Markov Date: Fri, 21 Apr 2023 13:56:36 +0400 Subject: [PATCH 06/10] fix --- LawFirm/LawFirmFileImplement/DataFileSingleton.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LawFirm/LawFirmFileImplement/DataFileSingleton.cs b/LawFirm/LawFirmFileImplement/DataFileSingleton.cs index a4880e5..b4acfca 100644 --- a/LawFirm/LawFirmFileImplement/DataFileSingleton.cs +++ b/LawFirm/LawFirmFileImplement/DataFileSingleton.cs @@ -27,7 +27,7 @@ namespace LawFirmFileImplement { Blanks = LoadData(BlankFileName, "Blank", x => Blank.Create(x)!)!; Documents = LoadData(DocumentFileName, "Document", x => Document.Create(x)!)!; - Orders = LoadData(DocumentFileName, "Order", x => Document.Create(x)!)!; + Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; } private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) -- 2.25.1 From 8d0be1216cb7423dead2d6966b91714fd7f79e6a Mon Sep 17 00:00:00 2001 From: Danil Markov Date: Fri, 21 Apr 2023 14:32:11 +0400 Subject: [PATCH 07/10] fix --- .../LawFirmFileImplement/DataFileSingleton.cs | 97 ++++---- .../Implements/DocumentStorage.cs | 132 +++++------ .../Implements/OrderStorage.cs | 148 ++++++------ .../LawFirmFileImplement/Models/Document.cs | 167 +++++++------ LawFirm/LawFirmView/FormCreateOrder.cs | 220 +++++++++--------- 5 files changed, 383 insertions(+), 381 deletions(-) diff --git a/LawFirm/LawFirmFileImplement/DataFileSingleton.cs b/LawFirm/LawFirmFileImplement/DataFileSingleton.cs index 6c0c8fd..8ed511f 100644 --- a/LawFirm/LawFirmFileImplement/DataFileSingleton.cs +++ b/LawFirm/LawFirmFileImplement/DataFileSingleton.cs @@ -3,50 +3,53 @@ using System.Xml.Linq; namespace LawFirmFileImplement { - internal class DataFileSingleton - { - private static DataFileSingleton? instance; - private readonly string BlankFileName = "Blank.xml"; - private readonly string OrderFileName = "Order.xml"; - private readonly string DocumentFileName = "Document.xml"; - public List Blanks { get; private set; } - public List Orders { get; private set; } - public List Documents { get; private set; } - public static DataFileSingleton GetInstance() - { - if (instance == null) - { - instance = new DataFileSingleton(); - } - return instance; - } - public void SaveBlanks() => SaveData(Blanks, BlankFileName, "Blanks", x => x.GetXElement); - public void SaveDocuments() => SaveData(Documents, DocumentFileName, "Documents", x => x.GetXElement); - public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); - private DataFileSingleton() - { - Blanks = LoadData(BlankFileName, "Blank", x => Blank.Create(x)!)!; - Documents = LoadData(DocumentFileName, "Document", x => Document.Create(x)!)!; - Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; - } - private static List? LoadData(string filename, string xmlNodeName, - Func selectFunction) - { - if (File.Exists(filename)) - { - return - XDocument.Load(filename)?.Root?.Elements(xmlNodeName)?.Select(selectFunction)?.ToList(); - } - return new List(); - } - private static void SaveData(List data, string filename, - string xmlNodeName, Func selectFunction) - { - if (data != null) - { - new XDocument(new XElement(xmlNodeName, - data.Select(selectFunction).ToArray())).Save(filename); - } - } - } -} + internal class DataFileSingleton + { + private static DataFileSingleton? instance; + private readonly string BlankFileName = "Blank.xml"; + private readonly string OrderFileName = "Order.xml"; + private readonly string DocumentFileName = "Document.xml"; + public List Blanks { get; private set; } + public List Orders { get; private set; } + public List Documents { get; private set; } + public static DataFileSingleton GetInstance() + { + if (instance == null) + { + instance = new DataFileSingleton(); + } + return instance; + } + public void SaveBlanks() => SaveData(Blanks, BlankFileName, "Blanks", x => x.GetXElement); + public void SaveDocuments() => SaveData(Documents, DocumentFileName, "Documents", x => x.GetXElement); + public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); + private DataFileSingleton() + { + Blanks = LoadData(BlankFileName, "Blank", x => Blank.Create(x)!)!; + Documents = LoadData(DocumentFileName, "Document", x => Document.Create(x)!)!; + Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; + } + private static List? LoadData(string filename, string xmlNodeName, + Func selectFunction) + { + if (File.Exists(filename)) + { + return XDocument.Load(filename)?. + Root?. + Elements(xmlNodeName)?. + Select(selectFunction)?. + ToList(); + } + return new List(); + } + private static void SaveData(List data, string filename, + string xmlNodeName, Func selectFunction) + { + if (data != null) + { + new XDocument(new XElement(xmlNodeName, + data.Select(selectFunction).ToArray())).Save(filename); + } + } + } +} \ No newline at end of file diff --git a/LawFirm/LawFirmFileImplement/Implements/DocumentStorage.cs b/LawFirm/LawFirmFileImplement/Implements/DocumentStorage.cs index 7a0d8da..9b8fc32 100644 --- a/LawFirm/LawFirmFileImplement/Implements/DocumentStorage.cs +++ b/LawFirm/LawFirmFileImplement/Implements/DocumentStorage.cs @@ -6,69 +6,69 @@ using LawFirmFileImplement.Models; namespace LawFirmFileImplement.Implements { - public class DocumentStorage : IDocumentStorage - { - private readonly DataFileSingleton source; - public DocumentStorage() - { - source = DataFileSingleton.GetInstance(); - } - public List GetFullList() - { - return source.Documents.Select(x => x.GetViewModel).ToList(); - } - public List GetFilteredList(DocumentSearchModel model) - { - if (string.IsNullOrEmpty(model.DocumentName)) - { - return new(); - } - return source.Documents.Where(x => - x.DocumentName.Contains(model.DocumentName)).Select(x => x.GetViewModel).ToList(); - } - public DocumentViewModel? GetElement(DocumentSearchModel model) - { - if (string.IsNullOrEmpty(model.DocumentName) && !model.Id.HasValue) - { - return null; - } - return source.Documents.FirstOrDefault(x => - (!string.IsNullOrEmpty(model.DocumentName) && x.DocumentName == - model.DocumentName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; - } - public DocumentViewModel? Insert(DocumentBindingModel model) - { - model.Id = source.Documents.Count > 0 ? source.Documents.Max(x => x.Id) + 1 : 1; - var newDocument = Document.Create(model); - if (newDocument == null) - { - return null; - } - source.Documents.Add(newDocument); - source.SaveDocuments(); - return newDocument.GetViewModel; - } - public DocumentViewModel? Update(DocumentBindingModel model) - { - var document = source.Documents.FirstOrDefault(x => x.Id == model.Id); - if (document == null) - { - return null; - } - document.Update(model); - source.SaveDocuments(); - return document.GetViewModel; - } - public DocumentViewModel? Delete(DocumentBindingModel model) - { - var element = source.Documents.FirstOrDefault(x => x.Id == model.Id); - if (element != null) - { - source.Documents.Remove(element); - source.SaveDocuments(); - return element.GetViewModel; - } - return null; - } - } -} + public class DocumentStorage : IDocumentStorage + { + private readonly DataFileSingleton source; + public DocumentStorage() + { + source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return source.Documents.Select(x => x.GetViewModel).ToList(); + } + public List GetFilteredList(DocumentSearchModel model) + { + if (string.IsNullOrEmpty(model.DocumentName)) + { + return new(); + } + return source.Documents.Where(x => + x.DocumentName.Contains(model.DocumentName)).Select(x => x.GetViewModel).ToList(); + } + public DocumentViewModel? GetElement(DocumentSearchModel model) + { + if (string.IsNullOrEmpty(model.DocumentName) && !model.Id.HasValue) + { + return null; + } + return source.Documents.FirstOrDefault(x => + (!string.IsNullOrEmpty(model.DocumentName) && x.DocumentName == + model.DocumentName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + public DocumentViewModel? Insert(DocumentBindingModel model) + { + model.Id = source.Documents.Count > 0 ? source.Documents.Max(x => x.Id) + 1 : 1; + var newDocument = Document.Create(model); + if (newDocument == null) + { + return null; + } + source.Documents.Add(newDocument); + source.SaveDocuments(); + return newDocument.GetViewModel; + } + public DocumentViewModel? Update(DocumentBindingModel model) + { + var document = source.Documents.FirstOrDefault(x => x.Id == model.Id); + if (document == null) + { + return null; + } + document.Update(model); + source.SaveDocuments(); + return document.GetViewModel; + } + public DocumentViewModel? Delete(DocumentBindingModel model) + { + var element = source.Documents.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + source.Documents.Remove(element); + source.SaveDocuments(); + return element.GetViewModel; + } + return null; + } + } +} \ No newline at end of file diff --git a/LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs b/LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs index 0a958dc..58e0231 100644 --- a/LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs +++ b/LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs @@ -6,78 +6,78 @@ using LawFirmFileImplement.Models; namespace LawFirmFileImplement.Implements { - public class OrderStorage : IOrderStorage - { - private readonly DataFileSingleton source; - public OrderStorage() - { - source = DataFileSingleton.GetInstance(); - } - public List GetFullList() - { - return source.Orders.Select(x => GetViewModel(x)).ToList(); - } - public List GetFilteredList(OrderSearchModel model) - { - if (!model.Id.HasValue) - { - return new(); - } - return source.Orders.Where(x => x.Id.Equals(model.Id)).Select(x => GetViewModel(x)).ToList(); - } - public OrderViewModel? GetElement(OrderSearchModel model) - { - if (!model.Id.HasValue) - { - return null; - } + public class OrderStorage : IOrderStorage + { + private readonly DataFileSingleton source; + public OrderStorage() + { + source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return source.Orders.Select(x => GetViewModel(x)).ToList(); + } + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + return source.Orders.Where(x => x.Id.Equals(model.Id)).Select(x => GetViewModel(x)).ToList(); + } + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } - return source.Orders.FirstOrDefault(x => - (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; - } - private OrderViewModel GetViewModel(Order order) - { - var viewModel = order.GetViewModel; - var document = source.Documents.FirstOrDefault(x => x.Id == order.DocumentId); - if (document != null) - { - viewModel.DocumentName = document.DocumentName; - } - return viewModel; - } - public OrderViewModel? Insert(OrderBindingModel model) - { - model.Id = source.Orders.Count > 0 ? source.Orders.Max(x => x.Id) + 1 : 1; - var newOrder = Order.Create(model); - if (newOrder == null) - { - return null; - } - source.Orders.Add(newOrder); - source.SaveOrders(); - return GetViewModel(newOrder); - } - public OrderViewModel? Update(OrderBindingModel model) - { - var order = source.Orders.FirstOrDefault(x => x.Id == model.Id); - if (order == null) - { - return null; - } - order.Update(model); - source.SaveOrders(); - return GetViewModel(order); - } - public OrderViewModel? Delete(OrderBindingModel model) - { - var element = source.Orders.FirstOrDefault(x => x.Id == model.Id); - if (element != null) - { - source.Orders.Remove(element); - source.SaveOrders(); - return GetViewModel(element); - } - return null; - } - } -} + return source.Orders.FirstOrDefault(x => + (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + private OrderViewModel GetViewModel(Order order) + { + var viewModel = order.GetViewModel; + var document = source.Documents.FirstOrDefault(x => x.Id == order.DocumentId); + if (document != null) + { + viewModel.DocumentName = document.DocumentName; + } + return viewModel; + } + public OrderViewModel? Insert(OrderBindingModel model) + { + model.Id = source.Orders.Count > 0 ? source.Orders.Max(x => x.Id) + 1 : 1; + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + source.Orders.Add(newOrder); + source.SaveOrders(); + return GetViewModel(newOrder); + } + public OrderViewModel? Update(OrderBindingModel model) + { + var order = source.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + source.SaveOrders(); + return GetViewModel(order); + } + public OrderViewModel? Delete(OrderBindingModel model) + { + var element = source.Orders.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + source.Orders.Remove(element); + source.SaveOrders(); + return GetViewModel(element); + } + return null; + } + } +} \ No newline at end of file diff --git a/LawFirm/LawFirmFileImplement/Models/Document.cs b/LawFirm/LawFirmFileImplement/Models/Document.cs index 83afe20..8672080 100644 --- a/LawFirm/LawFirmFileImplement/Models/Document.cs +++ b/LawFirm/LawFirmFileImplement/Models/Document.cs @@ -5,87 +5,86 @@ using System.Xml.Linq; namespace LawFirmFileImplement.Models { - public class Document : IDocumentModel - { - public int Id { get; private set; } - public string DocumentName { get; private set; } = string.Empty; - public double Price { get; private set; } - public Dictionary Blanks { get; private set; } = new(); - private Dictionary? _documentBlanks = - null; - public Dictionary DocumentBlanks - { - get - { - if (_documentBlanks == null) - { - var source = DataFileSingleton.GetInstance(); - _documentBlanks = Blanks.ToDictionary(x => x.Key, y => - ((source.Blanks.FirstOrDefault(z => z.Id == y.Key) as IBlankModel)!, - y.Value)); - } - return _documentBlanks; - } - } - public static Document? Create(DocumentBindingModel model) - { - if (model == null) - { - return null; - } - return new Document() - { - Id = model.Id, - DocumentName = model.DocumentName, - Price = model.Price, - Blanks = model.DocumentBlanks.ToDictionary(x => x.Key, x - => x.Value.Item2) - }; - } - public static Document? Create(XElement element) - { - if (element == null) - { - return null; - } - return new Document() - { - Id = Convert.ToInt32(element.Attribute("Id")!.Value), - DocumentName = element.Element("DocumentName")!.Value, - Price = Convert.ToDouble(element.Element("Price")!.Value), - Blanks = - element.Element("DocumentBlanks")!.Elements("DocumentBlank") - .ToDictionary(x => - Convert.ToInt32(x.Element("Key")?.Value), x => - Convert.ToInt32(x.Element("Value")?.Value)) - }; - } - public void Update(DocumentBindingModel model) - { - if (model == null) - { - return; - } - DocumentName = model.DocumentName; - Price = model.Price; - Blanks = model.DocumentBlanks.ToDictionary(x => x.Key, x => - x.Value.Item2); - _documentBlanks = null; - } - public DocumentViewModel GetViewModel => new() - { - Id = Id, - DocumentName = DocumentName, - Price = Price, - DocumentBlanks = DocumentBlanks - }; - public XElement GetXElement => new("Document", - new XAttribute("Id", Id), - new XElement("DocumentName", DocumentName), - new XElement("Price", Price.ToString()), - new XElement("DocumentBlanks", Blanks.Select(x => - new XElement("DocumentBlank", - new XElement("Key", x.Key), - new XElement("Value", x.Value))).ToArray())); - } -} + public class Document : IDocumentModel + { + public int Id { get; private set; } + public string DocumentName { get; private set; } = string.Empty; + public double Price { get; private set; } + public Dictionary Blanks { get; private set; } = new(); + private Dictionary? _documentBlanks = null; + public Dictionary DocumentBlanks + { + get + { + if (_documentBlanks == null) + { + var source = DataFileSingleton.GetInstance(); + _documentBlanks = Blanks.ToDictionary(x => x.Key, y => + ((source.Blanks.FirstOrDefault(z => z.Id == y.Key) as IBlankModel)!, + y.Value)); + } + return _documentBlanks; + } + } + public static Document? Create(DocumentBindingModel model) + { + if (model == null) + { + return null; + } + return new Document() + { + Id = model.Id, + DocumentName = model.DocumentName, + Price = model.Price, + Blanks = model.DocumentBlanks.ToDictionary(x => x.Key, x + => x.Value.Item2) + }; + } + public static Document? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Document() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + DocumentName = element.Element("DocumentName")!.Value, + Price = Convert.ToDouble(element.Element("Price")!.Value), + Blanks = + element.Element("DocumentBlanks")!.Elements("DocumentBlank") + .ToDictionary(x => + Convert.ToInt32(x.Element("Key")?.Value), x => + Convert.ToInt32(x.Element("Value")?.Value)) + }; + } + public void Update(DocumentBindingModel model) + { + if (model == null) + { + return; + } + DocumentName = model.DocumentName; + Price = model.Price; + Blanks = model.DocumentBlanks.ToDictionary(x => x.Key, x => + x.Value.Item2); + _documentBlanks = null; + } + public DocumentViewModel GetViewModel => new() + { + Id = Id, + DocumentName = DocumentName, + Price = Price, + DocumentBlanks = DocumentBlanks + }; + public XElement GetXElement => new("Document", + new XAttribute("Id", Id), + new XElement("DocumentName", DocumentName), + new XElement("Price", Price.ToString()), + new XElement("DocumentBlanks", Blanks.Select(x => + new XElement("DocumentBlank", + new XElement("Key", x.Key), + new XElement("Value", x.Value))).ToArray())); + } +} \ No newline at end of file diff --git a/LawFirm/LawFirmView/FormCreateOrder.cs b/LawFirm/LawFirmView/FormCreateOrder.cs index f6097f4..d706fdb 100644 --- a/LawFirm/LawFirmView/FormCreateOrder.cs +++ b/LawFirm/LawFirmView/FormCreateOrder.cs @@ -5,113 +5,113 @@ using Microsoft.Extensions.Logging; namespace LawFirmView { - public partial class FormCreateOrder : Form - { - private readonly ILogger _logger; - private readonly IDocumentLogic _logicD; - private readonly IOrderLogic _logicO; - public FormCreateOrder(ILogger logger, IDocumentLogic logicD, IOrderLogic logicO) - { - InitializeComponent(); - _logger = logger; - _logicD = logicD; - _logicO = logicO; - } - private void FormCreateOrder_Load(object sender, EventArgs e) - { - _logger.LogInformation("Загрузка документов для заказа"); - try - { - var list = _logicD.ReadList(null); - if (list != null) - { - comboBoxDocument.DisplayMember = "DocumentName"; - comboBoxDocument.ValueMember = "Id"; - comboBoxDocument.DataSource = list; - comboBoxDocument.SelectedItem = null; - } - } - catch (Exception ex) - { - _logger.LogError(ex, "Ошибка загрузки документов"); - MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - } - private void CalcSum() - { - if (comboBoxDocument.SelectedValue != null && - !string.IsNullOrEmpty(textBoxCount.Text)) - { - try - { - int id = Convert.ToInt32(comboBoxDocument.SelectedValue); - var document = _logicD.ReadElement(new DocumentSearchModel - { - Id = id - }); - int count = Convert.ToInt32(textBoxCount.Text); - textBoxSum.Text = Math.Round(count * (document?.Price ?? 0), 2).ToString(); - _logger.LogInformation("Расчет суммы заказа"); - } - catch (Exception ex) - { - _logger.LogError(ex, "Ошибка расчета суммы заказа"); - MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, - MessageBoxIcon.Error); - } - } - } - private void TextBoxCount_TextChanged(object sender, EventArgs e) - { - CalcSum(); - } - private void ComboBoxDocument_SelectedIndexChanged(object sender, EventArgs e) - { - CalcSum(); - } - private void ButtonSave_Click(object sender, EventArgs e) - { - if (string.IsNullOrEmpty(textBoxCount.Text)) - { - MessageBox.Show("Заполните поле Количество", "Ошибка", - MessageBoxButtons.OK, MessageBoxIcon.Error); - return; - } - if (comboBoxDocument.SelectedValue == null) - { - MessageBox.Show("Выберите документ", "Ошибка", - MessageBoxButtons.OK, MessageBoxIcon.Error); - return; - } - _logger.LogInformation("Создание заказа"); - try - { - var operationResult = _logicO.CreateOrder(new OrderBindingModel - { - DocumentId = Convert.ToInt32(comboBoxDocument.SelectedValue), - Count = Convert.ToInt32(textBoxCount.Text), - Sum = Convert.ToDouble(textBoxSum.Text) - }); - if (!operationResult) - { - throw new Exception("Ошибка при создании заказа. Дополнительная информация в логах."); - } - MessageBox.Show("Сохранение прошло успешно", "Сообщение", - MessageBoxButtons.OK, MessageBoxIcon.Information); - DialogResult = DialogResult.OK; - Close(); - } - catch (Exception ex) - { - _logger.LogError(ex, "Ошибка создания заказа"); - MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, - MessageBoxIcon.Error); - } - } - private void ButtonCancel_Click(object sender, EventArgs e) - { - DialogResult = DialogResult.Cancel; - Close(); - } - } -} + public partial class FormCreateOrder : Form + { + private readonly ILogger _logger; + private readonly IDocumentLogic _logicD; + private readonly IOrderLogic _logicO; + public FormCreateOrder(ILogger logger, IDocumentLogic logicD, IOrderLogic logicO) + { + InitializeComponent(); + _logger = logger; + _logicD = logicD; + _logicO = logicO; + } + private void FormCreateOrder_Load(object sender, EventArgs e) + { + _logger.LogInformation("Загрузка документов для заказа"); + try + { + var list = _logicD.ReadList(null); + if (list != null) + { + comboBoxDocument.DisplayMember = "DocumentName"; + comboBoxDocument.ValueMember = "Id"; + comboBoxDocument.DataSource = list; + comboBoxDocument.SelectedItem = null; + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки документов"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + private void CalcSum() + { + if (comboBoxDocument.SelectedValue != null && + !string.IsNullOrEmpty(textBoxCount.Text)) + { + try + { + int id = Convert.ToInt32(comboBoxDocument.SelectedValue); + var document = _logicD.ReadElement(new DocumentSearchModel + { + Id = id + }); + int count = Convert.ToInt32(textBoxCount.Text); + textBoxSum.Text = Math.Round(count * (document?.Price ?? 0), 2).ToString(); + _logger.LogInformation("Расчет суммы заказа"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка расчета суммы заказа"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + } + private void TextBoxCount_TextChanged(object sender, EventArgs e) + { + CalcSum(); + } + private void ComboBoxDocument_SelectedIndexChanged(object sender, EventArgs e) + { + CalcSum(); + } + private void ButtonSave_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxCount.Text)) + { + MessageBox.Show("Заполните поле Количество", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (comboBoxDocument.SelectedValue == null) + { + MessageBox.Show("Выберите документ", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Создание заказа"); + try + { + var operationResult = _logicO.CreateOrder(new OrderBindingModel + { + DocumentId = Convert.ToInt32(comboBoxDocument.SelectedValue), + Count = Convert.ToInt32(textBoxCount.Text), + Sum = Convert.ToDouble(textBoxSum.Text) + }); + if (!operationResult) + { + throw new Exception("Ошибка при создании заказа. Дополнительная информация в логах."); + } + MessageBox.Show("Сохранение прошло успешно", "Сообщение", + MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; + Close(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка создания заказа"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + private void ButtonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + } +} \ No newline at end of file -- 2.25.1 From e1859dc0eb1f3fd41c6ee244d7fa8fdbbdf5c948 Mon Sep 17 00:00:00 2001 From: Danil Markov Date: Fri, 21 Apr 2023 14:35:46 +0400 Subject: [PATCH 08/10] fix --- LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs b/LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs index 58e0231..89e152f 100644 --- a/LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs +++ b/LawFirm/LawFirmFileImplement/Implements/OrderStorage.cs @@ -80,4 +80,4 @@ namespace LawFirmFileImplement.Implements return null; } } -} \ No newline at end of file +} -- 2.25.1 From fffc0b10a953fd86376e4fe966f1f93f6cf1c796 Mon Sep 17 00:00:00 2001 From: Danil Markov Date: Tue, 25 Apr 2023 03:51:22 +0400 Subject: [PATCH 09/10] lab3hard. --- .../BusinessLogics/OrderLogic.cs | 2 +- .../BusinessLogics/ShopLogic.cs | 2 +- .../BindingModels/OrderBindingModel.cs | 2 +- .../BindingModels/ShopBindingModel.cs | 2 +- .../ViewModels/OrderViewModel.cs | 2 +- .../ViewModels/ShopViewModel.cs | 2 +- .../Implements/OrderStorage.cs | 27 +- .../Implements/ShopStorage.cs | 156 +++++++++++ .../LawFirmDatabase.cs | 7 +- .../20230424233004_lab3hard.Designer.cs | 248 ++++++++++++++++++ .../Migrations/20230424233004_lab3hard.cs | 79 ++++++ .../LawFirmDatabaseModelSnapshot.cs | 77 ++++++ .../LawFirmDatabaseImplement/Models/Shop.cs | 108 ++++++++ .../Models/ShopDocument.cs | 27 ++ LawFirm/LawFirmFileImplement/Models/Order.cs | 2 +- LawFirm/LawFirmListImplement/Models/Order.cs | 2 +- LawFirm/LawFirmView/FormMain.cs | 5 - .../LawFirmView/FormSellDocuments.Designer.cs | 2 +- LawFirm/LawFirmView/FormSellDocuments.cs | 2 +- LawFirm/LawFirmView/FormShop.cs | 2 +- 20 files changed, 731 insertions(+), 25 deletions(-) create mode 100644 LawFirm/LawFirmDatabaseImplement/Implements/ShopStorage.cs create mode 100644 LawFirm/LawFirmDatabaseImplement/Migrations/20230424233004_lab3hard.Designer.cs create mode 100644 LawFirm/LawFirmDatabaseImplement/Migrations/20230424233004_lab3hard.cs create mode 100644 LawFirm/LawFirmDatabaseImplement/Models/Shop.cs create mode 100644 LawFirm/LawFirmDatabaseImplement/Models/ShopDocument.cs diff --git a/LawFirm/LawFirmBusinessLogic/BusinessLogics/OrderLogic.cs b/LawFirm/LawFirmBusinessLogic/BusinessLogics/OrderLogic.cs index cb2e310..c6db378 100644 --- a/LawFirm/LawFirmBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/LawFirm/LawFirmBusinessLogic/BusinessLogics/OrderLogic.cs @@ -57,7 +57,7 @@ namespace LawFirmBusinessLogic.BusinessLogics if (model.Status == OrderStatus.Готов) { - model.DateImplement = DateTime.Now; + model.DateImplement = DateTime.SpecifyKind(DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc), DateTimeKind.Utc); var document = _documentStorage.GetElement(new() { Id = viewModel.DocumentId }); if (document == null) { diff --git a/LawFirm/LawFirmBusinessLogic/BusinessLogics/ShopLogic.cs b/LawFirm/LawFirmBusinessLogic/BusinessLogics/ShopLogic.cs index 5dec67d..48ba3d6 100644 --- a/LawFirm/LawFirmBusinessLogic/BusinessLogics/ShopLogic.cs +++ b/LawFirm/LawFirmBusinessLogic/BusinessLogics/ShopLogic.cs @@ -116,7 +116,7 @@ namespace LawFirmBusinessLogic.BusinessLogics if (count <= 0) { return false; - throw new ArgumentException("Количество поездок должно быть больше 0", nameof(count)); + throw new ArgumentException("Количество документов должно быть больше 0", nameof(count)); } _logger.LogInformation("AddDocument. ShopName:{ShopName}. Id:{Id}", model.ShopName, model.Id); var element = _shopStorage.GetElement(model); diff --git a/LawFirm/LawFirmContracts/BindingModels/OrderBindingModel.cs b/LawFirm/LawFirmContracts/BindingModels/OrderBindingModel.cs index 358159b..73392ad 100644 --- a/LawFirm/LawFirmContracts/BindingModels/OrderBindingModel.cs +++ b/LawFirm/LawFirmContracts/BindingModels/OrderBindingModel.cs @@ -10,7 +10,7 @@ namespace LawFirmContracts.BindingModels public int Count { get; set; } public double Sum { get; set; } public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; - public DateTime DateCreate { get; set; } = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc); + public DateTime DateCreate { get; set; } = DateTime.SpecifyKind(DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc), DateTimeKind.Utc); public DateTime? DateImplement { get; set; } } } diff --git a/LawFirm/LawFirmContracts/BindingModels/ShopBindingModel.cs b/LawFirm/LawFirmContracts/BindingModels/ShopBindingModel.cs index 6cc0b4e..c8bb4cd 100644 --- a/LawFirm/LawFirmContracts/BindingModels/ShopBindingModel.cs +++ b/LawFirm/LawFirmContracts/BindingModels/ShopBindingModel.cs @@ -11,7 +11,7 @@ namespace LawFirmContracts.BindingModels { public string ShopName { get; set; } = string.Empty; public string Address { get; set; } = string.Empty; - public DateTime DateOpen { get; set; } = DateTime.Now; + public DateTime DateOpen { get; set; } = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc); public int Capacity { get; set; } public Dictionary ShopDocuments { get; set; } = new(); public int Id { get; set; } diff --git a/LawFirm/LawFirmContracts/ViewModels/OrderViewModel.cs b/LawFirm/LawFirmContracts/ViewModels/OrderViewModel.cs index 59f4972..06c7dd3 100644 --- a/LawFirm/LawFirmContracts/ViewModels/OrderViewModel.cs +++ b/LawFirm/LawFirmContracts/ViewModels/OrderViewModel.cs @@ -18,7 +18,7 @@ namespace LawFirmContracts.ViewModels [DisplayName("Статус")] public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; [DisplayName("Дата создания")] - public DateTime DateCreate { get; set; } = DateTime.Now; + public DateTime DateCreate { get; set; } = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc); [DisplayName("Дата выполнения")] public DateTime? DateImplement { get; set; } } diff --git a/LawFirm/LawFirmContracts/ViewModels/ShopViewModel.cs b/LawFirm/LawFirmContracts/ViewModels/ShopViewModel.cs index 2f021bd..cf48275 100644 --- a/LawFirm/LawFirmContracts/ViewModels/ShopViewModel.cs +++ b/LawFirm/LawFirmContracts/ViewModels/ShopViewModel.cs @@ -17,7 +17,7 @@ namespace LawFirmContracts.ViewModels public string Address { get; set; } = string.Empty; [DisplayName("Дата открытия")] - public DateTime DateOpen { get; set; } = DateTime.Now; + public DateTime DateOpen { get; set; } = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc); [DisplayName("Вместимость магазина")] public int Capacity { get; set; } public Dictionary ShopDocuments { get; set; } = new(); diff --git a/LawFirm/LawFirmDatabaseImplement/Implements/OrderStorage.cs b/LawFirm/LawFirmDatabaseImplement/Implements/OrderStorage.cs index a7fb3e1..89e7f6d 100644 --- a/LawFirm/LawFirmDatabaseImplement/Implements/OrderStorage.cs +++ b/LawFirm/LawFirmDatabaseImplement/Implements/OrderStorage.cs @@ -13,7 +13,8 @@ namespace LawFirmDatabaseImplement.Implements { using var context = new LawFirmDatabase(); - var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + var element = context.Orders + .FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { @@ -35,7 +36,9 @@ namespace LawFirmDatabaseImplement.Implements using var context = new LawFirmDatabase(); - return GetViewModel(context.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))); + return GetViewModel(context.Orders + .Include(x => x.Document) + .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))); } public List GetFilteredList(OrderSearchModel model) @@ -47,14 +50,21 @@ namespace LawFirmDatabaseImplement.Implements using var context = new LawFirmDatabase(); - return context.Orders.Where(x => x.Id == model.Id).Select(x => GetViewModel(x)).ToList(); + return context.Orders + .Include(x => x.Document) + .Where(x => x.Id == model.Id) + .Select(x => GetViewModel(x)) + .ToList(); } public List GetFullList() { using var context = new LawFirmDatabase(); - return context.Orders.Select(x => GetViewModel(x)).ToList(); + return context.Orders + .Include(x => x.Document) + .Select(x => GetViewModel(x)) + .ToList(); } public OrderViewModel? Insert(OrderBindingModel model) @@ -69,14 +79,19 @@ namespace LawFirmDatabaseImplement.Implements context.Orders.Add(newOrder); context.SaveChanges(); - return GetViewModel(newOrder); + return context.Orders + .Include(x => x.Document) + .FirstOrDefault(x => x.Id == newOrder.Id) + ?.GetViewModel; } public OrderViewModel? Update(OrderBindingModel model) { using var context = new LawFirmDatabase(); - var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + var order = context.Orders + .Include(x => x.Document) + .FirstOrDefault(x => x.Id == model.Id); if (order == null) { diff --git a/LawFirm/LawFirmDatabaseImplement/Implements/ShopStorage.cs b/LawFirm/LawFirmDatabaseImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..f751325 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Implements/ShopStorage.cs @@ -0,0 +1,156 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.SearchModels; +using LawFirmContracts.StoragesContracts; +using LawFirmContracts.ViewModels; +using LawFirmDatabaseImplement.Models; +using LawFirmDataModels.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 ShopStorage : IShopStorage + { + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName) && + !model.Id.HasValue) + { + return null; + } + using var context = new LawFirmDatabase(); + 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 LawFirmDatabase(); + 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 LawFirmDatabase(); + 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 LawFirmDatabase(); + var newProduct = Shop.Create(context, model); + if (newProduct == null) + { + return null; + } + context.Shops.Add(newProduct); + context.SaveChanges(); + return newProduct.GetViewModel; + } + + public ShopViewModel? Update(ShopBindingModel model) + { + using var context = new LawFirmDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var shop = context.Shops.FirstOrDefault(rec => + rec.Id == model.Id); + if (shop == null) + { + return null; + } + shop.Update(model); + context.SaveChanges(); + shop.UpdateDocuments(context, model); + transaction.Commit(); + return shop.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public ShopViewModel? Delete(ShopBindingModel model) + { + using var context = new LawFirmDatabase(); + var element = context.Shops + .Include(x => x.Documents) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Shops.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + public bool SellDocuments(IDocumentModel model, int count) + { + using var context = new LawFirmDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + List shopsWithDocument = context.Shops.Include(x => x.Documents).ThenInclude(x => x.Document).Where(x => x.Documents.Any(x => x.DocumentId == model.Id)).ToList(); + foreach (var shop in shopsWithDocument) + { + int carInShopCount = shop.ShopDocuments[model.Id].Item2; + if (count - carInShopCount >= 0) + { + count -= carInShopCount; + context.ShopDocuments.Remove(shop.Documents.FirstOrDefault(x => x.DocumentId == model.Id)!); + shop.ShopDocuments.Remove(model.Id); + } + else + { + shop.ShopDocuments[model.Id] = (model, carInShopCount - count); + count = 0; + shop.UpdateDocuments(context, new() + { + Id = shop.Id, + ShopDocuments = shop.ShopDocuments + }); + } + if (count == 0) + { + context.SaveChanges(); + transaction.Commit(); + return true; + } + } + transaction.Rollback(); + return false; + } + catch + { + transaction.Rollback(); + throw; + } + } + } +} diff --git a/LawFirm/LawFirmDatabaseImplement/LawFirmDatabase.cs b/LawFirm/LawFirmDatabaseImplement/LawFirmDatabase.cs index 7a0e0a6..f8b9f7d 100644 --- a/LawFirm/LawFirmDatabaseImplement/LawFirmDatabase.cs +++ b/LawFirm/LawFirmDatabaseImplement/LawFirmDatabase.cs @@ -12,7 +12,7 @@ namespace LawFirmDatabaseImplement optionsBuilder.UseNpgsql(@" Host=localhost; Port=5432; - Database=RPP; + Database=RPP3hard; Username=postgres; Password=123"); } @@ -22,6 +22,7 @@ namespace LawFirmDatabaseImplement public virtual DbSet Documents { set; get; } public virtual DbSet DocumentBlanks { set; get; } public virtual DbSet Orders { set; get; } - - } + public virtual DbSet Shops { set; get; } + public virtual DbSet ShopDocuments { set; get; } + } } diff --git a/LawFirm/LawFirmDatabaseImplement/Migrations/20230424233004_lab3hard.Designer.cs b/LawFirm/LawFirmDatabaseImplement/Migrations/20230424233004_lab3hard.Designer.cs new file mode 100644 index 0000000..53c3c1b --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Migrations/20230424233004_lab3hard.Designer.cs @@ -0,0 +1,248 @@ +// +using System; +using LawFirmDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace LawFirmDatabaseImplement.Migrations +{ + [DbContext(typeof(LawFirmDatabase))] + [Migration("20230424233004_lab3hard")] + partial class lab3hard + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Blank", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("BlankName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Price") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Blanks"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Document", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("DocumentName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Price") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Documents"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.DocumentBlank", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("BlankId") + .HasColumnType("integer"); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("DocumentId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("BlankId"); + + b.HasIndex("DocumentId"); + + b.ToTable("DocumentBlanks"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("DateCreate") + .HasColumnType("timestamp with time zone"); + + b.Property("DateImplement") + .HasColumnType("timestamp with time zone"); + + b.Property("DocumentId") + .HasColumnType("integer"); + + b.Property("Status") + .HasColumnType("integer"); + + b.Property("Sum") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.HasIndex("DocumentId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("text"); + + b.Property("Capacity") + .HasColumnType("integer"); + + b.Property("DateOpen") + .HasColumnType("timestamp with time zone"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.ShopDocument", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("DocumentId") + .HasColumnType("integer"); + + b.Property("ShopId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("DocumentId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopDocuments"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.DocumentBlank", b => + { + b.HasOne("LawFirmDatabaseImplement.Models.Blank", "Blank") + .WithMany("DocumentBlanks") + .HasForeignKey("BlankId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("LawFirmDatabaseImplement.Models.Document", "Document") + .WithMany("Blanks") + .HasForeignKey("DocumentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Blank"); + + b.Navigation("Document"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Order", b => + { + b.HasOne("LawFirmDatabaseImplement.Models.Document", "Document") + .WithMany("Orders") + .HasForeignKey("DocumentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Document"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.ShopDocument", b => + { + b.HasOne("LawFirmDatabaseImplement.Models.Document", "Document") + .WithMany() + .HasForeignKey("DocumentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("LawFirmDatabaseImplement.Models.Shop", "Shop") + .WithMany("Documents") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Document"); + + b.Navigation("Shop"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Blank", b => + { + b.Navigation("DocumentBlanks"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Document", b => + { + b.Navigation("Blanks"); + + b.Navigation("Orders"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Shop", b => + { + b.Navigation("Documents"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/LawFirm/LawFirmDatabaseImplement/Migrations/20230424233004_lab3hard.cs b/LawFirm/LawFirmDatabaseImplement/Migrations/20230424233004_lab3hard.cs new file mode 100644 index 0000000..10e8a3e --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Migrations/20230424233004_lab3hard.cs @@ -0,0 +1,79 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace LawFirmDatabaseImplement.Migrations +{ + /// + public partial class lab3hard : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Shops", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + ShopName = table.Column(type: "text", nullable: false), + Address = table.Column(type: "text", nullable: false), + DateOpen = table.Column(type: "timestamp with time zone", nullable: false), + Capacity = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Shops", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "ShopDocuments", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + ShopId = table.Column(type: "integer", nullable: false), + DocumentId = table.Column(type: "integer", nullable: false), + Count = table.Column(type: "integer", 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/LawFirmDatabaseImplement/Migrations/LawFirmDatabaseModelSnapshot.cs b/LawFirm/LawFirmDatabaseImplement/Migrations/LawFirmDatabaseModelSnapshot.cs index 3a405da..2dbad13 100644 --- a/LawFirm/LawFirmDatabaseImplement/Migrations/LawFirmDatabaseModelSnapshot.cs +++ b/LawFirm/LawFirmDatabaseImplement/Migrations/LawFirmDatabaseModelSnapshot.cs @@ -121,6 +121,59 @@ namespace LawFirmDatabaseImplement.Migrations b.ToTable("Orders"); }); + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("text"); + + b.Property("Capacity") + .HasColumnType("integer"); + + b.Property("DateOpen") + .HasColumnType("timestamp with time zone"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.ShopDocument", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("DocumentId") + .HasColumnType("integer"); + + b.Property("ShopId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("DocumentId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopDocuments"); + }); + modelBuilder.Entity("LawFirmDatabaseImplement.Models.DocumentBlank", b => { b.HasOne("LawFirmDatabaseImplement.Models.Blank", "Blank") @@ -151,6 +204,25 @@ namespace LawFirmDatabaseImplement.Migrations b.Navigation("Document"); }); + modelBuilder.Entity("LawFirmDatabaseImplement.Models.ShopDocument", b => + { + b.HasOne("LawFirmDatabaseImplement.Models.Document", "Document") + .WithMany() + .HasForeignKey("DocumentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("LawFirmDatabaseImplement.Models.Shop", "Shop") + .WithMany("Documents") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Document"); + + b.Navigation("Shop"); + }); + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Blank", b => { b.Navigation("DocumentBlanks"); @@ -162,6 +234,11 @@ namespace LawFirmDatabaseImplement.Migrations b.Navigation("Orders"); }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Shop", b => + { + b.Navigation("Documents"); + }); #pragma warning restore 612, 618 } } diff --git a/LawFirm/LawFirmDatabaseImplement/Models/Shop.cs b/LawFirm/LawFirmDatabaseImplement/Models/Shop.cs new file mode 100644 index 0000000..9ffc0cc --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Models/Shop.cs @@ -0,0 +1,108 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.ViewModels; +using LawFirmDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LawFirmDatabaseImplement.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 DateOpen { get; set; } + [Required] + public int Capacity { get; set; } + + private Dictionary? _shopDocuments = null; + + [NotMapped] + public Dictionary ShopDocuments + { + get + { + if (_shopDocuments == null) + { + _shopDocuments = Documents + .ToDictionary(rec => rec.DocumentId, + rec => (rec.Document as IDocumentModel, + rec.Count)); + } + return _shopDocuments; + } + } + [ForeignKey("ShopId")] + public virtual List Documents { get; set; } = new(); + public static Shop Create(LawFirmDatabase context, ShopBindingModel model) + { + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + Address = model.Address, + DateOpen = model.DateOpen, + Capacity = model.Capacity, + Documents = model.ShopDocuments.Select(x => new ShopDocument + { + Document = context.Documents.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + + public void Update(ShopBindingModel model) + { + ShopName = model.ShopName; + Address = model.Address; + DateOpen = model.DateOpen; + Capacity = model.Capacity; + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Address = Address, + DateOpen = DateOpen, + Capacity = Capacity, + ShopDocuments = ShopDocuments + }; + + public void UpdateDocuments(LawFirmDatabase 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(); + 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 id in model.ShopDocuments) + { + context.ShopDocuments.Add(new ShopDocument + { + Shop = shop, + Document = context.Documents.First(x => x.Id == id.Key), + Count = id.Value.Item2 + }); + context.SaveChanges(); + } + _shopDocuments = null; + } + } +} diff --git a/LawFirm/LawFirmDatabaseImplement/Models/ShopDocument.cs b/LawFirm/LawFirmDatabaseImplement/Models/ShopDocument.cs new file mode 100644 index 0000000..e4527f4 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Models/ShopDocument.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 ShopDocument + { + public int Id { get; set; } + + [Required] + public int ShopId { get; set; } + + [Required] + public int DocumentId { get; set; } + + [Required] + public int Count { get; set; } + + public virtual Document Document { get; set; } = new(); + + public virtual Shop Shop { get; set; } = new(); + } +} diff --git a/LawFirm/LawFirmFileImplement/Models/Order.cs b/LawFirm/LawFirmFileImplement/Models/Order.cs index e9ce910..7c776da 100644 --- a/LawFirm/LawFirmFileImplement/Models/Order.cs +++ b/LawFirm/LawFirmFileImplement/Models/Order.cs @@ -13,7 +13,7 @@ namespace LawFirmFileImplement.Models public int Count { get; private set; } public double Sum { get; private set; } public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; - public DateTime DateCreate { get; private set; } = DateTime.Now; + public DateTime DateCreate { get; private set; } = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc); public DateTime? DateImplement { get; private set; } public static Order? Create(OrderBindingModel? model) { diff --git a/LawFirm/LawFirmListImplement/Models/Order.cs b/LawFirm/LawFirmListImplement/Models/Order.cs index 6140c15..3a5ee58 100644 --- a/LawFirm/LawFirmListImplement/Models/Order.cs +++ b/LawFirm/LawFirmListImplement/Models/Order.cs @@ -12,7 +12,7 @@ namespace LawFirmListImplement.Models public int Count { get; private set; } public double Sum { get; private set; } public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; - public DateTime DateCreate { get; private set; } = DateTime.Now; + public DateTime DateCreate { get; private set; } = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc); public DateTime? DateImplement { get; private set; } public static Order? Create(OrderBindingModel? model) { diff --git a/LawFirm/LawFirmView/FormMain.cs b/LawFirm/LawFirmView/FormMain.cs index 2ea2821..4b05d03 100644 --- a/LawFirm/LawFirmView/FormMain.cs +++ b/LawFirm/LawFirmView/FormMain.cs @@ -58,11 +58,6 @@ namespace LawFirmView { var operationResult = _orderLogic.TakeOrderInWork(new OrderBindingModel{ Id = id, - DocumentId = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["DocumentId"].Value), - Status = Enum.Parse(dataGridView.SelectedRows[0].Cells["Status"].Value.ToString()), - Count = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Count"].Value), - Sum = double.Parse(dataGridView.SelectedRows[0].Cells["Sum"].Value.ToString()), - DateCreate = DateTime.Parse(dataGridView.SelectedRows[0].Cells["DateCreate"].Value.ToString()) }); if (!operationResult) { diff --git a/LawFirm/LawFirmView/FormSellDocuments.Designer.cs b/LawFirm/LawFirmView/FormSellDocuments.Designer.cs index 1d5afea..898d107 100644 --- a/LawFirm/LawFirmView/FormSellDocuments.Designer.cs +++ b/LawFirm/LawFirmView/FormSellDocuments.Designer.cs @@ -69,7 +69,7 @@ this.labelDocument.Name = "labelDocument"; this.labelDocument.Size = new System.Drawing.Size(69, 20); this.labelDocument.TabIndex = 4; - this.labelDocument.Text = "Корабль"; + this.labelDocument.Text = "Документ"; // // labelCount // diff --git a/LawFirm/LawFirmView/FormSellDocuments.cs b/LawFirm/LawFirmView/FormSellDocuments.cs index 1d479fb..8c99061 100644 --- a/LawFirm/LawFirmView/FormSellDocuments.cs +++ b/LawFirm/LawFirmView/FormSellDocuments.cs @@ -39,7 +39,7 @@ namespace LawFirmView { if (comboBoxDocuments.SelectedValue == null) { - MessageBox.Show("Выберите корабль", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show("Выберите документ", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (string.IsNullOrEmpty(numericUpDownCount.Text)) diff --git a/LawFirm/LawFirmView/FormShop.cs b/LawFirm/LawFirmView/FormShop.cs index 5243b19..1414e03 100644 --- a/LawFirm/LawFirmView/FormShop.cs +++ b/LawFirm/LawFirmView/FormShop.cs @@ -95,7 +95,7 @@ namespace LawFirmView Id = _id ?? 0, ShopName = textBoxName.Text, Address = textBoxAddress.Text, - DateOpen = DateTime.Parse(dateTimePickerDateOpen.Text), + DateOpen = DateTime.SpecifyKind(DateTime.Parse(dateTimePickerDateOpen.Text), DateTimeKind.Utc), Capacity = (int)numericUpDownCapacity.Value, ShopDocuments = _shopDocuments }; -- 2.25.1 From cf82bfabdd95924c6ffbde1ec18f3a17daf70f91 Mon Sep 17 00:00:00 2001 From: Danil Markov Date: Tue, 25 Apr 2023 03:51:32 +0400 Subject: [PATCH 10/10] fix --- LawFirm/LawFirmView/FormSellDocuments.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/LawFirm/LawFirmView/FormSellDocuments.cs b/LawFirm/LawFirmView/FormSellDocuments.cs index 8c99061..7934bb8 100644 --- a/LawFirm/LawFirmView/FormSellDocuments.cs +++ b/LawFirm/LawFirmView/FormSellDocuments.cs @@ -47,7 +47,7 @@ namespace LawFirmView MessageBox.Show("Заполните количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } - _logger.LogInformation("Продажа поездок"); + _logger.LogInformation("Продажа документов"); try { var document = _documentLogic.ReadElement(new() @@ -56,7 +56,7 @@ namespace LawFirmView }); if (document == null) { - throw new Exception("Корабль не найден. Дополнительная информация в логах."); + throw new Exception("Документ не найден. Дополнительная информация в логах."); } var operationResult = _shopLogic.SellDocuments( model: document, @@ -64,7 +64,7 @@ namespace LawFirmView ); if (!operationResult) { - throw new Exception("Ошибка при продаже корабля. Дополнительная информация в логах."); + throw new Exception("Ошибка при продаже документа. Дополнительная информация в логах."); } MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); DialogResult = DialogResult.OK; @@ -72,7 +72,7 @@ namespace LawFirmView } catch (Exception ex) { - _logger.LogError(ex, "Ошибка сохранения корабля"); + _logger.LogError(ex, "Ошибка сохранения документа"); MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } -- 2.25.1