From 51d1b13efe8f54a9727f208cf0a14752676103db Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Fri, 24 Feb 2023 17:55:19 +0400 Subject: [PATCH 01/14] =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=BE=D0=B2=20?= =?UTF-8?q?=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D0=B8=20?= =?UTF-8?q?=D1=85=D1=80=D0=B0=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B2=20?= =?UTF-8?q?=D0=B1=D0=B0=D0=B7=D0=B5=20=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B=D1=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LawFirm/LawFirm.sln | 6 ++ .../LawFirmDatabase.cs | 28 ++++++ .../LawFirmDatabaseImplement.csproj | 23 +++++ .../LawFirmDatabaseImplement/Models/Blank.cs | 56 +++++++++++ .../Models/Document.cs | 99 +++++++++++++++++++ .../Models/DocumentBlank.cs | 23 +++++ .../LawFirmDatabaseImplement/Models/Order.cs | 79 +++++++++++++++ 7 files changed, 314 insertions(+) create mode 100644 LawFirm/LawFirmDatabaseImplement/LawFirmDatabase.cs create mode 100644 LawFirm/LawFirmDatabaseImplement/LawFirmDatabaseImplement.csproj 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 362180a..33cfb68 100644 --- a/LawFirm/LawFirm.sln +++ b/LawFirm/LawFirm.sln @@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LawFirmListImplements", "La EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LawFirmFileImplement", "LawFirmFileImplement\LawFirmFileImplement.csproj", "{E75AF81D-A466-470A-A3F7-EC0E6F33C866}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LawFirmDatabaseImplement", "LawFirmDatabaseImplement\LawFirmDatabaseImplement.csproj", "{CB0444D3-5BDA-42DB-95F9-60191AE9073A}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -45,6 +47,10 @@ Global {E75AF81D-A466-470A-A3F7-EC0E6F33C866}.Debug|Any CPU.Build.0 = Debug|Any CPU {E75AF81D-A466-470A-A3F7-EC0E6F33C866}.Release|Any CPU.ActiveCfg = Release|Any CPU {E75AF81D-A466-470A-A3F7-EC0E6F33C866}.Release|Any CPU.Build.0 = Release|Any CPU + {CB0444D3-5BDA-42DB-95F9-60191AE9073A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CB0444D3-5BDA-42DB-95F9-60191AE9073A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CB0444D3-5BDA-42DB-95F9-60191AE9073A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CB0444D3-5BDA-42DB-95F9-60191AE9073A}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/LawFirm/LawFirmDatabaseImplement/LawFirmDatabase.cs b/LawFirm/LawFirmDatabaseImplement/LawFirmDatabase.cs new file mode 100644 index 0000000..a893522 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/LawFirmDatabase.cs @@ -0,0 +1,28 @@ +using LawFirmDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LawFirmDatabaseImplement +{ + public class LawFirmDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"DataSource=danyaxren\SQLEXPRESS;Initial Catalog=LawFirmDatabaseFull;IntegratedSecurity=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + } + 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..1590021 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/LawFirmDatabaseImplement.csproj @@ -0,0 +1,23 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/LawFirm/LawFirmDatabaseImplement/Models/Blank.cs b/LawFirm/LawFirmDatabaseImplement/Models/Blank.cs new file mode 100644 index 0000000..a7db4b2 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Models/Blank.cs @@ -0,0 +1,56 @@ +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 Blank : IBlankModel + { + public int Id { get; private set; } + [Required] + public string BlankName { get; private set; } = String.Empty; + + [Required] + public double Cost { 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, + Cost = model.Cost + }; + } + + public void Update(BlankBindingModel? model) + { + if (model == null) + { + return; + } + BlankName = model.BlankName; + Cost = model.Cost; + } + public BlankViewModel GetViewModel => new() + { + Id = Id, + BlankName = BlankName, + Cost = Cost + }; + } +} diff --git a/LawFirm/LawFirmDatabaseImplement/Models/Document.cs b/LawFirm/LawFirmDatabaseImplement/Models/Document.cs new file mode 100644 index 0000000..e70bb12 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Models/Document.cs @@ -0,0 +1,99 @@ +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 Document : IDocumentModel + { + public int Id { get; private set; } + [Required] + public string DocumentName { get; private set; } = string.Empty; + [Required] + public double Price { get; private set; } + + private Dictionary? _documentBlanks = null; + + [NotMapped] + public Dictionary DocumentBlanks + { + get + { + if (_documentBlanks == null) + { + _documentBlanks = Blanks.ToDictionary(recPC => recPC.BlankId, recPC => (recPC.Blank as IBlankModel, recPC.Count)); + } + return _documentBlanks; + } + } + + [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 UpdateComponents(LawFirmDatabase context, DocumentBindingModel model) + { + var documentBlanks = context.DocumentBlanks.Where(rec => rec.DocumentId == model.Id).ToList(); + if (documentBlanks != null && documentBlanks.Count > 0) + { // удалили те, которых нет в модели + context.DocumentBlanks.RemoveRange(documentBlanks.Where(rec => !model.DocumentBlanks.ContainsKey(rec.BlankId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateBlank in documentBlanks) + { + updateBlank.Count = model.DocumentBlanks[updateBlank.BlankId].Item2; + model.DocumentBlanks.Remove(updateBlank.BlankId); + } + context.SaveChanges(); + } + var document = context.Documents.First(x => x.Id == Id); + foreach (var pc in model.DocumentBlanks) + { + context.DocumentBlanks.Add(new DocumentBlank + { + Document = document, + Blank = context.Blanks.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _documentBlanks = null; + } + } +} diff --git a/LawFirm/LawFirmDatabaseImplement/Models/DocumentBlank.cs b/LawFirm/LawFirmDatabaseImplement/Models/DocumentBlank.cs new file mode 100644 index 0000000..43894ec --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Models/DocumentBlank.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +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..51ab4bb --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Models/Order.cs @@ -0,0 +1,79 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.ViewModels; +using LawFirmDataModels.Enums; +using LawFirmDataModels.Models; +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 Order : IOrderModel + { + public int Id { get; private set; } + [Required] + public int DocumentId { get; private set; } + + public string DocumentName { get; private set; } = string.Empty; + [Required] + public int Count { get; private set; } + [Required] + public double Sum { get; private set; } + [Required] + public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; + [Required] + public DateTime DateCreate { get; private set; } = DateTime.Now; + + public DateTime? DateImplement { get; private set; } + + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order + { + DocumentId = model.DocumentId, + DocumentName = model.DocumentName, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement, + Id = model.Id, + }; + } + + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + DocumentId = model.DocumentId; + DocumentName = model.DocumentName; + Count = model.Count; + Sum = model.Sum; + Status = model.Status; + DateCreate = model.DateCreate; + DateImplement = model.DateImplement; + Id = model.Id; + } + + public OrderViewModel GetViewModel => new() + { + DocumentId = DocumentId, + DocumentName = DocumentName, + Count = Count, + Sum = Sum, + DateCreate = DateCreate, + DateImplement = DateImplement, + Id = Id, + Status = Status, + }; + } +} -- 2.25.1 From ceec6e155c18ba800de5bdd5a70f75e3d77e66b8 Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Fri, 24 Feb 2023 18:38:05 +0400 Subject: [PATCH 02/14] =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=BC=D0=B8=D0=B3=D1=80=D0=B0=D1=86=D0=B8=D0=B8?= =?UTF-8?q?=20=D0=B8=20=D0=91=D0=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LawFirm/LawFirm/LawFirmView.csproj | 5 + .../LawFirmDatabase.cs | 2 +- .../20230224135956_InitialCreate.Designer.cs | 173 ++++++++++++++++++ .../20230224135956_InitialCreate.cs | 126 +++++++++++++ .../LawFirmDatabaseModelSnapshot.cs | 170 +++++++++++++++++ 5 files changed, 475 insertions(+), 1 deletion(-) create mode 100644 LawFirm/LawFirmDatabaseImplement/Migrations/20230224135956_InitialCreate.Designer.cs create mode 100644 LawFirm/LawFirmDatabaseImplement/Migrations/20230224135956_InitialCreate.cs create mode 100644 LawFirm/LawFirmDatabaseImplement/Migrations/LawFirmDatabaseModelSnapshot.cs diff --git a/LawFirm/LawFirm/LawFirmView.csproj b/LawFirm/LawFirm/LawFirmView.csproj index 7f7c8cf..9d320d6 100644 --- a/LawFirm/LawFirm/LawFirmView.csproj +++ b/LawFirm/LawFirm/LawFirmView.csproj @@ -9,6 +9,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -17,6 +21,7 @@ + diff --git a/LawFirm/LawFirmDatabaseImplement/LawFirmDatabase.cs b/LawFirm/LawFirmDatabaseImplement/LawFirmDatabase.cs index a893522..6cabc79 100644 --- a/LawFirm/LawFirmDatabaseImplement/LawFirmDatabase.cs +++ b/LawFirm/LawFirmDatabaseImplement/LawFirmDatabase.cs @@ -15,7 +15,7 @@ namespace LawFirmDatabaseImplement { if (optionsBuilder.IsConfigured == false) { - optionsBuilder.UseSqlServer(@"DataSource=danyaxren\SQLEXPRESS;Initial Catalog=LawFirmDatabaseFull;IntegratedSecurity=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + optionsBuilder.UseSqlServer(@"Data Source=DANYAXREN\SQLEXPRESS;Initial Catalog=LawFirmDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); } base.OnConfiguring(optionsBuilder); } diff --git a/LawFirm/LawFirmDatabaseImplement/Migrations/20230224135956_InitialCreate.Designer.cs b/LawFirm/LawFirmDatabaseImplement/Migrations/20230224135956_InitialCreate.Designer.cs new file mode 100644 index 0000000..9a2c3f4 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Migrations/20230224135956_InitialCreate.Designer.cs @@ -0,0 +1,173 @@ +// +using System; +using LawFirmDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace LawFirmDatabaseImplement.Migrations +{ + [DbContext(typeof(LawFirmDatabase))] + [Migration("20230224135956_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Blank", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("BlankName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Blanks"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Document", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("DocumentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Documents"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.DocumentBlank", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("BlankId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DocumentId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("BlankId"); + + b.HasIndex("DocumentId"); + + b.ToTable("DocumentBlanks"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("DocumentId") + .HasColumnType("int"); + + b.Property("DocumentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + 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", null) + .WithMany("Orders") + .HasForeignKey("DocumentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + 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/20230224135956_InitialCreate.cs b/LawFirm/LawFirmDatabaseImplement/Migrations/20230224135956_InitialCreate.cs new file mode 100644 index 0000000..1c417b0 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Migrations/20230224135956_InitialCreate.cs @@ -0,0 +1,126 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#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: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + BlankName = table.Column(type: "nvarchar(max)", nullable: false), + Cost = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Blanks", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Documents", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + DocumentName = table.Column(type: "nvarchar(max)", nullable: false), + Price = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Documents", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "DocumentBlanks", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + DocumentId = table.Column(type: "int", nullable: false), + BlankId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", 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: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + DocumentId = table.Column(type: "int", nullable: false), + DocumentName = table.Column(type: "nvarchar(max)", nullable: false), + Count = table.Column(type: "int", nullable: false), + Sum = table.Column(type: "float", nullable: false), + Status = table.Column(type: "int", nullable: false), + DateCreate = table.Column(type: "datetime2", nullable: false), + DateImplement = table.Column(type: "datetime2", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Orders", x => x.Id); + table.ForeignKey( + name: "FK_Orders_Documents_DocumentId", + column: x => x.DocumentId, + principalTable: "Documents", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_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..a036d19 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Migrations/LawFirmDatabaseModelSnapshot.cs @@ -0,0 +1,170 @@ +// +using System; +using LawFirmDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace LawFirmDatabaseImplement.Migrations +{ + [DbContext(typeof(LawFirmDatabase))] + partial class LawFirmDatabaseModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Blank", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("BlankName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Blanks"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Document", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("DocumentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Documents"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.DocumentBlank", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("BlankId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DocumentId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("BlankId"); + + b.HasIndex("DocumentId"); + + b.ToTable("DocumentBlanks"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("DocumentId") + .HasColumnType("int"); + + b.Property("DocumentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + 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", null) + .WithMany("Orders") + .HasForeignKey("DocumentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + 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 + } + } +} -- 2.25.1 From 0e351cdabe2367abf3ffe24fa04443afd40322d0 Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Fri, 24 Feb 2023 19:23:26 +0400 Subject: [PATCH 03/14] =?UTF-8?q?=D0=91=D0=B0=D0=B7=D0=BE=D0=B2=D0=B0?= =?UTF-8?q?=D1=8F=20=D0=BB=D0=B0=D0=B13=20=D0=B2=D1=80=D0=BE=D0=B4=D0=B5?= =?UTF-8?q?=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=B5=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LawFirm/LawFirm/Program.cs | 2 +- .../Implements/BlankStorage.cs | 91 +++++++++++++ .../Implements/DocumentStorage.cs | 128 ++++++++++++++++++ .../Implements/OrderStorage.cs | 83 ++++++++++++ 4 files changed, 303 insertions(+), 1 deletion(-) create mode 100644 LawFirm/LawFirmDatabaseImplement/Implements/BlankStorage.cs create mode 100644 LawFirm/LawFirmDatabaseImplement/Implements/DocumentStorage.cs create mode 100644 LawFirm/LawFirmDatabaseImplement/Implements/OrderStorage.cs diff --git a/LawFirm/LawFirm/Program.cs b/LawFirm/LawFirm/Program.cs index 09dd87f..8babc23 100644 --- a/LawFirm/LawFirm/Program.cs +++ b/LawFirm/LawFirm/Program.cs @@ -1,7 +1,7 @@ using LawFirmBusinessLogic.BusinessLogics; using LawFirmContracts.BusinessLogicContracts; using LawFirmContracts.StorageContracts; -using LawFirmFileImplement.Implements; +using LawFirmDatabaseImplement.Implements; using LawFirmView; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; diff --git a/LawFirm/LawFirmDatabaseImplement/Implements/BlankStorage.cs b/LawFirm/LawFirmDatabaseImplement/Implements/BlankStorage.cs new file mode 100644 index 0000000..57174f5 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Implements/BlankStorage.cs @@ -0,0 +1,91 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.SearchModels; +using LawFirmContracts.StorageContracts; +using LawFirmContracts.ViewModels; +using LawFirmDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LawFirmDatabaseImplement.Implements +{ + public class BlankStorage : IBlankStorage + { + 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 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 List GetFullList() + { + using var context = new LawFirmDatabase(); + return context.Blanks.Select(x => x.GetViewModel).ToList(); + } + + 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 blank = context.Blanks.FirstOrDefault(x => x.Id == model.Id); + if (blank == null) + { + return null; + } + blank.Update(model); + context.SaveChanges(); + return blank.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..631fac6 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Implements/DocumentStorage.cs @@ -0,0 +1,128 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.SearchModels; +using LawFirmContracts.StorageContracts; +using LawFirmContracts.ViewModels; +using LawFirmDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection.Metadata.Ecma335; +using System.Text; +using System.Threading.Tasks; + +namespace LawFirmDatabaseImplement.Implements +{ + public class DocumentStorage : IDocumentStorage + { + public DocumentViewModel? GetElement(DocumentSearchModel model) + { + if (string.IsNullOrEmpty(model.DocumentName) && !model.Id.HasValue) + { + return null; + } + using var context = new LawFirmDatabase(); + /*return context.Documents.FirstOrDefault(x => + (!string.IsNullOrEmpty(model.DocumentName) && x.DocumentName == model.DocumentName) + || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;*/ + + var element = context.Documents.FirstOrDefault(x => + (!string.IsNullOrEmpty(model.DocumentName) && x.DocumentName == model.DocumentName) + || (model.Id.HasValue && x.Id == model.Id)); + + return element?.GetViewModel; + } + + public List GetFilteredList(DocumentSearchModel model) + { + if (string.IsNullOrEmpty(model.DocumentName)) + { + return new(); + } + using var context = new LawFirmDatabase(); + /*return context.Documents + .Where(x => x.DocumentName.Contains(model.DocumentName)) + .Select(x => x.GetViewModel) + .ToList();*/ + var filteredList = context.Documents.Where(x => x.DocumentName.Contains(model.DocumentName)).ToList(); + return filteredList.Select(x=> x.GetViewModel).ToList(); + } + + public List GetFullList() + { + using var context = new LawFirmDatabase(); + //return context.Documents.Select(x => x.GetViewModel).ToList(); + var fullList = context.Documents.ToList(); + return fullList.Select(x => x.GetViewModel).ToList(); + } + + public DocumentViewModel? Insert(DocumentBindingModel model) + { + using var context = new LawFirmDatabase(); + using var transaction = context.Database.BeginTransaction(); + { + try + { + var newDoc = Document.Create(context, model); + if (newDoc == null) + { + transaction.Rollback(); + return null; + } + context.Documents.Add(newDoc); + + context.SaveChanges(); + context.Database.CommitTransaction(); + + return newDoc.GetViewModel; + } + catch (Exception) + { + transaction.Rollback(); + return null; + } + } + } + + public DocumentViewModel? Update(DocumentBindingModel model) + { + using var context = new LawFirmDatabase(); + using var transaction = context.Database.BeginTransaction(); + { + try + { + var document = context.Documents.FirstOrDefault(x => x.Id == model.Id); + if (document == null) + { + transaction.Rollback(); + return null; + } + + document.Update(model); + + context.SaveChanges(); + context.Database.CommitTransaction(); + + return document.GetViewModel; + } + catch (Exception) + { + transaction.Rollback(); + return null; + } + } + } + public DocumentViewModel? Delete(DocumentBindingModel model) + { + using var context = new LawFirmDatabase(); + var element = context.Documents.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..1c37874 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,83 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.SearchModels; +using LawFirmContracts.StorageContracts; +using LawFirmContracts.ViewModels; +using LawFirmDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LawFirmDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new LawFirmDatabase(); + return context.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new LawFirmDatabase(); + return context.Orders + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new LawFirmDatabase(); + return context.Orders.Select(x => x.GetViewModel).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 newOrder.GetViewModel; + } + + 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 order.GetViewModel; + } + 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 element.GetViewModel; + } + return null; + } + } +} -- 2.25.1 From b3c2b05c5be9ee9a29bab483b996fc63f239edf6 Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Fri, 24 Feb 2023 17:55:19 +0400 Subject: [PATCH 04/14] =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=BE=D0=B2=20?= =?UTF-8?q?=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D0=B8=20?= =?UTF-8?q?=D1=85=D1=80=D0=B0=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B2=20?= =?UTF-8?q?=D0=B1=D0=B0=D0=B7=D0=B5=20=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B=D1=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LawFirm/LawFirm.sln | 6 ++ .../LawFirmDatabase.cs | 28 ++++++ .../LawFirmDatabaseImplement.csproj | 23 +++++ .../LawFirmDatabaseImplement/Models/Blank.cs | 56 +++++++++++ .../Models/Document.cs | 99 +++++++++++++++++++ .../Models/DocumentBlank.cs | 23 +++++ .../LawFirmDatabaseImplement/Models/Order.cs | 79 +++++++++++++++ 7 files changed, 314 insertions(+) create mode 100644 LawFirm/LawFirmDatabaseImplement/LawFirmDatabase.cs create mode 100644 LawFirm/LawFirmDatabaseImplement/LawFirmDatabaseImplement.csproj 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 362180a..33cfb68 100644 --- a/LawFirm/LawFirm.sln +++ b/LawFirm/LawFirm.sln @@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LawFirmListImplements", "La EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LawFirmFileImplement", "LawFirmFileImplement\LawFirmFileImplement.csproj", "{E75AF81D-A466-470A-A3F7-EC0E6F33C866}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LawFirmDatabaseImplement", "LawFirmDatabaseImplement\LawFirmDatabaseImplement.csproj", "{CB0444D3-5BDA-42DB-95F9-60191AE9073A}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -45,6 +47,10 @@ Global {E75AF81D-A466-470A-A3F7-EC0E6F33C866}.Debug|Any CPU.Build.0 = Debug|Any CPU {E75AF81D-A466-470A-A3F7-EC0E6F33C866}.Release|Any CPU.ActiveCfg = Release|Any CPU {E75AF81D-A466-470A-A3F7-EC0E6F33C866}.Release|Any CPU.Build.0 = Release|Any CPU + {CB0444D3-5BDA-42DB-95F9-60191AE9073A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CB0444D3-5BDA-42DB-95F9-60191AE9073A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CB0444D3-5BDA-42DB-95F9-60191AE9073A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CB0444D3-5BDA-42DB-95F9-60191AE9073A}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/LawFirm/LawFirmDatabaseImplement/LawFirmDatabase.cs b/LawFirm/LawFirmDatabaseImplement/LawFirmDatabase.cs new file mode 100644 index 0000000..a893522 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/LawFirmDatabase.cs @@ -0,0 +1,28 @@ +using LawFirmDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LawFirmDatabaseImplement +{ + public class LawFirmDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"DataSource=danyaxren\SQLEXPRESS;Initial Catalog=LawFirmDatabaseFull;IntegratedSecurity=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + } + 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..1590021 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/LawFirmDatabaseImplement.csproj @@ -0,0 +1,23 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/LawFirm/LawFirmDatabaseImplement/Models/Blank.cs b/LawFirm/LawFirmDatabaseImplement/Models/Blank.cs new file mode 100644 index 0000000..a7db4b2 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Models/Blank.cs @@ -0,0 +1,56 @@ +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 Blank : IBlankModel + { + public int Id { get; private set; } + [Required] + public string BlankName { get; private set; } = String.Empty; + + [Required] + public double Cost { 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, + Cost = model.Cost + }; + } + + public void Update(BlankBindingModel? model) + { + if (model == null) + { + return; + } + BlankName = model.BlankName; + Cost = model.Cost; + } + public BlankViewModel GetViewModel => new() + { + Id = Id, + BlankName = BlankName, + Cost = Cost + }; + } +} diff --git a/LawFirm/LawFirmDatabaseImplement/Models/Document.cs b/LawFirm/LawFirmDatabaseImplement/Models/Document.cs new file mode 100644 index 0000000..e70bb12 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Models/Document.cs @@ -0,0 +1,99 @@ +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 Document : IDocumentModel + { + public int Id { get; private set; } + [Required] + public string DocumentName { get; private set; } = string.Empty; + [Required] + public double Price { get; private set; } + + private Dictionary? _documentBlanks = null; + + [NotMapped] + public Dictionary DocumentBlanks + { + get + { + if (_documentBlanks == null) + { + _documentBlanks = Blanks.ToDictionary(recPC => recPC.BlankId, recPC => (recPC.Blank as IBlankModel, recPC.Count)); + } + return _documentBlanks; + } + } + + [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 UpdateComponents(LawFirmDatabase context, DocumentBindingModel model) + { + var documentBlanks = context.DocumentBlanks.Where(rec => rec.DocumentId == model.Id).ToList(); + if (documentBlanks != null && documentBlanks.Count > 0) + { // удалили те, которых нет в модели + context.DocumentBlanks.RemoveRange(documentBlanks.Where(rec => !model.DocumentBlanks.ContainsKey(rec.BlankId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateBlank in documentBlanks) + { + updateBlank.Count = model.DocumentBlanks[updateBlank.BlankId].Item2; + model.DocumentBlanks.Remove(updateBlank.BlankId); + } + context.SaveChanges(); + } + var document = context.Documents.First(x => x.Id == Id); + foreach (var pc in model.DocumentBlanks) + { + context.DocumentBlanks.Add(new DocumentBlank + { + Document = document, + Blank = context.Blanks.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _documentBlanks = null; + } + } +} diff --git a/LawFirm/LawFirmDatabaseImplement/Models/DocumentBlank.cs b/LawFirm/LawFirmDatabaseImplement/Models/DocumentBlank.cs new file mode 100644 index 0000000..43894ec --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Models/DocumentBlank.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +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..51ab4bb --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Models/Order.cs @@ -0,0 +1,79 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.ViewModels; +using LawFirmDataModels.Enums; +using LawFirmDataModels.Models; +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 Order : IOrderModel + { + public int Id { get; private set; } + [Required] + public int DocumentId { get; private set; } + + public string DocumentName { get; private set; } = string.Empty; + [Required] + public int Count { get; private set; } + [Required] + public double Sum { get; private set; } + [Required] + public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; + [Required] + public DateTime DateCreate { get; private set; } = DateTime.Now; + + public DateTime? DateImplement { get; private set; } + + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order + { + DocumentId = model.DocumentId, + DocumentName = model.DocumentName, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement, + Id = model.Id, + }; + } + + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + DocumentId = model.DocumentId; + DocumentName = model.DocumentName; + Count = model.Count; + Sum = model.Sum; + Status = model.Status; + DateCreate = model.DateCreate; + DateImplement = model.DateImplement; + Id = model.Id; + } + + public OrderViewModel GetViewModel => new() + { + DocumentId = DocumentId, + DocumentName = DocumentName, + Count = Count, + Sum = Sum, + DateCreate = DateCreate, + DateImplement = DateImplement, + Id = Id, + Status = Status, + }; + } +} -- 2.25.1 From 5e5d85698ba06b2a1356a2d9d129d274da710b1b Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Fri, 24 Feb 2023 18:38:05 +0400 Subject: [PATCH 05/14] =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=BC=D0=B8=D0=B3=D1=80=D0=B0=D1=86=D0=B8=D0=B8?= =?UTF-8?q?=20=D0=B8=20=D0=91=D0=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LawFirm/LawFirm/LawFirmView.csproj | 5 + .../LawFirmDatabase.cs | 2 +- .../20230224135956_InitialCreate.Designer.cs | 173 ++++++++++++++++++ .../20230224135956_InitialCreate.cs | 126 +++++++++++++ .../LawFirmDatabaseModelSnapshot.cs | 170 +++++++++++++++++ 5 files changed, 475 insertions(+), 1 deletion(-) create mode 100644 LawFirm/LawFirmDatabaseImplement/Migrations/20230224135956_InitialCreate.Designer.cs create mode 100644 LawFirm/LawFirmDatabaseImplement/Migrations/20230224135956_InitialCreate.cs create mode 100644 LawFirm/LawFirmDatabaseImplement/Migrations/LawFirmDatabaseModelSnapshot.cs diff --git a/LawFirm/LawFirm/LawFirmView.csproj b/LawFirm/LawFirm/LawFirmView.csproj index 7f7c8cf..9d320d6 100644 --- a/LawFirm/LawFirm/LawFirmView.csproj +++ b/LawFirm/LawFirm/LawFirmView.csproj @@ -9,6 +9,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -17,6 +21,7 @@ + diff --git a/LawFirm/LawFirmDatabaseImplement/LawFirmDatabase.cs b/LawFirm/LawFirmDatabaseImplement/LawFirmDatabase.cs index a893522..6cabc79 100644 --- a/LawFirm/LawFirmDatabaseImplement/LawFirmDatabase.cs +++ b/LawFirm/LawFirmDatabaseImplement/LawFirmDatabase.cs @@ -15,7 +15,7 @@ namespace LawFirmDatabaseImplement { if (optionsBuilder.IsConfigured == false) { - optionsBuilder.UseSqlServer(@"DataSource=danyaxren\SQLEXPRESS;Initial Catalog=LawFirmDatabaseFull;IntegratedSecurity=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + optionsBuilder.UseSqlServer(@"Data Source=DANYAXREN\SQLEXPRESS;Initial Catalog=LawFirmDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); } base.OnConfiguring(optionsBuilder); } diff --git a/LawFirm/LawFirmDatabaseImplement/Migrations/20230224135956_InitialCreate.Designer.cs b/LawFirm/LawFirmDatabaseImplement/Migrations/20230224135956_InitialCreate.Designer.cs new file mode 100644 index 0000000..9a2c3f4 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Migrations/20230224135956_InitialCreate.Designer.cs @@ -0,0 +1,173 @@ +// +using System; +using LawFirmDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace LawFirmDatabaseImplement.Migrations +{ + [DbContext(typeof(LawFirmDatabase))] + [Migration("20230224135956_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Blank", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("BlankName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Blanks"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Document", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("DocumentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Documents"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.DocumentBlank", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("BlankId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DocumentId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("BlankId"); + + b.HasIndex("DocumentId"); + + b.ToTable("DocumentBlanks"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("DocumentId") + .HasColumnType("int"); + + b.Property("DocumentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + 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", null) + .WithMany("Orders") + .HasForeignKey("DocumentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + 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/20230224135956_InitialCreate.cs b/LawFirm/LawFirmDatabaseImplement/Migrations/20230224135956_InitialCreate.cs new file mode 100644 index 0000000..1c417b0 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Migrations/20230224135956_InitialCreate.cs @@ -0,0 +1,126 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#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: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + BlankName = table.Column(type: "nvarchar(max)", nullable: false), + Cost = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Blanks", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Documents", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + DocumentName = table.Column(type: "nvarchar(max)", nullable: false), + Price = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Documents", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "DocumentBlanks", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + DocumentId = table.Column(type: "int", nullable: false), + BlankId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", 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: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + DocumentId = table.Column(type: "int", nullable: false), + DocumentName = table.Column(type: "nvarchar(max)", nullable: false), + Count = table.Column(type: "int", nullable: false), + Sum = table.Column(type: "float", nullable: false), + Status = table.Column(type: "int", nullable: false), + DateCreate = table.Column(type: "datetime2", nullable: false), + DateImplement = table.Column(type: "datetime2", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Orders", x => x.Id); + table.ForeignKey( + name: "FK_Orders_Documents_DocumentId", + column: x => x.DocumentId, + principalTable: "Documents", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_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..a036d19 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Migrations/LawFirmDatabaseModelSnapshot.cs @@ -0,0 +1,170 @@ +// +using System; +using LawFirmDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace LawFirmDatabaseImplement.Migrations +{ + [DbContext(typeof(LawFirmDatabase))] + partial class LawFirmDatabaseModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Blank", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("BlankName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Blanks"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Document", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("DocumentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Documents"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.DocumentBlank", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("BlankId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DocumentId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("BlankId"); + + b.HasIndex("DocumentId"); + + b.ToTable("DocumentBlanks"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("DocumentId") + .HasColumnType("int"); + + b.Property("DocumentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + 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", null) + .WithMany("Orders") + .HasForeignKey("DocumentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + 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 + } + } +} -- 2.25.1 From 3e8b6586d30897603854f5fae31465b3d493fcfa Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Fri, 24 Feb 2023 19:23:26 +0400 Subject: [PATCH 06/14] =?UTF-8?q?=D0=91=D0=B0=D0=B7=D0=BE=D0=B2=D0=B0?= =?UTF-8?q?=D1=8F=20=D0=BB=D0=B0=D0=B13=20=D0=B2=D1=80=D0=BE=D0=B4=D0=B5?= =?UTF-8?q?=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=B5=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LawFirm/LawFirm/Program.cs | 2 +- .../Implements/BlankStorage.cs | 91 +++++++++++++ .../Implements/DocumentStorage.cs | 128 ++++++++++++++++++ .../Implements/OrderStorage.cs | 83 ++++++++++++ 4 files changed, 303 insertions(+), 1 deletion(-) create mode 100644 LawFirm/LawFirmDatabaseImplement/Implements/BlankStorage.cs create mode 100644 LawFirm/LawFirmDatabaseImplement/Implements/DocumentStorage.cs create mode 100644 LawFirm/LawFirmDatabaseImplement/Implements/OrderStorage.cs diff --git a/LawFirm/LawFirm/Program.cs b/LawFirm/LawFirm/Program.cs index 09dd87f..8babc23 100644 --- a/LawFirm/LawFirm/Program.cs +++ b/LawFirm/LawFirm/Program.cs @@ -1,7 +1,7 @@ using LawFirmBusinessLogic.BusinessLogics; using LawFirmContracts.BusinessLogicContracts; using LawFirmContracts.StorageContracts; -using LawFirmFileImplement.Implements; +using LawFirmDatabaseImplement.Implements; using LawFirmView; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; diff --git a/LawFirm/LawFirmDatabaseImplement/Implements/BlankStorage.cs b/LawFirm/LawFirmDatabaseImplement/Implements/BlankStorage.cs new file mode 100644 index 0000000..57174f5 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Implements/BlankStorage.cs @@ -0,0 +1,91 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.SearchModels; +using LawFirmContracts.StorageContracts; +using LawFirmContracts.ViewModels; +using LawFirmDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LawFirmDatabaseImplement.Implements +{ + public class BlankStorage : IBlankStorage + { + 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 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 List GetFullList() + { + using var context = new LawFirmDatabase(); + return context.Blanks.Select(x => x.GetViewModel).ToList(); + } + + 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 blank = context.Blanks.FirstOrDefault(x => x.Id == model.Id); + if (blank == null) + { + return null; + } + blank.Update(model); + context.SaveChanges(); + return blank.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..631fac6 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Implements/DocumentStorage.cs @@ -0,0 +1,128 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.SearchModels; +using LawFirmContracts.StorageContracts; +using LawFirmContracts.ViewModels; +using LawFirmDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection.Metadata.Ecma335; +using System.Text; +using System.Threading.Tasks; + +namespace LawFirmDatabaseImplement.Implements +{ + public class DocumentStorage : IDocumentStorage + { + public DocumentViewModel? GetElement(DocumentSearchModel model) + { + if (string.IsNullOrEmpty(model.DocumentName) && !model.Id.HasValue) + { + return null; + } + using var context = new LawFirmDatabase(); + /*return context.Documents.FirstOrDefault(x => + (!string.IsNullOrEmpty(model.DocumentName) && x.DocumentName == model.DocumentName) + || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;*/ + + var element = context.Documents.FirstOrDefault(x => + (!string.IsNullOrEmpty(model.DocumentName) && x.DocumentName == model.DocumentName) + || (model.Id.HasValue && x.Id == model.Id)); + + return element?.GetViewModel; + } + + public List GetFilteredList(DocumentSearchModel model) + { + if (string.IsNullOrEmpty(model.DocumentName)) + { + return new(); + } + using var context = new LawFirmDatabase(); + /*return context.Documents + .Where(x => x.DocumentName.Contains(model.DocumentName)) + .Select(x => x.GetViewModel) + .ToList();*/ + var filteredList = context.Documents.Where(x => x.DocumentName.Contains(model.DocumentName)).ToList(); + return filteredList.Select(x=> x.GetViewModel).ToList(); + } + + public List GetFullList() + { + using var context = new LawFirmDatabase(); + //return context.Documents.Select(x => x.GetViewModel).ToList(); + var fullList = context.Documents.ToList(); + return fullList.Select(x => x.GetViewModel).ToList(); + } + + public DocumentViewModel? Insert(DocumentBindingModel model) + { + using var context = new LawFirmDatabase(); + using var transaction = context.Database.BeginTransaction(); + { + try + { + var newDoc = Document.Create(context, model); + if (newDoc == null) + { + transaction.Rollback(); + return null; + } + context.Documents.Add(newDoc); + + context.SaveChanges(); + context.Database.CommitTransaction(); + + return newDoc.GetViewModel; + } + catch (Exception) + { + transaction.Rollback(); + return null; + } + } + } + + public DocumentViewModel? Update(DocumentBindingModel model) + { + using var context = new LawFirmDatabase(); + using var transaction = context.Database.BeginTransaction(); + { + try + { + var document = context.Documents.FirstOrDefault(x => x.Id == model.Id); + if (document == null) + { + transaction.Rollback(); + return null; + } + + document.Update(model); + + context.SaveChanges(); + context.Database.CommitTransaction(); + + return document.GetViewModel; + } + catch (Exception) + { + transaction.Rollback(); + return null; + } + } + } + public DocumentViewModel? Delete(DocumentBindingModel model) + { + using var context = new LawFirmDatabase(); + var element = context.Documents.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..1c37874 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,83 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.SearchModels; +using LawFirmContracts.StorageContracts; +using LawFirmContracts.ViewModels; +using LawFirmDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LawFirmDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new LawFirmDatabase(); + return context.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new LawFirmDatabase(); + return context.Orders + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new LawFirmDatabase(); + return context.Orders.Select(x => x.GetViewModel).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 newOrder.GetViewModel; + } + + 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 order.GetViewModel; + } + 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 element.GetViewModel; + } + return null; + } + } +} -- 2.25.1 From 851b151d03ec93e25345294631d4de3aba24ba50 Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Fri, 10 Mar 2023 20:47:22 +0400 Subject: [PATCH 07/14] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/OrderStorage.cs | 10 +- .../20230310164628_OrderFix.Designer.cs | 171 ++++++++++++++++++ .../Migrations/20230310164628_OrderFix.cs | 29 +++ .../LawFirmDatabaseModelSnapshot.cs | 8 +- .../Models/Document.cs | 1 + .../LawFirmDatabaseImplement/Models/Order.cs | 12 +- 6 files changed, 212 insertions(+), 19 deletions(-) create mode 100644 LawFirm/LawFirmDatabaseImplement/Migrations/20230310164628_OrderFix.Designer.cs create mode 100644 LawFirm/LawFirmDatabaseImplement/Migrations/20230310164628_OrderFix.cs diff --git a/LawFirm/LawFirmDatabaseImplement/Implements/OrderStorage.cs b/LawFirm/LawFirmDatabaseImplement/Implements/OrderStorage.cs index 1c37874..66b9941 100644 --- a/LawFirm/LawFirmDatabaseImplement/Implements/OrderStorage.cs +++ b/LawFirm/LawFirmDatabaseImplement/Implements/OrderStorage.cs @@ -3,6 +3,7 @@ using LawFirmContracts.SearchModels; using LawFirmContracts.StorageContracts; using LawFirmContracts.ViewModels; using LawFirmDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; @@ -20,7 +21,7 @@ namespace LawFirmDatabaseImplement.Implements return null; } using var context = new LawFirmDatabase(); - return context.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + return context.Orders.Include(x => x.Document).FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; } public List GetFilteredList(OrderSearchModel model) @@ -32,6 +33,7 @@ namespace LawFirmDatabaseImplement.Implements using var context = new LawFirmDatabase(); return context.Orders .Where(x => x.Id == model.Id) + .Include(x => x.Document) .Select(x => x.GetViewModel) .ToList(); } @@ -39,7 +41,7 @@ namespace LawFirmDatabaseImplement.Implements public List GetFullList() { using var context = new LawFirmDatabase(); - return context.Orders.Select(x => x.GetViewModel).ToList(); + return context.Orders.Include(x => x.Document).Select(x => x.GetViewModel).ToList(); } public OrderViewModel? Insert(OrderBindingModel model) @@ -52,7 +54,7 @@ namespace LawFirmDatabaseImplement.Implements using var context = new LawFirmDatabase(); context.Orders.Add(newOrder); context.SaveChanges(); - return newOrder.GetViewModel; + return context.Orders.Include(x => x.Document).FirstOrDefault(x => x.Id == newOrder.Id)?.GetViewModel; } public OrderViewModel? Update(OrderBindingModel model) @@ -65,7 +67,7 @@ namespace LawFirmDatabaseImplement.Implements } order.Update(model); context.SaveChanges(); - return order.GetViewModel; + return context.Orders.Include(x => x.Document).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; } public OrderViewModel? Delete(OrderBindingModel model) { diff --git a/LawFirm/LawFirmDatabaseImplement/Migrations/20230310164628_OrderFix.Designer.cs b/LawFirm/LawFirmDatabaseImplement/Migrations/20230310164628_OrderFix.Designer.cs new file mode 100644 index 0000000..805c562 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Migrations/20230310164628_OrderFix.Designer.cs @@ -0,0 +1,171 @@ +// +using System; +using LawFirmDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace LawFirmDatabaseImplement.Migrations +{ + [DbContext(typeof(LawFirmDatabase))] + [Migration("20230310164628_OrderFix")] + partial class OrderFix + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Blank", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("BlankName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Blanks"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Document", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("DocumentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Documents"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.DocumentBlank", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("BlankId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DocumentId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("BlankId"); + + b.HasIndex("DocumentId"); + + b.ToTable("DocumentBlanks"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("DocumentId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("DocumentId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.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/20230310164628_OrderFix.cs b/LawFirm/LawFirmDatabaseImplement/Migrations/20230310164628_OrderFix.cs new file mode 100644 index 0000000..279f9f5 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Migrations/20230310164628_OrderFix.cs @@ -0,0 +1,29 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace LawFirmDatabaseImplement.Migrations +{ + /// + public partial class OrderFix : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropColumn( + name: "DocumentName", + table: "Orders"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "DocumentName", + table: "Orders", + type: "nvarchar(max)", + nullable: false, + defaultValue: ""); + } + } +} diff --git a/LawFirm/LawFirmDatabaseImplement/Migrations/LawFirmDatabaseModelSnapshot.cs b/LawFirm/LawFirmDatabaseImplement/Migrations/LawFirmDatabaseModelSnapshot.cs index a036d19..a9faa90 100644 --- a/LawFirm/LawFirmDatabaseImplement/Migrations/LawFirmDatabaseModelSnapshot.cs +++ b/LawFirm/LawFirmDatabaseImplement/Migrations/LawFirmDatabaseModelSnapshot.cs @@ -108,10 +108,6 @@ namespace LawFirmDatabaseImplement.Migrations b.Property("DocumentId") .HasColumnType("int"); - b.Property("DocumentName") - .IsRequired() - .HasColumnType("nvarchar(max)"); - b.Property("Status") .HasColumnType("int"); @@ -146,11 +142,13 @@ namespace LawFirmDatabaseImplement.Migrations modelBuilder.Entity("LawFirmDatabaseImplement.Models.Order", b => { - b.HasOne("LawFirmDatabaseImplement.Models.Document", null) + b.HasOne("LawFirmDatabaseImplement.Models.Document", "Document") .WithMany("Orders") .HasForeignKey("DocumentId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); + + b.Navigation("Document"); }); modelBuilder.Entity("LawFirmDatabaseImplement.Models.Blank", b => diff --git a/LawFirm/LawFirmDatabaseImplement/Models/Document.cs b/LawFirm/LawFirmDatabaseImplement/Models/Document.cs index e70bb12..ddb5347 100644 --- a/LawFirm/LawFirmDatabaseImplement/Models/Document.cs +++ b/LawFirm/LawFirmDatabaseImplement/Models/Document.cs @@ -39,6 +39,7 @@ namespace LawFirmDatabaseImplement.Models [ForeignKey("DocumentId")] public virtual List Orders { get; set; } = new(); + public static Document? Create(LawFirmDatabase context, DocumentBindingModel model) { return new Document() diff --git a/LawFirm/LawFirmDatabaseImplement/Models/Order.cs b/LawFirm/LawFirmDatabaseImplement/Models/Order.cs index 51ab4bb..45289e6 100644 --- a/LawFirm/LawFirmDatabaseImplement/Models/Order.cs +++ b/LawFirm/LawFirmDatabaseImplement/Models/Order.cs @@ -16,8 +16,6 @@ namespace LawFirmDatabaseImplement.Models public int Id { get; private set; } [Required] public int DocumentId { get; private set; } - - public string DocumentName { get; private set; } = string.Empty; [Required] public int Count { get; private set; } [Required] @@ -28,6 +26,7 @@ namespace LawFirmDatabaseImplement.Models public DateTime DateCreate { get; private set; } = DateTime.Now; public DateTime? DateImplement { get; private set; } + public virtual Document Document { get; set; } public static Order? Create(OrderBindingModel? model) { @@ -38,7 +37,6 @@ namespace LawFirmDatabaseImplement.Models return new Order { DocumentId = model.DocumentId, - DocumentName = model.DocumentName, Count = model.Count, Sum = model.Sum, Status = model.Status, @@ -54,26 +52,20 @@ namespace LawFirmDatabaseImplement.Models { return; } - DocumentId = model.DocumentId; - DocumentName = model.DocumentName; - Count = model.Count; - Sum = model.Sum; Status = model.Status; - DateCreate = model.DateCreate; DateImplement = model.DateImplement; - Id = model.Id; } public OrderViewModel GetViewModel => new() { DocumentId = DocumentId, - DocumentName = DocumentName, Count = Count, Sum = Sum, DateCreate = DateCreate, DateImplement = DateImplement, Id = Id, Status = Status, + DocumentName = Document.DocumentName }; } } -- 2.25.1 From cca8ab71f3f645149f9b415cfd6dae0c48abbb3c Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Sat, 11 Mar 2023 15:08:25 +0400 Subject: [PATCH 08/14] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/DocumentStorage.cs | 34 ++++++++++--------- .../Models/Document.cs | 2 ++ 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/LawFirm/LawFirmDatabaseImplement/Implements/DocumentStorage.cs b/LawFirm/LawFirmDatabaseImplement/Implements/DocumentStorage.cs index 631fac6..2fc7f8d 100644 --- a/LawFirm/LawFirmDatabaseImplement/Implements/DocumentStorage.cs +++ b/LawFirm/LawFirmDatabaseImplement/Implements/DocumentStorage.cs @@ -3,6 +3,7 @@ using LawFirmContracts.SearchModels; using LawFirmContracts.StorageContracts; using LawFirmContracts.ViewModels; using LawFirmDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; @@ -21,15 +22,12 @@ namespace LawFirmDatabaseImplement.Implements return null; } using var context = new LawFirmDatabase(); - /*return context.Documents.FirstOrDefault(x => - (!string.IsNullOrEmpty(model.DocumentName) && x.DocumentName == model.DocumentName) - || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;*/ - - var element = context.Documents.FirstOrDefault(x => - (!string.IsNullOrEmpty(model.DocumentName) && x.DocumentName == model.DocumentName) - || (model.Id.HasValue && x.Id == model.Id)); - - return element?.GetViewModel; + 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 List GetFilteredList(DocumentSearchModel model) @@ -39,20 +37,24 @@ namespace LawFirmDatabaseImplement.Implements return new(); } using var context = new LawFirmDatabase(); - /*return context.Documents + return context.Documents + .Include(x => x.Blanks) + .ThenInclude(x => x.Blank) .Where(x => x.DocumentName.Contains(model.DocumentName)) + .ToList() .Select(x => x.GetViewModel) - .ToList();*/ - var filteredList = context.Documents.Where(x => x.DocumentName.Contains(model.DocumentName)).ToList(); - return filteredList.Select(x=> x.GetViewModel).ToList(); + .ToList(); } public List GetFullList() { using var context = new LawFirmDatabase(); - //return context.Documents.Select(x => x.GetViewModel).ToList(); - var fullList = context.Documents.ToList(); - return fullList.Select(x => x.GetViewModel).ToList(); + return context.Documents + .Include(x => x.Blanks) + .ThenInclude(x => x.Blank) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); } public DocumentViewModel? Insert(DocumentBindingModel model) diff --git a/LawFirm/LawFirmDatabaseImplement/Models/Document.cs b/LawFirm/LawFirmDatabaseImplement/Models/Document.cs index ddb5347..1046285 100644 --- a/LawFirm/LawFirmDatabaseImplement/Models/Document.cs +++ b/LawFirm/LawFirmDatabaseImplement/Models/Document.cs @@ -42,6 +42,8 @@ namespace LawFirmDatabaseImplement.Models public static Document? Create(LawFirmDatabase context, DocumentBindingModel model) { + var blanks = context.Blanks; + return new Document() { Id = model.Id, -- 2.25.1 From cc30da7d11336edf1c80f7769808d6f69d23008c Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Sun, 12 Mar 2023 18:42:24 +0400 Subject: [PATCH 09/14] =?UTF-8?q?=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD?= =?UTF-8?q?=D1=8B=20=D0=BC=D0=BE=D0=B4=D0=B5=D0=BB=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/ShopStorage.cs | 109 +++++++++++++++++ .../LawFirmDatabase.cs | 2 + .../LawFirmDatabaseImplement/Models/Shop.cs | 113 ++++++++++++++++++ .../Models/ShopDocument.cs | 27 +++++ 4 files changed, 251 insertions(+) create mode 100644 LawFirm/LawFirmDatabaseImplement/Implements/ShopStorage.cs create mode 100644 LawFirm/LawFirmDatabaseImplement/Models/Shop.cs create mode 100644 LawFirm/LawFirmDatabaseImplement/Models/ShopDocument.cs diff --git a/LawFirm/LawFirmDatabaseImplement/Implements/ShopStorage.cs b/LawFirm/LawFirmDatabaseImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..adbb71c --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Implements/ShopStorage.cs @@ -0,0 +1,109 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.SearchModels; +using LawFirmContracts.StorageContracts; +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.Name) && !model.Id.HasValue) + { + return new(); + } + using var context = new LawFirmDatabase(); + return context.Shops.Include(x => x.Documents).ThenInclude(x => x.Document).FirstOrDefault(x => + (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) || + (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.Name)) + { + return new(); + } + using var context = new LawFirmDatabase(); + return context.Shops.Include(x => x.Documents).ThenInclude(x => x.Document).Where(x => x.Name.Contains(model.Name)).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 newShop = Shop.Create(context, model); + if (newShop == null) + { + return null; + } + + if (context.Shops.Any(x => x.Name == newShop.Name)) + { + throw new Exception("Название магазина уже занято"); + } + + context.Shops.Add(newShop); + context.SaveChanges(); + return newShop.GetViewModel; + } + + public ShopViewModel? Update(ShopBindingModel model) + { + using var context = new LawFirmDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var shop = context.Shops.FirstOrDefault(x => x.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 shop = context.Shops.Include(x => x.Documents).FirstOrDefault(x => x.Id == model.Id); + if (shop != null) + { + context.Shops.Remove(shop); + context.SaveChanges(); + return shop.GetViewModel; + } + return null; + } + + public bool SellDocument(IDocumentModel model, int count) + { + using var context = new LawFirmDatabase(); + using var transaction = context.Database.BeginTransaction(); + + } + + } +} diff --git a/LawFirm/LawFirmDatabaseImplement/LawFirmDatabase.cs b/LawFirm/LawFirmDatabaseImplement/LawFirmDatabase.cs index 6cabc79..cd64a04 100644 --- a/LawFirm/LawFirmDatabaseImplement/LawFirmDatabase.cs +++ b/LawFirm/LawFirmDatabaseImplement/LawFirmDatabase.cs @@ -23,6 +23,8 @@ 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/Models/Shop.cs b/LawFirm/LawFirmDatabaseImplement/Models/Shop.cs new file mode 100644 index 0000000..9249ae7 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Models/Shop.cs @@ -0,0 +1,113 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.ViewModels; +using LawFirmDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LawFirmDatabaseImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; set; } + [Required] + public string Name { get; set; } = string.Empty; + + [Required] + public string Adress { get; set; } = string.Empty; + + [Required] + public DateTime OpeningDate { get; set; } + + [ForeignKey("ShopId")] + public List Documents { get; set; } = new(); + + private Dictionary? _shopDocuments = null; + + [NotMapped] + public Dictionary ShopDocuments + { + get + { + if (_shopDocuments == null) + { + _shopDocuments = Documents.ToDictionary(recPC => recPC.DocumentId, recPC => (recPC.Document as IDocumentModel, recPC.Count)); + } + return _shopDocuments; + } + } + + [Required] + public int MaxCountDocuments { get; set; } + + public static Shop Create(LawFirmDatabase context, ShopBindingModel model) + { + return new Shop() + { + Id = model.Id, + Name = model.Name, + Adress = model.Adress, + OpeningDate = model.OpeningDate, + Documents = model.ShopDocuments.Select(x => new ShopDocument + { + Document = context.Documents.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList(), + MaxCountDocuments = model.MaxCountDocuments + }; + } + + public void Update(ShopBindingModel model) + { + Name = model.Name; + Adress = model.Adress; + OpeningDate = model.OpeningDate; + MaxCountDocuments = model.MaxCountDocuments; + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Adress = Adress, + OpeningDate = OpeningDate, + ShopDocuments = ShopDocuments, + MaxCountDocuments = MaxCountDocuments + }; + + 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(); + ShopDocuments = context.ShopDocuments.Where(rec => rec.ShopId == model.Id).ToList(); + // обновили количество у существующих записей + foreach (var updateDoc in ShopDocuments) + { + updateDoc.Count = model.ShopDocuments[updateDoc.DocumentId].Item2; + model.ShopDocuments.Remove(updateDoc.DocumentId); + } + context.SaveChanges(); + } + var shop = context.Shops.First(x => x.Id == Id); + foreach (var elem in model.ShopDocuments) + { + context.ShopDocuments.Add(new ShopDocument + { + Shop = shop, + Document = context.Documents.First(x => x.Id == elem.Key), + Count = elem.Value.Item2 + }); + context.SaveChanges(); + } + _shopDocuments = null; + } + } +} diff --git a/LawFirm/LawFirmDatabaseImplement/Models/ShopDocument.cs b/LawFirm/LawFirmDatabaseImplement/Models/ShopDocument.cs new file mode 100644 index 0000000..c853f60 --- /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 DocumentId { get; set; } + + [Required] + public int ShopId { get; set; } + + [Required] + public int Count { get; set; } + + public virtual Shop Shop { get; set; } = new(); + + public virtual Document Document { get; set; } = new(); + } +} -- 2.25.1 From 0fb113ffbda7539a60fdc3c4c63cfd55bbeb84f6 Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Sun, 12 Mar 2023 19:26:43 +0400 Subject: [PATCH 10/14] =?UTF-8?q?=D0=92=D1=80=D0=BE=D0=B4=D0=B5=20=D0=B3?= =?UTF-8?q?=D0=BE=D1=82=D0=BE=D0=B2=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/ShopStorage.cs | 56 +++- .../20230312152057_ShopsAdded.Designer.cs | 248 ++++++++++++++++++ .../Migrations/20230312152057_ShopsAdded.cs | 78 ++++++ .../LawFirmDatabaseModelSnapshot.cs | 77 ++++++ 4 files changed, 448 insertions(+), 11 deletions(-) create mode 100644 LawFirm/LawFirmDatabaseImplement/Migrations/20230312152057_ShopsAdded.Designer.cs create mode 100644 LawFirm/LawFirmDatabaseImplement/Migrations/20230312152057_ShopsAdded.cs diff --git a/LawFirm/LawFirmDatabaseImplement/Implements/ShopStorage.cs b/LawFirm/LawFirmDatabaseImplement/Implements/ShopStorage.cs index adbb71c..7d1c1f5 100644 --- a/LawFirm/LawFirmDatabaseImplement/Implements/ShopStorage.cs +++ b/LawFirm/LawFirmDatabaseImplement/Implements/ShopStorage.cs @@ -46,20 +46,31 @@ namespace LawFirmDatabaseImplement.Implements public ShopViewModel? Insert(ShopBindingModel model) { using var context = new LawFirmDatabase(); - var newShop = Shop.Create(context, model); - if (newShop == null) + using var transaction = context.Database.BeginTransaction(); + try { - return null; - } + var newShop = Shop.Create(context, model); + if (newShop == null) + { + return null; + } - if (context.Shops.Any(x => x.Name == newShop.Name)) + if (context.Shops.Any(x => x.Name == newShop.Name)) + { + throw new Exception("Название магазина уже занято"); + } + + context.Shops.Add(newShop); + context.SaveChanges(); + transaction.Commit(); + return newShop.GetViewModel; + } + catch { - throw new Exception("Название магазина уже занято"); + transaction.Rollback(); + throw; } - - context.Shops.Add(newShop); - context.SaveChanges(); - return newShop.GetViewModel; + } public ShopViewModel? Update(ShopBindingModel model) @@ -102,7 +113,30 @@ namespace LawFirmDatabaseImplement.Implements { using var context = new LawFirmDatabase(); using var transaction = context.Database.BeginTransaction(); - + foreach (var shopDocs in context.ShopDocuments.Where(x => x.DocumentId == model.Id)) + { + var min = Math.Min(count, shopDocs.Count); + shopDocs.Count -= min; + count -= min; + if (count <= 0) + { + break; + } + } + if (count == 0) + { + context.SaveChanges(); + transaction.Commit(); + } + else + { + transaction.Rollback(); + } + if (count > 0) + { + return false; + } + return true; } } diff --git a/LawFirm/LawFirmDatabaseImplement/Migrations/20230312152057_ShopsAdded.Designer.cs b/LawFirm/LawFirmDatabaseImplement/Migrations/20230312152057_ShopsAdded.Designer.cs new file mode 100644 index 0000000..522e89a --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Migrations/20230312152057_ShopsAdded.Designer.cs @@ -0,0 +1,248 @@ +// +using System; +using LawFirmDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace LawFirmDatabaseImplement.Migrations +{ + [DbContext(typeof(LawFirmDatabase))] + [Migration("20230312152057_ShopsAdded")] + partial class ShopsAdded + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Blank", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("BlankName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Blanks"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Document", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("DocumentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Documents"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.DocumentBlank", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("BlankId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DocumentId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("BlankId"); + + b.HasIndex("DocumentId"); + + b.ToTable("DocumentBlanks"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("DocumentId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("DocumentId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Adress") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("MaxCountDocuments") + .HasColumnType("int"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("OpeningDate") + .HasColumnType("datetime2"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.ShopDocument", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DocumentId") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("DocumentId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopDocuments"); + }); + + modelBuilder.Entity("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/20230312152057_ShopsAdded.cs b/LawFirm/LawFirmDatabaseImplement/Migrations/20230312152057_ShopsAdded.cs new file mode 100644 index 0000000..39e1de1 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Migrations/20230312152057_ShopsAdded.cs @@ -0,0 +1,78 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace LawFirmDatabaseImplement.Migrations +{ + /// + public partial class ShopsAdded : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Shops", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Name = table.Column(type: "nvarchar(max)", nullable: false), + Adress = table.Column(type: "nvarchar(max)", nullable: false), + OpeningDate = table.Column(type: "datetime2", nullable: false), + MaxCountDocuments = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Shops", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "ShopDocuments", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + DocumentId = table.Column(type: "int", nullable: false), + ShopId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_ShopDocuments", x => x.Id); + table.ForeignKey( + name: "FK_ShopDocuments_Documents_DocumentId", + column: x => x.DocumentId, + principalTable: "Documents", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_ShopDocuments_Shops_ShopId", + column: x => x.ShopId, + principalTable: "Shops", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_ShopDocuments_DocumentId", + table: "ShopDocuments", + column: "DocumentId"); + + migrationBuilder.CreateIndex( + name: "IX_ShopDocuments_ShopId", + table: "ShopDocuments", + column: "ShopId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "ShopDocuments"); + + migrationBuilder.DropTable( + name: "Shops"); + } + } +} diff --git a/LawFirm/LawFirmDatabaseImplement/Migrations/LawFirmDatabaseModelSnapshot.cs b/LawFirm/LawFirmDatabaseImplement/Migrations/LawFirmDatabaseModelSnapshot.cs index a9faa90..5a9de02 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("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Adress") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("MaxCountDocuments") + .HasColumnType("int"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("OpeningDate") + .HasColumnType("datetime2"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.ShopDocument", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DocumentId") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("DocumentId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopDocuments"); + }); + modelBuilder.Entity("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 } } -- 2.25.1 From 6583464c2890e340f8fd403d577d2acc7c11f27e Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Fri, 7 Apr 2023 21:45:37 +0400 Subject: [PATCH 11/14] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LawFirm/LawFirmDatabaseImplement/Implements/DocumentStorage.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/LawFirm/LawFirmDatabaseImplement/Implements/DocumentStorage.cs b/LawFirm/LawFirmDatabaseImplement/Implements/DocumentStorage.cs index 2fc7f8d..bcd3432 100644 --- a/LawFirm/LawFirmDatabaseImplement/Implements/DocumentStorage.cs +++ b/LawFirm/LawFirmDatabaseImplement/Implements/DocumentStorage.cs @@ -100,6 +100,7 @@ namespace LawFirmDatabaseImplement.Implements } document.Update(model); + document.UpdateComponents(context, model); context.SaveChanges(); context.Database.CommitTransaction(); -- 2.25.1 From 97736d8dde1c7402714184c66b36b5a2961069f1 Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Sun, 9 Apr 2023 16:13:54 +0400 Subject: [PATCH 12/14] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=20=D0=B0=D0=BF=D0=B4=D0=B5=D0=B9=D1=82=20?= =?UTF-8?q?=D0=BC=D0=B0=D0=B3=D0=B0=D0=B7=D0=B8=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LawFirm/LawFirmDatabaseImplement/Implements/ShopStorage.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/LawFirm/LawFirmDatabaseImplement/Implements/ShopStorage.cs b/LawFirm/LawFirmDatabaseImplement/Implements/ShopStorage.cs index 7d1c1f5..ef78595 100644 --- a/LawFirm/LawFirmDatabaseImplement/Implements/ShopStorage.cs +++ b/LawFirm/LawFirmDatabaseImplement/Implements/ShopStorage.cs @@ -79,14 +79,17 @@ namespace LawFirmDatabaseImplement.Implements using var transaction = context.Database.BeginTransaction(); try { - var shop = context.Shops.FirstOrDefault(x => x.Id == model.Id); + var shop = context.Shops.Include(x => x.Documents).FirstOrDefault(x => x.Id == model.Id); if (shop == null) { return null; } shop.Update(model); context.SaveChanges(); - shop.UpdateDocuments(context, model); + if (model.ShopDocuments.Count > 0) + { + shop.UpdateDocuments(context, model); + } transaction.Commit(); return shop.GetViewModel; } -- 2.25.1 From da91f0641cf83bf0af8bdfd8b393c64daa6a1ba9 Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Mon, 24 Apr 2023 10:23:02 +0400 Subject: [PATCH 13/14] fix --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index ca1c7a3..9485fc0 100644 --- a/.gitignore +++ b/.gitignore @@ -398,3 +398,4 @@ FodyWeavers.xsd # JetBrains Rider *.sln.iml +/LawFirm/ImplementationExtensions -- 2.25.1 From 7e88c9f5bd8a5ef8267458864891bb6d8113d76d Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Mon, 24 Apr 2023 10:23:16 +0400 Subject: [PATCH 14/14] fix --- ...0424062106_ShopDocumentBindFix.Designer.cs | 250 ++++++++++++++++++ .../20230424062106_ShopDocumentBindFix.cs | 22 ++ .../LawFirmDatabaseModelSnapshot.cs | 4 +- .../Models/Document.cs | 2 + 4 files changed, 277 insertions(+), 1 deletion(-) create mode 100644 LawFirm/LawFirmDatabaseImplement/Migrations/20230424062106_ShopDocumentBindFix.Designer.cs create mode 100644 LawFirm/LawFirmDatabaseImplement/Migrations/20230424062106_ShopDocumentBindFix.cs diff --git a/LawFirm/LawFirmDatabaseImplement/Migrations/20230424062106_ShopDocumentBindFix.Designer.cs b/LawFirm/LawFirmDatabaseImplement/Migrations/20230424062106_ShopDocumentBindFix.Designer.cs new file mode 100644 index 0000000..c2f175f --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Migrations/20230424062106_ShopDocumentBindFix.Designer.cs @@ -0,0 +1,250 @@ +// +using System; +using LawFirmDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace LawFirmDatabaseImplement.Migrations +{ + [DbContext(typeof(LawFirmDatabase))] + [Migration("20230424062106_ShopDocumentBindFix")] + partial class ShopDocumentBindFix + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Blank", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("BlankName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Blanks"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Document", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("DocumentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Documents"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.DocumentBlank", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("BlankId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DocumentId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("BlankId"); + + b.HasIndex("DocumentId"); + + b.ToTable("DocumentBlanks"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("DocumentId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("DocumentId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Adress") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("MaxCountDocuments") + .HasColumnType("int"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("OpeningDate") + .HasColumnType("datetime2"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.ShopDocument", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DocumentId") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("DocumentId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopDocuments"); + }); + + modelBuilder.Entity("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("Shops") + .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"); + + b.Navigation("Shops"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Shop", b => + { + b.Navigation("Documents"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/LawFirm/LawFirmDatabaseImplement/Migrations/20230424062106_ShopDocumentBindFix.cs b/LawFirm/LawFirmDatabaseImplement/Migrations/20230424062106_ShopDocumentBindFix.cs new file mode 100644 index 0000000..8c6e611 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Migrations/20230424062106_ShopDocumentBindFix.cs @@ -0,0 +1,22 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace LawFirmDatabaseImplement.Migrations +{ + /// + public partial class ShopDocumentBindFix : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + + } + } +} diff --git a/LawFirm/LawFirmDatabaseImplement/Migrations/LawFirmDatabaseModelSnapshot.cs b/LawFirm/LawFirmDatabaseImplement/Migrations/LawFirmDatabaseModelSnapshot.cs index 5a9de02..02fb9a4 100644 --- a/LawFirm/LawFirmDatabaseImplement/Migrations/LawFirmDatabaseModelSnapshot.cs +++ b/LawFirm/LawFirmDatabaseImplement/Migrations/LawFirmDatabaseModelSnapshot.cs @@ -207,7 +207,7 @@ namespace LawFirmDatabaseImplement.Migrations modelBuilder.Entity("LawFirmDatabaseImplement.Models.ShopDocument", b => { b.HasOne("LawFirmDatabaseImplement.Models.Document", "Document") - .WithMany() + .WithMany("Shops") .HasForeignKey("DocumentId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); @@ -233,6 +233,8 @@ namespace LawFirmDatabaseImplement.Migrations b.Navigation("Blanks"); b.Navigation("Orders"); + + b.Navigation("Shops"); }); modelBuilder.Entity("LawFirmDatabaseImplement.Models.Shop", b => diff --git a/LawFirm/LawFirmDatabaseImplement/Models/Document.cs b/LawFirm/LawFirmDatabaseImplement/Models/Document.cs index 1046285..685b88d 100644 --- a/LawFirm/LawFirmDatabaseImplement/Models/Document.cs +++ b/LawFirm/LawFirmDatabaseImplement/Models/Document.cs @@ -38,6 +38,8 @@ namespace LawFirmDatabaseImplement.Models public virtual List Blanks { get; set; } = new(); [ForeignKey("DocumentId")] public virtual List Orders { get; set; } = new(); + [ForeignKey("DocumentId")] + public virtual List Shops { get; set; } = new(); public static Document? Create(LawFirmDatabase context, DocumentBindingModel model) -- 2.25.1