diff --git a/ComputersShop/ComputersShop.sln b/ComputersShop/ComputersShop.sln index 970e25b..0210723 100644 --- a/ComputersShop/ComputersShop.sln +++ b/ComputersShop/ComputersShop.sln @@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputersShopListImplement" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputersShopFileImplement", "ComputersShopFileImplement\ComputersShopFileImplement.csproj", "{E6E9ED14-BE04-46E4-AE5A-C9672B606616}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputersShopDatabaseImplement", "ComputersShopDatabaseImplement\ComputersShopDatabaseImplement.csproj", "{D2C7A8C8-06D0-45E3-A562-AE8A5AF6A452}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -45,6 +47,10 @@ Global {E6E9ED14-BE04-46E4-AE5A-C9672B606616}.Debug|Any CPU.Build.0 = Debug|Any CPU {E6E9ED14-BE04-46E4-AE5A-C9672B606616}.Release|Any CPU.ActiveCfg = Release|Any CPU {E6E9ED14-BE04-46E4-AE5A-C9672B606616}.Release|Any CPU.Build.0 = Release|Any CPU + {D2C7A8C8-06D0-45E3-A562-AE8A5AF6A452}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D2C7A8C8-06D0-45E3-A562-AE8A5AF6A452}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D2C7A8C8-06D0-45E3-A562-AE8A5AF6A452}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D2C7A8C8-06D0-45E3-A562-AE8A5AF6A452}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/ComputersShop/ComputersShopBusinessLogic/ComputersShopBusinessLogic.csproj b/ComputersShop/ComputersShopBusinessLogic/ComputersShopBusinessLogic.csproj index 1915704..30e5f21 100644 --- a/ComputersShop/ComputersShopBusinessLogic/ComputersShopBusinessLogic.csproj +++ b/ComputersShop/ComputersShopBusinessLogic/ComputersShopBusinessLogic.csproj @@ -1,7 +1,7 @@  - net7.0 + net6.0 enable diff --git a/ComputersShop/ComputersShopContracts/BusinessLogicsContracts/IOrderLogic.cs b/ComputersShop/ComputersShopContracts/BusinessLogicsContracts/IOrderLogic.cs index 6824880..cd57b9f 100644 --- a/ComputersShop/ComputersShopContracts/BusinessLogicsContracts/IOrderLogic.cs +++ b/ComputersShop/ComputersShopContracts/BusinessLogicsContracts/IOrderLogic.cs @@ -1,5 +1,6 @@ using ComputersShopContracts.BindingModels; using ComputersShopContracts.SearchModels; +using ComputersShopContracts.ViewModels; using System; using System.Collections.Generic; using System.Linq; @@ -10,7 +11,7 @@ namespace ComputersShopContracts.BusinessLogicsContracts { public interface IOrderLogic { - List? ReadList(OrderSearchModel? model); + List? ReadList(OrderSearchModel? model); bool CreateOrder(OrderBindingModel model); bool TakeOrderInWork(OrderBindingModel model); bool FinishOrder(OrderBindingModel model); diff --git a/ComputersShop/ComputersShopContracts/ComputersShopContracts.csproj b/ComputersShop/ComputersShopContracts/ComputersShopContracts.csproj index 5e03ea8..3dca1b9 100644 --- a/ComputersShop/ComputersShopContracts/ComputersShopContracts.csproj +++ b/ComputersShop/ComputersShopContracts/ComputersShopContracts.csproj @@ -1,7 +1,7 @@ - + - net7.0 + net6.0 enable enable diff --git a/ComputersShop/ComputersShopDataModels/ComputersShopDataModels.csproj b/ComputersShop/ComputersShopDataModels/ComputersShopDataModels.csproj index 7840f16..463eca5 100644 --- a/ComputersShop/ComputersShopDataModels/ComputersShopDataModels.csproj +++ b/ComputersShop/ComputersShopDataModels/ComputersShopDataModels.csproj @@ -1,7 +1,7 @@ - + - net7.0-windows + net6.0 enable enable diff --git a/ComputersShop/ComputersShopDatabaseImplement/ComputersShopDatabase.cs b/ComputersShop/ComputersShopDatabaseImplement/ComputersShopDatabase.cs new file mode 100644 index 0000000..432c63c --- /dev/null +++ b/ComputersShop/ComputersShopDatabaseImplement/ComputersShopDatabase.cs @@ -0,0 +1,27 @@ +using ComputersShopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopDatabaseImplement +{ + public class ComputersShopDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder + optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=.\SQLEXPRESS;Initial Catalog=ComputersShopDatabase;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..b83ea69 --- /dev/null +++ b/ComputersShop/ComputersShopDatabaseImplement/ComputersShopDatabaseImplement.csproj @@ -0,0 +1,22 @@ + + + + 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..dc8519e --- /dev/null +++ b/ComputersShop/ComputersShopDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,84 @@ +using ComputersShopContracts; +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 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(); + } + 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..6b9bafe --- /dev/null +++ b/ComputersShop/ComputersShopDatabaseImplement/Implements/ComputerStorage.cs @@ -0,0 +1,106 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.SearchModels; +using ComputersShopContracts.StoragesContracts; +using ComputersShopContracts.ViewModels; +using ComputersShopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +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 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(); + } + 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 product = context.Computers.FirstOrDefault(rec => + rec.Id == model.Id); + if (product == null) + { + return null; + } + product.Update(model); + context.SaveChanges(); + product.UpdateComponents(context, model); + transaction.Commit(); + return product.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..dadddeb --- /dev/null +++ b/ComputersShop/ComputersShopDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,85 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.SearchModels; +using ComputersShopContracts.StoragesContracts; +using ComputersShopContracts.ViewModels; +using ComputersShopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public List GetFullList() + { + using var context = new ComputersShopDatabase(); + return context.Orders.Include(x => x.Computer).Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new ComputersShopDatabase(); + return context.Orders.Include(x => x.Computer).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 ComputersShopDatabase(); + return context.Orders.Include(x => x.Computer).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + using var context = new ComputersShopDatabase(); + if (model == null) + return null; + + context.Orders.Add(newOrder); + context.SaveChanges(); + return newOrder.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 order.GetViewModel; + } + + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new ComputersShopDatabase(); + 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/ComputersShop/ComputersShopDatabaseImplement/Migrations/20240426163119_InitialCreate.Designer.cs b/ComputersShop/ComputersShopDatabaseImplement/Migrations/20240426163119_InitialCreate.Designer.cs new file mode 100644 index 0000000..eaed8bc --- /dev/null +++ b/ComputersShop/ComputersShopDatabaseImplement/Migrations/20240426163119_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("20240426163119_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .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/20240426163119_InitialCreate.cs b/ComputersShop/ComputersShopDatabaseImplement/Migrations/20240426163119_InitialCreate.cs new file mode 100644 index 0000000..77f25f2 --- /dev/null +++ b/ComputersShop/ComputersShopDatabaseImplement/Migrations/20240426163119_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..9545da3 --- /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.3") + .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..7168ead --- /dev/null +++ b/ComputersShop/ComputersShopDatabaseImplement/Models/Component.cs @@ -0,0 +1,62 @@ +using ComputersShopContracts.ViewModels; +using ComputersShopContracts; +using ComputersShopDataModels.Models; +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; + +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..4d2bee4 --- /dev/null +++ b/ComputersShop/ComputersShopDatabaseImplement/Models/Computer.cs @@ -0,0 +1,102 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.ViewModels; +using ComputersShopDataModels.Models; +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; + +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..91059a9 --- /dev/null +++ b/ComputersShop/ComputersShopDatabaseImplement/Models/ComputerComponent.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace 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..713fe4b --- /dev/null +++ b/ComputersShop/ComputersShopDatabaseImplement/Models/Order.cs @@ -0,0 +1,78 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.ViewModels; +using ComputersShopDataModels.Enums; +using ComputersShopDataModels.Models; + +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 Order : IOrderModel + { + public int Id { get; private set; } + + [Required] + public int ComputerId { get; private set; } + + public virtual Computer Computer { 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(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, + }; + } + + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + Status = model.Status; + DateImplement = model.DateImplement; + } + + public OrderViewModel GetViewModel => new() + { + Id = Id, + ComputerId = ComputerId, + ComputerName = Computer.ComputerName, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + }; + } +} diff --git a/ComputersShop/ComputersShopFileImplement/ComputersShopFileImplement.csproj b/ComputersShop/ComputersShopFileImplement/ComputersShopFileImplement.csproj index 1b0116c..b897a9c 100644 --- a/ComputersShop/ComputersShopFileImplement/ComputersShopFileImplement.csproj +++ b/ComputersShop/ComputersShopFileImplement/ComputersShopFileImplement.csproj @@ -1,7 +1,7 @@  - net7.0 + net6.0 enable enable diff --git a/ComputersShop/ComputersShopFileImplement/DataFileSingleton.cs b/ComputersShop/ComputersShopFileImplement/DataFileSingleton.cs index 47a3a29..68b064d 100644 --- a/ComputersShop/ComputersShopFileImplement/DataFileSingleton.cs +++ b/ComputersShop/ComputersShopFileImplement/DataFileSingleton.cs @@ -1,4 +1,7 @@ -namespace ComputersShopFileImplement +using ComputersShopFileImplement.Models; +using System.Xml.Linq; + +namespace ComputersShopFileImplement { public class DataFileSingleton { diff --git a/ComputersShop/ComputersShopFileImplement/Implements/ComponentStorage.cs b/ComputersShop/ComputersShopFileImplement/Implements/ComponentStorage.cs index 8f91791..8046997 100644 --- a/ComputersShop/ComputersShopFileImplement/Implements/ComponentStorage.cs +++ b/ComputersShop/ComputersShopFileImplement/Implements/ComponentStorage.cs @@ -1,4 +1,9 @@ -using System; +using ComputersShopContracts; +using ComputersShopContracts.SearchModels; +using ComputersShopContracts.StoragesContracts; +using ComputersShopContracts.ViewModels; +using ComputersShopFileImplement.Models; +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/ComputersShop/ComputersShopFileImplement/Implements/ComputerStorage.cs b/ComputersShop/ComputersShopFileImplement/Implements/ComputerStorage.cs index 83c9117..95bf566 100644 --- a/ComputersShop/ComputersShopFileImplement/Implements/ComputerStorage.cs +++ b/ComputersShop/ComputersShopFileImplement/Implements/ComputerStorage.cs @@ -1,4 +1,9 @@ -using System; +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.SearchModels; +using ComputersShopContracts.StoragesContracts; +using ComputersShopContracts.ViewModels; +using ComputersShopFileImplement.Models; +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/ComputersShop/ComputersShopFileImplement/Implements/OrderStorage.cs b/ComputersShop/ComputersShopFileImplement/Implements/OrderStorage.cs index 4fa0676..bd69fd6 100644 --- a/ComputersShop/ComputersShopFileImplement/Implements/OrderStorage.cs +++ b/ComputersShop/ComputersShopFileImplement/Implements/OrderStorage.cs @@ -1,4 +1,9 @@ -using System; +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.SearchModels; +using ComputersShopContracts.StoragesContracts; +using ComputersShopContracts.ViewModels; +using ComputersShopFileImplement.Models; +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/ComputersShop/ComputersShopFileImplement/Models/Component.cs b/ComputersShop/ComputersShopFileImplement/Models/Component.cs index fc4eb87..c8ee0d0 100644 --- a/ComputersShop/ComputersShopFileImplement/Models/Component.cs +++ b/ComputersShop/ComputersShopFileImplement/Models/Component.cs @@ -1,4 +1,7 @@ -using System; +using ComputersShopContracts; +using ComputersShopContracts.ViewModels; +using ComputersShopDataModels.Models; +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/ComputersShop/ComputersShopFileImplement/Models/Computer.cs b/ComputersShop/ComputersShopFileImplement/Models/Computer.cs index 63b1ded..f47a031 100644 --- a/ComputersShop/ComputersShopFileImplement/Models/Computer.cs +++ b/ComputersShop/ComputersShopFileImplement/Models/Computer.cs @@ -1,4 +1,7 @@ -using System; +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.ViewModels; +using ComputersShopDataModels.Models; +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/ComputersShop/ComputersShopFileImplement/Models/Order.cs b/ComputersShop/ComputersShopFileImplement/Models/Order.cs index 9ed9928..b48b2be 100644 --- a/ComputersShop/ComputersShopFileImplement/Models/Order.cs +++ b/ComputersShop/ComputersShopFileImplement/Models/Order.cs @@ -1,4 +1,8 @@ -using System; +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.ViewModels; +using ComputersShopDataModels.Enums; +using ComputersShopDataModels.Models; +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/ComputersShop/ComputersShopListImplement/ComputersShopListImplement.csproj b/ComputersShop/ComputersShopListImplement/ComputersShopListImplement.csproj index 7e8d546..b4bbe77 100644 --- a/ComputersShop/ComputersShopListImplement/ComputersShopListImplement.csproj +++ b/ComputersShop/ComputersShopListImplement/ComputersShopListImplement.csproj @@ -1,14 +1,13 @@  - net7.0-windows + net6.0 enable enable - diff --git a/ComputersShop/ComputersShopListImplement/Models/Computer.cs b/ComputersShop/ComputersShopListImplement/Models/Computer.cs index e179ffe..35ef329 100644 --- a/ComputersShop/ComputersShopListImplement/Models/Computer.cs +++ b/ComputersShop/ComputersShopListImplement/Models/Computer.cs @@ -1,4 +1,7 @@ -using System; +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.ViewModels; +using ComputersShopDataModels.Models; +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/ComputersShop/ComputersShopListImplement/Models/Order.cs b/ComputersShop/ComputersShopListImplement/Models/Order.cs index 42b0c0c..c74ef0b 100644 --- a/ComputersShop/ComputersShopListImplement/Models/Order.cs +++ b/ComputersShop/ComputersShopListImplement/Models/Order.cs @@ -1,4 +1,8 @@ -using System; +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.ViewModels; +using ComputersShopDataModels.Enums; +using ComputersShopDataModels.Models; +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/ComputersShop/ComputersShopView/ComputersShopView.csproj b/ComputersShop/ComputersShopView/ComputersShopView.csproj index f042247..bc2c743 100644 --- a/ComputersShop/ComputersShopView/ComputersShopView.csproj +++ b/ComputersShop/ComputersShopView/ComputersShopView.csproj @@ -2,13 +2,17 @@ WinExe - net7.0-windows + net6.0-windows enable true enable + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -18,6 +22,7 @@ + diff --git a/ComputersShop/ComputersShopView/FormComputerComponent.Designer.cs b/ComputersShop/ComputersShopView/FormComputerComponent.Designer.cs index 6437d0e..0fc15f7 100644 --- a/ComputersShop/ComputersShopView/FormComputerComponent.Designer.cs +++ b/ComputersShop/ComputersShopView/FormComputerComponent.Designer.cs @@ -101,7 +101,7 @@ Controls.Add(label2); Controls.Add(label1); Name = "FormComputerComponent"; - Text = "FormComputerComponent"; + Text = "Компоненты изделия"; ResumeLayout(false); PerformLayout(); } diff --git a/ComputersShop/ComputersShopView/FormComputerComponent.resx b/ComputersShop/ComputersShopView/FormComputerComponent.resx index 1af7de1..af32865 100644 --- a/ComputersShop/ComputersShopView/FormComputerComponent.resx +++ b/ComputersShop/ComputersShopView/FormComputerComponent.resx @@ -1,17 +1,17 @@  - diff --git a/ComputersShop/ComputersShopView/FormComputers.Designer.cs b/ComputersShop/ComputersShopView/FormComputers.Designer.cs index 2644bf6..5aed066 100644 --- a/ComputersShop/ComputersShopView/FormComputers.Designer.cs +++ b/ComputersShop/ComputersShopView/FormComputers.Designer.cs @@ -96,7 +96,7 @@ Controls.Add(buttonAdd); Controls.Add(dataGridView); Name = "FormComputers"; - Text = "Computers"; + Text = "Изделия"; Load += FormComputers_Load; ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); ResumeLayout(false);