From 51d1b13efe8f54a9727f208cf0a14752676103db Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Fri, 24 Feb 2023 17:55:19 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=BE=D0=B2=20=D1=80?= =?UTF-8?q?=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D0=B8=20=D1=85?= =?UTF-8?q?=D1=80=D0=B0=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B2=20=D0=B1?= =?UTF-8?q?=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, + }; + } +}