From 0bdd8aa43c5a6468e7752c8940ccd785ce042fc6 Mon Sep 17 00:00:00 2001 From: asoc1al Date: Sun, 26 May 2024 12:45:19 +0400 Subject: [PATCH] . --- .../AutoWorkshopBusinessLogic.csproj | 2 +- .../AutoWorkshopContracts.csproj | 4 +- .../AutoWorkshopDataModels.csproj | 4 +- .../AutoWorkshopDatabase.cs | 30 +++ .../AutoWorkshopDatabaseImplement.csproj | 24 +++ .../Implements/ComponentStorage.cs | 82 +++++++++ .../Implements/OrderStorage.cs | 94 ++++++++++ .../Implements/RepairStorage.cs | 109 +++++++++++ .../20240504064327_InitialCreate.Designer.cs | 171 ++++++++++++++++++ .../20240504064327_InitialCreate.cs | 126 +++++++++++++ .../AutoWorkshopDatabaseModelSnapshot.cs | 168 +++++++++++++++++ .../Models/Component.cs | 61 +++++++ AutoWorkshopDatabaseImplement/Models/Order.cs | 70 +++++++ .../Models/Repair.cs | 106 +++++++++++ .../Models/RepairComponent.cs | 22 +++ .../AutoWorkshopFileImplement.csproj | 4 +- .../AutoWorkshopListImplement.csproj | 4 +- AutoWorkshopView/AutoWorkshopView.csproj | 7 +- AutoWorkshopView/MainForm.Designer.cs | 26 +-- AutoWorkshopView/Program.cs | 2 +- 20 files changed, 1091 insertions(+), 25 deletions(-) create mode 100644 AutoWorkshopDatabaseImplement/AutoWorkshopDatabase.cs create mode 100644 AutoWorkshopDatabaseImplement/AutoWorkshopDatabaseImplement.csproj create mode 100644 AutoWorkshopDatabaseImplement/Implements/ComponentStorage.cs create mode 100644 AutoWorkshopDatabaseImplement/Implements/OrderStorage.cs create mode 100644 AutoWorkshopDatabaseImplement/Implements/RepairStorage.cs create mode 100644 AutoWorkshopDatabaseImplement/Migrations/20240504064327_InitialCreate.Designer.cs create mode 100644 AutoWorkshopDatabaseImplement/Migrations/20240504064327_InitialCreate.cs create mode 100644 AutoWorkshopDatabaseImplement/Migrations/AutoWorkshopDatabaseModelSnapshot.cs create mode 100644 AutoWorkshopDatabaseImplement/Models/Component.cs create mode 100644 AutoWorkshopDatabaseImplement/Models/Order.cs create mode 100644 AutoWorkshopDatabaseImplement/Models/Repair.cs create mode 100644 AutoWorkshopDatabaseImplement/Models/RepairComponent.cs diff --git a/AutoWorkshopBusinessLogic/AutoWorkshopBusinessLogic.csproj b/AutoWorkshopBusinessLogic/AutoWorkshopBusinessLogic.csproj index a7b458f..ed29e63 100644 --- a/AutoWorkshopBusinessLogic/AutoWorkshopBusinessLogic.csproj +++ b/AutoWorkshopBusinessLogic/AutoWorkshopBusinessLogic.csproj @@ -1,4 +1,4 @@ - + net8.0 diff --git a/AutoWorkshopContracts/AutoWorkshopContracts.csproj b/AutoWorkshopContracts/AutoWorkshopContracts.csproj index bf4f9d2..7c2e15c 100644 --- a/AutoWorkshopContracts/AutoWorkshopContracts.csproj +++ b/AutoWorkshopContracts/AutoWorkshopContracts.csproj @@ -1,7 +1,7 @@ - + - net8.0 + net6.0 enable enable diff --git a/AutoWorkshopDataModels/AutoWorkshopDataModels.csproj b/AutoWorkshopDataModels/AutoWorkshopDataModels.csproj index fa71b7a..132c02c 100644 --- a/AutoWorkshopDataModels/AutoWorkshopDataModels.csproj +++ b/AutoWorkshopDataModels/AutoWorkshopDataModels.csproj @@ -1,7 +1,7 @@ - + - net8.0 + net6.0 enable enable diff --git a/AutoWorkshopDatabaseImplement/AutoWorkshopDatabase.cs b/AutoWorkshopDatabaseImplement/AutoWorkshopDatabase.cs new file mode 100644 index 0000000..a717cf2 --- /dev/null +++ b/AutoWorkshopDatabaseImplement/AutoWorkshopDatabase.cs @@ -0,0 +1,30 @@ +using AutoWorkshopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using Npgsql.EntityFrameworkCore.PostgreSQL; + +namespace AutoWorkshopDatabaseImplement +{ + public class AutoWorkshopDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder OptionsBuilder) + { + if (OptionsBuilder.IsConfigured == false) + { + OptionsBuilder.UseNpgsql(@"Host=localhost;Database=AutoWorkshop;Username=postgres;Password=postgres"); + } + + base.OnConfiguring(OptionsBuilder); + + AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); + AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true); + } + + public virtual DbSet Components { set; get; } + + public virtual DbSet Repairs { set; get; } + + public virtual DbSet RepairComponents { set; get; } + + public virtual DbSet Orders { set; get; } + } +} diff --git a/AutoWorkshopDatabaseImplement/AutoWorkshopDatabaseImplement.csproj b/AutoWorkshopDatabaseImplement/AutoWorkshopDatabaseImplement.csproj new file mode 100644 index 0000000..3a93e34 --- /dev/null +++ b/AutoWorkshopDatabaseImplement/AutoWorkshopDatabaseImplement.csproj @@ -0,0 +1,24 @@ + + + + net8.0 + enable + enable + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + + diff --git a/AutoWorkshopDatabaseImplement/Implements/ComponentStorage.cs b/AutoWorkshopDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..4bf7dec --- /dev/null +++ b/AutoWorkshopDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,82 @@ +using AutoWorkshopContracts.BindingModels; +using AutoWorkshopContracts.SearchModels; +using AutoWorkshopContracts.StoragesContracts; +using AutoWorkshopContracts.ViewModels; +using AutoWorkshopDatabaseImplement.Models; + +namespace AutoWorkshopDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var Context = new AutoWorkshopDatabase(); + return Context.Components.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(ComponentSearchModel Model) + { + if (string.IsNullOrEmpty(Model.ComponentName)) + return new(); + + using var Context = new AutoWorkshopDatabase(); + return Context.Components + .Where(x => x.ComponentName.Contains(Model.ComponentName)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public ComponentViewModel? GetElement(ComponentSearchModel Model) + { + if (string.IsNullOrEmpty(Model.ComponentName) && !Model.Id.HasValue) + return null; + + using var Context = new AutoWorkshopDatabase(); + return Context.Components + .FirstOrDefault(x => (!string.IsNullOrEmpty(Model.ComponentName) && x.ComponentName == Model.ComponentName) || + (Model.Id.HasValue && x.Id == Model.Id))?.GetViewModel; + } + + public ComponentViewModel? Insert(ComponentBindingModel Model) + { + var NewComponent = Component.Create(Model); + + if (NewComponent == null) + return null; + + using var Context = new AutoWorkshopDatabase(); + Context.Components.Add(NewComponent); + Context.SaveChanges(); + + return NewComponent.GetViewModel; + } + + public ComponentViewModel? Update(ComponentBindingModel Model) + { + using var Context = new AutoWorkshopDatabase(); + + var Component = Context.Components.FirstOrDefault(x => x.Id == Model.Id); + + if (Component == null) + return null; + + Component.Update(Model); + Context.SaveChanges(); + + return Component.GetViewModel; + } + + public ComponentViewModel? Delete(ComponentBindingModel Model) + { + using var Context = new AutoWorkshopDatabase(); + var Component = Context.Components.FirstOrDefault(rec => rec.Id == Model.Id); + + if (Component == null) + return null; + + Context.Components.Remove(Component); + Context.SaveChanges(); + return Component.GetViewModel; + } + } +} diff --git a/AutoWorkshopDatabaseImplement/Implements/OrderStorage.cs b/AutoWorkshopDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..b42ccd7 --- /dev/null +++ b/AutoWorkshopDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,94 @@ +using AutoWorkshopContracts.BindingModels; +using AutoWorkshopContracts.SearchModels; +using AutoWorkshopContracts.StoragesContracts; +using AutoWorkshopContracts.ViewModels; +using AutoWorkshopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace AutoWorkshopDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public List GetFullList() + { + using var Context = new AutoWorkshopDatabase(); + + return Context.Orders + .Include(x => x.Repair) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(OrderSearchModel Model) + { + if (!Model.Id.HasValue) + return new(); + + using var Context = new AutoWorkshopDatabase(); + + return Context.Orders + .Include(x => x.Repair) + .Where(x => x.Id == Model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + + public OrderViewModel? GetElement(OrderSearchModel Model) + { + if (!Model.Id.HasValue) + return null; + + using var Context = new AutoWorkshopDatabase(); + + return Context.Orders + .Include(x => x.Repair) + .FirstOrDefault(x => Model.Id.HasValue && x.Id == Model.Id)? + .GetViewModel; + } + + public OrderViewModel? Insert(OrderBindingModel Model) + { + if (Model == null) + return null; + + var NewOrder = Order.Create(Model); + if (NewOrder == null) + return null; + + using var Context = new AutoWorkshopDatabase(); + + Context.Orders.Add(NewOrder); + Context.SaveChanges(); + + return Context.Orders.Include(x => x.Repair).FirstOrDefault(x => x.Id == NewOrder.Id)?.GetViewModel; + } + + public OrderViewModel? Update(OrderBindingModel Model) + { + using var Context = new AutoWorkshopDatabase(); + + 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.Repair).FirstOrDefault(x => x.Id == Model.Id)?.GetViewModel; + } + + public OrderViewModel? Delete(OrderBindingModel Model) + { + using var Context = new AutoWorkshopDatabase(); + var Order = Context.Orders.FirstOrDefault(rec => rec.Id == Model.Id); + + if (Order == null) + return null; + + Context.Orders.Remove(Order); + Context.SaveChanges(); + + return Order.GetViewModel; + } + } +} diff --git a/AutoWorkshopDatabaseImplement/Implements/RepairStorage.cs b/AutoWorkshopDatabaseImplement/Implements/RepairStorage.cs new file mode 100644 index 0000000..41be0a8 --- /dev/null +++ b/AutoWorkshopDatabaseImplement/Implements/RepairStorage.cs @@ -0,0 +1,109 @@ +using AutoWorkshopContracts.BindingModels; +using AutoWorkshopContracts.SearchModels; +using AutoWorkshopContracts.StoragesContracts; +using AutoWorkshopContracts.ViewModels; +using AutoWorkshopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace AutoWorkshopDatabaseImplement.Implements +{ + public class RepairStorage : IRepairStorage + { + public List GetFullList() + { + using var Context = new AutoWorkshopDatabase(); + return Context.Repairs + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(RepairSearchModel Model) + { + if (string.IsNullOrEmpty(Model.RepairName)) + return new(); + + using var Context = new AutoWorkshopDatabase(); + + return Context.Repairs + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .Where(x => x.RepairName.Contains(Model.RepairName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public RepairViewModel? GetElement(RepairSearchModel Model) + { + if (string.IsNullOrEmpty(Model.RepairName) && !Model.Id.HasValue) + return null; + + using var Context = new AutoWorkshopDatabase(); + + return Context.Repairs + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .FirstOrDefault(x => (!string.IsNullOrEmpty(Model.RepairName) && x.RepairName == Model.RepairName) || + (Model.Id.HasValue && x.Id == Model.Id))? + .GetViewModel; + } + + public RepairViewModel? Insert(RepairBindingModel Model) + { + using var Context = new AutoWorkshopDatabase(); + + var NewRepair = Repair.Create(Context, Model); + if (NewRepair == null) + return null; + + Context.Repairs.Add(NewRepair); + Context.SaveChanges(); + + return NewRepair.GetViewModel; + } + + public RepairViewModel? Update(RepairBindingModel Model) + { + using var Context = new AutoWorkshopDatabase(); + + using var Transaction = Context.Database.BeginTransaction(); + try + { + var Repair = Context.Repairs.FirstOrDefault(rec => rec.Id == Model.Id); + + if (Repair == null) + return null; + + Repair.Update(Model); + Context.SaveChanges(); + Repair.UpdateComponents(Context, Model); + + Transaction.Commit(); + return Repair.GetViewModel; + } + catch + { + Transaction.Rollback(); + throw; + } + } + public RepairViewModel? Delete(RepairBindingModel Model) + { + using var Context = new AutoWorkshopDatabase(); + + var Repair = Context.Repairs + .Include(x => x.Components) + .FirstOrDefault(rec => rec.Id == Model.Id); + + if (Repair == null) + return null; + + Context.Repairs.Remove(Repair); + Context.SaveChanges(); + return Repair.GetViewModel; + } + } +} diff --git a/AutoWorkshopDatabaseImplement/Migrations/20240504064327_InitialCreate.Designer.cs b/AutoWorkshopDatabaseImplement/Migrations/20240504064327_InitialCreate.Designer.cs new file mode 100644 index 0000000..f0baf2b --- /dev/null +++ b/AutoWorkshopDatabaseImplement/Migrations/20240504064327_InitialCreate.Designer.cs @@ -0,0 +1,171 @@ +// +using System; +using AutoWorkshopDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace AutoWorkshopDatabaseImplement.Migrations +{ + [DbContext(typeof(AutoWorkshopDatabase))] + [Migration("20240504064327_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.17") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("AutoWorkshopDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Cost") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("AutoWorkshopDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("DateCreate") + .HasColumnType("timestamp without time zone"); + + b.Property("DateImplement") + .HasColumnType("timestamp without time zone"); + + b.Property("RepairId") + .HasColumnType("integer"); + + b.Property("Status") + .HasColumnType("integer"); + + b.Property("Sum") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.HasIndex("RepairId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("AutoWorkshopDatabaseImplement.Models.Repair", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Price") + .HasColumnType("double precision"); + + b.Property("RepairName") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Repairs"); + }); + + modelBuilder.Entity("AutoWorkshopDatabaseImplement.Models.RepairComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("integer"); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("RepairId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("RepairId"); + + b.ToTable("RepairComponents"); + }); + + modelBuilder.Entity("AutoWorkshopDatabaseImplement.Models.Order", b => + { + b.HasOne("AutoWorkshopDatabaseImplement.Models.Repair", "Repair") + .WithMany("Orders") + .HasForeignKey("RepairId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Repair"); + }); + + modelBuilder.Entity("AutoWorkshopDatabaseImplement.Models.RepairComponent", b => + { + b.HasOne("AutoWorkshopDatabaseImplement.Models.Component", "Component") + .WithMany("RepairComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("AutoWorkshopDatabaseImplement.Models.Repair", "Repair") + .WithMany("Components") + .HasForeignKey("RepairId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Repair"); + }); + + modelBuilder.Entity("AutoWorkshopDatabaseImplement.Models.Component", b => + { + b.Navigation("RepairComponents"); + }); + + modelBuilder.Entity("AutoWorkshopDatabaseImplement.Models.Repair", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/AutoWorkshopDatabaseImplement/Migrations/20240504064327_InitialCreate.cs b/AutoWorkshopDatabaseImplement/Migrations/20240504064327_InitialCreate.cs new file mode 100644 index 0000000..dd113b0 --- /dev/null +++ b/AutoWorkshopDatabaseImplement/Migrations/20240504064327_InitialCreate.cs @@ -0,0 +1,126 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace AutoWorkshopDatabaseImplement.Migrations +{ + /// + public partial class InitialCreate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Components", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + ComponentName = table.Column(type: "text", nullable: false), + Cost = table.Column(type: "double precision", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Components", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Repairs", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + RepairName = table.Column(type: "text", nullable: false), + Price = table.Column(type: "double precision", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Repairs", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + RepairId = 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 without time zone", nullable: false), + DateImplement = table.Column(type: "timestamp without time zone", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Orders", x => x.Id); + table.ForeignKey( + name: "FK_Orders_Repairs_RepairId", + column: x => x.RepairId, + principalTable: "Repairs", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "RepairComponents", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + RepairId = table.Column(type: "integer", nullable: false), + ComponentId = table.Column(type: "integer", nullable: false), + Count = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_RepairComponents", x => x.Id); + table.ForeignKey( + name: "FK_RepairComponents_Components_ComponentId", + column: x => x.ComponentId, + principalTable: "Components", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_RepairComponents_Repairs_RepairId", + column: x => x.RepairId, + principalTable: "Repairs", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_Orders_RepairId", + table: "Orders", + column: "RepairId"); + + migrationBuilder.CreateIndex( + name: "IX_RepairComponents_ComponentId", + table: "RepairComponents", + column: "ComponentId"); + + migrationBuilder.CreateIndex( + name: "IX_RepairComponents_RepairId", + table: "RepairComponents", + column: "RepairId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "RepairComponents"); + + migrationBuilder.DropTable( + name: "Components"); + + migrationBuilder.DropTable( + name: "Repairs"); + } + } +} diff --git a/AutoWorkshopDatabaseImplement/Migrations/AutoWorkshopDatabaseModelSnapshot.cs b/AutoWorkshopDatabaseImplement/Migrations/AutoWorkshopDatabaseModelSnapshot.cs new file mode 100644 index 0000000..40a9aca --- /dev/null +++ b/AutoWorkshopDatabaseImplement/Migrations/AutoWorkshopDatabaseModelSnapshot.cs @@ -0,0 +1,168 @@ +// +using System; +using AutoWorkshopDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace AutoWorkshopDatabaseImplement.Migrations +{ + [DbContext(typeof(AutoWorkshopDatabase))] + partial class AutoWorkshopDatabaseModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.17") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("AutoWorkshopDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Cost") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("AutoWorkshopDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("DateCreate") + .HasColumnType("timestamp without time zone"); + + b.Property("DateImplement") + .HasColumnType("timestamp without time zone"); + + b.Property("RepairId") + .HasColumnType("integer"); + + b.Property("Status") + .HasColumnType("integer"); + + b.Property("Sum") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.HasIndex("RepairId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("AutoWorkshopDatabaseImplement.Models.Repair", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Price") + .HasColumnType("double precision"); + + b.Property("RepairName") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Repairs"); + }); + + modelBuilder.Entity("AutoWorkshopDatabaseImplement.Models.RepairComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("integer"); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("RepairId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("RepairId"); + + b.ToTable("RepairComponents"); + }); + + modelBuilder.Entity("AutoWorkshopDatabaseImplement.Models.Order", b => + { + b.HasOne("AutoWorkshopDatabaseImplement.Models.Repair", "Repair") + .WithMany("Orders") + .HasForeignKey("RepairId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Repair"); + }); + + modelBuilder.Entity("AutoWorkshopDatabaseImplement.Models.RepairComponent", b => + { + b.HasOne("AutoWorkshopDatabaseImplement.Models.Component", "Component") + .WithMany("RepairComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("AutoWorkshopDatabaseImplement.Models.Repair", "Repair") + .WithMany("Components") + .HasForeignKey("RepairId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Repair"); + }); + + modelBuilder.Entity("AutoWorkshopDatabaseImplement.Models.Component", b => + { + b.Navigation("RepairComponents"); + }); + + modelBuilder.Entity("AutoWorkshopDatabaseImplement.Models.Repair", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/AutoWorkshopDatabaseImplement/Models/Component.cs b/AutoWorkshopDatabaseImplement/Models/Component.cs new file mode 100644 index 0000000..bc6ff06 --- /dev/null +++ b/AutoWorkshopDatabaseImplement/Models/Component.cs @@ -0,0 +1,61 @@ +using AutoWorkshopContracts.BindingModels; +using AutoWorkshopContracts.ViewModels; +using AutoWorkshopDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace AutoWorkshopDatabaseImplement.Models +{ + public class Component : IComponentModel + { + public int Id { get; private set; } + + [Required] + public string ComponentName { get; private set; } = string.Empty; + + [Required] + public double Cost { get; set; } + + [ForeignKey("ComponentId")] + public virtual List RepairComponents { get; set; } = new(); + + public static Component? Create(ComponentBindingModel Model) + { + if (Model == null) + return null; + + return new Component() + { + Id = Model.Id, + ComponentName = Model.ComponentName, + Cost = Model.Cost + }; + } + + public static Component Create(ComponentViewModel Model) + { + return new Component + { + Id = Model.Id, + ComponentName = Model.ComponentName, + Cost = Model.Cost + }; + } + + public void Update(ComponentBindingModel Model) + { + if (Model == null) + return; + + ComponentName = Model.ComponentName; + Cost = Model.Cost; + } + + public ComponentViewModel GetViewModel => new() + { + Id = Id, + ComponentName = ComponentName, + Cost = Cost + }; + } +} diff --git a/AutoWorkshopDatabaseImplement/Models/Order.cs b/AutoWorkshopDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..0a423f4 --- /dev/null +++ b/AutoWorkshopDatabaseImplement/Models/Order.cs @@ -0,0 +1,70 @@ +using AutoWorkshopContracts.BindingModels; +using AutoWorkshopContracts.ViewModels; +using AutoWorkshopDataModels.Enums; +using AutoWorkshopDataModels.Models; +using System.ComponentModel.DataAnnotations; + +namespace AutoWorkshopDatabaseImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + + [Required] + public int RepairId { get; private set; } + + public virtual Repair Repair { get; set; } + + [Required] + public int Count { get; private set; } + + [Required] + public double Sum { get; private set; } + + [Required] + public OrderStatus Status { get; private set; } = OrderStatus.Undefined; + + [Required] + public DateTime DateCreate { get; private set; } = DateTime.Now; + + public DateTime? DateImplement { get; private set; } + + public static Order? Create(OrderBindingModel Model) + { + if (Model is null) + return null; + + return new Order() + { + Id = Model.Id, + RepairId = Model.RepairId, + Count = Model.Count, + Sum = Model.Sum, + Status = Model.Status, + DateCreate = Model.DateCreate, + DateImplement = Model.DateImplement, + }; + } + + public void Update(OrderBindingModel Model) + { + if (Model is null) + return; + + Status = Model.Status; + DateImplement = Model.DateImplement; + } + + public OrderViewModel GetViewModel => new() + { + Id = Id, + RepairId = RepairId, + RepairName = Repair.RepairName, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + }; + } +} diff --git a/AutoWorkshopDatabaseImplement/Models/Repair.cs b/AutoWorkshopDatabaseImplement/Models/Repair.cs new file mode 100644 index 0000000..e4707ba --- /dev/null +++ b/AutoWorkshopDatabaseImplement/Models/Repair.cs @@ -0,0 +1,106 @@ +using AutoWorkshopContracts.BindingModels; +using AutoWorkshopContracts.ViewModels; +using AutoWorkshopDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace AutoWorkshopDatabaseImplement.Models +{ + public class Repair : IRepairModel + { + public int Id { get; set; } + + [Required] + public string RepairName { get; set; } = string.Empty; + + [Required] + public double Price { get; set; } + + private Dictionary? _repairComponents = null; + + [NotMapped] + public Dictionary RepairComponents + { + get + { + if (_repairComponents == null) + { + _repairComponents = Components.ToDictionary(RepComp => RepComp.ComponentId, RepComp => + (RepComp.Component as IComponentModel, RepComp.Count)); + } + + return _repairComponents; + } + } + + [ForeignKey("RepairId")] + public virtual List Components { get; set; } = new(); + + [ForeignKey("RepairId")] + public virtual List Orders { get; set; } = new(); + + public static Repair Create(AutoWorkshopDatabase Context, RepairBindingModel Model) + { + return new Repair() + { + Id = Model.Id, + RepairName = Model.RepairName, + Price = Model.Price, + Components = Model.RepairComponents.Select(x => new RepairComponent + { + Component = Context.Components.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + + public void Update(RepairBindingModel Model) + { + RepairName = Model.RepairName; + Price = Model.Price; + } + + public RepairViewModel GetViewModel => new() + { + Id = Id, + RepairName = RepairName, + Price = Price, + RepairComponents = RepairComponents + }; + + public void UpdateComponents(AutoWorkshopDatabase Context, RepairBindingModel Model) + { + var RepairComponents = Context.RepairComponents.Where(rec => rec.RepairId == Model.Id).ToList(); + + if (RepairComponents != null && RepairComponents.Count > 0) + { + Context.RepairComponents.RemoveRange(RepairComponents.Where(rec => !Model.RepairComponents.ContainsKey(rec.ComponentId))); + Context.SaveChanges(); + + foreach (var ComponentToUpdate in RepairComponents) + { + ComponentToUpdate.Count = Model.RepairComponents[ComponentToUpdate.ComponentId].Item2; + Model.RepairComponents.Remove(ComponentToUpdate.ComponentId); + } + + Context.SaveChanges(); + } + + var Repair = Context.Repairs.First(x => x.Id == Id); + + foreach (var RepairComponent in Model.RepairComponents) + { + Context.RepairComponents.Add(new RepairComponent + { + Repair = Repair, + Component = Context.Components.First(x => x.Id == RepairComponent.Key), + Count = RepairComponent.Value.Item2 + }); + + Context.SaveChanges(); + } + + _repairComponents = null; + } + } +} diff --git a/AutoWorkshopDatabaseImplement/Models/RepairComponent.cs b/AutoWorkshopDatabaseImplement/Models/RepairComponent.cs new file mode 100644 index 0000000..0dcfd38 --- /dev/null +++ b/AutoWorkshopDatabaseImplement/Models/RepairComponent.cs @@ -0,0 +1,22 @@ +using System.ComponentModel.DataAnnotations; + +namespace AutoWorkshopDatabaseImplement.Models +{ + public class RepairComponent + { + public int Id { get; set; } + + [Required] + public int RepairId { get; set; } + + [Required] + public int ComponentId { get; set; } + + [Required] + public int Count { get; set; } + + public virtual Component Component { get; set; } = new(); + + public virtual Repair Repair { get; set; } = new(); + } +} diff --git a/AutoWorkshopFileImplement/AutoWorkshopFileImplement.csproj b/AutoWorkshopFileImplement/AutoWorkshopFileImplement.csproj index 06be0dc..6deaaf6 100644 --- a/AutoWorkshopFileImplement/AutoWorkshopFileImplement.csproj +++ b/AutoWorkshopFileImplement/AutoWorkshopFileImplement.csproj @@ -1,7 +1,7 @@ - + - net8.0 + net6.0 enable enable diff --git a/AutoWorkshopImplement/AutoWorkshopListImplement.csproj b/AutoWorkshopImplement/AutoWorkshopListImplement.csproj index 06be0dc..6deaaf6 100644 --- a/AutoWorkshopImplement/AutoWorkshopListImplement.csproj +++ b/AutoWorkshopImplement/AutoWorkshopListImplement.csproj @@ -1,7 +1,7 @@ - + - net8.0 + net6.0 enable enable diff --git a/AutoWorkshopView/AutoWorkshopView.csproj b/AutoWorkshopView/AutoWorkshopView.csproj index b76dcff..7012f26 100644 --- a/AutoWorkshopView/AutoWorkshopView.csproj +++ b/AutoWorkshopView/AutoWorkshopView.csproj @@ -10,8 +10,11 @@ - - + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/AutoWorkshopView/MainForm.Designer.cs b/AutoWorkshopView/MainForm.Designer.cs index bf9bc04..17becca 100644 --- a/AutoWorkshopView/MainForm.Designer.cs +++ b/AutoWorkshopView/MainForm.Designer.cs @@ -49,7 +49,7 @@ MenuStrip.Location = new Point(0, 0); MenuStrip.Name = "MenuStrip"; MenuStrip.Padding = new Padding(5, 2, 0, 2); - MenuStrip.Size = new Size(947, 24); + MenuStrip.Size = new Size(1134, 24); MenuStrip.TabIndex = 0; MenuStrip.Text = "TopMenuStrip"; // @@ -81,15 +81,15 @@ DataGridView.Margin = new Padding(3, 2, 3, 2); DataGridView.Name = "DataGridView"; DataGridView.RowHeadersWidth = 51; - DataGridView.Size = new Size(770, 305); + DataGridView.Size = new Size(881, 305); DataGridView.TabIndex = 1; // // CreateOrderButton // - CreateOrderButton.Location = new Point(786, 23); + CreateOrderButton.Location = new Point(897, 23); CreateOrderButton.Margin = new Padding(3, 2, 3, 2); CreateOrderButton.Name = "CreateOrderButton"; - CreateOrderButton.Size = new Size(157, 30); + CreateOrderButton.Size = new Size(227, 30); CreateOrderButton.TabIndex = 2; CreateOrderButton.Text = "Создать заказ"; CreateOrderButton.UseVisualStyleBackColor = true; @@ -97,10 +97,10 @@ // // TakeInWorkButton // - TakeInWorkButton.Location = new Point(786, 57); + TakeInWorkButton.Location = new Point(897, 57); TakeInWorkButton.Margin = new Padding(3, 2, 3, 2); TakeInWorkButton.Name = "TakeInWorkButton"; - TakeInWorkButton.Size = new Size(157, 30); + TakeInWorkButton.Size = new Size(227, 30); TakeInWorkButton.TabIndex = 3; TakeInWorkButton.Text = "Отдать заказ в работу"; TakeInWorkButton.UseVisualStyleBackColor = true; @@ -108,10 +108,10 @@ // // ReadyButton // - ReadyButton.Location = new Point(786, 91); + ReadyButton.Location = new Point(897, 91); ReadyButton.Margin = new Padding(3, 2, 3, 2); ReadyButton.Name = "ReadyButton"; - ReadyButton.Size = new Size(157, 30); + ReadyButton.Size = new Size(227, 30); ReadyButton.TabIndex = 4; ReadyButton.Text = "Заказ готов"; ReadyButton.UseVisualStyleBackColor = true; @@ -119,10 +119,10 @@ // // IssuedButton // - IssuedButton.Location = new Point(786, 125); + IssuedButton.Location = new Point(897, 125); IssuedButton.Margin = new Padding(3, 2, 3, 2); IssuedButton.Name = "IssuedButton"; - IssuedButton.Size = new Size(157, 30); + IssuedButton.Size = new Size(227, 30); IssuedButton.TabIndex = 5; IssuedButton.Text = "Заказ выдан"; IssuedButton.UseVisualStyleBackColor = true; @@ -130,10 +130,10 @@ // // RefreshButton // - RefreshButton.Location = new Point(786, 159); + RefreshButton.Location = new Point(897, 159); RefreshButton.Margin = new Padding(3, 2, 3, 2); RefreshButton.Name = "RefreshButton"; - RefreshButton.Size = new Size(157, 30); + RefreshButton.Size = new Size(227, 30); RefreshButton.TabIndex = 6; RefreshButton.Text = "Обновить"; RefreshButton.UseVisualStyleBackColor = true; @@ -143,7 +143,7 @@ // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(947, 338); + ClientSize = new Size(1134, 338); Controls.Add(RefreshButton); Controls.Add(IssuedButton); Controls.Add(ReadyButton); diff --git a/AutoWorkshopView/Program.cs b/AutoWorkshopView/Program.cs index bad6fb6..9468cce 100644 --- a/AutoWorkshopView/Program.cs +++ b/AutoWorkshopView/Program.cs @@ -1,7 +1,7 @@ using AutoWorkshopBusinessLogic.BusinessLogics; using AutoWorkshopContracts.BusinessLogicContracts; using AutoWorkshopContracts.StoragesContracts; -using AutoWorkshopFileImplement.Implements; +using AutoWorkshopDatabaseImplement.Implements; using AutoWorkshopView.Forms; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; -- 2.25.1