diff --git a/AbstractComputerDataModel/ComputerShopDataModels.csproj b/AbstractComputerDataModel/ComputerShopDataModels.csproj index 895206e..58a4c94 100644 --- a/AbstractComputerDataModel/ComputerShopDataModels.csproj +++ b/AbstractComputerDataModel/ComputerShopDataModels.csproj @@ -7,6 +7,12 @@ + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/ComputerShopBusinessLogic/ComputerShopBusinessLogic.csproj b/ComputerShopBusinessLogic/ComputerShopBusinessLogic.csproj index b63d171..266d7e2 100644 --- a/ComputerShopBusinessLogic/ComputerShopBusinessLogic.csproj +++ b/ComputerShopBusinessLogic/ComputerShopBusinessLogic.csproj @@ -7,6 +7,12 @@ + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/ComputerShopContracts/ComputerShopContracts.csproj b/ComputerShopContracts/ComputerShopContracts.csproj index f838e9c..8935112 100644 --- a/ComputerShopContracts/ComputerShopContracts.csproj +++ b/ComputerShopContracts/ComputerShopContracts.csproj @@ -7,6 +7,12 @@ + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/ComputerShopDatabaseImplement/ComputerShopDatabase.cs b/ComputerShopDatabaseImplement/ComputerShopDatabase.cs new file mode 100644 index 0000000..79a9953 --- /dev/null +++ b/ComputerShopDatabaseImplement/ComputerShopDatabase.cs @@ -0,0 +1,26 @@ +using ComputerShopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopDatabaseImplement +{ + internal class ComputerShopDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-IRUVF5S\SQLEXPRESS;Initial Catalog=ComputerShopDatabaseFull;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/ComputerShopDatabaseImplement/ComputerShopDatabaseImplement.csproj b/ComputerShopDatabaseImplement/ComputerShopDatabaseImplement.csproj new file mode 100644 index 0000000..76a1da5 --- /dev/null +++ b/ComputerShopDatabaseImplement/ComputerShopDatabaseImplement.csproj @@ -0,0 +1,23 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/ComputerShopDatabaseImplement/Implements/ComponentStorage.cs b/ComputerShopDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..da71ab4 --- /dev/null +++ b/ComputerShopDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,89 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.SearchModels; +using ComputerShopContracts.StoragesContracts; +using ComputerShopContracts.ViewModels; +using ComputerShopDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new ComputerShopDatabase(); + return context.Components + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(ComponentSearchModel + model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + using var context = new ComputerShopDatabase(); + 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 ComputerShopDatabase(); + 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 ComputerShopDatabase(); + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new ComputerShopDatabase(); + 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 ComputerShopDatabase(); + 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/ComputerShopDatabaseImplement/Implements/ComputerStorage.cs b/ComputerShopDatabaseImplement/Implements/ComputerStorage.cs new file mode 100644 index 0000000..7eb9f95 --- /dev/null +++ b/ComputerShopDatabaseImplement/Implements/ComputerStorage.cs @@ -0,0 +1,110 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.SearchModels; +using ComputerShopContracts.StoragesContracts; +using ComputerShopContracts.ViewModels; +using ComputerShopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopDatabaseImplement.Implements +{ + public class ComputerStorage : IComputerStorage + { + public List GetFullList() + { + using var context = new ComputerShopDatabase(); + 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 ComputerShopDatabase(); + 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 ComputerShopDatabase(); + 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 ComputerShopDatabase(); + var newProduct = Computer.Create(context, model); + if (newProduct == null) + { + return null; + } + context.Computers.Add(newProduct); + context.SaveChanges(); + return newProduct.GetViewModel; + } + public ComputerViewModel? Update(ComputerBindingModel model) + { + using var context = new ComputerShopDatabase(); + 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 ComputerShopDatabase(); + 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/ComputerShopDatabaseImplement/Implements/OrderStorage.cs b/ComputerShopDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..bae5a5c --- /dev/null +++ b/ComputerShopDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,85 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.SearchModels; +using ComputerShopContracts.StoragesContracts; +using ComputerShopContracts.ViewModels; +using ComputerShopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new ComputerShopDatabase(); + return context.Orders.Include(x => x.Computer).FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new ComputerShopDatabase(); + 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 ComputerShopDatabase(); + 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 ComputerShopDatabase(); + 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 ComputerShopDatabase(); + 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 ComputerShopDatabase(); + 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/ComputerShopDatabaseImplement/Migrations/20230326111439_FirstMig.Designer.cs b/ComputerShopDatabaseImplement/Migrations/20230326111439_FirstMig.Designer.cs new file mode 100644 index 0000000..d7c8596 --- /dev/null +++ b/ComputerShopDatabaseImplement/Migrations/20230326111439_FirstMig.Designer.cs @@ -0,0 +1,175 @@ +// +using System; +using ComputerShopDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace ComputerShopDatabaseImplement.Migrations +{ + [DbContext(typeof(ComputerShopDatabase))] + [Migration("20230326111439_FirstMig")] + partial class FirstMig + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.4") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("ComputerShopDatabaseImplement.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("ComputerShopDatabaseImplement.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("ComputerShopDatabaseImplement.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("ComputerShopDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComputerId") + .HasColumnType("int"); + + b.Property("ComputerName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + 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("ComputerShopDatabaseImplement.Models.ComputerComponent", b => + { + b.HasOne("ComputerShopDatabaseImplement.Models.Component", "Component") + .WithMany("ComputerComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ComputerShopDatabaseImplement.Models.Computer", "Computer") + .WithMany("Components") + .HasForeignKey("ComputerId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Computer"); + }); + + modelBuilder.Entity("ComputerShopDatabaseImplement.Models.Order", b => + { + b.HasOne("ComputerShopDatabaseImplement.Models.Computer", "Computer") + .WithMany("Orders") + .HasForeignKey("ComputerId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Computer"); + }); + + modelBuilder.Entity("ComputerShopDatabaseImplement.Models.Component", b => + { + b.Navigation("ComputerComponents"); + }); + + modelBuilder.Entity("ComputerShopDatabaseImplement.Models.Computer", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/ComputerShopDatabaseImplement/Migrations/20230326111439_FirstMig.cs b/ComputerShopDatabaseImplement/Migrations/20230326111439_FirstMig.cs new file mode 100644 index 0000000..51cfabc --- /dev/null +++ b/ComputerShopDatabaseImplement/Migrations/20230326111439_FirstMig.cs @@ -0,0 +1,126 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace ComputerShopDatabaseImplement.Migrations +{ + /// + public partial class FirstMig : 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"), + ComputerName = table.Column(type: "nvarchar(max)", nullable: false), + 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/ComputerShopDatabaseImplement/Migrations/ComputerShopDatabaseModelSnapshot.cs b/ComputerShopDatabaseImplement/Migrations/ComputerShopDatabaseModelSnapshot.cs new file mode 100644 index 0000000..5cbeec7 --- /dev/null +++ b/ComputerShopDatabaseImplement/Migrations/ComputerShopDatabaseModelSnapshot.cs @@ -0,0 +1,172 @@ +// +using System; +using ComputerShopDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace ComputerShopDatabaseImplement.Migrations +{ + [DbContext(typeof(ComputerShopDatabase))] + partial class ComputerShopDatabaseModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.4") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("ComputerShopDatabaseImplement.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("ComputerShopDatabaseImplement.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("ComputerShopDatabaseImplement.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("ComputerShopDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComputerId") + .HasColumnType("int"); + + b.Property("ComputerName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + 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("ComputerShopDatabaseImplement.Models.ComputerComponent", b => + { + b.HasOne("ComputerShopDatabaseImplement.Models.Component", "Component") + .WithMany("ComputerComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ComputerShopDatabaseImplement.Models.Computer", "Computer") + .WithMany("Components") + .HasForeignKey("ComputerId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Computer"); + }); + + modelBuilder.Entity("ComputerShopDatabaseImplement.Models.Order", b => + { + b.HasOne("ComputerShopDatabaseImplement.Models.Computer", "Computer") + .WithMany("Orders") + .HasForeignKey("ComputerId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Computer"); + }); + + modelBuilder.Entity("ComputerShopDatabaseImplement.Models.Component", b => + { + b.Navigation("ComputerComponents"); + }); + + modelBuilder.Entity("ComputerShopDatabaseImplement.Models.Computer", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/ComputerShopDatabaseImplement/Models/Component.cs b/ComputerShopDatabaseImplement/Models/Component.cs new file mode 100644 index 0000000..800f9c9 --- /dev/null +++ b/ComputerShopDatabaseImplement/Models/Component.cs @@ -0,0 +1,62 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.ViewModels; +using ComputerShopDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace ComputerShopDatabaseImplement.Models +{ + internal 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/ComputerShopDatabaseImplement/Models/Computer.cs b/ComputerShopDatabaseImplement/Models/Computer.cs new file mode 100644 index 0000000..3d4db22 --- /dev/null +++ b/ComputerShopDatabaseImplement/Models/Computer.cs @@ -0,0 +1,99 @@ +using ComputerShopDataModels.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; +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.ViewModels; + +namespace ComputerShopDatabaseImplement.Models +{ + internal 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(ComputerShopDatabase 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(ComputerShopDatabase context, ComputerBindingModel model) + { + var computerComponents = context.ComputerComponents.Where(rec => + rec.Id == 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/ComputerShopDatabaseImplement/Models/ComputerComponent.cs b/ComputerShopDatabaseImplement/Models/ComputerComponent.cs new file mode 100644 index 0000000..0ec2ab5 --- /dev/null +++ b/ComputerShopDatabaseImplement/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 ComputerShopDatabaseImplement.Models +{ + internal 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/ComputerShopDatabaseImplement/Models/Order.cs b/ComputerShopDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..429d42c --- /dev/null +++ b/ComputerShopDatabaseImplement/Models/Order.cs @@ -0,0 +1,76 @@ +using ComputerShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.ViewModels; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using ComputerShopDataModels.Enums; +using System.Reflection.Metadata; + +namespace ComputerShopDatabaseImplement.Models +{ + internal class Order : IOrderModel + { + public string ComputerName { get; private set; } = String.Empty; + 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; set; } + + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order + { + ComputerId = model.ComputerId, + ComputerName = model.ComputerName, + 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 = ComputerName + }; + + } +} diff --git a/ComputerShopFileImplement/ComputerShopFileImplement.csproj b/ComputerShopFileImplement/ComputerShopFileImplement.csproj index 63a55ef..733e6e0 100644 --- a/ComputerShopFileImplement/ComputerShopFileImplement.csproj +++ b/ComputerShopFileImplement/ComputerShopFileImplement.csproj @@ -6,6 +6,15 @@ enable + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + diff --git a/ComputerShopListImplement/ComputerShopListImplement.csproj b/ComputerShopListImplement/ComputerShopListImplement.csproj index 5cf7fb2..76a1da5 100644 --- a/ComputerShopListImplement/ComputerShopListImplement.csproj +++ b/ComputerShopListImplement/ComputerShopListImplement.csproj @@ -6,6 +6,15 @@ enable + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + diff --git a/ComputersShop/ComputersShop.csproj b/ComputersShop/ComputersShop.csproj index 8d64a7e..a883a0a 100644 --- a/ComputersShop/ComputersShop.csproj +++ b/ComputersShop/ComputersShop.csproj @@ -9,6 +9,12 @@ + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -17,6 +23,7 @@ + diff --git a/ComputersShop/ComputersShop.sln b/ComputersShop/ComputersShop.sln index 5683abc..a545107 100644 --- a/ComputersShop/ComputersShop.sln +++ b/ComputersShop/ComputersShop.sln @@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputerShopBusinessLogic", EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputerShopListImplement", "..\ComputerShopListImplement\ComputerShopListImplement.csproj", "{D632C3A7-3E0E-4B53-B2B4-696A9F6B8D38}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputerShopFileImplement", "..\ComputerShopFileImplement\ComputerShopFileImplement.csproj", "{AC377840-5B04-4C11-BBFA-C7933C40AB2C}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputerShopFileImplement", "..\ComputerShopFileImplement\ComputerShopFileImplement.csproj", "{AC377840-5B04-4C11-BBFA-C7933C40AB2C}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputerShopDatabaseImplement", "..\ComputerShopDatabaseImplement\ComputerShopDatabaseImplement.csproj", "{B742E338-3F3E-47B1-899E-B4E7303571BB}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -45,6 +47,10 @@ Global {AC377840-5B04-4C11-BBFA-C7933C40AB2C}.Debug|Any CPU.Build.0 = Debug|Any CPU {AC377840-5B04-4C11-BBFA-C7933C40AB2C}.Release|Any CPU.ActiveCfg = Release|Any CPU {AC377840-5B04-4C11-BBFA-C7933C40AB2C}.Release|Any CPU.Build.0 = Release|Any CPU + {B742E338-3F3E-47B1-899E-B4E7303571BB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B742E338-3F3E-47B1-899E-B4E7303571BB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B742E338-3F3E-47B1-899E-B4E7303571BB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B742E338-3F3E-47B1-899E-B4E7303571BB}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/ComputersShop/Program.cs b/ComputersShop/Program.cs index 15ec7db..5c97bbb 100644 --- a/ComputersShop/Program.cs +++ b/ComputersShop/Program.cs @@ -1,7 +1,7 @@ using ComputerShopBusinessLogic.BusinessLogics; using ComputerShopContracts.BusinessLogicsContracts; using ComputerShopContracts.StoragesContracts; -using ComputerShopFileImplement.Implements; +using ComputerShopDatabaseImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging;