diff --git a/LawFirm/LawFirm.sln b/LawFirm/LawFirm.sln index c2716e6..b860d97 100644 --- a/LawFirm/LawFirm.sln +++ b/LawFirm/LawFirm.sln @@ -19,6 +19,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LawFirmFileImplement", "Law {6F47A7CF-F09A-4B49-8563-9A04CD34A12C} = {6F47A7CF-F09A-4B49-8563-9A04CD34A12C} EndProjectSection EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LawFirmDatabaseImplement", "LawFirmDatabaseImplement\LawFirmDatabaseImplement.csproj", "{9DCD1DFD-676E-469A-8919-A8E66CDA34B4}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -49,6 +51,10 @@ Global {2A2A140A-BD69-428C-955B-A062C67B7473}.Debug|Any CPU.Build.0 = Debug|Any CPU {2A2A140A-BD69-428C-955B-A062C67B7473}.Release|Any CPU.ActiveCfg = Release|Any CPU {2A2A140A-BD69-428C-955B-A062C67B7473}.Release|Any CPU.Build.0 = Release|Any CPU + {9DCD1DFD-676E-469A-8919-A8E66CDA34B4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9DCD1DFD-676E-469A-8919-A8E66CDA34B4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9DCD1DFD-676E-469A-8919-A8E66CDA34B4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9DCD1DFD-676E-469A-8919-A8E66CDA34B4}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/LawFirm/LawFirmBusinessLogic/BusinessLogics/OrderLogic.cs b/LawFirm/LawFirmBusinessLogic/BusinessLogics/OrderLogic.cs index cb2e310..c6db378 100644 --- a/LawFirm/LawFirmBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/LawFirm/LawFirmBusinessLogic/BusinessLogics/OrderLogic.cs @@ -57,7 +57,7 @@ namespace LawFirmBusinessLogic.BusinessLogics if (model.Status == OrderStatus.Готов) { - model.DateImplement = DateTime.Now; + model.DateImplement = DateTime.SpecifyKind(DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc), DateTimeKind.Utc); var document = _documentStorage.GetElement(new() { Id = viewModel.DocumentId }); if (document == null) { diff --git a/LawFirm/LawFirmContracts/BindingModels/OrderBindingModel.cs b/LawFirm/LawFirmContracts/BindingModels/OrderBindingModel.cs index 4b582fe..73392ad 100644 --- a/LawFirm/LawFirmContracts/BindingModels/OrderBindingModel.cs +++ b/LawFirm/LawFirmContracts/BindingModels/OrderBindingModel.cs @@ -10,7 +10,7 @@ namespace LawFirmContracts.BindingModels public int Count { get; set; } public double Sum { get; set; } public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; - public DateTime DateCreate { get; set; } = DateTime.Now; + public DateTime DateCreate { get; set; } = DateTime.SpecifyKind(DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc), DateTimeKind.Utc); public DateTime? DateImplement { get; set; } } } diff --git a/LawFirm/LawFirmContracts/BindingModels/ShopBindingModel.cs b/LawFirm/LawFirmContracts/BindingModels/ShopBindingModel.cs index 6cc0b4e..c8bb4cd 100644 --- a/LawFirm/LawFirmContracts/BindingModels/ShopBindingModel.cs +++ b/LawFirm/LawFirmContracts/BindingModels/ShopBindingModel.cs @@ -11,7 +11,7 @@ namespace LawFirmContracts.BindingModels { public string ShopName { get; set; } = string.Empty; public string Address { get; set; } = string.Empty; - public DateTime DateOpen { get; set; } = DateTime.Now; + public DateTime DateOpen { get; set; } = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc); public int Capacity { get; set; } public Dictionary ShopDocuments { get; set; } = new(); public int Id { get; set; } diff --git a/LawFirm/LawFirmContracts/ViewModels/OrderViewModel.cs b/LawFirm/LawFirmContracts/ViewModels/OrderViewModel.cs index 59f4972..06c7dd3 100644 --- a/LawFirm/LawFirmContracts/ViewModels/OrderViewModel.cs +++ b/LawFirm/LawFirmContracts/ViewModels/OrderViewModel.cs @@ -18,7 +18,7 @@ namespace LawFirmContracts.ViewModels [DisplayName("Статус")] public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; [DisplayName("Дата создания")] - public DateTime DateCreate { get; set; } = DateTime.Now; + public DateTime DateCreate { get; set; } = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc); [DisplayName("Дата выполнения")] public DateTime? DateImplement { get; set; } } diff --git a/LawFirm/LawFirmContracts/ViewModels/ShopViewModel.cs b/LawFirm/LawFirmContracts/ViewModels/ShopViewModel.cs index 2f021bd..cf48275 100644 --- a/LawFirm/LawFirmContracts/ViewModels/ShopViewModel.cs +++ b/LawFirm/LawFirmContracts/ViewModels/ShopViewModel.cs @@ -17,7 +17,7 @@ namespace LawFirmContracts.ViewModels public string Address { get; set; } = string.Empty; [DisplayName("Дата открытия")] - public DateTime DateOpen { get; set; } = DateTime.Now; + public DateTime DateOpen { get; set; } = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc); [DisplayName("Вместимость магазина")] public int Capacity { get; set; } public Dictionary ShopDocuments { get; set; } = new(); diff --git a/LawFirm/LawFirmDatabaseImplement/Implements/BlankStorage.cs b/LawFirm/LawFirmDatabaseImplement/Implements/BlankStorage.cs new file mode 100644 index 0000000..9ab873f --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Implements/BlankStorage.cs @@ -0,0 +1,84 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.SearchModels; +using LawFirmContracts.StoragesContracts; +using LawFirmContracts.ViewModels; +using LawFirmDatabaseImplement.Models; + +namespace LawFirmDatabaseImplement.Implements +{ + public class BlankStorage : IBlankStorage + { + public List GetFullList() + { + using var context = new LawFirmDatabase(); + return context.Blanks + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(BlankSearchModel + model) + { + if (string.IsNullOrEmpty(model.BlankName)) + { + return new(); + } + using var context = new LawFirmDatabase(); + return context.Blanks + .Where(x => x.BlankName.Contains(model.BlankName)) + .Select(x => x.GetViewModel) + .ToList(); + } + public BlankViewModel? GetElement(BlankSearchModel model) + { + if (string.IsNullOrEmpty(model.BlankName) && !model.Id.HasValue) + { + return null; + } + using var context = new LawFirmDatabase(); + return context.Blanks + .FirstOrDefault(x => + (!string.IsNullOrEmpty(model.BlankName) && x.BlankName == + model.BlankName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + public BlankViewModel? Insert(BlankBindingModel model) + { + var newBlank = Blank.Create(model); + if (newBlank == null) + { + return null; + } + using var context = new LawFirmDatabase(); + context.Blanks.Add(newBlank); + context.SaveChanges(); + return newBlank.GetViewModel; + } + public BlankViewModel? Update(BlankBindingModel model) + { + using var context = new LawFirmDatabase(); + var component = context.Blanks.FirstOrDefault(x => x.Id == + model.Id); + if (component == null) + { + return null; + } + component.Update(model); + context.SaveChanges(); + return component.GetViewModel; + } + public BlankViewModel? Delete(BlankBindingModel model) + { + using var context = new LawFirmDatabase(); + var element = context.Blanks.FirstOrDefault(rec => rec.Id == + model.Id); + if (element != null) + { + context.Blanks.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/LawFirm/LawFirmDatabaseImplement/Implements/DocumentStorage.cs b/LawFirm/LawFirmDatabaseImplement/Implements/DocumentStorage.cs new file mode 100644 index 0000000..9b1ad9a --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Implements/DocumentStorage.cs @@ -0,0 +1,107 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.SearchModels; +using LawFirmContracts.StoragesContracts; +using LawFirmContracts.ViewModels; +using LawFirmDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + + +namespace LawFirmDatabaseImplement.Implements +{ + public class DocumentStorage : IDocumentStorage + { + public List GetFullList() + { + using var context = new LawFirmDatabase(); + return context.Documents + .Include(x => x.Blanks) + .ThenInclude(x => x.Blank) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(DocumentSearchModel model) + { + if (string.IsNullOrEmpty(model.DocumentName)) + { + return new(); + } + using var context = new LawFirmDatabase(); + return context.Documents + .Include(x => x.Blanks) + .ThenInclude(x => x.Blank) + .Where(x => x.DocumentName.Contains(model.DocumentName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public DocumentViewModel? GetElement(DocumentSearchModel model) + { + if (string.IsNullOrEmpty(model.DocumentName) && + !model.Id.HasValue) + { + return null; + } + using var context = new LawFirmDatabase(); + return context.Documents + .Include(x => x.Blanks) + .ThenInclude(x => x.Blank) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.DocumentName) && + x.DocumentName == model.DocumentName) || + (model.Id.HasValue && x.Id == + model.Id)) + ?.GetViewModel; + } + public DocumentViewModel? Insert(DocumentBindingModel model) + { + using var context = new LawFirmDatabase(); + var newDocument = Document.Create(context, model); + if (newDocument == null) + { + return null; + } + context.Documents.Add(newDocument); + context.SaveChanges(); + return newDocument.GetViewModel; + } + public DocumentViewModel? Update(DocumentBindingModel model) + { + using var context = new LawFirmDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var product = context.Documents.FirstOrDefault(rec => + rec.Id == model.Id); + if (product == null) + { + return null; + } + product.Update(model); + context.SaveChanges(); + product.UpdateBlanks(context, model); + transaction.Commit(); + return product.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public DocumentViewModel? Delete(DocumentBindingModel model) + { + using var context = new LawFirmDatabase(); + var element = context.Documents + .Include(x => x.Blanks) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Documents.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } + +} diff --git a/LawFirm/LawFirmDatabaseImplement/Implements/OrderStorage.cs b/LawFirm/LawFirmDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..89e7f6d --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,116 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.SearchModels; +using LawFirmContracts.StoragesContracts; +using LawFirmContracts.ViewModels; +using LawFirmDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace LawFirmDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new LawFirmDatabase(); + + var element = context.Orders + .FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + + return GetViewModel(element); + } + + return null; + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + + using var context = new LawFirmDatabase(); + + return GetViewModel(context.Orders + .Include(x => x.Document) + .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))); + } + + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + + using var context = new LawFirmDatabase(); + + return context.Orders + .Include(x => x.Document) + .Where(x => x.Id == model.Id) + .Select(x => GetViewModel(x)) + .ToList(); + } + + public List GetFullList() + { + using var context = new LawFirmDatabase(); + + return context.Orders + .Include(x => x.Document) + .Select(x => GetViewModel(x)) + .ToList(); + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + + using var context = new LawFirmDatabase(); + + context.Orders.Add(newOrder); + context.SaveChanges(); + return context.Orders + .Include(x => x.Document) + .FirstOrDefault(x => x.Id == newOrder.Id) + ?.GetViewModel; + } + + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new LawFirmDatabase(); + + var order = context.Orders + .Include(x => x.Document) + .FirstOrDefault(x => x.Id == model.Id); + + if (order == null) + { + return null; + } + + order.Update(model); + context.SaveChanges(); + + return GetViewModel(order); + } + + private static OrderViewModel GetViewModel(Order order) + { + var viewModel = order.GetViewModel; + using var context = new LawFirmDatabase(); + var element = context.Documents.FirstOrDefault(x => x.Id == order.DocumentId); + viewModel.DocumentName = element.DocumentName; + return viewModel; + } + } +} diff --git a/LawFirm/LawFirmDatabaseImplement/Implements/ShopStorage.cs b/LawFirm/LawFirmDatabaseImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..f751325 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Implements/ShopStorage.cs @@ -0,0 +1,156 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.SearchModels; +using LawFirmContracts.StoragesContracts; +using LawFirmContracts.ViewModels; +using LawFirmDatabaseImplement.Models; +using LawFirmDataModels.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LawFirmDatabaseImplement.Implements +{ + public class ShopStorage : IShopStorage + { + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName) && + !model.Id.HasValue) + { + return null; + } + using var context = new LawFirmDatabase(); + return context.Shops + .Include(x => x.Documents) + .ThenInclude(x => x.Document) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ShopName) && + x.ShopName == model.ShopName) || + (model.Id.HasValue && x.Id == + model.Id)) + ?.GetViewModel; + } + + public List GetFilteredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName)) + { + return new(); + } + using var context = new LawFirmDatabase(); + return context.Shops + .Include(x => x.Documents) + .ThenInclude(x => x.Document) + .Where(x => x.ShopName.Contains(model.ShopName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new LawFirmDatabase(); + return context.Shops.Include(x => x.Documents) + .ThenInclude(x => x.Document) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public ShopViewModel? Insert(ShopBindingModel model) + { + using var context = new LawFirmDatabase(); + var newProduct = Shop.Create(context, model); + if (newProduct == null) + { + return null; + } + context.Shops.Add(newProduct); + context.SaveChanges(); + return newProduct.GetViewModel; + } + + public ShopViewModel? Update(ShopBindingModel model) + { + using var context = new LawFirmDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var shop = context.Shops.FirstOrDefault(rec => + rec.Id == model.Id); + if (shop == null) + { + return null; + } + shop.Update(model); + context.SaveChanges(); + shop.UpdateDocuments(context, model); + transaction.Commit(); + return shop.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public ShopViewModel? Delete(ShopBindingModel model) + { + using var context = new LawFirmDatabase(); + var element = context.Shops + .Include(x => x.Documents) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Shops.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + public bool SellDocuments(IDocumentModel model, int count) + { + using var context = new LawFirmDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + List shopsWithDocument = context.Shops.Include(x => x.Documents).ThenInclude(x => x.Document).Where(x => x.Documents.Any(x => x.DocumentId == model.Id)).ToList(); + foreach (var shop in shopsWithDocument) + { + int carInShopCount = shop.ShopDocuments[model.Id].Item2; + if (count - carInShopCount >= 0) + { + count -= carInShopCount; + context.ShopDocuments.Remove(shop.Documents.FirstOrDefault(x => x.DocumentId == model.Id)!); + shop.ShopDocuments.Remove(model.Id); + } + else + { + shop.ShopDocuments[model.Id] = (model, carInShopCount - count); + count = 0; + shop.UpdateDocuments(context, new() + { + Id = shop.Id, + ShopDocuments = shop.ShopDocuments + }); + } + if (count == 0) + { + context.SaveChanges(); + transaction.Commit(); + return true; + } + } + transaction.Rollback(); + return false; + } + catch + { + transaction.Rollback(); + throw; + } + } + } +} diff --git a/LawFirm/LawFirmDatabaseImplement/LawFirmDatabase.cs b/LawFirm/LawFirmDatabaseImplement/LawFirmDatabase.cs new file mode 100644 index 0000000..f8b9f7d --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/LawFirmDatabase.cs @@ -0,0 +1,28 @@ +using LawFirmDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace LawFirmDatabaseImplement +{ + public class LawFirmDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseNpgsql(@" + Host=localhost; + Port=5432; + Database=RPP3hard; + Username=postgres; + Password=123"); + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Blanks { set; get; } + public virtual DbSet Documents { set; get; } + public virtual DbSet DocumentBlanks { set; get; } + public virtual DbSet Orders { set; get; } + public virtual DbSet Shops { set; get; } + public virtual DbSet ShopDocuments { set; get; } + } +} diff --git a/LawFirm/LawFirmDatabaseImplement/LawFirmDatabaseImplement.csproj b/LawFirm/LawFirmDatabaseImplement/LawFirmDatabaseImplement.csproj new file mode 100644 index 0000000..a972da9 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/LawFirmDatabaseImplement.csproj @@ -0,0 +1,24 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + diff --git a/LawFirm/LawFirmDatabaseImplement/Migrations/20230313153615_InitialCreate.Designer.cs b/LawFirm/LawFirmDatabaseImplement/Migrations/20230313153615_InitialCreate.Designer.cs new file mode 100644 index 0000000..b1a5f47 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Migrations/20230313153615_InitialCreate.Designer.cs @@ -0,0 +1,171 @@ +// +using System; +using LawFirmDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace LawFirmDatabaseImplement.Migrations +{ + [DbContext(typeof(LawFirmDatabase))] + [Migration("20230313153615_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Blank", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("BlankName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Price") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Blanks"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Document", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("DocumentName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Price") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Documents"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.DocumentBlank", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("BlankId") + .HasColumnType("integer"); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("DocumentId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("BlankId"); + + b.HasIndex("DocumentId"); + + b.ToTable("DocumentBlanks"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("DateCreate") + .HasColumnType("timestamp with time zone"); + + b.Property("DateImplement") + .HasColumnType("timestamp with time zone"); + + b.Property("DocumentId") + .HasColumnType("integer"); + + b.Property("Status") + .HasColumnType("integer"); + + b.Property("Sum") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.HasIndex("DocumentId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.DocumentBlank", b => + { + b.HasOne("LawFirmDatabaseImplement.Models.Blank", "Blank") + .WithMany("DocumentBlanks") + .HasForeignKey("BlankId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("LawFirmDatabaseImplement.Models.Document", "Document") + .WithMany("Blanks") + .HasForeignKey("DocumentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Blank"); + + b.Navigation("Document"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Order", b => + { + b.HasOne("LawFirmDatabaseImplement.Models.Document", "Document") + .WithMany("Orders") + .HasForeignKey("DocumentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Document"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Blank", b => + { + b.Navigation("DocumentBlanks"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Document", b => + { + b.Navigation("Blanks"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/LawFirm/LawFirmDatabaseImplement/Migrations/20230313153615_InitialCreate.cs b/LawFirm/LawFirmDatabaseImplement/Migrations/20230313153615_InitialCreate.cs new file mode 100644 index 0000000..36edd73 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Migrations/20230313153615_InitialCreate.cs @@ -0,0 +1,126 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace LawFirmDatabaseImplement.Migrations +{ + /// + public partial class InitialCreate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Blanks", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + BlankName = table.Column(type: "text", nullable: false), + Price = table.Column(type: "double precision", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Blanks", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Documents", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + DocumentName = table.Column(type: "text", nullable: false), + Price = table.Column(type: "double precision", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Documents", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "DocumentBlanks", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + DocumentId = table.Column(type: "integer", nullable: false), + BlankId = table.Column(type: "integer", nullable: false), + Count = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_DocumentBlanks", x => x.Id); + table.ForeignKey( + name: "FK_DocumentBlanks_Blanks_BlankId", + column: x => x.BlankId, + principalTable: "Blanks", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_DocumentBlanks_Documents_DocumentId", + column: x => x.DocumentId, + principalTable: "Documents", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + DocumentId = table.Column(type: "integer", nullable: false), + Count = table.Column(type: "integer", nullable: false), + Sum = table.Column(type: "double precision", nullable: false), + Status = table.Column(type: "integer", nullable: false), + DateCreate = table.Column(type: "timestamp with time zone", nullable: false), + DateImplement = table.Column(type: "timestamp with time zone", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Orders", x => x.Id); + table.ForeignKey( + name: "FK_Orders_Documents_DocumentId", + column: x => x.DocumentId, + principalTable: "Documents", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_DocumentBlanks_BlankId", + table: "DocumentBlanks", + column: "BlankId"); + + migrationBuilder.CreateIndex( + name: "IX_DocumentBlanks_DocumentId", + table: "DocumentBlanks", + column: "DocumentId"); + + migrationBuilder.CreateIndex( + name: "IX_Orders_DocumentId", + table: "Orders", + column: "DocumentId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "DocumentBlanks"); + + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "Blanks"); + + migrationBuilder.DropTable( + name: "Documents"); + } + } +} diff --git a/LawFirm/LawFirmDatabaseImplement/Migrations/20230424233004_lab3hard.Designer.cs b/LawFirm/LawFirmDatabaseImplement/Migrations/20230424233004_lab3hard.Designer.cs new file mode 100644 index 0000000..53c3c1b --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Migrations/20230424233004_lab3hard.Designer.cs @@ -0,0 +1,248 @@ +// +using System; +using LawFirmDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace LawFirmDatabaseImplement.Migrations +{ + [DbContext(typeof(LawFirmDatabase))] + [Migration("20230424233004_lab3hard")] + partial class lab3hard + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Blank", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("BlankName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Price") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Blanks"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Document", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("DocumentName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Price") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Documents"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.DocumentBlank", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("BlankId") + .HasColumnType("integer"); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("DocumentId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("BlankId"); + + b.HasIndex("DocumentId"); + + b.ToTable("DocumentBlanks"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("DateCreate") + .HasColumnType("timestamp with time zone"); + + b.Property("DateImplement") + .HasColumnType("timestamp with time zone"); + + b.Property("DocumentId") + .HasColumnType("integer"); + + b.Property("Status") + .HasColumnType("integer"); + + b.Property("Sum") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.HasIndex("DocumentId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("text"); + + b.Property("Capacity") + .HasColumnType("integer"); + + b.Property("DateOpen") + .HasColumnType("timestamp with time zone"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.ShopDocument", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("DocumentId") + .HasColumnType("integer"); + + b.Property("ShopId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("DocumentId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopDocuments"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.DocumentBlank", b => + { + b.HasOne("LawFirmDatabaseImplement.Models.Blank", "Blank") + .WithMany("DocumentBlanks") + .HasForeignKey("BlankId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("LawFirmDatabaseImplement.Models.Document", "Document") + .WithMany("Blanks") + .HasForeignKey("DocumentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Blank"); + + b.Navigation("Document"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Order", b => + { + b.HasOne("LawFirmDatabaseImplement.Models.Document", "Document") + .WithMany("Orders") + .HasForeignKey("DocumentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Document"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.ShopDocument", b => + { + b.HasOne("LawFirmDatabaseImplement.Models.Document", "Document") + .WithMany() + .HasForeignKey("DocumentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("LawFirmDatabaseImplement.Models.Shop", "Shop") + .WithMany("Documents") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Document"); + + b.Navigation("Shop"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Blank", b => + { + b.Navigation("DocumentBlanks"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Document", b => + { + b.Navigation("Blanks"); + + b.Navigation("Orders"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Shop", b => + { + b.Navigation("Documents"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/LawFirm/LawFirmDatabaseImplement/Migrations/20230424233004_lab3hard.cs b/LawFirm/LawFirmDatabaseImplement/Migrations/20230424233004_lab3hard.cs new file mode 100644 index 0000000..10e8a3e --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Migrations/20230424233004_lab3hard.cs @@ -0,0 +1,79 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace LawFirmDatabaseImplement.Migrations +{ + /// + public partial class lab3hard : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Shops", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + ShopName = table.Column(type: "text", nullable: false), + Address = table.Column(type: "text", nullable: false), + DateOpen = table.Column(type: "timestamp with time zone", nullable: false), + Capacity = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Shops", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "ShopDocuments", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + ShopId = table.Column(type: "integer", nullable: false), + DocumentId = table.Column(type: "integer", nullable: false), + Count = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_ShopDocuments", x => x.Id); + table.ForeignKey( + name: "FK_ShopDocuments_Documents_DocumentId", + column: x => x.DocumentId, + principalTable: "Documents", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_ShopDocuments_Shops_ShopId", + column: x => x.ShopId, + principalTable: "Shops", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_ShopDocuments_DocumentId", + table: "ShopDocuments", + column: "DocumentId"); + + migrationBuilder.CreateIndex( + name: "IX_ShopDocuments_ShopId", + table: "ShopDocuments", + column: "ShopId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "ShopDocuments"); + + migrationBuilder.DropTable( + name: "Shops"); + } + } +} diff --git a/LawFirm/LawFirmDatabaseImplement/Migrations/LawFirmDatabaseModelSnapshot.cs b/LawFirm/LawFirmDatabaseImplement/Migrations/LawFirmDatabaseModelSnapshot.cs new file mode 100644 index 0000000..2dbad13 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Migrations/LawFirmDatabaseModelSnapshot.cs @@ -0,0 +1,245 @@ +// +using System; +using LawFirmDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace LawFirmDatabaseImplement.Migrations +{ + [DbContext(typeof(LawFirmDatabase))] + partial class LawFirmDatabaseModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Blank", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("BlankName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Price") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Blanks"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Document", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("DocumentName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Price") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Documents"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.DocumentBlank", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("BlankId") + .HasColumnType("integer"); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("DocumentId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("BlankId"); + + b.HasIndex("DocumentId"); + + b.ToTable("DocumentBlanks"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("DateCreate") + .HasColumnType("timestamp with time zone"); + + b.Property("DateImplement") + .HasColumnType("timestamp with time zone"); + + b.Property("DocumentId") + .HasColumnType("integer"); + + b.Property("Status") + .HasColumnType("integer"); + + b.Property("Sum") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.HasIndex("DocumentId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("text"); + + b.Property("Capacity") + .HasColumnType("integer"); + + b.Property("DateOpen") + .HasColumnType("timestamp with time zone"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.ShopDocument", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("DocumentId") + .HasColumnType("integer"); + + b.Property("ShopId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("DocumentId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopDocuments"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.DocumentBlank", b => + { + b.HasOne("LawFirmDatabaseImplement.Models.Blank", "Blank") + .WithMany("DocumentBlanks") + .HasForeignKey("BlankId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("LawFirmDatabaseImplement.Models.Document", "Document") + .WithMany("Blanks") + .HasForeignKey("DocumentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Blank"); + + b.Navigation("Document"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Order", b => + { + b.HasOne("LawFirmDatabaseImplement.Models.Document", "Document") + .WithMany("Orders") + .HasForeignKey("DocumentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Document"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.ShopDocument", b => + { + b.HasOne("LawFirmDatabaseImplement.Models.Document", "Document") + .WithMany() + .HasForeignKey("DocumentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("LawFirmDatabaseImplement.Models.Shop", "Shop") + .WithMany("Documents") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Document"); + + b.Navigation("Shop"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Blank", b => + { + b.Navigation("DocumentBlanks"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Document", b => + { + b.Navigation("Blanks"); + + b.Navigation("Orders"); + }); + + modelBuilder.Entity("LawFirmDatabaseImplement.Models.Shop", b => + { + b.Navigation("Documents"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/LawFirm/LawFirmDatabaseImplement/Models/Blank.cs b/LawFirm/LawFirmDatabaseImplement/Models/Blank.cs new file mode 100644 index 0000000..69c6f5b --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Models/Blank.cs @@ -0,0 +1,56 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.ViewModels; +using LawFirmDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace LawFirmDatabaseImplement.Models +{ + public class Blank : IBlankModel + { + public int Id { get; private set; } + [Required] + public string BlankName { get; private set; } = string.Empty; + [Required] + public double Price { get; set; } + [ForeignKey("BlankId")] + public virtual List DocumentBlanks { get; set; } = new(); + public static Blank? Create(BlankBindingModel model) + { + if (model == null) + { + return null; + } + return new Blank() + { + Id = model.Id, + BlankName = model.BlankName, + Price = model.Price + }; + } + public static Blank Create(BlankViewModel model) + { + return new Blank + { + Id = model.Id, + BlankName = model.BlankName, + Price = model.Price + }; + } + public void Update(BlankBindingModel model) + { + if (model == null) + { + return; + } + BlankName = model.BlankName; + Price = model.Price; + } + public BlankViewModel GetViewModel => new() + { + Id = Id, + BlankName = BlankName, + Price = Price + }; + } +} diff --git a/LawFirm/LawFirmDatabaseImplement/Models/Document.cs b/LawFirm/LawFirmDatabaseImplement/Models/Document.cs new file mode 100644 index 0000000..166110d --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Models/Document.cs @@ -0,0 +1,95 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.ViewModels; +using LawFirmDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace LawFirmDatabaseImplement.Models +{ + public class Document : IDocumentModel + { + public int Id { get; set; } + [Required] + public string DocumentName { get; set; } = string.Empty; + [Required] + public double Price { get; set; } + private Dictionary? _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 UpdateBlanks(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..ec1a6a9 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Models/DocumentBlank.cs @@ -0,0 +1,18 @@ +using System.ComponentModel; +using System.ComponentModel.DataAnnotations; + +namespace LawFirmDatabaseImplement.Models +{ + public class DocumentBlank + { + public int Id { get; set; } + [Required] + public int DocumentId { get; set; } + [Required] + public int BlankId { get; set; } + [Required] + public int Count { get; set; } + public virtual Blank Blank { get; set; } = new(); + public virtual Document Document { get; set; } = new(); + } +} diff --git a/LawFirm/LawFirmDatabaseImplement/Models/Order.cs b/LawFirm/LawFirmDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..1f72f68 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Models/Order.cs @@ -0,0 +1,67 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.ViewModels; +using LawFirmDataModels.Enums; +using LawFirmDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace LawFirmDatabaseImplement.Models +{ + public class Order : IOrderModel + { + [Required] + public int DocumentId { get; set; } + [Required] + public int Count { get; set; } + [Required] + public double Sum { get; set; } + [Required] + public OrderStatus Status { get; set; } + [Required] + public DateTime DateCreate { get; set; } + + public DateTime? DateImplement { get; set; } + + public int Id { get; set; } + public virtual Document Document { get; set; } + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + DocumentId = model.DocumentId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement + }; + } + + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + Status = model.Status; + DateImplement = model.DateImplement; + } + + public OrderViewModel GetViewModel => new() + { + Id = Id, + DocumentId = DocumentId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + DocumentName = Document.DocumentName + }; + } +} diff --git a/LawFirm/LawFirmDatabaseImplement/Models/Shop.cs b/LawFirm/LawFirmDatabaseImplement/Models/Shop.cs new file mode 100644 index 0000000..9ffc0cc --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Models/Shop.cs @@ -0,0 +1,108 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.ViewModels; +using LawFirmDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LawFirmDatabaseImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; set; } + [Required] + public string ShopName { get; set; } = string.Empty; + [Required] + public string Address { get; set; } = string.Empty; + [Required] + public DateTime DateOpen { get; set; } + [Required] + public int Capacity { get; set; } + + private Dictionary? _shopDocuments = null; + + [NotMapped] + public Dictionary ShopDocuments + { + get + { + if (_shopDocuments == null) + { + _shopDocuments = Documents + .ToDictionary(rec => rec.DocumentId, + rec => (rec.Document as IDocumentModel, + rec.Count)); + } + return _shopDocuments; + } + } + [ForeignKey("ShopId")] + public virtual List Documents { get; set; } = new(); + public static Shop Create(LawFirmDatabase context, ShopBindingModel model) + { + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + Address = model.Address, + DateOpen = model.DateOpen, + Capacity = model.Capacity, + Documents = model.ShopDocuments.Select(x => new ShopDocument + { + Document = context.Documents.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + + public void Update(ShopBindingModel model) + { + ShopName = model.ShopName; + Address = model.Address; + DateOpen = model.DateOpen; + Capacity = model.Capacity; + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Address = Address, + DateOpen = DateOpen, + Capacity = Capacity, + ShopDocuments = ShopDocuments + }; + + public void UpdateDocuments(LawFirmDatabase context, ShopBindingModel model) + { + var shopDocuments = context.ShopDocuments.Where(rec => rec.ShopId == model.Id).ToList(); + if (shopDocuments != null && shopDocuments.Count > 0) + { + context.ShopDocuments.RemoveRange(shopDocuments.Where(rec => !model.ShopDocuments.ContainsKey(rec.DocumentId))); + context.SaveChanges(); + foreach (var updateDocument in shopDocuments) + { + updateDocument.Count = model.ShopDocuments[updateDocument.DocumentId].Item2; + model.ShopDocuments.Remove(updateDocument.DocumentId); + } + context.SaveChanges(); + } + var shop = context.Shops.First(x => x.Id == Id); + foreach (var id in model.ShopDocuments) + { + context.ShopDocuments.Add(new ShopDocument + { + Shop = shop, + Document = context.Documents.First(x => x.Id == id.Key), + Count = id.Value.Item2 + }); + context.SaveChanges(); + } + _shopDocuments = null; + } + } +} diff --git a/LawFirm/LawFirmDatabaseImplement/Models/ShopDocument.cs b/LawFirm/LawFirmDatabaseImplement/Models/ShopDocument.cs new file mode 100644 index 0000000..e4527f4 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Models/ShopDocument.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LawFirmDatabaseImplement.Models +{ + public class ShopDocument + { + public int Id { get; set; } + + [Required] + public int ShopId { get; set; } + + [Required] + public int DocumentId { get; set; } + + [Required] + public int Count { get; set; } + + public virtual Document Document { get; set; } = new(); + + public virtual Shop Shop { get; set; } = new(); + } +} diff --git a/LawFirm/LawFirmFileImplement/Models/Document.cs b/LawFirm/LawFirmFileImplement/Models/Document.cs index 9c2eca2..117f779 100644 --- a/LawFirm/LawFirmFileImplement/Models/Document.cs +++ b/LawFirm/LawFirmFileImplement/Models/Document.cs @@ -11,20 +11,19 @@ namespace LawFirmFileImplement.Models public string DocumentName { get; private set; } = string.Empty; public double Price { get; private set; } public Dictionary Blanks { get; private set; } = new(); - private Dictionary? _productBlanks = - null; + private Dictionary? _documentBlanks = null; public Dictionary DocumentBlanks { get { - if (_productBlanks == null) + if (_documentBlanks == null) { var source = DataFileSingleton.GetInstance(); - _productBlanks = Blanks.ToDictionary(x => x.Key, y => + _documentBlanks = Blanks.ToDictionary(x => x.Key, y => ((source.Blanks.FirstOrDefault(z => z.Id == y.Key) as IBlankModel)!, y.Value)); } - return _productBlanks; + return _documentBlanks; } } public static Document? Create(DocumentBindingModel model) @@ -70,7 +69,7 @@ namespace LawFirmFileImplement.Models Price = model.Price; Blanks = model.DocumentBlanks.ToDictionary(x => x.Key, x => x.Value.Item2); - _productBlanks = null; + _documentBlanks = null; } public DocumentViewModel GetViewModel => new() { diff --git a/LawFirm/LawFirmFileImplement/Models/Order.cs b/LawFirm/LawFirmFileImplement/Models/Order.cs index e9ce910..7c776da 100644 --- a/LawFirm/LawFirmFileImplement/Models/Order.cs +++ b/LawFirm/LawFirmFileImplement/Models/Order.cs @@ -13,7 +13,7 @@ namespace LawFirmFileImplement.Models public int Count { get; private set; } public double Sum { get; private set; } public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; - public DateTime DateCreate { get; private set; } = DateTime.Now; + public DateTime DateCreate { get; private set; } = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc); public DateTime? DateImplement { get; private set; } public static Order? Create(OrderBindingModel? model) { diff --git a/LawFirm/LawFirmListImplement/Models/Order.cs b/LawFirm/LawFirmListImplement/Models/Order.cs index 6140c15..3a5ee58 100644 --- a/LawFirm/LawFirmListImplement/Models/Order.cs +++ b/LawFirm/LawFirmListImplement/Models/Order.cs @@ -12,7 +12,7 @@ namespace LawFirmListImplement.Models public int Count { get; private set; } public double Sum { get; private set; } public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; - public DateTime DateCreate { get; private set; } = DateTime.Now; + public DateTime DateCreate { get; private set; } = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc); public DateTime? DateImplement { get; private set; } public static Order? Create(OrderBindingModel? model) { diff --git a/LawFirm/LawFirmView/FormMain.cs b/LawFirm/LawFirmView/FormMain.cs index 2ea2821..4b05d03 100644 --- a/LawFirm/LawFirmView/FormMain.cs +++ b/LawFirm/LawFirmView/FormMain.cs @@ -58,11 +58,6 @@ namespace LawFirmView { var operationResult = _orderLogic.TakeOrderInWork(new OrderBindingModel{ Id = id, - DocumentId = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["DocumentId"].Value), - Status = Enum.Parse(dataGridView.SelectedRows[0].Cells["Status"].Value.ToString()), - Count = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Count"].Value), - Sum = double.Parse(dataGridView.SelectedRows[0].Cells["Sum"].Value.ToString()), - DateCreate = DateTime.Parse(dataGridView.SelectedRows[0].Cells["DateCreate"].Value.ToString()) }); if (!operationResult) { diff --git a/LawFirm/LawFirmView/FormShop.cs b/LawFirm/LawFirmView/FormShop.cs index 5243b19..1414e03 100644 --- a/LawFirm/LawFirmView/FormShop.cs +++ b/LawFirm/LawFirmView/FormShop.cs @@ -95,7 +95,7 @@ namespace LawFirmView Id = _id ?? 0, ShopName = textBoxName.Text, Address = textBoxAddress.Text, - DateOpen = DateTime.Parse(dateTimePickerDateOpen.Text), + DateOpen = DateTime.SpecifyKind(DateTime.Parse(dateTimePickerDateOpen.Text), DateTimeKind.Utc), Capacity = (int)numericUpDownCapacity.Value, ShopDocuments = _shopDocuments }; diff --git a/LawFirm/LawFirmView/LawFirmView.csproj b/LawFirm/LawFirmView/LawFirmView.csproj index 5a20cce..ae9cbfe 100644 --- a/LawFirm/LawFirmView/LawFirmView.csproj +++ b/LawFirm/LawFirmView/LawFirmView.csproj @@ -9,6 +9,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -16,6 +20,7 @@ + diff --git a/LawFirm/LawFirmView/Program.cs b/LawFirm/LawFirmView/Program.cs index 9fb2eea..5a24bd9 100644 --- a/LawFirm/LawFirmView/Program.cs +++ b/LawFirm/LawFirmView/Program.cs @@ -1,7 +1,7 @@ using LawFirmBusinessLogic.BusinessLogics; using LawFirmContracts.BusinessLogicsContracts; using LawFirmContracts.StoragesContracts; -using LawFirmFileImplement.Implements; +using LawFirmDatabaseImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging;