diff --git a/RenovationWork.sln b/RenovationWork.sln index 03fbac7..25cd6c5 100644 --- a/RenovationWork.sln +++ b/RenovationWork.sln @@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RenovationWorkBusinessLogic EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RenovationWorkListImplement", "RenovationWorkListImplement\RenovationWorkListImplement.csproj", "{5F454425-8555-4E69-A0EC-0EEC94309A06}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RenovationWorkFileImplement", "RenovationWorkFileImplement\RenovationWorkFileImplement.csproj", "{F50D3080-2C2A-4189-AECA-11FA748D9C6E}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RenovationWorkFileImplement", "RenovationWorkFileImplement\RenovationWorkFileImplement.csproj", "{F50D3080-2C2A-4189-AECA-11FA748D9C6E}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RenovationWorkDatabaseImplement", "RenovationWorkDatabaseImplement\RenovationWorkDatabaseImplement.csproj", "{4292A786-31D0-464B-BEA5-6261659330A0}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -45,6 +47,10 @@ Global {F50D3080-2C2A-4189-AECA-11FA748D9C6E}.Debug|Any CPU.Build.0 = Debug|Any CPU {F50D3080-2C2A-4189-AECA-11FA748D9C6E}.Release|Any CPU.ActiveCfg = Release|Any CPU {F50D3080-2C2A-4189-AECA-11FA748D9C6E}.Release|Any CPU.Build.0 = Release|Any CPU + {4292A786-31D0-464B-BEA5-6261659330A0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4292A786-31D0-464B-BEA5-6261659330A0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4292A786-31D0-464B-BEA5-6261659330A0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4292A786-31D0-464B-BEA5-6261659330A0}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/RenovationWork/FormRepairs.Designer.cs b/RenovationWork/FormRepairs.Designer.cs index 638fec6..34d9422 100644 --- a/RenovationWork/FormRepairs.Designer.cs +++ b/RenovationWork/FormRepairs.Designer.cs @@ -28,79 +28,83 @@ /// private void InitializeComponent() { - dataGridView = new DataGridView(); - buttonAdd = new Button(); - buttonUpd = new Button(); - buttonDel = new Button(); - buttonRef = new Button(); - ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); - SuspendLayout(); + this.dataGridView = new System.Windows.Forms.DataGridView(); + this.buttonAdd = new System.Windows.Forms.Button(); + this.buttonUpd = new System.Windows.Forms.Button(); + this.buttonDel = new System.Windows.Forms.Button(); + this.buttonRef = new System.Windows.Forms.Button(); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); + this.SuspendLayout(); // // dataGridView // - dataGridView.BackgroundColor = SystemColors.ControlLightLight; - dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; - dataGridView.Location = new Point(2, 2); - dataGridView.Name = "dataGridView"; - dataGridView.RowTemplate.Height = 25; - dataGridView.Size = new Size(553, 444); - dataGridView.TabIndex = 0; + this.dataGridView.BackgroundColor = System.Drawing.SystemColors.ControlLightLight; + this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.dataGridView.Location = new System.Drawing.Point(2, 3); + this.dataGridView.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.dataGridView.Name = "dataGridView"; + this.dataGridView.RowHeadersWidth = 51; + this.dataGridView.RowTemplate.Height = 25; + this.dataGridView.Size = new System.Drawing.Size(632, 592); + this.dataGridView.TabIndex = 0; // // buttonAdd // - buttonAdd.Location = new Point(561, 12); - buttonAdd.Name = "buttonAdd"; - buttonAdd.Size = new Size(98, 23); - buttonAdd.TabIndex = 1; - buttonAdd.Text = "Добавить"; - buttonAdd.UseVisualStyleBackColor = true; - buttonAdd.Click += buttonAdd_Click; + this.buttonAdd.Location = new System.Drawing.Point(641, 16); + this.buttonAdd.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.buttonAdd.Name = "buttonAdd"; + this.buttonAdd.Size = new System.Drawing.Size(112, 31); + this.buttonAdd.TabIndex = 1; + this.buttonAdd.Text = "Добавить"; + this.buttonAdd.UseVisualStyleBackColor = true; + this.buttonAdd.Click += new System.EventHandler(this.buttonAdd_Click); // // buttonUpd // - buttonUpd.Location = new Point(561, 41); - buttonUpd.Name = "buttonUpd"; - buttonUpd.Size = new Size(98, 23); - buttonUpd.TabIndex = 2; - buttonUpd.Text = "Изменить"; - buttonUpd.UseVisualStyleBackColor = true; - buttonUpd.Click += buttonUpd_Click; + this.buttonUpd.Location = new System.Drawing.Point(641, 55); + this.buttonUpd.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.buttonUpd.Name = "buttonUpd"; + this.buttonUpd.Size = new System.Drawing.Size(112, 31); + this.buttonUpd.TabIndex = 2; + this.buttonUpd.Text = "Изменить"; + this.buttonUpd.UseVisualStyleBackColor = true; // // buttonDel // - buttonDel.Location = new Point(561, 70); - buttonDel.Name = "buttonDel"; - buttonDel.Size = new Size(98, 23); - buttonDel.TabIndex = 3; - buttonDel.Text = "Удалить"; - buttonDel.UseVisualStyleBackColor = true; - buttonDel.Click += buttonDel_Click; + this.buttonDel.Location = new System.Drawing.Point(641, 93); + this.buttonDel.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.buttonDel.Name = "buttonDel"; + this.buttonDel.Size = new System.Drawing.Size(112, 31); + this.buttonDel.TabIndex = 3; + this.buttonDel.Text = "Удалить"; + this.buttonDel.UseVisualStyleBackColor = true; // // buttonRef // - buttonRef.Location = new Point(561, 99); - buttonRef.Name = "buttonRef"; - buttonRef.Size = new Size(98, 23); - buttonRef.TabIndex = 4; - buttonRef.Text = "Обновить"; - buttonRef.UseVisualStyleBackColor = true; - buttonRef.Click += buttonRef_Click; + this.buttonRef.Location = new System.Drawing.Point(641, 132); + this.buttonRef.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.buttonRef.Name = "buttonRef"; + this.buttonRef.Size = new System.Drawing.Size(112, 31); + this.buttonRef.TabIndex = 4; + this.buttonRef.Text = "Обновить"; + this.buttonRef.UseVisualStyleBackColor = true; // // FormRepairs // - AutoScaleDimensions = new SizeF(7F, 15F); - AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(671, 448); - Controls.Add(buttonRef); - Controls.Add(buttonDel); - Controls.Add(buttonUpd); - Controls.Add(buttonAdd); - Controls.Add(dataGridView); - Name = "FormRepairs"; - Text = "Список работ"; - Load += FormRepairs_Load; - ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); - ResumeLayout(false); + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(767, 597); + this.Controls.Add(this.buttonRef); + this.Controls.Add(this.buttonDel); + this.Controls.Add(this.buttonUpd); + this.Controls.Add(this.buttonAdd); + this.Controls.Add(this.dataGridView); + this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.Name = "FormRepairs"; + this.Text = "Список работ"; + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); + this.ResumeLayout(false); + } #endregion diff --git a/RenovationWork/FormRepairs.resx b/RenovationWork/FormRepairs.resx index af32865..f298a7b 100644 --- a/RenovationWork/FormRepairs.resx +++ b/RenovationWork/FormRepairs.resx @@ -1,64 +1,4 @@ - - - + diff --git a/RenovationWork/Program.cs b/RenovationWork/Program.cs index c90ed8a..6bfba32 100644 --- a/RenovationWork/Program.cs +++ b/RenovationWork/Program.cs @@ -1,7 +1,7 @@ using RenovationWorkBusinessLogic.BusinessLogics; using RenovationWorkContracts.BusinessLogicsContracts; using RenovationWorkContracts.StoragesContracts; -using RenovationWorkFileImplement.Implements; +using RenovationWorkDatabaseImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; diff --git a/RenovationWork/Properties/Settings.Designer.cs b/RenovationWork/Properties/Settings.Designer.cs new file mode 100644 index 0000000..54a331a --- /dev/null +++ b/RenovationWork/Properties/Settings.Designer.cs @@ -0,0 +1,26 @@ +//------------------------------------------------------------------------------ +// +// Этот код создан программой. +// Исполняемая версия:4.0.30319.42000 +// +// Изменения в этом файле могут привести к неправильной работе и будут потеряны в случае +// повторной генерации кода. +// +//------------------------------------------------------------------------------ + +namespace RenovationWorkView.Properties { + + + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "17.3.0.0")] + internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { + + private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); + + public static Settings Default { + get { + return defaultInstance; + } + } + } +} diff --git a/RenovationWork/Properties/Settings.settings b/RenovationWork/Properties/Settings.settings new file mode 100644 index 0000000..049245f --- /dev/null +++ b/RenovationWork/Properties/Settings.settings @@ -0,0 +1,6 @@ + + + + + + diff --git a/RenovationWork/RenovationWorkView.csproj b/RenovationWork/RenovationWorkView.csproj index 9816f97..1f32740 100644 --- a/RenovationWork/RenovationWorkView.csproj +++ b/RenovationWork/RenovationWorkView.csproj @@ -9,6 +9,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -19,6 +23,7 @@ + diff --git a/RenovationWorkBusinessLogic/BusinessLogics/RepairLogic.cs b/RenovationWorkBusinessLogic/BusinessLogics/RepairLogic.cs index 3301975..bb0fc84 100644 --- a/RenovationWorkBusinessLogic/BusinessLogics/RepairLogic.cs +++ b/RenovationWorkBusinessLogic/BusinessLogics/RepairLogic.cs @@ -92,11 +92,11 @@ namespace RenovationWorkBusinessLogic.BusinessLogics } if (string.IsNullOrEmpty(model.RepairName)) { - throw new ArgumentNullException("Нет названия компьютера", nameof(model.RepairName)); + throw new ArgumentNullException("Нет названия ремонта", nameof(model.RepairName)); } if (model.Price <= 0) { - throw new ArgumentNullException("Цена компьютера должна быть больше 0", nameof(model.Price)); + throw new ArgumentNullException("Цена ремонта должна быть больше 0", nameof(model.Price)); } _logger.LogInformation("Repair. RepairName:{RepairName}.Price:{ Price}. Id: { Id} ", model.RepairName, model.Price, model.Id); var element = _repairStorage.GetElement(new RepairSearchModel @@ -105,7 +105,7 @@ namespace RenovationWorkBusinessLogic.BusinessLogics }); if (element != null && element.Id != model.Id) { - throw new InvalidOperationException("Компьютер с таким названием уже есть"); + throw new InvalidOperationException("Ремонт с таким названием уже есть"); } } } diff --git a/RenovationWorkDatabaseImplement/Implements/ComponentStorage.cs b/RenovationWorkDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..b740ec0 --- /dev/null +++ b/RenovationWorkDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,84 @@ +using RenovationWorkContracts.BindingModels; +using RenovationWorkContracts.SearchModels; +using RenovationWorkContracts.StoragesContracts; +using RenovationWorkContracts.ViewModels; +using RenovationWorkDatabaseImplement; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RenovationWorkDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new RenovationWorkDatabase(); + return context.Components.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + using var context = new RenovationWorkDatabase(); + 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 RenovationWorkDatabase(); + 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 RenovationWorkDatabase(); + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new RenovationWorkDatabase(); + 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 RenovationWorkDatabase(); + var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Components.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/RenovationWorkDatabaseImplement/Implements/OrderStorage.cs b/RenovationWorkDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..df37477 --- /dev/null +++ b/RenovationWorkDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,84 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using RenovationWorkContracts.BindingModels; +using RenovationWorkContracts.SearchModels; +using RenovationWorkContracts.StoragesContracts; +using RenovationWorkContracts.ViewModels; +using RenovationWorkDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace RenovationWorkDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public List GetFullList() + { + using var context = new RenovationWorkDatabase(); + 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 RenovationWorkDatabase(); + 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 new(); + } + using var context = new RenovationWorkDatabase(); + return context.Orders.Include(x => x.Repair).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + using var context = new RenovationWorkDatabase(); + if (model == null) + return null; + var newOrder = Order.Create(context, model); + if (newOrder == null) + { + return null; + } + context.Orders.Add(newOrder); + context.SaveChanges(); + return newOrder.GetViewModel; + } + + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new RenovationWorkDatabase(); + var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + context.SaveChanges(); + return order.GetViewModel; + } + + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new RenovationWorkDatabase(); + var order = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + if (order != null) + { + context.Orders.Remove(order); + context.SaveChanges(); + return order.GetViewModel; + } + return null; + } + } +} diff --git a/RenovationWorkDatabaseImplement/Implements/RepairStorage.cs b/RenovationWorkDatabaseImplement/Implements/RepairStorage.cs new file mode 100644 index 0000000..df1a56c --- /dev/null +++ b/RenovationWorkDatabaseImplement/Implements/RepairStorage.cs @@ -0,0 +1,100 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using RenovationWorkContracts.BindingModels; +using RenovationWorkContracts.SearchModels; +using RenovationWorkContracts.StoragesContracts; +using RenovationWorkContracts.ViewModels; +using RenovationWorkDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + + +namespace RenovationWorkDatabaseImplement.Implements +{ + public class RepairStorage : IRepairStorage + { + public List GetFullList() + { + using var context = new RenovationWorkDatabase(); + 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 RenovationWorkDatabase(); + 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 RenovationWorkDatabase(); + 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 RenovationWorkDatabase(); + 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 RenovationWorkDatabase(); + 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 RenovationWorkDatabase(); + var element = context.Repairs.Include(x => x.Components).FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Repairs.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/RenovationWorkDatabaseImplement/Migrations/20240402112947_InitialCreate.Designer.cs b/RenovationWorkDatabaseImplement/Migrations/20240402112947_InitialCreate.Designer.cs new file mode 100644 index 0000000..c801571 --- /dev/null +++ b/RenovationWorkDatabaseImplement/Migrations/20240402112947_InitialCreate.Designer.cs @@ -0,0 +1,171 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using RenovationWorkDatabaseImplement; + +#nullable disable + +namespace RenovationWorkDatabaseImplement_.Migrations +{ + [DbContext(typeof(RenovationWorkDatabase))] + [Migration("20240402112947_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.17") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("RepairId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("RepairId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Models.Repair", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Price") + .HasColumnType("float"); + + b.Property("RepairName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Repairs"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Models.RepairComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("RepairId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("RepairId"); + + b.ToTable("RepairComponents"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Models.Order", b => + { + b.HasOne("RenovationWorkDatabaseImplement.Models.Repair", "Repair") + .WithMany("Orders") + .HasForeignKey("RepairId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Repair"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Models.RepairComponent", b => + { + b.HasOne("RenovationWorkDatabaseImplement.Component", "Component") + .WithMany("RepairComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("RenovationWorkDatabaseImplement.Models.Repair", "Repair") + .WithMany("Components") + .HasForeignKey("RepairId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Repair"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Component", b => + { + b.Navigation("RepairComponents"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Models.Repair", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/RenovationWorkDatabaseImplement/Migrations/20240402112947_InitialCreate.cs b/RenovationWorkDatabaseImplement/Migrations/20240402112947_InitialCreate.cs new file mode 100644 index 0000000..c8a0ccb --- /dev/null +++ b/RenovationWorkDatabaseImplement/Migrations/20240402112947_InitialCreate.cs @@ -0,0 +1,125 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace RenovationWorkDatabaseImplement_.Migrations +{ + /// + public partial class InitialCreate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Components", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ComponentName = table.Column(type: "nvarchar(max)", nullable: false), + Cost = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Components", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Repairs", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + RepairName = table.Column(type: "nvarchar(max)", nullable: false), + Price = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Repairs", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + RepairId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false), + Sum = table.Column(type: "float", nullable: false), + Status = table.Column(type: "int", nullable: false), + DateCreate = table.Column(type: "datetime2", nullable: false), + DateImplement = table.Column(type: "datetime2", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Orders", x => x.Id); + table.ForeignKey( + name: "FK_Orders_Repairs_RepairId", + column: x => x.RepairId, + principalTable: "Repairs", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "RepairComponents", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + RepairId = table.Column(type: "int", nullable: false), + ComponentId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", 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/RenovationWorkDatabaseImplement/Migrations/RenovationWorkDatabaseModelSnapshot.cs b/RenovationWorkDatabaseImplement/Migrations/RenovationWorkDatabaseModelSnapshot.cs new file mode 100644 index 0000000..1b8b353 --- /dev/null +++ b/RenovationWorkDatabaseImplement/Migrations/RenovationWorkDatabaseModelSnapshot.cs @@ -0,0 +1,168 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using RenovationWorkDatabaseImplement; + +#nullable disable + +namespace RenovationWorkDatabaseImplement_.Migrations +{ + [DbContext(typeof(RenovationWorkDatabase))] + partial class RenovationWorkDatabaseModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.17") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("RepairId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("RepairId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Models.Repair", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Price") + .HasColumnType("float"); + + b.Property("RepairName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Repairs"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Models.RepairComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("RepairId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("RepairId"); + + b.ToTable("RepairComponents"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Models.Order", b => + { + b.HasOne("RenovationWorkDatabaseImplement.Models.Repair", "Repair") + .WithMany("Orders") + .HasForeignKey("RepairId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Repair"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Models.RepairComponent", b => + { + b.HasOne("RenovationWorkDatabaseImplement.Component", "Component") + .WithMany("RepairComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("RenovationWorkDatabaseImplement.Models.Repair", "Repair") + .WithMany("Components") + .HasForeignKey("RepairId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Repair"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Component", b => + { + b.Navigation("RepairComponents"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Models.Repair", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/RenovationWorkDatabaseImplement/Models/Component.cs b/RenovationWorkDatabaseImplement/Models/Component.cs new file mode 100644 index 0000000..b1e7dbd --- /dev/null +++ b/RenovationWorkDatabaseImplement/Models/Component.cs @@ -0,0 +1,64 @@ +using System; +using RenovationWorkContracts.BindingModels; +using RenovationWorkContracts.ViewModels; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using RenovationWorkDataModels.Models; +using RenovationWorkDatabaseImplement.Models; +using System.Collections.Generic; + +namespace RenovationWorkDatabaseImplement +{ + 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/RenovationWorkDatabaseImplement/Models/Order.cs b/RenovationWorkDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..ccefca9 --- /dev/null +++ b/RenovationWorkDatabaseImplement/Models/Order.cs @@ -0,0 +1,74 @@ +using RenovationWorkContracts.BindingModels; +using RenovationWorkContracts.ViewModels; +using RenovationWorkDataModels.Enums; +using RenovationWorkDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RenovationWorkDatabaseImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + + [Required] + public int RepairId { get; private set; } + + public virtual Repair Repair { get; set; } = new(); + + [Required] + public int Count { get; private set; } + + [Required] + public double Sum { get; private set; } + + [Required] + public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; + + [Required] + public DateTime DateCreate { get; private set; } = DateTime.Now; + + public DateTime? DateImplement { get; private set; } + + public static Order Create(RenovationWorkDatabase context, OrderBindingModel model) + { + return new Order() + { + Id = model.Id, + RepairId = model.RepairId, + Repair = context.Repairs.First(x => x.Id == model.RepairId), + 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, + RepairId = RepairId, + RepairName = Repair.RepairName, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + }; + } +} \ No newline at end of file diff --git a/RenovationWorkDatabaseImplement/Models/Repair.cs b/RenovationWorkDatabaseImplement/Models/Repair.cs new file mode 100644 index 0000000..49ee272 --- /dev/null +++ b/RenovationWorkDatabaseImplement/Models/Repair.cs @@ -0,0 +1,102 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using RenovationWorkContracts.BindingModels; +using RenovationWorkContracts.ViewModels; +using RenovationWorkDataModels.Models; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; + +namespace RenovationWorkDatabaseImplement.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(recPC => recPC.ComponentId, recPC => + (recPC.Component as IComponentModel, recPC.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(RenovationWorkDatabase 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(RenovationWorkDatabase 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 updateComponent in repairComponents) + { + updateComponent.Count = model.RepairComponents[updateComponent.ComponentId].Item2; + model.RepairComponents.Remove(updateComponent.ComponentId); + } + context.SaveChanges(); + } + + var repair = context.Repairs.First(x => x.Id == Id); + foreach (var pc in model.RepairComponents) + { + context.RepairComponents.Add(new RepairComponent + { + Repair = repair, + Component = context.Components.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _repairComponents = null; + } + } +} + diff --git a/RenovationWorkDatabaseImplement/Models/RepairComponent.cs b/RenovationWorkDatabaseImplement/Models/RepairComponent.cs new file mode 100644 index 0000000..2684586 --- /dev/null +++ b/RenovationWorkDatabaseImplement/Models/RepairComponent.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.ComponentModel.DataAnnotations; + +namespace RenovationWorkDatabaseImplement.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/RenovationWorkDatabaseImplement/RenovationWorkDatabase.cs b/RenovationWorkDatabaseImplement/RenovationWorkDatabase.cs new file mode 100644 index 0000000..b69891d --- /dev/null +++ b/RenovationWorkDatabaseImplement/RenovationWorkDatabase.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using RenovationWorkDatabaseImplement.Models; + +namespace RenovationWorkDatabaseImplement +{ + public class RenovationWorkDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (!optionsBuilder.IsConfigured) + { + optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=RenovationWorkDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + 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/RenovationWorkDatabaseImplement/RenovationWorkDatabaseImplement.csproj b/RenovationWorkDatabaseImplement/RenovationWorkDatabaseImplement.csproj new file mode 100644 index 0000000..3657dad --- /dev/null +++ b/RenovationWorkDatabaseImplement/RenovationWorkDatabaseImplement.csproj @@ -0,0 +1,24 @@ + + + + net6.0-windows + RenovationWorkDatabaseImplement_ + enable + true + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + +