From 326ea686fa85ca3f284c261bd60d2838913b8d60 Mon Sep 17 00:00:00 2001 From: devil_1nc Date: Sun, 9 Apr 2023 14:42:51 +0400 Subject: [PATCH] lab3 needs patching --- .../BindingModels/PackageBindingModel.cs | 2 +- .../AbstractSoftwareInstallationDatabase.cs | 28 +++ ...ftwareInstallationDatabaseImplement.csproj | 24 +++ .../Implements/OrderStorage.cs | 85 +++++++++ .../Implements/PackageStorage.cs | 129 +++++++++++++ .../Implements/SoftwareStorage.cs | 91 +++++++++ .../20230409100744_Initial.Designer.cs | 172 ++++++++++++++++++ .../Migrations/20230409100744_Initial.cs | 126 +++++++++++++ ...ftwareInstallationDatabaseModelSnapshot.cs | 169 +++++++++++++++++ .../Models/Order.cs | 72 ++++++++ .../Models/Package.cs | 104 +++++++++++ .../Models/PackageSoftware.cs | 22 +++ .../Models/Software.cs | 63 +++++++ SoftwareInstallation/SoftwareInstallation.sln | 8 +- .../SoftwareInstallation/Program.cs | 2 +- .../SoftwareInstallationView.csproj | 6 +- 16 files changed, 1099 insertions(+), 4 deletions(-) create mode 100644 SoftwareInstallation/AbstractSoftwareInstallationDatabaseImplement/AbstractSoftwareInstallationDatabase.cs create mode 100644 SoftwareInstallation/AbstractSoftwareInstallationDatabaseImplement/AbstractSoftwareInstallationDatabaseImplement.csproj create mode 100644 SoftwareInstallation/AbstractSoftwareInstallationDatabaseImplement/Implements/OrderStorage.cs create mode 100644 SoftwareInstallation/AbstractSoftwareInstallationDatabaseImplement/Implements/PackageStorage.cs create mode 100644 SoftwareInstallation/AbstractSoftwareInstallationDatabaseImplement/Implements/SoftwareStorage.cs create mode 100644 SoftwareInstallation/AbstractSoftwareInstallationDatabaseImplement/Migrations/20230409100744_Initial.Designer.cs create mode 100644 SoftwareInstallation/AbstractSoftwareInstallationDatabaseImplement/Migrations/20230409100744_Initial.cs create mode 100644 SoftwareInstallation/AbstractSoftwareInstallationDatabaseImplement/Migrations/AbstractSoftwareInstallationDatabaseModelSnapshot.cs create mode 100644 SoftwareInstallation/AbstractSoftwareInstallationDatabaseImplement/Models/Order.cs create mode 100644 SoftwareInstallation/AbstractSoftwareInstallationDatabaseImplement/Models/Package.cs create mode 100644 SoftwareInstallation/AbstractSoftwareInstallationDatabaseImplement/Models/PackageSoftware.cs create mode 100644 SoftwareInstallation/AbstractSoftwareInstallationDatabaseImplement/Models/Software.cs diff --git a/SoftwareInstallation/AbstractSoftwareInstallationContracts/BindingModels/PackageBindingModel.cs b/SoftwareInstallation/AbstractSoftwareInstallationContracts/BindingModels/PackageBindingModel.cs index de834db..3b2f746 100644 --- a/SoftwareInstallation/AbstractSoftwareInstallationContracts/BindingModels/PackageBindingModel.cs +++ b/SoftwareInstallation/AbstractSoftwareInstallationContracts/BindingModels/PackageBindingModel.cs @@ -13,6 +13,6 @@ namespace AbstractSoftwareInstallationContracts.BindingModels public string PackageName { get; set; } = string.Empty; public double Price { get; set; } public Dictionary PackageSoftware{get;set;} = new(); - + public object PackageSoftwares { get; set; } } } diff --git a/SoftwareInstallation/AbstractSoftwareInstallationDatabaseImplement/AbstractSoftwareInstallationDatabase.cs b/SoftwareInstallation/AbstractSoftwareInstallationDatabaseImplement/AbstractSoftwareInstallationDatabase.cs new file mode 100644 index 0000000..2f41e0f --- /dev/null +++ b/SoftwareInstallation/AbstractSoftwareInstallationDatabaseImplement/AbstractSoftwareInstallationDatabase.cs @@ -0,0 +1,28 @@ +using AbstractPackageInstallationDatabaseImplement.Models; +using AbstractSoftwareInstallationDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AbstractSoftwareInstallationDatabaseImplement +{ + public class AbstractSoftwareInstallationDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder +optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseNpgsql("Host = localhost; Port = 5432; Database = SoftwareInstallation; Username = postgres; Password = postgres"); + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Softwares { set; get; } + public virtual DbSet Packages { set; get; } + public virtual DbSet PackageSoftwares { set; get; } + public virtual DbSet Orders { set; get; } + } +} diff --git a/SoftwareInstallation/AbstractSoftwareInstallationDatabaseImplement/AbstractSoftwareInstallationDatabaseImplement.csproj b/SoftwareInstallation/AbstractSoftwareInstallationDatabaseImplement/AbstractSoftwareInstallationDatabaseImplement.csproj new file mode 100644 index 0000000..1f9db50 --- /dev/null +++ b/SoftwareInstallation/AbstractSoftwareInstallationDatabaseImplement/AbstractSoftwareInstallationDatabaseImplement.csproj @@ -0,0 +1,24 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + diff --git a/SoftwareInstallation/AbstractSoftwareInstallationDatabaseImplement/Implements/OrderStorage.cs b/SoftwareInstallation/AbstractSoftwareInstallationDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..18adebc --- /dev/null +++ b/SoftwareInstallation/AbstractSoftwareInstallationDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,85 @@ +using AbstractPackageInstallationDatabaseImplement.Models; +using AbstractSoftwareInstallationContracts.BindingModels; +using AbstractSoftwareInstallationContracts.SearchModels; +using AbstractSoftwareInstallationContracts.StoragesContracts; +using AbstractSoftwareInstallationContracts.ViewModels; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AbstractSoftwareInstallationDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new AbstractSoftwareInstallationDatabase(); + return context.Orders.Include(x => x.Package).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 AbstractSoftwareInstallationDatabase(); + return context.Orders + .Where(x => x.Id == model.Id) + .Include(x => x.Package) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new AbstractSoftwareInstallationDatabase(); + return context.Orders.Include(x => x.Package).Select(x => x.GetViewModel).ToList(); + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + using var context = new AbstractSoftwareInstallationDatabase(); + context.Orders.Add(newOrder); + context.SaveChanges(); + return context.Orders.Include(x => x.Package).FirstOrDefault(x => x.Id == newOrder.Id)?.GetViewModel; + } + + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new AbstractSoftwareInstallationDatabase(); + var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + context.SaveChanges(); + return context.Orders.Include(x => x.Package).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + } + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new AbstractSoftwareInstallationDatabase(); + var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/SoftwareInstallation/AbstractSoftwareInstallationDatabaseImplement/Implements/PackageStorage.cs b/SoftwareInstallation/AbstractSoftwareInstallationDatabaseImplement/Implements/PackageStorage.cs new file mode 100644 index 0000000..fee68fa --- /dev/null +++ b/SoftwareInstallation/AbstractSoftwareInstallationDatabaseImplement/Implements/PackageStorage.cs @@ -0,0 +1,129 @@ +using AbstractSoftwareInstallationContracts.BindingModels; +using AbstractSoftwareInstallationContracts.SearchModels; +using AbstractSoftwareInstallationContracts.StoragesContracts; +using AbstractSoftwareInstallationContracts.ViewModels; +using AbstractSoftwareInstallationDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AbstractSoftwareInstallationDatabaseImplement.Implements +{ + public class PackageStorage : IPackageStorage + { + public PackageViewModel? GetElement(PackageSearchModel model) + { + if (string.IsNullOrEmpty(model.PackageName) && !model.Id.HasValue) + { + return null; + } + using var context = new AbstractSoftwareInstallationDatabase(); + return context.Packages.Include(x => x.Softwares) + .ThenInclude(x => x.Software) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.PackageName) && + x.PackageName == model.PackageName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public List GetFilteredList(PackageSearchModel model) + { + if (string.IsNullOrEmpty(model.PackageName)) + { + return new(); + } + using var context = new AbstractSoftwareInstallationDatabase(); + return context.Packages + .Include(x => x.Softwares) + .ThenInclude(x => x.Software) + .Where(x => x.PackageName.Contains(model.PackageName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new AbstractSoftwareInstallationDatabase(); + return context.Packages + .Include(x => x.Softwares) + .ThenInclude(x => x.Software) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public PackageViewModel? Insert(PackageBindingModel model) + { + using var context = new AbstractSoftwareInstallationDatabase(); + using var transaction = context.Database.BeginTransaction(); + { + try + { + var newDoc = Package.Create(context, model); + if (newDoc == null) + { + transaction.Rollback(); + return null; + } + context.Packages.Add(newDoc); + + context.SaveChanges(); + context.Database.CommitTransaction(); + + return newDoc.GetViewModel; + } + catch (Exception) + { + transaction.Rollback(); + return null; + } + } + } + + public PackageViewModel? Update(PackageBindingModel model) + { + using var context = new AbstractSoftwareInstallationDatabase(); + using var transaction = context.Database.BeginTransaction(); + { + try + { + var Package = context.Packages.FirstOrDefault(x => x.Id == model.Id); + if (Package == null) + { + transaction.Rollback(); + return null; + } + + Package.Update(model); + Package.UpdateSoftwares(context, model); + + context.SaveChanges(); + context.Database.CommitTransaction(); + + return Package.GetViewModel; + } + catch (Exception) + { + transaction.Rollback(); + return null; + } + } + } + public PackageViewModel? Delete(PackageBindingModel model) + { + using var context = new AbstractSoftwareInstallationDatabase(); + var element = context.Packages.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Packages.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/SoftwareInstallation/AbstractSoftwareInstallationDatabaseImplement/Implements/SoftwareStorage.cs b/SoftwareInstallation/AbstractSoftwareInstallationDatabaseImplement/Implements/SoftwareStorage.cs new file mode 100644 index 0000000..1a34700 --- /dev/null +++ b/SoftwareInstallation/AbstractSoftwareInstallationDatabaseImplement/Implements/SoftwareStorage.cs @@ -0,0 +1,91 @@ +using AbstractSoftwareInstallationContracts.BindingModels; +using AbstractSoftwareInstallationContracts.SearchModels; +using AbstractSoftwareInstallationContracts.StoragesContracts; +using AbstractSoftwareInstallationContracts.ViewModels; +using AbstractSoftwareInstallationDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AbstractSoftwareInstallationDatabaseImplement.Implements +{ + public class SoftwareStorage : ISoftwareStorage + { + public SoftwareViewModel? GetElement(SoftwareSearchModel model) + { + if (string.IsNullOrEmpty(model.SoftwareName) && !model.Id.HasValue) + { + return null; + } + using var context = new AbstractSoftwareInstallationDatabase(); + return context.Softwares.FirstOrDefault(x => + (!string.IsNullOrEmpty(model.SoftwareName) && x.SoftwareName == model.SoftwareName) + || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + + } + + public List GetFilteredList(SoftwareSearchModel model) + { + if (string.IsNullOrEmpty(model.SoftwareName)) + { + return new(); + } + using var context = new AbstractSoftwareInstallationDatabase(); + return context.Softwares + .Where(x => x.SoftwareName.Contains(model.SoftwareName)) + .Select(x => x.GetViewModel) + .ToList(); + + } + + public List GetFullList() + { + using var context = new AbstractSoftwareInstallationDatabase(); + return context.Softwares.Select(x => x.GetViewModel).ToList(); + } + + public SoftwareViewModel? Insert(SoftwareBindingModel model) + { + var newSoftware = Software.Create(model); + if (newSoftware == null) + { + return null; + } + using var context = new AbstractSoftwareInstallationDatabase(); + context.Softwares.Add(newSoftware); + context.SaveChanges(); + return newSoftware.GetViewModel; + + } + + public SoftwareViewModel? Update(SoftwareBindingModel model) + { + using var context = new AbstractSoftwareInstallationDatabase(); + var Software = context.Softwares.FirstOrDefault(x => x.Id == model.Id); + if (Software == null) + { + return null; + } + Software.Update(model); + context.SaveChanges(); + return Software.GetViewModel; + } + public SoftwareViewModel? Delete(SoftwareBindingModel model) + { + using var context = new AbstractSoftwareInstallationDatabase(); + var element = context.Softwares.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Softwares.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + + } + + + } +} diff --git a/SoftwareInstallation/AbstractSoftwareInstallationDatabaseImplement/Migrations/20230409100744_Initial.Designer.cs b/SoftwareInstallation/AbstractSoftwareInstallationDatabaseImplement/Migrations/20230409100744_Initial.Designer.cs new file mode 100644 index 0000000..b5332f9 --- /dev/null +++ b/SoftwareInstallation/AbstractSoftwareInstallationDatabaseImplement/Migrations/20230409100744_Initial.Designer.cs @@ -0,0 +1,172 @@ +// +using System; +using AbstractSoftwareInstallationDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace AbstractSoftwareInstallationDatabaseImplement.Migrations +{ + [DbContext(typeof(AbstractSoftwareInstallationDatabase))] + [Migration("20230409100744_Initial")] + partial class Initial + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.4") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("AbstractPackageInstallationDatabaseImplement.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") + .IsRequired() + .HasColumnType("timestamp with time zone"); + + b.Property("PackageId") + .HasColumnType("integer"); + + b.Property("Status") + .HasColumnType("integer"); + + b.Property("Sum") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.HasIndex("PackageId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("AbstractSoftwareInstallationDatabaseImplement.Models.Package", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("PackageName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Price") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Packages"); + }); + + modelBuilder.Entity("AbstractSoftwareInstallationDatabaseImplement.Models.PackageSoftware", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("PackageId") + .HasColumnType("integer"); + + b.Property("SoftwareId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("PackageId"); + + b.HasIndex("SoftwareId"); + + b.ToTable("PackageSoftwares"); + }); + + modelBuilder.Entity("AbstractSoftwareInstallationDatabaseImplement.Models.Software", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Cost") + .HasColumnType("double precision"); + + b.Property("SoftwareName") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Softwares"); + }); + + modelBuilder.Entity("AbstractPackageInstallationDatabaseImplement.Models.Order", b => + { + b.HasOne("AbstractSoftwareInstallationDatabaseImplement.Models.Package", "Package") + .WithMany("Orders") + .HasForeignKey("PackageId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Package"); + }); + + modelBuilder.Entity("AbstractSoftwareInstallationDatabaseImplement.Models.PackageSoftware", b => + { + b.HasOne("AbstractSoftwareInstallationDatabaseImplement.Models.Package", "Package") + .WithMany("Softwares") + .HasForeignKey("PackageId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("AbstractSoftwareInstallationDatabaseImplement.Models.Software", "Software") + .WithMany("PackageSoftwares") + .HasForeignKey("SoftwareId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Package"); + + b.Navigation("Software"); + }); + + modelBuilder.Entity("AbstractSoftwareInstallationDatabaseImplement.Models.Package", b => + { + b.Navigation("Orders"); + + b.Navigation("Softwares"); + }); + + modelBuilder.Entity("AbstractSoftwareInstallationDatabaseImplement.Models.Software", b => + { + b.Navigation("PackageSoftwares"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/SoftwareInstallation/AbstractSoftwareInstallationDatabaseImplement/Migrations/20230409100744_Initial.cs b/SoftwareInstallation/AbstractSoftwareInstallationDatabaseImplement/Migrations/20230409100744_Initial.cs new file mode 100644 index 0000000..669d2c6 --- /dev/null +++ b/SoftwareInstallation/AbstractSoftwareInstallationDatabaseImplement/Migrations/20230409100744_Initial.cs @@ -0,0 +1,126 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace AbstractSoftwareInstallationDatabaseImplement.Migrations +{ + /// + public partial class Initial : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Packages", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + PackageName = table.Column(type: "text", nullable: false), + Price = table.Column(type: "double precision", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Packages", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Softwares", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + SoftwareName = table.Column(type: "text", nullable: false), + Cost = table.Column(type: "double precision", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Softwares", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + PackageId = 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: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Orders", x => x.Id); + table.ForeignKey( + name: "FK_Orders_Packages_PackageId", + column: x => x.PackageId, + principalTable: "Packages", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "PackageSoftwares", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + PackageId = table.Column(type: "integer", nullable: false), + SoftwareId = table.Column(type: "integer", nullable: false), + Count = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_PackageSoftwares", x => x.Id); + table.ForeignKey( + name: "FK_PackageSoftwares_Packages_PackageId", + column: x => x.PackageId, + principalTable: "Packages", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_PackageSoftwares_Softwares_SoftwareId", + column: x => x.SoftwareId, + principalTable: "Softwares", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_Orders_PackageId", + table: "Orders", + column: "PackageId"); + + migrationBuilder.CreateIndex( + name: "IX_PackageSoftwares_PackageId", + table: "PackageSoftwares", + column: "PackageId"); + + migrationBuilder.CreateIndex( + name: "IX_PackageSoftwares_SoftwareId", + table: "PackageSoftwares", + column: "SoftwareId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "PackageSoftwares"); + + migrationBuilder.DropTable( + name: "Packages"); + + migrationBuilder.DropTable( + name: "Softwares"); + } + } +} diff --git a/SoftwareInstallation/AbstractSoftwareInstallationDatabaseImplement/Migrations/AbstractSoftwareInstallationDatabaseModelSnapshot.cs b/SoftwareInstallation/AbstractSoftwareInstallationDatabaseImplement/Migrations/AbstractSoftwareInstallationDatabaseModelSnapshot.cs new file mode 100644 index 0000000..9f9f358 --- /dev/null +++ b/SoftwareInstallation/AbstractSoftwareInstallationDatabaseImplement/Migrations/AbstractSoftwareInstallationDatabaseModelSnapshot.cs @@ -0,0 +1,169 @@ +// +using System; +using AbstractSoftwareInstallationDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace AbstractSoftwareInstallationDatabaseImplement.Migrations +{ + [DbContext(typeof(AbstractSoftwareInstallationDatabase))] + partial class AbstractSoftwareInstallationDatabaseModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.4") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("AbstractPackageInstallationDatabaseImplement.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") + .IsRequired() + .HasColumnType("timestamp with time zone"); + + b.Property("PackageId") + .HasColumnType("integer"); + + b.Property("Status") + .HasColumnType("integer"); + + b.Property("Sum") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.HasIndex("PackageId"); + + b.ToTable("Orders", (string)null); + }); + + modelBuilder.Entity("AbstractSoftwareInstallationDatabaseImplement.Models.Package", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("PackageName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Price") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Packages", (string)null); + }); + + modelBuilder.Entity("AbstractSoftwareInstallationDatabaseImplement.Models.PackageSoftware", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("PackageId") + .HasColumnType("integer"); + + b.Property("SoftwareId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("PackageId"); + + b.HasIndex("SoftwareId"); + + b.ToTable("PackageSoftwares", (string)null); + }); + + modelBuilder.Entity("AbstractSoftwareInstallationDatabaseImplement.Models.Software", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Cost") + .HasColumnType("double precision"); + + b.Property("SoftwareName") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Softwares", (string)null); + }); + + modelBuilder.Entity("AbstractPackageInstallationDatabaseImplement.Models.Order", b => + { + b.HasOne("AbstractSoftwareInstallationDatabaseImplement.Models.Package", "Package") + .WithMany("Orders") + .HasForeignKey("PackageId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Package"); + }); + + modelBuilder.Entity("AbstractSoftwareInstallationDatabaseImplement.Models.PackageSoftware", b => + { + b.HasOne("AbstractSoftwareInstallationDatabaseImplement.Models.Package", "Package") + .WithMany("Softwares") + .HasForeignKey("PackageId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("AbstractSoftwareInstallationDatabaseImplement.Models.Software", "Software") + .WithMany("PackageSoftwares") + .HasForeignKey("SoftwareId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Package"); + + b.Navigation("Software"); + }); + + modelBuilder.Entity("AbstractSoftwareInstallationDatabaseImplement.Models.Package", b => + { + b.Navigation("Orders"); + + b.Navigation("Softwares"); + }); + + modelBuilder.Entity("AbstractSoftwareInstallationDatabaseImplement.Models.Software", b => + { + b.Navigation("PackageSoftwares"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/SoftwareInstallation/AbstractSoftwareInstallationDatabaseImplement/Models/Order.cs b/SoftwareInstallation/AbstractSoftwareInstallationDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..941b087 --- /dev/null +++ b/SoftwareInstallation/AbstractSoftwareInstallationDatabaseImplement/Models/Order.cs @@ -0,0 +1,72 @@ +using AbstractSoftwareInstallationContracts.BindingModels; +using AbstractSoftwareInstallationContracts.ViewModels; +using AbstractSoftwareInstallationDatabaseImplement.Models; +using AbstractSoftwareInstallationDataModels; +using AbstractSoftwareInstallationDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AbstractPackageInstallationDatabaseImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + [Required] + public int PackageId { get; private set; } + [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 virtual Package Package { get; set; } + + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order + { + PackageId = model.PackageId, + 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; + } + Status = model.Status; + DateImplement = model.DateImplement; + } + + public OrderViewModel GetViewModel => new() + { + PackageId = PackageId, + Count = Count, + Sum = Sum, + DateCreate = DateCreate, + DateImplement = DateImplement, + Id = Id, + Status = Status, + PackageName = Package.PackageName + }; + } +} diff --git a/SoftwareInstallation/AbstractSoftwareInstallationDatabaseImplement/Models/Package.cs b/SoftwareInstallation/AbstractSoftwareInstallationDatabaseImplement/Models/Package.cs new file mode 100644 index 0000000..5879a81 --- /dev/null +++ b/SoftwareInstallation/AbstractSoftwareInstallationDatabaseImplement/Models/Package.cs @@ -0,0 +1,104 @@ +using AbstractPackageInstallationDatabaseImplement.Models; +using AbstractSoftwareInstallationContracts.BindingModels; +using AbstractSoftwareInstallationContracts.ViewModels; +using AbstractSoftwareInstallationDataModels.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 AbstractSoftwareInstallationDatabaseImplement.Models +{ + public class Package : IPackageModel + { + public int Id { get; set; } + [Required] + public string PackageName { get; set; } = string.Empty; + [Required] + public double Price { get; set; } + private Dictionary? _PackageSoftwares = + null; + [NotMapped] + public Dictionary PackageSoftware + { + get + { + if (_PackageSoftwares == null) + { + _PackageSoftwares = Softwares + .ToDictionary(recPC => recPC.SoftwareId, recPC => + (recPC.Software as ISoftwareModel, recPC.Count)); + } + return _PackageSoftwares; + } + } + [ForeignKey("PackageId")] + public virtual List Softwares { get; set; } = new(); + [ForeignKey("PackageId")] + public virtual List Orders { get; set; } = new(); + public static Package Create(AbstractSoftwareInstallationDatabase context, + PackageBindingModel model) + { + return new Package() + { + Id = model.Id, + PackageName = model.PackageName, + Price = model.Price, + Softwares = model.PackageSoftware.Select(x => new + PackageSoftware + { + Software = context.Softwares.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(PackageBindingModel model) + { + PackageName = model.PackageName; + Price = model.Price; + } + public PackageViewModel GetViewModel => new() + { + Id = Id, + PackageName = PackageName, + Price = Price, + PackageSoftware = PackageSoftware + }; + public void UpdateSoftwares(AbstractSoftwareInstallationDatabase context, + PackageBindingModel model) + { + var PackageSoftwares = context.PackageSoftwares.Where(rec => + rec.PackageId == model.Id).ToList(); + if (PackageSoftwares != null && PackageSoftwares.Count > 0) + { // удалили те, которых нет в модели + context.PackageSoftwares.RemoveRange(PackageSoftwares.Where(rec + => !model.PackageSoftware.ContainsKey(rec.SoftwareId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateSoftware in PackageSoftwares) + { + updateSoftware.Count = + model.PackageSoftware[updateSoftware.SoftwareId].Item2; + model.PackageSoftware.Remove(updateSoftware.SoftwareId); + } + context.SaveChanges(); + } + var Package = context.Packages.First(x => x.Id == Id); + foreach (var pc in model.PackageSoftware) + { + context.PackageSoftwares.Add(new PackageSoftware + { + Package = Package, + Software = context.Softwares.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _PackageSoftwares = null; + } + + } +} diff --git a/SoftwareInstallation/AbstractSoftwareInstallationDatabaseImplement/Models/PackageSoftware.cs b/SoftwareInstallation/AbstractSoftwareInstallationDatabaseImplement/Models/PackageSoftware.cs new file mode 100644 index 0000000..eeca146 --- /dev/null +++ b/SoftwareInstallation/AbstractSoftwareInstallationDatabaseImplement/Models/PackageSoftware.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AbstractSoftwareInstallationDatabaseImplement.Models +{ + public class PackageSoftware + { + public int Id { get; set; } + [Required] + public int PackageId { get; set; } + [Required] + public int SoftwareId { get; set; } + [Required] + public int Count { get; set; } + public virtual Software Software { get; set; } = new(); + public virtual Package Package { get; set; } = new(); + } +} diff --git a/SoftwareInstallation/AbstractSoftwareInstallationDatabaseImplement/Models/Software.cs b/SoftwareInstallation/AbstractSoftwareInstallationDatabaseImplement/Models/Software.cs new file mode 100644 index 0000000..be64827 --- /dev/null +++ b/SoftwareInstallation/AbstractSoftwareInstallationDatabaseImplement/Models/Software.cs @@ -0,0 +1,63 @@ +using AbstractSoftwareInstallationContracts.BindingModels; +using AbstractSoftwareInstallationContracts.ViewModels; +using AbstractSoftwareInstallationDataModels.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 AbstractSoftwareInstallationDatabaseImplement.Models +{ + public class Software : ISoftwareModel + { + public int Id { get; private set; } + [Required] + public string SoftwareName { get; private set; } = string.Empty; + [Required] + public double Cost { get; set; } + [ForeignKey("SoftwareId")] + public virtual List PackageSoftwares { get; set; } = + new(); + public static Software? Create(SoftwareBindingModel model) + { + if (model == null) + { + return null; + } + return new Software() + { + Id = model.Id, + SoftwareName = model.SoftwareName, + Cost = model.Cost + }; + } + public static Software Create(SoftwareViewModel model) + { + return new Software + { + Id = model.Id, + SoftwareName = model.SoftwareName, + Cost = model.Cost + }; + } + public void Update(SoftwareBindingModel model) + { + if (model == null) + { + return; + } + SoftwareName = model.SoftwareName; + Cost = model.Cost; + } + public SoftwareViewModel GetViewModel => new() + { + Id = Id, + SoftwareName = SoftwareName, + Cost = Cost + }; + + } +} diff --git a/SoftwareInstallation/SoftwareInstallation.sln b/SoftwareInstallation/SoftwareInstallation.sln index 1a4bc7c..418534c 100644 --- a/SoftwareInstallation/SoftwareInstallation.sln +++ b/SoftwareInstallation/SoftwareInstallation.sln @@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractSoftwareInstallatio EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractSoftwareInstallationListImplement", "AbstractSoftwareInstallationListImplement\AbstractSoftwareInstallationListImplement.csproj", "{31AD2872-9651-476A-9868-C4404FEEB0E4}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbstractSoftwareInstallationFileImplement", "AbstractSoftwareInstallationFileImplement\AbstractSoftwareInstallationFileImplement.csproj", "{BEE13C3F-1BB8-46B4-BC87-CFC367E52A77}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractSoftwareInstallationFileImplement", "AbstractSoftwareInstallationFileImplement\AbstractSoftwareInstallationFileImplement.csproj", "{BEE13C3F-1BB8-46B4-BC87-CFC367E52A77}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbstractSoftwareInstallationDatabaseImplement", "AbstractSoftwareInstallationDatabaseImplement\AbstractSoftwareInstallationDatabaseImplement.csproj", "{C0E293F4-1C74-4852-BE77-2B30F51BA844}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -45,6 +47,10 @@ Global {BEE13C3F-1BB8-46B4-BC87-CFC367E52A77}.Debug|Any CPU.Build.0 = Debug|Any CPU {BEE13C3F-1BB8-46B4-BC87-CFC367E52A77}.Release|Any CPU.ActiveCfg = Release|Any CPU {BEE13C3F-1BB8-46B4-BC87-CFC367E52A77}.Release|Any CPU.Build.0 = Release|Any CPU + {C0E293F4-1C74-4852-BE77-2B30F51BA844}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C0E293F4-1C74-4852-BE77-2B30F51BA844}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C0E293F4-1C74-4852-BE77-2B30F51BA844}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C0E293F4-1C74-4852-BE77-2B30F51BA844}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/SoftwareInstallation/SoftwareInstallation/Program.cs b/SoftwareInstallation/SoftwareInstallation/Program.cs index 1b6b0b8..cf7b6fb 100644 --- a/SoftwareInstallation/SoftwareInstallation/Program.cs +++ b/SoftwareInstallation/SoftwareInstallation/Program.cs @@ -1,4 +1,4 @@ -using AbstractSoftwareInstallationFileImplement.Implements; +using AbstractSoftwareInstallationDatabaseImplement.Implements; using AbstractSoftwareInstallationBusinessLogic; using AbstractSoftwareInstallationBusinessLogic.BusinessLogic; using AbstractSoftwareInstallationContracts.BusinessLogicsContracts; diff --git a/SoftwareInstallation/SoftwareInstallation/SoftwareInstallationView.csproj b/SoftwareInstallation/SoftwareInstallation/SoftwareInstallationView.csproj index 3bbfa37..e701f9c 100644 --- a/SoftwareInstallation/SoftwareInstallation/SoftwareInstallationView.csproj +++ b/SoftwareInstallation/SoftwareInstallation/SoftwareInstallationView.csproj @@ -9,6 +9,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -17,7 +21,7 @@ - +