From 7bdb87da79afd92524730376ab8d6f05ad26b377 Mon Sep 17 00:00:00 2001 From: bulatova_karina Date: Tue, 26 Mar 2024 15:15:35 +0400 Subject: [PATCH 1/3] lab3 maybe full --- ComputersShop/ComputersShop.sln | 8 +- .../ComputerShopDatabase.cs | 28 +++ .../ComputersShopDatabaseImplement.csproj | 23 +++ .../Implements/ComponentStorage.cs | 78 ++++++++ .../Implements/ComputerStorage.cs | 94 ++++++++++ .../Implements/OrderStorage.cs | 78 ++++++++ .../20240326105531_InitialCreate.Designer.cs | 171 ++++++++++++++++++ .../20240326105531_InitialCreate.cs | 125 +++++++++++++ .../ComputersShopDatabaseModelSnapshot.cs | 168 +++++++++++++++++ .../Models/Component.cs | 61 +++++++ .../Models/Computer.cs | 94 ++++++++++ .../Models/ComputerComponent.cs | 23 +++ .../Models/Order.cs | 66 +++++++ .../ComputersShopView.csproj | 5 + 14 files changed, 1021 insertions(+), 1 deletion(-) create mode 100644 ComputersShop/ComputersShopDatabaseImplement/ComputerShopDatabase.cs create mode 100644 ComputersShop/ComputersShopDatabaseImplement/ComputersShopDatabaseImplement.csproj create mode 100644 ComputersShop/ComputersShopDatabaseImplement/Implements/ComponentStorage.cs create mode 100644 ComputersShop/ComputersShopDatabaseImplement/Implements/ComputerStorage.cs create mode 100644 ComputersShop/ComputersShopDatabaseImplement/Implements/OrderStorage.cs create mode 100644 ComputersShop/ComputersShopDatabaseImplement/Migrations/20240326105531_InitialCreate.Designer.cs create mode 100644 ComputersShop/ComputersShopDatabaseImplement/Migrations/20240326105531_InitialCreate.cs create mode 100644 ComputersShop/ComputersShopDatabaseImplement/Migrations/ComputersShopDatabaseModelSnapshot.cs create mode 100644 ComputersShop/ComputersShopDatabaseImplement/Models/Component.cs create mode 100644 ComputersShop/ComputersShopDatabaseImplement/Models/Computer.cs create mode 100644 ComputersShop/ComputersShopDatabaseImplement/Models/ComputerComponent.cs create mode 100644 ComputersShop/ComputersShopDatabaseImplement/Models/Order.cs diff --git a/ComputersShop/ComputersShop.sln b/ComputersShop/ComputersShop.sln index ab0a8d0..416ea04 100644 --- a/ComputersShop/ComputersShop.sln +++ b/ComputersShop/ComputersShop.sln @@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputersShopListImplement" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputersShopBusinessLogic", "ComputersShopBusinessLogic\ComputersShopBusinessLogic.csproj", "{E8E8A4F7-E499-48CB-B3FB-25DCF234DC7F}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputersShopFileImplement", "ComputersShopFileImplement\ComputersShopFileImplement.csproj", "{13634451-A24C-49E1-9558-E63566167957}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputersShopFileImplement", "ComputersShopFileImplement\ComputersShopFileImplement.csproj", "{13634451-A24C-49E1-9558-E63566167957}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputersShopDatabaseImplement", "ComputersShopDatabaseImplement\ComputersShopDatabaseImplement.csproj", "{EC5789DC-656D-457F-B6A0-702CCE7EE688}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -45,6 +47,10 @@ Global {13634451-A24C-49E1-9558-E63566167957}.Debug|Any CPU.Build.0 = Debug|Any CPU {13634451-A24C-49E1-9558-E63566167957}.Release|Any CPU.ActiveCfg = Release|Any CPU {13634451-A24C-49E1-9558-E63566167957}.Release|Any CPU.Build.0 = Release|Any CPU + {EC5789DC-656D-457F-B6A0-702CCE7EE688}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EC5789DC-656D-457F-B6A0-702CCE7EE688}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EC5789DC-656D-457F-B6A0-702CCE7EE688}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EC5789DC-656D-457F-B6A0-702CCE7EE688}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/ComputersShop/ComputersShopDatabaseImplement/ComputerShopDatabase.cs b/ComputersShop/ComputersShopDatabaseImplement/ComputerShopDatabase.cs new file mode 100644 index 0000000..530c7a3 --- /dev/null +++ b/ComputersShop/ComputersShopDatabaseImplement/ComputerShopDatabase.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ComputersShopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + + +namespace ComputersShopDatabaseImplement +{ + public class ComputersShopDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-1DE5E8N\SQLEXPRESS;Initial Catalog=ComputersShopDatabaseFull; + Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Components { set; get; } + public virtual DbSet Computers { set; get; } + public virtual DbSet ComputerComponents { set; get; } + public virtual DbSet Orders { set; get; } + } +} diff --git a/ComputersShop/ComputersShopDatabaseImplement/ComputersShopDatabaseImplement.csproj b/ComputersShop/ComputersShopDatabaseImplement/ComputersShopDatabaseImplement.csproj new file mode 100644 index 0000000..dec845d --- /dev/null +++ b/ComputersShop/ComputersShopDatabaseImplement/ComputersShopDatabaseImplement.csproj @@ -0,0 +1,23 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/ComputersShop/ComputersShopDatabaseImplement/Implements/ComponentStorage.cs b/ComputersShop/ComputersShopDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..efafb2a --- /dev/null +++ b/ComputersShop/ComputersShopDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,78 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.SearchModels; +using ComputersShopContracts.StoragesContracts; +using ComputersShopContracts.ViewModels; +using ComputersShopDatabaseImplement.Models; + +namespace ComputersShopDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new ComputersShopDatabase(); + return context.Components.Select(x => x.GetViewModel).ToList(); + } + public List GetFiltredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + using var context = new ComputersShopDatabase(); + 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 ComputersShopDatabase(); + 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 ComputersShopDatabase(); + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new ComputersShopDatabase(); + 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 ComputersShopDatabase(); + 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/ComputersShop/ComputersShopDatabaseImplement/Implements/ComputerStorage.cs b/ComputersShop/ComputersShopDatabaseImplement/Implements/ComputerStorage.cs new file mode 100644 index 0000000..16e79af --- /dev/null +++ b/ComputersShop/ComputersShopDatabaseImplement/Implements/ComputerStorage.cs @@ -0,0 +1,94 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ComputersShopContracts.StoragesContracts; +using ComputersShopContracts.SearchModels; +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.ViewModels; +using ComputersShopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace ComputersShopDatabaseImplement.Implements +{ + public class ComputerStorage : IComputerStorage + { + public List GetFullList() + { + using var context = new ComputersShopDatabase(); + return context.Computers.Include(x => x.Components).ThenInclude(x => x.Component) + .ToList().Select(x => x.GetViewModel).ToList(); + } + public List GetFiltredList(ComputerSearchModel model) + { + if (string.IsNullOrEmpty(model.ComputerName)) + { + return new(); + } + using var context = new ComputersShopDatabase(); + return context.Computers.Include(x => x.Components).ThenInclude(x => x.Component) + .Where(x => x.ComputerName.Contains(model.ComputerName)).ToList() + .Select(x => x.GetViewModel).ToList(); + } + public ComputerViewModel? GetElement(ComputerSearchModel model) + { + if (string.IsNullOrEmpty(model.ComputerName) && !model.Id.HasValue) + { + return null; + } + using var context = new ComputersShopDatabase(); + return context.Computers.Include(x => x.Components).ThenInclude(x => x.Component) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComputerName) && + x.ComputerName == model.ComputerName) || + (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + public ComputerViewModel? Insert(ComputerBindingModel model) + { + using var context = new ComputersShopDatabase(); + var newComputer = Computer.Create(context, model); + if (newComputer == null) + { + return null; + } + context.Computers.Add(newComputer); + context.SaveChanges(); + return newComputer.GetViewModel; + } + public ComputerViewModel? Update(ComputerBindingModel model) + { + using var context = new ComputersShopDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var computer = context.Computers.FirstOrDefault(rec => rec.Id == model.Id); + if (computer == null) + { + return null; + } + computer.Update(model); + context.SaveChanges(); + computer.UpdateComponents(context, model); + transaction.Commit(); + return computer.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public ComputerViewModel? Delete(ComputerBindingModel model) + { + using var context = new ComputersShopDatabase(); + var element = context.Computers.Include(x => x.Components).FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Computers.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/ComputersShop/ComputersShopDatabaseImplement/Implements/OrderStorage.cs b/ComputersShop/ComputersShopDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..998f763 --- /dev/null +++ b/ComputersShop/ComputersShopDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,78 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.SearchModels; +using ComputersShopContracts.StoragesContracts; +using ComputersShopContracts.ViewModels; +using ComputersShopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace ComputersShopDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new ComputersShopDatabase(); + return context.Orders.Include(x => x.Computer).FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + public List GetFiltredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new ComputersShopDatabase(); + return context.Orders.Where(x => x.Id == model.Id).Include(x => x.Computer) + .Select(x => x.GetViewModel).ToList(); + } + public List GetFullList() + { + using var context = new ComputersShopDatabase(); + return context.Orders.Include(x => x.Computer).Select(x => x.GetViewModel).ToList(); + } + public OrderViewModel? Insert(OrderBindingModel model) + { + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + using var context = new ComputersShopDatabase(); + context.Orders.Add(newOrder); + context.SaveChanges(); + return context.Orders.Include(x => x.Computer).FirstOrDefault(x => x.Id == newOrder.Id)?.GetViewModel; + } + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new ComputersShopDatabase(); + 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.Computer).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + } + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new ComputersShopDatabase(); + 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/ComputersShop/ComputersShopDatabaseImplement/Migrations/20240326105531_InitialCreate.Designer.cs b/ComputersShop/ComputersShopDatabaseImplement/Migrations/20240326105531_InitialCreate.Designer.cs new file mode 100644 index 0000000..1ac4371 --- /dev/null +++ b/ComputersShop/ComputersShopDatabaseImplement/Migrations/20240326105531_InitialCreate.Designer.cs @@ -0,0 +1,171 @@ +// +using System; +using ComputersShopDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace ComputersShopDatabaseImplement.Migrations +{ + [DbContext(typeof(ComputersShopDatabase))] + [Migration("20240326105531_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("ComputersShopDatabaseImplement.Models.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("ComputersShopDatabaseImplement.Models.Computer", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComputerName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Computers"); + }); + + modelBuilder.Entity("ComputersShopDatabaseImplement.Models.ComputerComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("ComputerId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("ComputerId"); + + b.ToTable("ComputerComponents"); + }); + + modelBuilder.Entity("ComputersShopDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComputerId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("ComputerId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("ComputersShopDatabaseImplement.Models.ComputerComponent", b => + { + b.HasOne("ComputersShopDatabaseImplement.Models.Component", "Component") + .WithMany("ComputerComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ComputersShopDatabaseImplement.Models.Computer", "Computer") + .WithMany("Components") + .HasForeignKey("ComputerId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Computer"); + }); + + modelBuilder.Entity("ComputersShopDatabaseImplement.Models.Order", b => + { + b.HasOne("ComputersShopDatabaseImplement.Models.Computer", "Computer") + .WithMany("Orders") + .HasForeignKey("ComputerId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Computer"); + }); + + modelBuilder.Entity("ComputersShopDatabaseImplement.Models.Component", b => + { + b.Navigation("ComputerComponents"); + }); + + modelBuilder.Entity("ComputersShopDatabaseImplement.Models.Computer", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/ComputersShop/ComputersShopDatabaseImplement/Migrations/20240326105531_InitialCreate.cs b/ComputersShop/ComputersShopDatabaseImplement/Migrations/20240326105531_InitialCreate.cs new file mode 100644 index 0000000..77f25f2 --- /dev/null +++ b/ComputersShop/ComputersShopDatabaseImplement/Migrations/20240326105531_InitialCreate.cs @@ -0,0 +1,125 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace ComputersShopDatabaseImplement.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: "Computers", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ComputerName = table.Column(type: "nvarchar(max)", nullable: false), + Price = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Computers", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "ComputerComponents", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ComputerId = 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_ComputerComponents", x => x.Id); + table.ForeignKey( + name: "FK_ComputerComponents_Components_ComponentId", + column: x => x.ComponentId, + principalTable: "Components", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_ComputerComponents_Computers_ComputerId", + column: x => x.ComputerId, + principalTable: "Computers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ComputerId = 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_Computers_ComputerId", + column: x => x.ComputerId, + principalTable: "Computers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_ComputerComponents_ComponentId", + table: "ComputerComponents", + column: "ComponentId"); + + migrationBuilder.CreateIndex( + name: "IX_ComputerComponents_ComputerId", + table: "ComputerComponents", + column: "ComputerId"); + + migrationBuilder.CreateIndex( + name: "IX_Orders_ComputerId", + table: "Orders", + column: "ComputerId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "ComputerComponents"); + + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "Components"); + + migrationBuilder.DropTable( + name: "Computers"); + } + } +} diff --git a/ComputersShop/ComputersShopDatabaseImplement/Migrations/ComputersShopDatabaseModelSnapshot.cs b/ComputersShop/ComputersShopDatabaseImplement/Migrations/ComputersShopDatabaseModelSnapshot.cs new file mode 100644 index 0000000..00020d3 --- /dev/null +++ b/ComputersShop/ComputersShopDatabaseImplement/Migrations/ComputersShopDatabaseModelSnapshot.cs @@ -0,0 +1,168 @@ +// +using System; +using ComputersShopDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace ComputersShopDatabaseImplement.Migrations +{ + [DbContext(typeof(ComputersShopDatabase))] + partial class ComputersShopDatabaseModelSnapshot : 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("ComputersShopDatabaseImplement.Models.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("ComputersShopDatabaseImplement.Models.Computer", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComputerName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Computers"); + }); + + modelBuilder.Entity("ComputersShopDatabaseImplement.Models.ComputerComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("ComputerId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("ComputerId"); + + b.ToTable("ComputerComponents"); + }); + + modelBuilder.Entity("ComputersShopDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComputerId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("ComputerId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("ComputersShopDatabaseImplement.Models.ComputerComponent", b => + { + b.HasOne("ComputersShopDatabaseImplement.Models.Component", "Component") + .WithMany("ComputerComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ComputersShopDatabaseImplement.Models.Computer", "Computer") + .WithMany("Components") + .HasForeignKey("ComputerId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Computer"); + }); + + modelBuilder.Entity("ComputersShopDatabaseImplement.Models.Order", b => + { + b.HasOne("ComputersShopDatabaseImplement.Models.Computer", "Computer") + .WithMany("Orders") + .HasForeignKey("ComputerId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Computer"); + }); + + modelBuilder.Entity("ComputersShopDatabaseImplement.Models.Component", b => + { + b.Navigation("ComputerComponents"); + }); + + modelBuilder.Entity("ComputersShopDatabaseImplement.Models.Computer", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/ComputersShop/ComputersShopDatabaseImplement/Models/Component.cs b/ComputersShop/ComputersShopDatabaseImplement/Models/Component.cs new file mode 100644 index 0000000..0f7d27c --- /dev/null +++ b/ComputersShop/ComputersShopDatabaseImplement/Models/Component.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.ViewModels; +using ComputersShopDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace ComputersShopDatabaseImplement.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 ComputerComponents { 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/ComputersShop/ComputersShopDatabaseImplement/Models/Computer.cs b/ComputersShop/ComputersShopDatabaseImplement/Models/Computer.cs new file mode 100644 index 0000000..b6410ad --- /dev/null +++ b/ComputersShop/ComputersShopDatabaseImplement/Models/Computer.cs @@ -0,0 +1,94 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ComputersShopDataModels.Models; +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.ViewModels; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; + +namespace ComputersShopDatabaseImplement.Models +{ + public class Computer : IComputerModel + { + public int Id { get; set; } + [Required] + public string ComputerName { get; set; } = string.Empty; + [Required] + public double Price { get; set; } + private Dictionary? _computerComponents = null; + [NotMapped] + public Dictionary ComputerComponents + { + get + { + if (_computerComponents == null) + { + _computerComponents = Components.ToDictionary(recPC => recPC.ComponentId, recPC => + (recPC.Component as IComponentModel, recPC.Count)); + } + return _computerComponents; + } + } + [ForeignKey("ComputerId")] + public virtual List Components { get; set; } = new(); + [ForeignKey("ComputerId")] + public virtual List Orders { get; set; } = new(); + public static Computer Create(ComputersShopDatabase context, ComputerBindingModel model) + { + return new Computer() + { + Id = model.Id, + ComputerName = model.ComputerName, + Price = model.Price, + Components = model.ComputerComponents.Select(x => new ComputerComponent + { + Component = context.Components.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(ComputerBindingModel model) + { + ComputerName = model.ComputerName; + Price = model.Price; + } + public ComputerViewModel GetViewModel => new() + { + Id = Id, + ComputerName = ComputerName, + Price = Price, + ComputerComponents = ComputerComponents + }; + public void UpdateComponents(ComputersShopDatabase context, ComputerBindingModel model) + { + var computerComponents = context.ComputerComponents.Where(rec => rec.ComputerId == model.Id).ToList(); + if (computerComponents != null && computerComponents.Count > 0) + { // удалили те, которых нет в модели + context.ComputerComponents.RemoveRange(computerComponents.Where(rec => !model.ComputerComponents.ContainsKey(rec.ComponentId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateComponent in computerComponents) + { + updateComponent.Count = model.ComputerComponents[updateComponent.ComponentId].Item2; + model.ComputerComponents.Remove(updateComponent.ComponentId); + } + context.SaveChanges(); + } + var computer = context.Computers.First(x => x.Id == Id); + foreach (var pc in model.ComputerComponents) + { + context.ComputerComponents.Add(new ComputerComponent + { + Computer = computer, + Component = context.Components.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _computerComponents = null; + } + } +} diff --git a/ComputersShop/ComputersShopDatabaseImplement/Models/ComputerComponent.cs b/ComputersShop/ComputersShopDatabaseImplement/Models/ComputerComponent.cs new file mode 100644 index 0000000..7454c5e --- /dev/null +++ b/ComputersShop/ComputersShopDatabaseImplement/Models/ComputerComponent.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopDatabaseImplement.Models +{ + public class ComputerComponent + { + public int Id { get; set; } + [Required] + public int ComputerId { get; set; } + [Required] + public int ComponentId { get; set; } + [Required] + public int Count { get; set; } + public virtual Component Component { get; set; } = new(); + public virtual Computer Computer { get; set; } = new(); + } + +} diff --git a/ComputersShop/ComputersShopDatabaseImplement/Models/Order.cs b/ComputersShop/ComputersShopDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..3fe84c4 --- /dev/null +++ b/ComputersShop/ComputersShopDatabaseImplement/Models/Order.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.ViewModels; +using ComputersShopDataModels.Enums; + +namespace ComputersShopDatabaseImplement.Models +{ + public class Order + { + public int Id { get; private set; } + [Required] + public int ComputerId { 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 Computer Computer { get; private set; } + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order + { + ComputerId = model.ComputerId, + 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() + { + ComputerId = ComputerId, + Count = Count, + Sum = Sum, + DateCreate = DateCreate, + DateImplement = DateImplement, + Id = Id, + Status = Status, + ComputerName = Computer.ComputerName + }; + } +} diff --git a/ComputersShop/ComputersShopView/ComputersShopView.csproj b/ComputersShop/ComputersShopView/ComputersShopView.csproj index d5493b0..5b980ce 100644 --- a/ComputersShop/ComputersShopView/ComputersShopView.csproj +++ b/ComputersShop/ComputersShopView/ComputersShopView.csproj @@ -19,6 +19,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -30,6 +34,7 @@ + -- 2.25.1 From 7ce7fe4c2da886962b618644f6b283777bacd2d3 Mon Sep 17 00:00:00 2001 From: bulatova_karina Date: Tue, 7 May 2024 21:35:09 +0400 Subject: [PATCH 2/3] =?UTF-8?q?=D0=BD=D0=B0=D1=87=D0=B0=D0=BB=D0=BE=20?= =?UTF-8?q?=D1=82=D1=80=D0=B5=D1=82=D1=8C=D0=B5=D0=B9=20=D1=83=D1=81=D0=BB?= =?UTF-8?q?=D0=BE=D0=B6=D0=BD=D0=B5=D0=BD=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ComputerShopDatabase.cs | 2 + .../Implements/ComponentStorage.cs | 34 ++-- .../Implements/ComputerStorage.cs | 41 +++-- .../Implements/OrderStorage.cs | 61 ++++--- .../Implements/ShopStorage.cs | 168 ++++++++++++++++++ .../Models/Shop.cs | 104 +++++++++++ .../Models/ShopComputer.cs | 24 +++ .../Implements/OrderStorage.cs | 6 +- ComputersShop/ComputersShopView/FormSell.cs | 24 +-- 9 files changed, 397 insertions(+), 67 deletions(-) create mode 100644 ComputersShop/ComputersShopDatabaseImplement/Implements/ShopStorage.cs create mode 100644 ComputersShop/ComputersShopDatabaseImplement/Models/Shop.cs create mode 100644 ComputersShop/ComputersShopDatabaseImplement/Models/ShopComputer.cs diff --git a/ComputersShop/ComputersShopDatabaseImplement/ComputerShopDatabase.cs b/ComputersShop/ComputersShopDatabaseImplement/ComputerShopDatabase.cs index 530c7a3..cbf540d 100644 --- a/ComputersShop/ComputersShopDatabaseImplement/ComputerShopDatabase.cs +++ b/ComputersShop/ComputersShopDatabaseImplement/ComputerShopDatabase.cs @@ -24,5 +24,7 @@ namespace ComputersShopDatabaseImplement public virtual DbSet Computers { set; get; } public virtual DbSet ComputerComponents { set; get; } public virtual DbSet Orders { set; get; } + public virtual DbSet Shops { set; get; } + public virtual DbSet ShopComputers { set; get; } } } diff --git a/ComputersShop/ComputersShopDatabaseImplement/Implements/ComponentStorage.cs b/ComputersShop/ComputersShopDatabaseImplement/Implements/ComponentStorage.cs index efafb2a..d8afdd4 100644 --- a/ComputersShop/ComputersShopDatabaseImplement/Implements/ComponentStorage.cs +++ b/ComputersShop/ComputersShopDatabaseImplement/Implements/ComponentStorage.cs @@ -16,17 +16,22 @@ namespace ComputersShopDatabaseImplement.Implements public List GetFullList() { using var context = new ComputersShopDatabase(); - return context.Components.Select(x => x.GetViewModel).ToList(); + return context.Components + .Select(x => x.GetViewModel) + .ToList(); } - public List GetFiltredList(ComponentSearchModel model) + public List GetFilteredList(ComponentSearchModel model) { if (string.IsNullOrEmpty(model.ComponentName)) { return new(); } + using var context = new ComputersShopDatabase(); - return context.Components.Where(x => x.ComponentName.Contains(model.ComponentName)). - Select(x => x.GetViewModel).ToList(); + return context.Components + .Where(x => x.ComponentName.Contains(model.ComponentName)) + .Select(x => x.GetViewModel) + .ToList(); } public ComponentViewModel? GetElement(ComponentSearchModel model) { @@ -34,9 +39,13 @@ namespace ComputersShopDatabaseImplement.Implements { return null; } + using var context = new ComputersShopDatabase(); - return context.Components.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComponentName) - && x.ComponentName == model.ComponentName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + 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) { @@ -45,6 +54,7 @@ namespace ComputersShopDatabaseImplement.Implements { return null; } + using var context = new ComputersShopDatabase(); context.Components.Add(newComponent); context.SaveChanges(); @@ -58,6 +68,7 @@ namespace ComputersShopDatabaseImplement.Implements { return null; } + component.Update(model); context.SaveChanges(); return component.GetViewModel; @@ -66,13 +77,14 @@ namespace ComputersShopDatabaseImplement.Implements { using var context = new ComputersShopDatabase(); var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id); - if (element != null) + if (element == null) { - context.Components.Remove(element); - context.SaveChanges(); - return element.GetViewModel; + return null; } - return null; + + context.Components.Remove(element); + context.SaveChanges(); + return element.GetViewModel; } } } diff --git a/ComputersShop/ComputersShopDatabaseImplement/Implements/ComputerStorage.cs b/ComputersShop/ComputersShopDatabaseImplement/Implements/ComputerStorage.cs index 16e79af..0d56b26 100644 --- a/ComputersShop/ComputersShopDatabaseImplement/Implements/ComputerStorage.cs +++ b/ComputersShop/ComputersShopDatabaseImplement/Implements/ComputerStorage.cs @@ -9,6 +9,7 @@ using ComputersShopContracts.BindingModels; using ComputersShopContracts.ViewModels; using ComputersShopDatabaseImplement.Models; using Microsoft.EntityFrameworkCore; +using System.Numerics; namespace ComputersShopDatabaseImplement.Implements { @@ -17,19 +18,28 @@ namespace ComputersShopDatabaseImplement.Implements public List GetFullList() { using var context = new ComputersShopDatabase(); - return context.Computers.Include(x => x.Components).ThenInclude(x => x.Component) - .ToList().Select(x => x.GetViewModel).ToList(); + return context.Computers + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); } - public List GetFiltredList(ComputerSearchModel model) + public List GetFilteredList(ComputerSearchModel model) { if (string.IsNullOrEmpty(model.ComputerName)) { return new(); } + using var context = new ComputersShopDatabase(); - return context.Computers.Include(x => x.Components).ThenInclude(x => x.Component) - .Where(x => x.ComputerName.Contains(model.ComputerName)).ToList() - .Select(x => x.GetViewModel).ToList(); + return context.Computers + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .Where(x => x.ComputerName.Contains(model.ComputerName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); } public ComputerViewModel? GetElement(ComputerSearchModel model) { @@ -37,11 +47,15 @@ namespace ComputersShopDatabaseImplement.Implements { return null; } + using var context = new ComputersShopDatabase(); - return context.Computers.Include(x => x.Components).ThenInclude(x => x.Component) - .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComputerName) && - x.ComputerName == model.ComputerName) || - (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + return context.Computers + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComputerName) && + x.ComputerName == model.ComputerName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; } public ComputerViewModel? Insert(ComputerBindingModel model) { @@ -51,6 +65,7 @@ namespace ComputersShopDatabaseImplement.Implements { return null; } + context.Computers.Add(newComputer); context.SaveChanges(); return newComputer.GetViewModel; @@ -66,6 +81,7 @@ namespace ComputersShopDatabaseImplement.Implements { return null; } + computer.Update(model); context.SaveChanges(); computer.UpdateComponents(context, model); @@ -81,7 +97,9 @@ namespace ComputersShopDatabaseImplement.Implements public ComputerViewModel? Delete(ComputerBindingModel model) { using var context = new ComputersShopDatabase(); - var element = context.Computers.Include(x => x.Components).FirstOrDefault(rec => rec.Id == model.Id); + var element = context.Computers + .Include(x => x.Components) + .FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { context.Computers.Remove(element); @@ -92,3 +110,4 @@ namespace ComputersShopDatabaseImplement.Implements } } } + diff --git a/ComputersShop/ComputersShopDatabaseImplement/Implements/OrderStorage.cs b/ComputersShop/ComputersShopDatabaseImplement/Implements/OrderStorage.cs index 998f763..542447f 100644 --- a/ComputersShop/ComputersShopDatabaseImplement/Implements/OrderStorage.cs +++ b/ComputersShop/ComputersShopDatabaseImplement/Implements/OrderStorage.cs @@ -14,29 +14,36 @@ namespace ComputersShopDatabaseImplement.Implements { public class OrderStorage : IOrderStorage { + public List GetFullList() + { + using var context = new ComputersShopDatabase(); + return context.Orders + .Select(x => GetViewModel(x)) + .ToList(); + } + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + + using var context = new ComputersShopDatabase(); + return context.Orders + .Where(x => x.Id.Equals(model.Id)) + .Select(x => GetViewModel(x)) + .ToList(); + } public OrderViewModel? GetElement(OrderSearchModel model) { if (!model.Id.HasValue) { return null; } + using var context = new ComputersShopDatabase(); - return context.Orders.Include(x => x.Computer).FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; - } - public List GetFiltredList(OrderSearchModel model) - { - if (!model.Id.HasValue) - { - return new(); - } - using var context = new ComputersShopDatabase(); - return context.Orders.Where(x => x.Id == model.Id).Include(x => x.Computer) - .Select(x => x.GetViewModel).ToList(); - } - public List GetFullList() - { - using var context = new ComputersShopDatabase(); - return context.Orders.Include(x => x.Computer).Select(x => x.GetViewModel).ToList(); + return GetViewModel(context.Orders + .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))); } public OrderViewModel? Insert(OrderBindingModel model) { @@ -45,10 +52,11 @@ namespace ComputersShopDatabaseImplement.Implements { return null; } + using var context = new ComputersShopDatabase(); context.Orders.Add(newOrder); context.SaveChanges(); - return context.Orders.Include(x => x.Computer).FirstOrDefault(x => x.Id == newOrder.Id)?.GetViewModel; + return GetViewModel(newOrder); } public OrderViewModel? Update(OrderBindingModel model) { @@ -58,21 +66,34 @@ namespace ComputersShopDatabaseImplement.Implements { return null; } + order.Update(model); context.SaveChanges(); - return context.Orders.Include(x => x.Computer).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + return GetViewModel(order); } public OrderViewModel? Delete(OrderBindingModel model) { using var context = new ComputersShopDatabase(); - var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + var element = context.Orders.FirstOrDefault(x => x.Id == model.Id); if (element != null) { context.Orders.Remove(element); context.SaveChanges(); - return element.GetViewModel; + return GetViewModel(element); } return null; } + private static OrderViewModel GetViewModel(Order order) + { + using var context = new ComputersShopDatabase(); + var viewModel = order.GetViewModel; + var computer = context.Computers.FirstOrDefault(x => x.Id == order.ComputerId); + if (computer != null) + { + viewModel.ComputerName = computer.ComputerName; + } + return viewModel; + } } } + diff --git a/ComputersShop/ComputersShopDatabaseImplement/Implements/ShopStorage.cs b/ComputersShop/ComputersShopDatabaseImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..bf472ff --- /dev/null +++ b/ComputersShop/ComputersShopDatabaseImplement/Implements/ShopStorage.cs @@ -0,0 +1,168 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.SearchModels; +using ComputersShopContracts.StoragesContracts; +using ComputersShopContracts.ViewModels; +using ComputersShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopDatabaseImplement.Implements +{ + public class ShopStorage : IShopStorage + { + public List GetFullList() + { + using var context = new ComputersShopDatabase(); + return context.Shops + .Include(x => x.Computers) + .ThenInclude(x => x.Computer) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName)) + { + return new(); + } + + using var context = new ComputersShopDatabase(); + return context.Shops + .Include(x => x.Computers) + .ThenInclude(x => x.Computer) + .Where(x => x.ShopName.Contains(model.ShopName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue) + { + return null; + } + + using var context = new ComputersShopDatabase(); + return context.Shops + .Include(x => x.Computers) + .ThenInclude(x => x.Computer) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ShopName) && + x.ShopName == model.ShopName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + public ShopViewModel? Insert(ShopBindingModel model) + { + using var context = new ComputersShopDatabase(); + var newShop = Shop.Create(context, model); + if (newShop == null) + { + return null; + } + + context.Shops.Add(newShop); + context.SaveChanges(); + return newShop.GetViewModel; + } + public ShopViewModel? Update(ShopBindingModel model) + { + using var context = new ComputersShopDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var shop = context.Shops.FirstOrDefault(rec => rec.Id == model.Id); + if (shop == null) + { + return null; + } + + shop.Update(model); + context.SaveChanges(); + shop.UpdateComputers(context, model); + transaction.Commit(); + return shop.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public ShopViewModel? Delete(ShopBindingModel model) + { + using var context = new ComputersShopDatabase(); + var element = context.Shops + .Include(x => x.Computers) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Shops.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + public bool SellComputers(IComputerModel model, int count) + { + using var context = new ComputersShopDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var shops = context.Shops + .Include(x => x.Computers) + .ThenInclude(x => x.Computer) + .ToList() + .Where(x => x.ShopComputers.ContainsKey(model.Id)); + + foreach (var shop in shops) + { + int countInCurrentShop = shop.ShopComputers[model.Id].Item2; + if (countInCurrentShop <= count) + { + var elem = context.ShopComputers + .Where(x => x.ComputerId == model.Id) + .FirstOrDefault(x => x.ShopId == shop.Id); + context.ShopComputers.Remove(elem); + shop.ShopComputers.Remove(model.Id); + count -= countInCurrentShop; + } + else + { + shop.ShopComputers[model.Id] = (shop.ShopComputers[model.Id].Item1, countInCurrentShop - count); + count = 0; + shop.UpdateComputers(context, new ShopBindingModel + { + Id = shop.Id, + ShopName = shop.ShopName, + Address = shop.Address, + DateOpening = shop.DateOpening, + ShopComputers = shop.ShopComputers, + MaxComputers = shop.MaxComputers + }); + } + if (count <= 0) + { + context.SaveChanges(); + transaction.Commit(); + return true; + } + } + transaction.Rollback(); + return false; + } + catch + { + transaction.Rollback(); + throw; + } + } + public bool CheckCount(IComputerModel model, int count) + { + throw new NotImplementedException(); + } + } +} diff --git a/ComputersShop/ComputersShopDatabaseImplement/Models/Shop.cs b/ComputersShop/ComputersShopDatabaseImplement/Models/Shop.cs new file mode 100644 index 0000000..8719b35 --- /dev/null +++ b/ComputersShop/ComputersShopDatabaseImplement/Models/Shop.cs @@ -0,0 +1,104 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ComputersShopDataModels.Models; + +namespace ComputersShopDatabaseImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; set; } + [Required] + public string ShopName { get; set; } = string.Empty; + [Required] + public string Address { get; set; } = string.Empty; + [Required] + public DateTime DateOpening { get; set; } + private Dictionary? _shopComputers = null; + + [NotMapped] + public Dictionary ShopComputers + { + get + { + if (_shopComputers == null) + { + _shopComputers = Computers + .ToDictionary(recSP => recSP.ComputerId, recSP => (recSP.Computer as IComputerModel, recSP.Count)); + } + return _shopComputers; + } + } + public int MaxComputers { get; set; } + [ForeignKey("ShopId")] + public List Computers { get; set; } = new(); + public static Shop Create(ComputersShopDatabase context, ShopBindingModel model) + { + return new Shop + { + Id = model.Id, + ShopName = model.ShopName, + Address = model.Address, + DateOpening = model.DateOpening, + MaxComputers = model.MaxComputers, + Computers = model.ShopComputers.Select(x => new ShopComputer + { + Computer = context.Computers.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(ShopBindingModel model) + { + ShopName = model.ShopName; + Address = model.Address; + DateOpening = model.DateOpening; + MaxComputers = model.MaxComputers; + } + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Address = Address, + DateOpening = DateOpening, + ShopComputers = ShopComputers, + MaxComputers = MaxComputers + }; + public void UpdateComputers(ComputersShopDatabase context, ShopBindingModel model) + { + var shopComputers = context.ShopComputers.Where(rec => rec.ShopId == model.Id).ToList(); + if (shopComputers != null && shopComputers.Count > 0) + { + // Удаление изделий, которых нет в магазине + context.ShopComputers.RemoveRange(shopComputers.Where(rec => !model.ShopComputers.ContainsKey(rec.ComputerId))); + context.SaveChanges(); + // Обновление количества у существующих записей + foreach (var updateComputers in shopComputers) + { + updateComputers.Count = model.ShopComputers[updateComputers.ComputerId].Item2; + model.ShopComputers.Remove(updateComputers.ComputerId); + } + context.SaveChanges(); + } + + var shop = context.Shops.First(x => x.Id == Id); + foreach (var sp in model.ShopComputers) + { + context.ShopComputers.Add(new ShopComputer + { + Shop = shop, + Computer = context.Computers.First(x => x.Id == sp.Key), + Count = sp.Value.Item2 + }); + context.SaveChanges(); + } + _shopComputers = null; + } + } +} diff --git a/ComputersShop/ComputersShopDatabaseImplement/Models/ShopComputer.cs b/ComputersShop/ComputersShopDatabaseImplement/Models/ShopComputer.cs new file mode 100644 index 0000000..c616c30 --- /dev/null +++ b/ComputersShop/ComputersShopDatabaseImplement/Models/ShopComputer.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Numerics; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopDatabaseImplement.Models +{ + public class ShopComputer + { + public int Id { get; set; } + [Required] + public int ShopId { get; set; } + [Required] + public int ComputerId { get; set; } + [Required] + public int Count { get; set; } + public virtual Shop Shop { get; set; } = new(); + public virtual Computer Computer { get; set; } = new(); + } +} + diff --git a/ComputersShop/ComputersShopFileImplement/Implements/OrderStorage.cs b/ComputersShop/ComputersShopFileImplement/Implements/OrderStorage.cs index 9be75ba..ca02429 100644 --- a/ComputersShop/ComputersShopFileImplement/Implements/OrderStorage.cs +++ b/ComputersShop/ComputersShopFileImplement/Implements/OrderStorage.cs @@ -86,10 +86,10 @@ namespace ComputersShopFileImplement.Implements private OrderViewModel GetViewModel(Order order) { var viewModel = order.GetViewModel; - var plane = _source.Computers.FirstOrDefault(x => x.Id == order.ComputerId); - if (plane != null) + var computer = _source.Computers.FirstOrDefault(x => x.Id == order.ComputerId); + if (computer != null) { - viewModel.ComputerName = plane.ComputerName; + viewModel.ComputerName = computer.ComputerName; } return viewModel; } diff --git a/ComputersShop/ComputersShopView/FormSell.cs b/ComputersShop/ComputersShopView/FormSell.cs index 5de3f68..954458a 100644 --- a/ComputersShop/ComputersShopView/FormSell.cs +++ b/ComputersShop/ComputersShopView/FormSell.cs @@ -15,21 +15,13 @@ namespace ComputersShopView public partial class FormSell : Form { private readonly ILogger _logger; - - /// - /// Бизнес-логика для изделий - /// private readonly IComputerLogic _logicC; - - /// - /// Бизнес-логика для магазинов - /// private readonly IShopLogic _logicS; - public FormSell(ILogger logger, IComputerLogic planeLogic, IShopLogic shopLogic) + public FormSell(ILogger logger, IComputerLogic computerLogic, IShopLogic shopLogic) { InitializeComponent(); _logger = logger; - _logicC = planeLogic; + _logicC = computerLogic; _logicS = shopLogic; } private void FormSell_Load(object sender, EventArgs e) @@ -52,12 +44,6 @@ namespace ComputersShopView MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } - - /// - /// Кнопка "Сохранить" - /// - /// - /// private void buttonSave_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(textBoxCount.Text)) @@ -92,12 +78,6 @@ namespace ComputersShopView MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } - - /// - /// Кнопка "Отмена" - /// - /// - /// private void buttonCancel_Click(object sender, EventArgs e) { DialogResult = DialogResult.Cancel; -- 2.25.1 From db93b2f4a9759dc28f9b220f12ff5557a3c21994 Mon Sep 17 00:00:00 2001 From: bulatova_karina Date: Sun, 19 May 2024 10:55:41 +0400 Subject: [PATCH 3/3] =?UTF-8?q?=D1=82=D1=80=D0=B5=D1=82=D1=8C=D1=8F=20?= =?UTF-8?q?=D1=83=D1=81=D0=BB=D0=BE=D0=B6=D0=BD=D0=B5=D0=BD=D0=BA=D0=B0=20?= =?UTF-8?q?=D0=B2=D1=80=D0=BE=D0=B4=D0=B5=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=D0=B5=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ComputerShopDatabase.cs | 2 +- .../Implements/ShopStorage.cs | 3 + ... => 20240519063447_HardCreate.Designer.cs} | 81 ++++++++++++++++++- ...Create.cs => 20240519063447_HardCreate.cs} | 61 +++++++++++++- .../ComputersShopDatabaseModelSnapshot.cs | 77 ++++++++++++++++++ .../Models/Order.cs | 28 +++---- .../Models/Shop.cs | 1 + .../Models/ShopComputer.cs | 1 - .../Implements/OrderStorage.cs | 3 +- .../ComputersShopView/FormMain.Designer.cs | 14 ++-- ComputersShop/ComputersShopView/Program.cs | 2 +- 11 files changed, 244 insertions(+), 29 deletions(-) rename ComputersShop/ComputersShopDatabaseImplement/Migrations/{20240326105531_InitialCreate.Designer.cs => 20240519063447_HardCreate.Designer.cs} (67%) rename ComputersShop/ComputersShopDatabaseImplement/Migrations/{20240326105531_InitialCreate.cs => 20240519063447_HardCreate.cs} (65%) diff --git a/ComputersShop/ComputersShopDatabaseImplement/ComputerShopDatabase.cs b/ComputersShop/ComputersShopDatabaseImplement/ComputerShopDatabase.cs index cbf540d..6a58b74 100644 --- a/ComputersShop/ComputersShopDatabaseImplement/ComputerShopDatabase.cs +++ b/ComputersShop/ComputersShopDatabaseImplement/ComputerShopDatabase.cs @@ -15,7 +15,7 @@ namespace ComputersShopDatabaseImplement { if (optionsBuilder.IsConfigured == false) { - optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-1DE5E8N\SQLEXPRESS;Initial Catalog=ComputersShopDatabaseFull; + optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-1DE5E8N\SQLEXPRESS;Initial Catalog=ComputersShopDatabaseHard3; Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); } base.OnConfiguring(optionsBuilder); diff --git a/ComputersShop/ComputersShopDatabaseImplement/Implements/ShopStorage.cs b/ComputersShop/ComputersShopDatabaseImplement/Implements/ShopStorage.cs index bf472ff..a37fa87 100644 --- a/ComputersShop/ComputersShopDatabaseImplement/Implements/ShopStorage.cs +++ b/ComputersShop/ComputersShopDatabaseImplement/Implements/ShopStorage.cs @@ -2,7 +2,9 @@ using ComputersShopContracts.SearchModels; using ComputersShopContracts.StoragesContracts; using ComputersShopContracts.ViewModels; +using ComputersShopDatabaseImplement.Models; using ComputersShopDataModels.Models; +using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; @@ -166,3 +168,4 @@ namespace ComputersShopDatabaseImplement.Implements } } } + diff --git a/ComputersShop/ComputersShopDatabaseImplement/Migrations/20240326105531_InitialCreate.Designer.cs b/ComputersShop/ComputersShopDatabaseImplement/Migrations/20240519063447_HardCreate.Designer.cs similarity index 67% rename from ComputersShop/ComputersShopDatabaseImplement/Migrations/20240326105531_InitialCreate.Designer.cs rename to ComputersShop/ComputersShopDatabaseImplement/Migrations/20240519063447_HardCreate.Designer.cs index 1ac4371..83a2c51 100644 --- a/ComputersShop/ComputersShopDatabaseImplement/Migrations/20240326105531_InitialCreate.Designer.cs +++ b/ComputersShop/ComputersShopDatabaseImplement/Migrations/20240519063447_HardCreate.Designer.cs @@ -12,8 +12,8 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace ComputersShopDatabaseImplement.Migrations { [DbContext(typeof(ComputersShopDatabase))] - [Migration("20240326105531_InitialCreate")] - partial class InitialCreate + [Migration("20240519063447_HardCreate")] + partial class HardCreate { /// protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -124,6 +124,59 @@ namespace ComputersShopDatabaseImplement.Migrations b.ToTable("Orders"); }); + modelBuilder.Entity("ComputersShopDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("DateOpening") + .HasColumnType("datetime2"); + + b.Property("MaxComputers") + .HasColumnType("int"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("ComputersShopDatabaseImplement.Models.ShopComputer", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComputerId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComputerId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopComputers"); + }); + modelBuilder.Entity("ComputersShopDatabaseImplement.Models.ComputerComponent", b => { b.HasOne("ComputersShopDatabaseImplement.Models.Component", "Component") @@ -154,6 +207,25 @@ namespace ComputersShopDatabaseImplement.Migrations b.Navigation("Computer"); }); + modelBuilder.Entity("ComputersShopDatabaseImplement.Models.ShopComputer", b => + { + b.HasOne("ComputersShopDatabaseImplement.Models.Computer", "Computer") + .WithMany() + .HasForeignKey("ComputerId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ComputersShopDatabaseImplement.Models.Shop", "Shop") + .WithMany("Computers") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Computer"); + + b.Navigation("Shop"); + }); + modelBuilder.Entity("ComputersShopDatabaseImplement.Models.Component", b => { b.Navigation("ComputerComponents"); @@ -165,6 +237,11 @@ namespace ComputersShopDatabaseImplement.Migrations b.Navigation("Orders"); }); + + modelBuilder.Entity("ComputersShopDatabaseImplement.Models.Shop", b => + { + b.Navigation("Computers"); + }); #pragma warning restore 612, 618 } } diff --git a/ComputersShop/ComputersShopDatabaseImplement/Migrations/20240326105531_InitialCreate.cs b/ComputersShop/ComputersShopDatabaseImplement/Migrations/20240519063447_HardCreate.cs similarity index 65% rename from ComputersShop/ComputersShopDatabaseImplement/Migrations/20240326105531_InitialCreate.cs rename to ComputersShop/ComputersShopDatabaseImplement/Migrations/20240519063447_HardCreate.cs index 77f25f2..404b041 100644 --- a/ComputersShop/ComputersShopDatabaseImplement/Migrations/20240326105531_InitialCreate.cs +++ b/ComputersShop/ComputersShopDatabaseImplement/Migrations/20240519063447_HardCreate.cs @@ -6,7 +6,7 @@ using Microsoft.EntityFrameworkCore.Migrations; namespace ComputersShopDatabaseImplement.Migrations { /// - public partial class InitialCreate : Migration + public partial class HardCreate : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) @@ -39,6 +39,22 @@ namespace ComputersShopDatabaseImplement.Migrations table.PrimaryKey("PK_Computers", x => x.Id); }); + migrationBuilder.CreateTable( + name: "Shops", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ShopName = table.Column(type: "nvarchar(max)", nullable: false), + Address = table.Column(type: "nvarchar(max)", nullable: false), + DateOpening = table.Column(type: "datetime2", nullable: false), + MaxComputers = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Shops", x => x.Id); + }); + migrationBuilder.CreateTable( name: "ComputerComponents", columns: table => new @@ -90,6 +106,33 @@ namespace ComputersShopDatabaseImplement.Migrations onDelete: ReferentialAction.Cascade); }); + migrationBuilder.CreateTable( + name: "ShopComputers", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ShopId = table.Column(type: "int", nullable: false), + ComputerId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_ShopComputers", x => x.Id); + table.ForeignKey( + name: "FK_ShopComputers_Computers_ComputerId", + column: x => x.ComputerId, + principalTable: "Computers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_ShopComputers_Shops_ShopId", + column: x => x.ShopId, + principalTable: "Shops", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + migrationBuilder.CreateIndex( name: "IX_ComputerComponents_ComponentId", table: "ComputerComponents", @@ -104,6 +147,16 @@ namespace ComputersShopDatabaseImplement.Migrations name: "IX_Orders_ComputerId", table: "Orders", column: "ComputerId"); + + migrationBuilder.CreateIndex( + name: "IX_ShopComputers_ComputerId", + table: "ShopComputers", + column: "ComputerId"); + + migrationBuilder.CreateIndex( + name: "IX_ShopComputers_ShopId", + table: "ShopComputers", + column: "ShopId"); } /// @@ -115,11 +168,17 @@ namespace ComputersShopDatabaseImplement.Migrations migrationBuilder.DropTable( name: "Orders"); + migrationBuilder.DropTable( + name: "ShopComputers"); + migrationBuilder.DropTable( name: "Components"); migrationBuilder.DropTable( name: "Computers"); + + migrationBuilder.DropTable( + name: "Shops"); } } } diff --git a/ComputersShop/ComputersShopDatabaseImplement/Migrations/ComputersShopDatabaseModelSnapshot.cs b/ComputersShop/ComputersShopDatabaseImplement/Migrations/ComputersShopDatabaseModelSnapshot.cs index 00020d3..28117ea 100644 --- a/ComputersShop/ComputersShopDatabaseImplement/Migrations/ComputersShopDatabaseModelSnapshot.cs +++ b/ComputersShop/ComputersShopDatabaseImplement/Migrations/ComputersShopDatabaseModelSnapshot.cs @@ -121,6 +121,59 @@ namespace ComputersShopDatabaseImplement.Migrations b.ToTable("Orders"); }); + modelBuilder.Entity("ComputersShopDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("DateOpening") + .HasColumnType("datetime2"); + + b.Property("MaxComputers") + .HasColumnType("int"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("ComputersShopDatabaseImplement.Models.ShopComputer", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComputerId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComputerId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopComputers"); + }); + modelBuilder.Entity("ComputersShopDatabaseImplement.Models.ComputerComponent", b => { b.HasOne("ComputersShopDatabaseImplement.Models.Component", "Component") @@ -151,6 +204,25 @@ namespace ComputersShopDatabaseImplement.Migrations b.Navigation("Computer"); }); + modelBuilder.Entity("ComputersShopDatabaseImplement.Models.ShopComputer", b => + { + b.HasOne("ComputersShopDatabaseImplement.Models.Computer", "Computer") + .WithMany() + .HasForeignKey("ComputerId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ComputersShopDatabaseImplement.Models.Shop", "Shop") + .WithMany("Computers") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Computer"); + + b.Navigation("Shop"); + }); + modelBuilder.Entity("ComputersShopDatabaseImplement.Models.Component", b => { b.Navigation("ComputerComponents"); @@ -162,6 +234,11 @@ namespace ComputersShopDatabaseImplement.Migrations b.Navigation("Orders"); }); + + modelBuilder.Entity("ComputersShopDatabaseImplement.Models.Shop", b => + { + b.Navigation("Computers"); + }); #pragma warning restore 612, 618 } } diff --git a/ComputersShop/ComputersShopDatabaseImplement/Models/Order.cs b/ComputersShop/ComputersShopDatabaseImplement/Models/Order.cs index 3fe84c4..7a252be 100644 --- a/ComputersShop/ComputersShopDatabaseImplement/Models/Order.cs +++ b/ComputersShop/ComputersShopDatabaseImplement/Models/Order.cs @@ -12,34 +12,34 @@ namespace ComputersShopDatabaseImplement.Models { public class Order { - public int Id { get; private set; } + public int Id { get; set; } [Required] - public int ComputerId { get; private set; } + public int ComputerId { get; set; } [Required] - public int Count { get; private set; } + public int Count { get; set; } [Required] - public double Sum { get; private set; } + public double Sum { get; set; } [Required] - public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; + public OrderStatus Status { get; set; } [Required] - public DateTime DateCreate { get; private set; } = DateTime.Now; - public DateTime? DateImplement { get; private set; } - public virtual Computer Computer { get; private set; } + public DateTime DateCreate { get; set; } + public DateTime? DateImplement { get; set; } public static Order? Create(OrderBindingModel? model) { if (model == null) { return null; } + return new Order { + Id = model.Id, ComputerId = model.ComputerId, Count = model.Count, Sum = model.Sum, Status = model.Status, DateCreate = model.DateCreate, - DateImplement = model.DateImplement, - Id = model.Id, + DateImplement = model.DateImplement }; } public void Update(OrderBindingModel? model) @@ -48,19 +48,19 @@ namespace ComputersShopDatabaseImplement.Models { return; } + Status = model.Status; DateImplement = model.DateImplement; } public OrderViewModel GetViewModel => new() { + Id = Id, ComputerId = ComputerId, Count = Count, Sum = Sum, - DateCreate = DateCreate, - DateImplement = DateImplement, - Id = Id, Status = Status, - ComputerName = Computer.ComputerName + DateCreate = DateCreate, + DateImplement = DateImplement }; } } diff --git a/ComputersShop/ComputersShopDatabaseImplement/Models/Shop.cs b/ComputersShop/ComputersShopDatabaseImplement/Models/Shop.cs index 8719b35..6d2bb4f 100644 --- a/ComputersShop/ComputersShopDatabaseImplement/Models/Shop.cs +++ b/ComputersShop/ComputersShopDatabaseImplement/Models/Shop.cs @@ -102,3 +102,4 @@ namespace ComputersShopDatabaseImplement.Models } } } + diff --git a/ComputersShop/ComputersShopDatabaseImplement/Models/ShopComputer.cs b/ComputersShop/ComputersShopDatabaseImplement/Models/ShopComputer.cs index c616c30..21db4bd 100644 --- a/ComputersShop/ComputersShopDatabaseImplement/Models/ShopComputer.cs +++ b/ComputersShop/ComputersShopDatabaseImplement/Models/ShopComputer.cs @@ -21,4 +21,3 @@ namespace ComputersShopDatabaseImplement.Models public virtual Computer Computer { get; set; } = new(); } } - diff --git a/ComputersShop/ComputersShopFileImplement/Implements/OrderStorage.cs b/ComputersShop/ComputersShopFileImplement/Implements/OrderStorage.cs index ca02429..a123381 100644 --- a/ComputersShop/ComputersShopFileImplement/Implements/OrderStorage.cs +++ b/ComputersShop/ComputersShopFileImplement/Implements/OrderStorage.cs @@ -43,8 +43,7 @@ namespace ComputersShopFileImplement.Implements return null; } - return GetViewModel(_source.Orders - .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))); + return GetViewModel(_source.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))); } public OrderViewModel? Insert(OrderBindingModel model) { diff --git a/ComputersShop/ComputersShopView/FormMain.Designer.cs b/ComputersShop/ComputersShopView/FormMain.Designer.cs index 0c656bf..0b303ae 100644 --- a/ComputersShop/ComputersShopView/FormMain.Designer.cs +++ b/ComputersShop/ComputersShopView/FormMain.Designer.cs @@ -71,35 +71,35 @@ // компонентыToolStripMenuItem // this.компонентыToolStripMenuItem.Name = "компонентыToolStripMenuItem"; - this.компонентыToolStripMenuItem.Size = new System.Drawing.Size(224, 26); + this.компонентыToolStripMenuItem.Size = new System.Drawing.Size(218, 26); this.компонентыToolStripMenuItem.Text = "Компоненты"; this.компонентыToolStripMenuItem.Click += new System.EventHandler(this.КомпонентыToolStripMenuItem_Click); // // изделияToolStripMenuItem // this.изделияToolStripMenuItem.Name = "изделияToolStripMenuItem"; - this.изделияToolStripMenuItem.Size = new System.Drawing.Size(224, 26); + this.изделияToolStripMenuItem.Size = new System.Drawing.Size(218, 26); this.изделияToolStripMenuItem.Text = "Изделия"; this.изделияToolStripMenuItem.Click += new System.EventHandler(this.ИзделияToolStripMenuItem_Click); // // магазиныToolStripMenuItem // this.магазиныToolStripMenuItem.Name = "магазиныToolStripMenuItem"; - this.магазиныToolStripMenuItem.Size = new System.Drawing.Size(224, 26); + this.магазиныToolStripMenuItem.Size = new System.Drawing.Size(218, 26); this.магазиныToolStripMenuItem.Text = "Магазины"; this.магазиныToolStripMenuItem.Click += new System.EventHandler(this.МагазиныToolStripMenuItem_Click); // // поставкиToolStripMenuItem // this.поставкиToolStripMenuItem.Name = "поставкиToolStripMenuItem"; - this.поставкиToolStripMenuItem.Size = new System.Drawing.Size(224, 26); + this.поставкиToolStripMenuItem.Size = new System.Drawing.Size(218, 26); this.поставкиToolStripMenuItem.Text = "Поставки"; this.поставкиToolStripMenuItem.Click += new System.EventHandler(this.ПоставкиToolStripMenuItem_Click); // // продажаИзделийToolStripMenuItem // this.продажаИзделийToolStripMenuItem.Name = "продажаИзделийToolStripMenuItem"; - this.продажаИзделийToolStripMenuItem.Size = new System.Drawing.Size(224, 26); + this.продажаИзделийToolStripMenuItem.Size = new System.Drawing.Size(218, 26); this.продажаИзделийToolStripMenuItem.Text = "Продажа изделий"; this.продажаИзделийToolStripMenuItem.Click += new System.EventHandler(this.ПродажаизделийToolStripMenuItem_Click); // @@ -144,7 +144,7 @@ this.buttonOrderReady.TabIndex = 4; this.buttonOrderReady.Text = "Заказ готов"; this.buttonOrderReady.UseVisualStyleBackColor = true; - this.buttonOrderReady.Click += new System.EventHandler(this.ButtonOrderReady_Click); + this.buttonOrderReady.Click += new System.EventHandler(this.ButtonIssuedOrder_Click); // // buttonIssuedOrder // @@ -154,7 +154,7 @@ this.buttonIssuedOrder.TabIndex = 5; this.buttonIssuedOrder.Text = "Заказ выдан"; this.buttonIssuedOrder.UseVisualStyleBackColor = true; - this.buttonIssuedOrder.Click += new System.EventHandler(this.ButtonIssuedOrder_Click); + this.buttonIssuedOrder.Click += new System.EventHandler(this.ButtonOrderReady_Click); // // buttonRef // diff --git a/ComputersShop/ComputersShopView/Program.cs b/ComputersShop/ComputersShopView/Program.cs index 8071061..ae94c9f 100644 --- a/ComputersShop/ComputersShopView/Program.cs +++ b/ComputersShop/ComputersShopView/Program.cs @@ -1,7 +1,7 @@ using ComputersShopBusinessLogic.BusinessLogics; using ComputersShopContracts.BusinessLogicsContracts; using ComputersShopContracts.StoragesContracts; -using ComputersShopFileImplement.Implements; +using ComputersShopDatabaseImplement.Implements; using ComputersShopView; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; -- 2.25.1